Hello everyone and thanks in forehand for any help with my problem.
So, I have developed my model, and let’s just say it is placed on my PC in the directory “C:\My_problem\My_model.gms”. The directory contains my excel file with problem input data and my GDX file. When I start optimization from the GAMS application everything works fine but somehow when I try to start optimization from Python I get errors. Everything I have found about using Python API is too complicated. Is there a simplest possibility of just a trigger function? Like, find the model at location “C:\My_problem\My_model.gms” and run optimization. All the information on where GAMS can read input parameters from Excel is already in my .gms file so I wouldn’t like to define any of that in Python code. Sorry if the question is trivial, but I am new to Python programming and I would like to develop an application to automate my processes.
Greetings,
bdasovic
The very first example (https://www.gams.com/latest/docs/apis/examples_python/transport1_8py_source.html) should do most of this. You need to add the working_directory=r’C:\My_problem’ argument to the GamsWorkspace constructor. Don’t do the “ws.gamslib(‘trnsport’)” and point to your GAMS model in the add_job_from_file method “t1 = ws.add_job_from_file(‘My_model.gms’)”. That should do it.
-Michael
So i have tried following for gms code in the directory “C:/Users/borna/OneDrive/Documents/GAMS/Studio/workspace” and file “Doktorat_MINLP_model.gms”
from gams import *
import os
import sys
if __name__ == "__main__":
if len(sys.argv) > 1:
ws = GamsWorkspace(working_directory=r'C:/Users/borna/OneDrive/Documents/GAMS/Studio/workspace')
else:
ws = GamsWorkspace()
ws.gamslib("Doktorat_MINLP_model.gms")
t1 = ws.add_job_from_file('Doktorat_MINLP_model.gms')
t1.run()
print("Ran with Default:")
but it returns an error:
PS C:\Users\borna\OneDrive\Radna površina\MINLP Schedule Optimization\code files> & C:/Users/borna/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/borna/OneDrive/Radna povrsina/MINLP Schedule Optimization/code files/runGAMS.py"
*** ERROR in gamslib
No entry found for trnsport.gms
Traceback (most recent call last):
File "c:\Users\borna\OneDrive\Radna površina\MINLP Schedule Optimization\code files\runGAMS.py", line 12, in <module>
ws.gamslib("trnsport.gms")
File "C:\Users\borna\AppData\Local\Programs\Python\Python39\lib\site-packages\gams\workspace.py", line 646, in gamslib self._xxxlib("gams", model)
File "C:\Users\borna\AppData\Local\Programs\Python\Python39\lib\site-packages\gams\workspace.py", line 639, in _xxxlib raise GamsException(libname + "lib return code not 0 (" + str(exitcode) + ")")
gams.workspace.GamsException: gamslib return code not 0 (2)
Am I doing something wrong here? Thank you in forehand.
What about the Don’t do the “ws.gamslib(‘trnsport’)”? You should understand what the methods do. That’s why there is documentation: https://www.gams.com/latest/docs/apis/python/classgams_1_1workspace_1_1GamsWorkspace.html#a894a7073a0c32ff4f87a45c2c34e92c4. The gamslib retrieves a model from the GAMS Model Library. Just don’t do the gamslib call at all.
-Michael
PS Never run in synchronized folders. GAMS interacts with the disk in various ways and with synchronized folders like OneDrive, GoogleDrive or similar as working directory because the file locking policies of these synced folders can cause trouble.
Dear Michael,
I am really sorry if my questions are trivial, but as I said earlier I don’t have much experience with Python. Thank you for your help and suggestions, I have managed to make my code work as intended.
Thank you once again
bdasovic