Defining initial values

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
bfsantoro
User
User
Posts: 6
Joined: 4 years ago

Defining initial values

Post by bfsantoro »

reactorModel_v6.gms
(5.87 KiB) Downloaded 296 times
I am currently struggling with the definition of initial conditions when using the Python API. As far as I understand, when running a model from GAMS IDE the level value is considered as the initial point of the search. Therefore, if I call the same solve statement twice without changing any of the level values, the initial point of the second call is the optimal solution of the previous one, and then the solution of both problems is exactly the same.

However, with the Python API it seems to be harder to access the level values of variables. My intention with the attached code was to manually define initial values for ALL variables in the model and then solve it multiple times. My rationale was that if the initial point was always the same, the solution would also be the same. However, there are some small changes in the objective function. For the present example, the results of 4 runs are:

Obj: 110.47759190140623,
Obj: 110.47759190141258
Obj: 110.47759190457893
Obj: 110.47759190358889

For this particular minimal example the change is almost negligible, but for my actual complete model it could be as much as 3 orders of magnitude.

Any help in how to change my code to be able to achieve exactly the same result while manually defining the initial conditions will be much appreciated. I am using Gams 31 and its Python Interface, with Python 3.8, but the same problem happens with different versions too.

PS: I apologize for this not-so-minimal MWE. I tried to reproduce this issue with smaller models but when a GamsModifier was used to define the Primal of all variables, the optimization results turned out to be consistently the same.
py_NMPC_Caller.gms
This file is actually a Python Script. I had to change the extension to be able to upload it.
(3.72 KiB) Downloaded 307 times
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Defining initial values

Post by bussieck »

You also get different results if you start Conopt from the same point just in plain GAMS:

Code: Select all

y.l('ny1',t) = 54.2249;
y.l['ny2',t] = 397.0828;
y.l['ny3',t] = 0.659;

u.l['nu1',t] = 0.1;
u.l['nu2',t] = 325;

uCont.l['nu1',tCont] = 0.1;
uCont.l['nu2',tCont] = 325;

obj.l = 0;

solve reactor using NLP minimizing obj;

y.l('ny1',t) = 54.2249;
y.l['ny2',t] = 397.0828;
y.l['ny3',t] = 0.659;

u.l['nu1',t] = 0.1;
u.l['nu2',t] = 325;

uCont.l['nu1',tCont] = 0.1;
uCont.l['nu2',tCont] = 325;

obj.l = 0;

solve reactor using NLP minimizing obj;
gives you

Code: Select all

     31   4        1.1087270812E+02 8.0E+00     7 1.6E+00    6 F  T
     36   4        1.1047759101E+02 1.2E-05     7 1.0E+00    2 F  T
     40   4        1.1047759190E+02 4.3E-10     7
 
 ** Optimal solution. Reduced gradient less than tolerance.
and

Code: Select all

     46   4        1.1047759190E+02 2.4E-03     7 1.0E+00    2 F  T
     48   4        1.1047759190E+02 5.6E-10     7
 
 ** Optimal solution. Reduced gradient less than tolerance.
Don't look at the values but since it does different number of iterations indicates that is takes a different path and hence you might end up with slightly different solutions. You also need to set the dual solution or instruct Conopt to ignore the dual information from of a starting point. This can be done, e.g. by setting bratio (in the GAMS model) to 1: option bratio=1; If I do that I get identical solutions:

Code: Select all

python py_NMPC_Caller.py | grep Obj
  Obj: 110.47759190140746
  Obj: 110.47759190140746
  Obj: 110.47759190140746
  Obj: 110.47759190140746
  Obj: 110.47759190140616
  Obj: 110.47759190140616
  Obj: 110.47759190140616
  Obj: 110.47759190140616 
-Michael
Post Reply