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

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

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

Post 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
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

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

Post 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
vbind
User
User
Posts: 7
Joined: 4 years ago

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

Post 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
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

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

Post 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
Post Reply