Page 1 of 1

x.fx(i,j) equivalent in Python API

Posted: Wed Nov 11, 2020 8:22 pm
by vbind
Hello everybody,

I would like to know how I can fix some variables in a model from the Python API. For example in transport4.py (as included in GAMS). Can anybody provide some example code?

Best regards,
Viktor

Re: x.fx(i,j) equivalent in Python API

Posted: Fri Dec 04, 2020 4:23 pm
by abhosekar
In Python tutorial, please follow the example transport7.py that shows how to set an upper bound on a variable using GamsModifier.

Using the same idea, you can do the following.

Code: Select all

mi.instantiate("diet use lp min cost ", GamsModifier(x, UpdateAction.Fixed, xfixed))
where xfixed is a parameter, x is a variable. Please note that the only difference compared to transport7.py is that instead of UpdateAction.Upper, you are now using UpdateAction.Fixed.

If you would like to fix a specific 'i', 'j' pair, you simply define that while adding the value using .add_record() as shown in transport7.py.

Hope this helps!

- Atharv

Re: x.fx(i,j) equivalent in Python API

Posted: Tue Dec 22, 2020 9:56 am
by vbind
Dear Atharv,

thanks a lot. This is what I was looking for.

In the example transport7.py UpdateAction.Upper was used to fix the value of the variable x (in combination with the fact that variable x is positive). Is there a specific reason to use UpdateAction.Upper instead of UpdateAction.Fixed?

And is it possible to show the GAMS code which is equivalent to the model as solved via the Python API?

Best regards,
Viktor

Re: x.fx(i,j) equivalent in Python API

Posted: Tue Dec 22, 2020 3:55 pm
by abhosekar
In the demo example, the purpose is to show a way to set an upper bound. It should be noted that in transport7.py, the upper bound is not set to 0. The number 0 which you find as an argument to add_parameter is the dimensionality of the parameter. The upper bound is defined using bmultlist.

The best way to check anything that you changed from python is through python. For example, you can check the value of your variable x
print(mi.sync_db["x"].first_record().level)

Hope this helps!

- Atharv