Python API trigger to solve problem from a file Topic is solved

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

Python API trigger to solve problem from a file

Post by bdasovic »

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
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: Python API trigger to solve problem from a file

Post by bussieck »

The very first example (https://www.gams.com/latest/docs/apis/e ... ource.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
bdasovic
User
User
Posts: 3
Joined: 2 years ago

Re: Python API trigger to solve problem from a file

Post by bdasovic »

bussieck wrote: 1 year ago The very first example (https://www.gams.com/latest/docs/apis/e ... ource.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"

Code: Select all

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:

Code: Select all

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.
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: Python API trigger to solve problem from a file

Post by bussieck »

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/p ... c2c34e92c4. 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.
bdasovic
User
User
Posts: 3
Joined: 2 years ago

Re: Python API trigger to solve problem from a file

Post by bdasovic »

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 :D

bdasovic
Post Reply