Embedded Python code - Import parameter with gams.get

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
LuisG
User
User
Posts: 8
Joined: 6 years ago

Embedded Python code - Import parameter with gams.get

Post by LuisG »

Hi all,

I am stuck with the gams.get method. In particular, I am not able to use the parameters defined in GAMS in the Python section. For instance, I have tried the following:

parameter ParA;
ParA = 5;

$onEmbeddedCode Python:

ParA = int(gams.get("ParA"))

$offEmbeddedCode


I get the following error: "Exception from Python: int() argument must be a string, a bytes-like object or a number, not 'ECSymbol"
If I try to import the parameter as a list ... ParA = list(gams.get("ParA")) ... it seems that the list is empty. I have tried some options like ValueFormat, but without success so far.

Do you maybe have an idea what I am missing here?

Best regards,
Luis
User avatar
Clemens
Posts: 57
Joined: 7 years ago

Re: Embedded Python code - Import parameter with gams.get

Post by Clemens »

Luis,

First of all, note that the assignment ParA = 5; happens during GAMS execution time and the $onEmbeddedCode/$offEmbeddedCode section is performaned during GAMS compilation time. That means that in the embedded code section, no value will be assigned to ParA. For more information about GAMS execution/compilation time see: https://www.gams.com/latest/docs/UG_Gam ... ll_TwoPass

The following code uses embedded code at execution time and prints the value of ParA to the GAMS log:

Code: Select all

parameter ParA;
ParA = 5;

embeddedCode Python:
  ParA = list(gams.get("ParA"))[0]
  gams.printLog(str(ParA))
endEmbeddedCode
For more information about embedded code and the structure of the data that is used for passing data between GAMS and Python, you might want to have a look at the documentation: https://www.gams.com/latest/docs/UG_EmbeddedCode.html

Hope that helps,
Clemens
LuisG
User
User
Posts: 8
Joined: 6 years ago

Re: Embedded Python code - Import parameter with gams.get

Post by LuisG »

Hi Clemens,

thank you so much for your answer, this really helps me a lot! I had looked into the documentation, but without understanding my mistake. So it would probably have taken me a long time to find the solution...

Best regards,
Luis
Post Reply