GAMS and Python

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

GAMS and Python

Post by David » 1 year 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:")

User avatar
Clemens
Posts: 46
Joined: 3 years ago

Re: GAMS and Python

Post by Clemens » 1 year 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: 2 years ago

Re: GAMS and Python

Post by David » 1 year ago

Wow that works beautifully !

Thank you very much!

Myron Bennett
User
User
Posts: 4
Joined: 1 year ago

Re: GAMS and Python

Post by Myron Bennett » 1 year ago

cmislib API suets me as a developer from implementation details, although it provides an intuitive way to work ... Hopefully, the compilation error will be eliminated, I gonna take this review,thanks

GeorgeHouse
User
User
Posts: 1
Joined: 6 months ago

Re: GAMS and Python

Post by GeorgeHouse » 6 months ago

Hey Clemens, thanks so much for putting it. The code works wonderfully. Really really appriciated!!!

Sapnapawar
User
User
Posts: 1
Joined: 1 month ago

Re: GAMS and Python

Post by Sapnapawar » 1 month ago

WOW! It's really a great information. This was really helpful for me. Good work. Keep up!

Post Reply