Python API - Solver Statusthon API - Solver Status Topic is solved

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
mostafagv
User
User
Posts: 1
Joined: 2 weeks ago

Python API - Solver Statusthon API - Solver Status

Post by mostafagv » 2 weeks ago

Hi,

I want to get the flag to see if the model is feasible or infeasible. I tried objects and classes from the tutorial but could not get it to work.

Code: Select all

from gams import *
import os
cwd = os.getcwd()
filename='test_if_else.gms'
ws = GamsWorkspace(system_directory='/Applications/GAMS25.0/sysdir/')

gamscode="""
set i /1,2/;
positive variables
x(i)
y
;
variables
z
;
equations
obj
cons1
cons2
;
obj.. sum[i,x(i)]+y=e=z;
cons1.. 3*sum[i,x(i)]+2*y =g= %s;
cons2.. sum[i,x(i)]+y =l=1;

model test /obj, cons1, cons2/;
solve test using lp minimizing z;
model test2 /obj, cons1/;


if (test.modelStat =1,
	display x.l,y.l;
elseif test.modelStat =4,
	display "model finished with infeasibility";
	solve test2 using lp minimizing z;

	
else
	display "model is not correct";
)


display x.l,y.l;"""%str(5)


filename = "test_write.gms"
file_gams= open(filename,"w")
file_gams.write(gamscode)
file_gams.close()

t1=ws.add_job_from_file(cwd+'/'+filename)

t1.run()

variable_list=['x','y']
for v in variable_list:
    for o in t1.out_db[v]:
        print(o)
        print("%s : "%v,o.marginal )

I appreciate if someone can help.
Thanks
Mostafa GV
Data/Optimization Scientist

User avatar
dirkse
Moderator
Moderator
Posts: 29
Joined: 1 year ago
Location: Fairfax, VA

Re: Python API - Solver Statusthon API - Solver Status

Post by dirkse » 2 weeks ago

Mostafa,

Your GAMS source contains two models, and yet you ask for the flag that indicates if the model is infeasible. What model do you mean? In general, GAMS does not return a code or status about any model, even if there is only one such in the GAMS source. The usual thing to do is create whatever parameters or scalars you want to hold the values you want to get back, make the assignments in the GAMS model, and bring back the data to Python as you'd do with any other parameters.

If you have an instance of a GAMSModelInstance object, then you can query for the model status, solution status, etc. The example Transport8.py https://www.gams.com/latest/docs/apis/e ... 8_8py.html says more about this. But probably that's more than you want at this stage.

-Steve

Post Reply