GAMS and Python

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
David
User
User
Posts: 8
Joined: 1 year ago

GAMS and Python

Post by David » 2 months ago

Hello,

I'm trying to run my model through python. I installed the python API and the transport1.py model works. If I however change the ws.add_job_from_file("trnsport.gms") to my model file it gives me a gams return code (2). If I open my sankey.gms file directly through the gams gui then everything works correctly. What am I doing wrong?

Thanks!

Code: Select all

from __future__ import print_function
from gams import *
import os
import sys

version = GamsWorkspace.api_version
sysdir = "c:\\GAMS\\win64\\" + version[0:4]


if __name__ == "__main__":
#    if len(sys.argv) > 1:
#        ws = GamsWorkspace(system_directory = sys.argv[1])
#    else:
    ws = GamsWorkspace(system_directory =sysdir)
    t1 = ws.add_job_from_file("C:\\GAMS\\win64\\24.9\\TEST\\sankey.gms")
    opt = ws.add_options()
    opt.all_model_types = "cplex"
    t1.run(opt)
    print("Ran with cplex:")

Clemens
Posts: 19
Joined: 1 year ago

Re: GAMS and Python

Post by Clemens » 2 months ago

David,

The return code you receive indicates a compilation error. When an instance of GamsWorkspace is created, the working_directory is set to a temporary directory. The GamsJob.run() call runs the model in this directory. If your model relies on certain other files like include or GDX files, the GAMS process might not find them due to the temporary location. A quick solution for this is to set the working_directory of the GamsWorkspace to the directory of your model (e.g. the current directory):

Code: Select all

ws = GamsWorkspace(working_directory=".")
Note that also all temporary files are written to the specified working_directory.

If this does not solve the problem, you can do some debugging. Specify the debug argument of the GamsWorkspace and get the actual working_directory:

Code: Select all

ws = GamsWorkspace(debug=DebugLevel.KeepFiles)
print(ws.working_directory)
Then have a look at the content of the working directory. You will most likely find a .lst file which has certain information about your model and reveals the cause of errors.

Best,
Clemens

David
User
User
Posts: 8
Joined: 1 year ago

Re: GAMS and Python

Post by David » 2 months ago

Wow that works beautifully !

Thank you very much!

Post Reply