GamsModifier don't work

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
silence
User
User
Posts: 8
Joined: 1 year ago

GamsModifier don't work

Post by silence » 4 months ago

Hi,

I try to use a GamsModelInstance to optimize multiple times with two parameters changing.The one parameter is two-dimensional, and the other one is one-dimensional.I have followed the TUTORIAL to creat a GamsModelInstance ,but something went wrong. It did optimize multiple times with original parameters,it means two parameters didn't uudate. It is strange that when i print two parameters in mi.sync_db, they have changed. And No error report when running, I have to ask for help
The code and documents involved are shown as follow:
code.txt
(8.08 KiB) Downloaded 36 times
理论泄流表.7z
(1.23 KiB) Downloaded 33 times
随机生成序列2.7z
(3.4 KiB) Downloaded 32 times

User avatar
bussieck
Moderator
Moderator
Posts: 615
Joined: 4 years ago

Re: GamsModifier don't work

Post by bussieck » 4 months ago

Hi,

I had to adjust your path string not only to match my directory structure but also added the r for raw string (data_dir = r"D:\Users\mbussieck\Downloads\随机生成序列2/"). I could run the Python script okay. I don't know whay goes wrong. before the first solve, the update database contains a single element. I added a "mi.sync_db.export(f'sync{i}.gdx')" before the "mi.solve()" to inspect this at leisure. Qtable was empty and Q_sw consisted of a single element:

Code: Select all

Parameter Qtable(*,*) Theoretical discharge table /
 /;

Parameter Q_sw(*) domestic sewage discharge(10 million m3) /
'72' 0.702251762063773 /;
For the second to fifth teration I got similar GDX files with Qtable all empty and Q_sw values for label '72' with values ranging from 0.70 to 0.91. I am not surprised that you don't see changes in the model when solving. The equation that contains Q_sw('72') is cons9('72') and here the optimal solution has a big slack for cons9 (upper bound is 1.08 while optimal level is 0). Changing the RHS by 0.7-0.9 will not change the optimal solution.

I am not sure what modifiers do you expect in the solve. The code

Code: Select all

        for s in qs:
            Q_sw.clear()
            Q_sw.add_record(s).value = qs[s]
        for j in range(4):
            for k in range(72):
                Qtable.clear()
                Qtable.add_record([str(j),str(k)]).value = qtab.iloc[j][k]
looks extremely fishy to me. Why fill the symbol to clear it again in the next iteration. This way only the last element will survive (it's probably 0 for qtable). With the update type BaseCase you basically solve the original model over and over (with a slightly different Q_sw('72') which has no impact on the solution). Moreover, your indexes i, t, and w in GAMS start with 1. When you fill Qtable you start with 0. That's can't be good either.

Moreover, why bother with model instance. The model executes in no time and the solver takes 19 secs. For this scenario there is absolutely no advantages of using model instance compared to GAMSJobs. Since you seem to know all your "scenarios" ahead of time you could also just use GUSS and stay entirely in GAMS without dealing with Python at all (expect for the scenario reading). Why switch tools at all if you can do it all in one? I have attached the GAMS code (which uses Python embedded code for the reading part) and dies a simple loop to solve the scenarios. I did not bother with the output, but if you really need Python for that, use embedded code again here.

-Michael
new11.gms
(4.5 KiB) Downloaded 32 times

silence
User
User
Posts: 8
Joined: 1 year ago

Re: GamsModifier don't work

Post by silence » 3 months ago

bussieck wrote:
4 months ago
Hi,

I had to adjust your path string not only to match my directory structure but also added the r for raw string (data_dir = r"D:\Users\mbussieck\Downloads\随机生成序列2/"). I could run the Python script okay. I don't know whay goes wrong. before the first solve, the update database contains a single element. I added a "mi.sync_db.export(f'sync{i}.gdx')" before the "mi.solve()" to inspect this at leisure. Qtable was empty and Q_sw consisted of a single element:

Code: Select all

Parameter Qtable(*,*) Theoretical discharge table /
 /;

Parameter Q_sw(*) domestic sewage discharge(10 million m3) /
'72' 0.702251762063773 /;
For the second to fifth teration I got similar GDX files with Qtable all empty and Q_sw values for label '72' with values ranging from 0.70 to 0.91. I am not surprised that you don't see changes in the model when solving. The equation that contains Q_sw('72') is cons9('72') and here the optimal solution has a big slack for cons9 (upper bound is 1.08 while optimal level is 0). Changing the RHS by 0.7-0.9 will not change the optimal solution.

I am not sure what modifiers do you expect in the solve. The code

Code: Select all

        for s in qs:
            Q_sw.clear()
            Q_sw.add_record(s).value = qs[s]
        for j in range(4):
            for k in range(72):
                Qtable.clear()
                Qtable.add_record([str(j),str(k)]).value = qtab.iloc[j][k]
looks extremely fishy to me. Why fill the symbol to clear it again in the next iteration. This way only the last element will survive (it's probably 0 for qtable). With the update type BaseCase you basically solve the original model over and over (with a slightly different Q_sw('72') which has no impact on the solution). Moreover, your indexes i, t, and w in GAMS start with 1. When you fill Qtable you start with 0. That's can't be good either.

Moreover, why bother with model instance. The model executes in no time and the solver takes 19 secs. For this scenario there is absolutely no advantages of using model instance compared to GAMSJobs. Since you seem to know all your "scenarios" ahead of time you could also just use GUSS and stay entirely in GAMS without dealing with Python at all (expect for the scenario reading). Why switch tools at all if you can do it all in one? I have attached the GAMS code (which uses Python embedded code for the reading part) and dies a simple loop to solve the scenarios. I did not bother with the output, but if you really need Python for that, use embedded code again here.

-Michael

new11.gms
Thanks for your answer.
First, I had try the GAMS code which you provided.But it was a pity that it went wrong with error 10 at 10th row.I tried to fix the error,it still doesn't work.Is there a requirement for the version fo Gams and python? GAMS 25.1.3 and Pyhon 3.7 are in my computer, can they make GUSS happen?
Second,let me kindly explain the reason I use model instance with python. Relatively speaking, I am more familiar with python,especially subsequent analysis and plot are largely dependent on python. With your help,I have fix the code of model instance and realized updating two parameters simultaneously.And then, a new problem developed. The same code and input in model instance of python and Gams Studio(for example,the input both are 2.csv ), the optimal solution is different :cry: I checked it many times, but still not sure why, could you help me ?
Thanks a lot!
codes.rar
(302.87 KiB) Downloaded 28 times

User avatar
bussieck
Moderator
Moderator
Posts: 615
Joined: 4 years ago

Re: GamsModifier don't work

Post by bussieck » 3 months ago

There are many good reasons to work with up-to-date software. You should consider to work with an up-to-date version of GAMS. The GMSPython of GAMS 25 probably did not come with pandas. There can be many different reasons why you get different objective functions (did you set optcr to 0?). I think you can get great help if you prepare your questions and the material in a way that many users in the forum can take a look at your problem. Just attaching a rar archive and saying things don't work isn't enough to engage free help.

Best,
-Michael

silence
User
User
Posts: 8
Joined: 1 year ago

Re: GamsModifier don't work

Post by silence » 3 months ago

bussieck wrote:
3 months ago
There are many good reasons to work with up-to-date software. You should consider to work with an up-to-date version of GAMS. The GMSPython of GAMS 25 probably did not come with pandas. There can be many different reasons why you get different objective functions (did you set optcr to 0?). I think you can get great help if you prepare your questions and the material in a way that many users in the forum can take a look at your problem. Just attaching a rar archive and saying things don't work isn't enough to engage free help.

Best,
-Michael
That's very helpful. Thank you very much for your advice. I finally figured out what I have done wrong, there is a parameter don't update with change scenarios. In the Gams statement as follow, ton(i) is a parameter, if GamsModifier(ton), the error 52 VAR $-control appeared. I attempt to change ton(i) as a variable, use ton.l(i) in con2(i,t) equation, and GamsModifier(ton,UpdateAction.Primal,tonlevel). But it reminds ton is not in the model. I try to added an assignment equation,the solution became infeasible. Is my method wrong? Do you have any good suggestions?Please, thanks.

Code: Select all

con2(i,t)..  sum(k$(ord(k)>=max(ord(t)-ton(i)+1,1)and ord(k)<=ord(t)), don(i,k)) =l= d(i,t)  

User avatar
bussieck
Moderator
Moderator
Posts: 615
Joined: 4 years ago

Re: GamsModifier don't work

Post by bussieck » 3 months ago

Again, due to the fast generation and log solution times you should just work with GAMSJob instead of GAMSModelInstance. If you for whatever reason need to work with a GAMSModelInstance you need to reformulate the constraint. Think of a modifier as a fixed variable. Variables can't be used in $() conditions. As you figured out var.l does not help. The var.l value is used when GAMS generates the model instance and does not change afterwards. Often you can rewrite $() logic with the function ifthen (https://www.gams.com/latest/docs/UG_Par ... _22_ifThen). I guess the following should work in your case:

Code: Select all

con2(i,t)..  sum(k$(ord(k)<=ord(t)), ifthen(ord(k)>=max(ord(t)-ton(i)+1,1),1,0)*don(i,k)) =l= d(i,t) ;
It all looks fine, but could be potentially horrible since you, i.e. GAMS generates a dense i,t,k lower triangle matrix block for this equation filled with mostly zeros. In the original $() formulation GAMS will generate a band matrix in the lower triangle depending on the ton values.

GAMSJob is the solution!

-Michael

Post Reply