embeddedcode: Python

Problems with syntax of GAMS
Post Reply
djain42
User
User
Posts: 1
Joined: 2 weeks ago

embeddedcode: Python

Post by djain42 »

Hi,

I am trying to get the level values of my variable using embeddedcode: Python to do some transformation on them in Python and finally output a CSV file. I am running into an issue while doing gams.get('x.l'). There must be some other way to get the level values. Does anyone have any idea how to do this?
Thanks in advance
User avatar
Clemens
Posts: 63
Joined: 7 years ago

Re: embeddedcode: Python

Post by Clemens »

Hi,

With gams.get() in embedded code Python, you will get all values of a symbol - e.g. level, marginal, etc. for a GAMS variable and need to process them manually afterwards. Here is some code snippet that reads and prints a variable x:

Code: Select all

embeddedCode Python:
var_x = list(gams.get('x'))
print(var_x)
endEmbeddedCode
For more information see the documentation for embedded Python code: https://www.gams.com/latest/docs/UG_Emb ... ode_Python

Depending on the transformations that you want to do, GAMS Connect might also be helpful for you:

Code: Select all

embeddedCode Connect:
- GAMSReader:  # read variable x from GAMS into the Connect database
    symbols:
      - name: x
- Projection:  # turn variable x into parameter x_new with level values only
    name: x.l(i,j)
    newName: x_new(i,j)
    asParameter: True
- CSVWriter:  # write x_new to CSV
    file: x_level.csv
    name: x_new
endembeddedCode
For more information on GAMS Connect, see the documentation: https://www.gams.com/latest/docs/UG_GAMSCONNECT.html

Best,
Clemens
Post Reply