Just Open, Run and Exit GAMS using Python

Archive of Gamsworld Google Group
Archiver
User
User
Posts: 7876
Joined: 1 year ago

Just Open, Run and Exit GAMS using Python

Postby Archiver » 3 years ago


Hello,

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:

Open a particular GAMS model file (eg. bandwidth.gms)
Run the MIP model
Read the optimal solution after the model is solved and write it in a text file.
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.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 1 year ago

Re: Just Open, Run and Exit GAMS using Python

Postby Archiver » 3 years ago


Hi,

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!

Am Freitag, 11. Juli 2014 18:11:32 UTC+2 schrieb Abhay Lidbe:

Hello,

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:

Open a particular GAMS model file (eg. bandwidth.gms)
Run the MIP model
Read the optimal solution after the model is solved and write it in a text file.
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.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 1 year ago

Re: Just Open, Run and Exit GAMS using Python

Postby Archiver » 3 years ago


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"")
job.run()
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.

Best
Clemens


On 06.11.2014 16:35, Tim Schröder wrote:
> Hi,
>
> 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!
>
> Am Freitag, 11. Juli 2014 18:11:32 UTC+2 schrieb Abhay Lidbe:
>
> Hello,
>
> 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:
>
> Open a particular GAMS model file (eg. bandwidth.gms)
> Run the MIP model
> Read the optimal solution after the model is solved and write it in a text file.
> 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.
>
> --
> To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
> To post to this group, send email to gamsworld@googlegroups.com.
> Visit this group at http://groups.google.com/group/gamsworld.
> For more options, visit https://groups.google.com/d/optout.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 1 year ago

Re: Just Open, Run and Exit GAMS using Python

Postby Archiver » 2 years ago


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,
Majed

On Friday, November 7, 2014 at 7:39:19 AM UTC, C. Westphal wrote:

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"")
job.run()
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.

Best
Clemens


On 06.11.2014 16:35, Tim Schröder wrote:
> Hi,
>
> 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!
>
> Am Freitag, 11. Juli 2014 18:11:32 UTC+2 schrieb Abhay Lidbe:
>
> Hello,
>
> 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:
>
> Open a particular GAMS model file (eg. bandwidth.gms)
> Run the MIP model
> Read the optimal solution after the model is solved and write it in a text file.
> 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.
>
> --
> 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.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 1 year ago

Re: Just Open, Run and Exit GAMS using Python

Postby Archiver » 2 years ago


Majed,
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 ... l?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

Best
Clemens


On 10/28/2015 4:00 PM, Majed Khadem wrote:
> 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,
> Majed
>
> On Friday, November 7, 2014 at 7:39:19 AM UTC, C. Westphal wrote:
>
> 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"")
> job.run()
> 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.
>
> Best
> Clemens
>
>
> On 06.11.2014 16:35, Tim Schröder wrote:
>> Hi,
>>
>> 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!
>>
>> Am Freitag, 11. Juli 2014 18:11:32 UTC+2 schrieb Abhay Lidbe:
>>
>> Hello,
>>
>> 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:
>>
>> Open a particular GAMS model file (eg. bandwidth.gms)
>> Run the MIP model
>> Read the optimal solution after the model is solved and write it in a text file.
>> 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.
>>
>> --
>> 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.
>
> --
> To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
> To post to this group, send email to gamsworld@googlegroups.com.
> Visit this group at http://groups.google.com/group/gamsworld.
> For more options, visit https://groups.google.com/d/optout.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 1 year ago

Re: Just Open, Run and Exit GAMS using Python

Postby Archiver » 1 year ago


I had a similar problem - im having issues importing dll files

ImportError Traceback (most recent call last)
in () ----> 1 import gams C:\GAMS\win64\24.6\apifiles\Python\api_34\gams\__init__.py in () 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" ] 7 ----> 8 from .workspace import * 9 from .database import * 10 from .options import * ImportError: DLL load failed: The specified module could not be found.


On Friday, July 11, 2014 at 12:11:32 PM UTC-4, Abhay Lidbe wrote:

Hello,

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:

Open a particular GAMS model file (eg. bandwidth.gms)
Run the MIP model
Read the optimal solution after the model is solved and write it in a text file.
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.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 1 year ago

Re: Just Open, Run and Exit GAMS using Python

Postby Archiver » 1 year ago


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?

Em sexta-feira, 7 de novembro de 2014 07:39:19 UTC, C. Westphal escreveu:

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"")
job.run()
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.

Best
Clemens


On 06.11.2014 16:35, Tim Schröder wrote:
> Hi,
>
> 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!
>
> Am Freitag, 11. Juli 2014 18:11:32 UTC+2 schrieb Abhay Lidbe:
>
> Hello,
>
> 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:
>
> Open a particular GAMS model file (eg. bandwidth.gms)
> Run the MIP model
> Read the optimal solution after the model is solved and write it in a text file.
> 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.
>
> --
> 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.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.


Who is online

Users browsing this forum: No registered users and 1 guest