Just Open, Run and Exit GAMS using Python

Skip to first unread message

Abhay Lidbe

Jul 11, 2014, 12:11:32 PM7/11/14
to gams...@googlegroups.com

I am developing a routine in Python where it operates a simulation software, reads the parameter values, sends it to GAMS, optimizes and sends back the solution to the simulation software for another run.

I am completely new to Python. So far I am able to control the simulation software by Python. But I am not able to do the same for GAMS. Went through the GAMS API tutorial but of not much help. I just need to perform following basic activities:
  1. Open a particular GAMS model file (eg. bandwidth.gms)
  2. Run the MIP model
  3. Read the optimal solution after the model is solved and write it in a text file.
  4. Exit (close) the GAMS model

All other required algorithms, input file, solver choice, etc are written in the .gms file, hence I don't really want to get access to those features. Also my 'apifiles' folder required to run GAMS in python is located in the path C:\\Program Files\... which needs  administrator's permission for access.

Can anyone help me with the snipped of the code to be used in Python for this purpose?

Thanks in advance.

Tim Schröder

Nov 6, 2014, 10:35:52 AM11/6/14
to gams...@googlegroups.com

I am having a similer problem. Did you find a solution by now and would you mind sharing it with me? I am not quite understanding how to access models which are not in the model library already. Thanks in advance!

Clemens Westphal

Nov 7, 2014, 2:39:19 AM11/7/14
to gams...@googlegroups.com
You can create a GamsJob instance from a GAMS file (.gms) by using the add_job_from_file()
method of the GamsWorkspace class and can execute it by using the run() method. The data (including
the results) of the model can be accessed using the property out_db of the GamsJob after the run()
method has carried out. See the following code snippet:

ws = GamsWorkspace()
job = ws.add_job_from_file("C:\\myFolder\\myModel.gms"")
GamsDatabase db = job.out_db

I encourage you to have a look at the Python API tutorial (http://www.gams.com/dd/docs/api/python/index.html)
This should provide all the basic information you need to start using it. You might also want to look at the
example located at "[myGamsFolder]\apifiles\Python". Especially the example "transport1.py" might be helpful
if you are very new to the Python API.

You received this message because you are subscribed to the Google Groups "gamsworld" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.
To post to this group, send email to gams...@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Majed Khadem

Oct 28, 2015, 11:01:22 AM10/28/15
to gamsworld
Hi Clements,

Thanks for your helpful post. I'm keen to try solving a gams model from python. But the code snippet you provided seems to lack some library import lines at the beginning. Can you please clarify it and introduce any neccesarry module to run a gams code from python.

Many thanks,

Clemens Westphal

Oct 28, 2015, 11:51:23 AM10/28/15
to gams...@googlegroups.com
we have several examples on how to use the Python API that come with the GAMS distribution. The best resource for you to
get started is: http://www.gams.com/help/topic/gams.doc/apis/python/index.html?cp=0_4_2
If you click on the Tutorial link, you will find helpful information that should provide you with everything you need to start.
Running examples can be found in your GAMS installation: [GAMSDIR]/apifiles/Python


Graeme Pillemer

Feb 19, 2016, 2:35:40 AM2/19/16
to gamsworld
I had a similar problem - im having issues importing dll files
ImportError                               Traceback (most recent call last)
<ipython-input-5-b682e4e62ed2> in <module>()
----> 1 import gams

C:\GAMS\win64\24.6\apifiles\Python\api_34\gams\__init__.py in <module>()
      6            "PageContr", "Action", "LogLine", "AppendOut", "StepSum", "Suppress", "DFormat", "TFormat", "IntVarUp", "Case", "DumpParms", "ForceWork", "DumpOpt", "ErrMsg", "StringChk", "CharSet", "Keep", "TraceOpt", "SysOut", "SolPrint", "HoldFixed", "On115", "ExecMode", "InteractiveSolver", "FileCase", "SavePoint", "SolveLink", "Sys10", "Sys11", "ZeroResRep", "gdxCompress", "gdxConvert", "AppendExpand", "NoNewVarEqu", "gdxUels", "strictSingleton", "FDOpt", "PrefixLoadPath" ]
----> 8 from .workspace import *
      9 from .database import *
     10 from .options import *

ImportError: DLL load failed: The specified module could not be found.

Sofia Raimundo

Apr 28, 2016, 11:48:55 PM4/28/16
to gamsworld
I tryed to use this code also, but I get an error to, like this: Parsing error SyntaxError: EOL while scanning string literal (line 74)

Can someone help me please?
Reply all
Reply to author
0 new messages