How do I call the gams solvers with a .gms but without openning gams ? (python)

262 views
Skip to first unread message

Lucas Ianni

unread,
Aug 5, 2012, 9:40:09 AM8/5/12
to gams...@googlegroups.com
Hi

Ive made a specific python program to solve a optimization problem =)
The user enter the problem calling the functions and my program convert it to GAMS logic and save in a .gms format!!!
But I dont want to give the user the work to open the GAMS and click the F9 button hehehe...

To call open my .gms file directly from python its pretty easy but i want to run GAMS ( and and dont know how do this ). For my any suggestion about how to run GAMS externally in windows would help!

there is a way to put commands in shortcut ? like C:\GAMS\win64\23.8\gamside.exe -solve mymode.gams ? Other ideas ?

Thank you!

Lucas Ianni

unread,
Aug 27, 2012, 11:16:35 AM8/27/12
to gams...@googlegroups.com
ow thanks for the answer man!

Ive solved it in a simple way:

startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        output = subprocess.Popen("gams " + gamspath + "\\" + filename + ".gms Lo=2 Lf="+ gamspath + "\\" + filename +"solved.txt" , stdout=subprocess.PIPE, startupinfo=startupinfo)

solvedFile = open(gamspath + "\\" + filename +"solved.txt" )
      
I dont know way but the stdout wasnt giving the .log of cplex solver...so I decided to save it in a .txt file heehehe

Thanks for the help!          

2012/8/27 Johannes Hedtrich <jhed...@googlemail.com>
Just use the gams.exe ? Python surely has a way to execute a program/shell command, so it would merely be:

(whatever python needs).call "path_to/gams.exe mymodel.gms"

Capture the output of STDOUT and STDERR and check that for successful completion etc.

--
You received this message because you are subscribed to the Google Groups "gamsworld" group.
To view this discussion on the web visit https://groups.google.com/d/msg/gamsworld/-/aAPNqxOofVoJ.
To post to this group, send email to gams...@googlegroups.com.
To unsubscribe from this group, send email to gamsworld+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.

Sofia Raimundo

unread,
Apr 28, 2016, 9:56:22 AM4/28/16
to gamsworld
Hi!
I'm trying to do the same, but what's the gamspath? When I run your code it gives me an error at this line  "output = subprocess.Popen("gams " + gamspath + "\\" + filename + ".gms Lo=2 Lf="+ gamspath + "\\" + filename +"solved.txt" , stdout=subprocess.PIPE, startupinfo=startupinfo)" saying "NameError: name 'gamspath' is not defined". What can I do?

Claudio Delpino

unread,
Apr 28, 2016, 10:12:40 AM4/28/16
to gams...@googlegroups.com
Hola Sofía: 

Py error is pretty clear: "NameError: name 'gamspath' is not defined". 

In the code you are reusing, probably Lucas defined gamspath as a string holding the path to a folder where the .gms file is to be found and where the output will be placed. 

The path in informatics refers to the location in the filesystem. I.e. "C:\mymodel"

Regards
Claudio

--
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.

Sofia Raimundo

unread,
Apr 28, 2016, 10:37:15 AM4/28/16
to gamsworld
Thank's!
I changed my code and before this code I defined:

gamspath= "F:\Otim\correct"
filename= "F:\Otim\correct\El_GrV03.gms"

but still gives error: 
"Runtime error 
Traceback (most recent call last):
  File "<string>", line 47, in <module>
  File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 896, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified"

Can you help me please?

Sofia Raimundo

unread,
Apr 28, 2016, 11:25:26 AM4/28/16
to gamsworld
Hi Claudio!

I changed my code based on your answer, and before that code I defined:
gamspath= "F:\Otim\correct"
filename= "F:\Otim\correct\ElGrV03.gms"

but it still gives error like this:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 47, in <module>
  File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\ArcGIS10.2\Lib\subprocess.py", line 896, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Can you help me please? Thank's!

Em quinta-feira, 28 de abril de 2016 15:12:40 UTC+1, Claudio Delpino escreveu:

Sofia Raimundo

unread,
Apr 28, 2016, 11:48:55 PM4/28/16
to gamsworld


Em domingo, 5 de agosto de 2012 14:40:09 UTC+1, Lucas Ianni escreveu:

Sofia Raimundo

unread,
Apr 28, 2016, 11:48:55 PM4/28/16
to gamsworld
So you don't know how can I run a file .gms from python script?


Em domingo, 5 de agosto de 2012 14:40:09 UTC+1, Lucas Ianni escreveu:
Reply all
Reply to author
Forward
0 new messages