Search code examples
pyomogams-math

Unexplained error using GAMS as pyomo solver


I am planning to learn python and GAMS for my research. So, I am trying to solve an NLP problem using GAMS as a pyomo solver, but occasionally I am faced with puzzling errors.

For example, I wrote the following program to determine the parameters of y=a1+a2*x using the least squares method.

#Example1 Regression
import pyomo.environ as pyo

#Model definition
model_linear = pyo.ConcreteModel();


#Set declaration
model_linear.m = pyo.RangeSet(6);

#variable definition
model_linear.a1 = pyo.Var(domain=pyo.Reals);
model_linear.a2 = pyo.Var(domain=pyo.Reals);



#Parameter declaration
model_linear.datapoints_y = pyo.Param(model_linear.m,initialize={1:127,2:151,3:379,4:421,5:460,6:426});
model_linear.datapoints_x = pyo.Param(model_linear.m,initialize={1:-5,2:-3,3:-1,4:5,5:3,6:1});


#objective functions
model_linear.obj = pyo.Objective(expr=sum((model_linear.datapoints_y[m]-
                                (model_linear.a1+model_linear.a2*model_linear.datapoints_x[m]))**2
                                for m in model_linear.m),sense=pyo.minimize);


#Solver options

solver=pyo.SolverFactory('gams')
solver.options['mtype']= "nlp"
results = solver.solve(model_linear, solver = 'antigone');


results.write()
print("\n Results \n");
print("Squared deviation for linear regression model =",model_linear.obj());
print("Coefficient 1 for linear regression (a1) =", model_linear.a1());
print("Coefficient 2 for linear regression (a2) =", model_linear.a2());

When I run the program, I get the following message.

In [ ]:runfile('C:/Users/Murata/Documents/python work/GAMStest/NLPexam1a.py', wdir='C:/Users/Murata/Documents/python work/GAMStest')
Traceback (most recent call last):

 File "C:\Users\Murata\anaconda3\envs\ct-env\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
   exec(code, globals, locals)

 File "c:\users\murata\documents\python work\gamstest\nlpexam1a.py", line 32, in <module>
   results = solver.solve(model_linear, solver = 'antigone');

 File "C:\Users\Murata\anaconda3\envs\ct-env\lib\site-packages\pyomo\solvers\plugins\solvers\GAMS.py", line 853, in solve
   model_soln, stat_vars = self._parse_dat_results(

 File "C:\Users\Murata\anaconda3\envs\ct-env\lib\site-packages\pyomo\solvers\plugins\solvers\GAMS.py", line 1235, in _parse_dat_results
   model_soln[items[0]] = (items[1], items[2])

IndexError: list index out of range

I think error seems to occur from program to manipulate GAMS from Python. What steps can I take to solve the problem? If there are any mistakes in my code, please point them out.

The environment I'm using; OS:Windows 10 pro Python:3.10.8 pyomo:6.4.2 GAMS:40.4.0

Any help would be much appreciated and thanks in advance for your time!


Solution

  • No problems here running your code on W10 with GAMS 38 and Pyomo 6.2.

    It is also solvable using scip solver (download here: https://www.scipopt.org/download.php?fname=scipampl-7.0.0.win.x86_64.intel.opt.spx2.exe.zip), and change the solver to e.g.: solver = pyo.SolverFactory("scip", executable='C:/Python/Pyomo/Solvers/scip/scipampl-7.0.0.win.x86_64.intel.opt.spx2.exe')

    Cheers, GFA