Container#
The Container class in GAMSPy serves as a central hub for managing essential data structures such as sets, parameters, variables,
and constraints, providing a structured approach for optimization problems.
Symbol Creation#
Every symbol in your optimization problem must belong to a Container.
All symbols added to a Container can be accessed by indexing into the Container:
from gamspy import Container, Set
m = Container()
i = Set(m, "i", records=["seattle", "san-diego"])
print(m["i"]) # returns a reference to i variable
Each symbol is added to the container as soon as it is created. If the symbol already exists in the container, the existing symbol is returned.
from gamspy import Container, Set
m = Container()
i1 = Set(m, "i", records=["seattle", "san-diego"])
i2 = Set(m, "i", records=["seattle", "san-diego"])
print(id(i1) == id(i2)) # True
Creating a symbol with the same name but different records overwrites the records of the existing symbol.
from gamspy import Container, Set
m = Container()
i1 = Set(m, "i", records=["seattle", "san-diego"])
i2 = Set(m, "i", records=["seattle", "san-diego", "topeka"])
print(id(i1) == id(i2)) # True
print(i2.records) # ['seattle', 'san-diego', 'topeka']
An alternative way to create a symbol in GAMSPy and add it to the container is as follows
from gamspy import Container
m = Container()
i = m.addSet("i", records=["seattle", "san-diego"])
print(i.records)
Symbols can be created without any data. Data can be provided later
from gamspy import Container
m = Container()
i = m.addSet("i")
i.setRecords(["seattle", "san-diego"])
print(i.records)
Explicit symbols names are useful when interacting with parts of the module where symbols need to be recognized by name, e.g. toLatex and GDX imports or exports (see below). If no name is provided, GAMSPy will autogenerate a name
from gamspy import Container
m = Container()
i = m.addSet()
print(i.name) # something like 's795f053a_7d21_4a17_a6c3_5a947e051930'
Warning
.records attribute of a symbol contains a Pandas DataFrame which holds the symbol’s records and
should be treated as a read-only attribute. If you want to change the records of a symbol, use
setRecords function. setRecords ensures that the GAMSPy state is synchronized with GAMS
execution system.
Reading and Writing Symbols#
The Container class provides I/O functions for reading and writing symbols to GAMS Data eXchange (GDX) files.
Writing#
Symbols created within a specific Container can be saved to a GDX file using the write function.
from gamspy import Container, Set
m = Container()
i = Set(m, "i", records=["seattle", "san-diego"])
m.write("data.gdx")
Reading#
Symbol records can be read from a GDX file by either specifying the load_from argument during the Container construction or by using the read function.
To create a Container with symbols from a GDX file, use the load_from argument:
from gamspy import Container
m = Container(load_from="data.gdx")
print(m.listSymbols())
We can verify that symbol i is in the container m.
Alternatively, you can use the read function to populate the container.
from gamspy import Container
m = Container()
m.read("data.gdx")
print(m.listSymbols())
Loading Records to Existing Symbols#
You can load the records of a symbol from a GDX file if the symbol is already declared by using loadRecordsFromGdx.
from gamspy import Container
m = Container()
i = Set(m, name="i")
m.loadRecordsFromGdx("data.gdx")
print(i.records)
The only difference between read and loadRecordsFromGdx is that while read creates the symbol in the Container
if it does not already exist, loadRecordsFromGdx requires the symbol to be declared beforehand.
Generating the Executed GAMS Code#
GAMSPy utilizes the GAMS execution system and instructs it to perform certain operations. You can check these executed operations by inspecting the corresponding GAMS code at any point in the program by calling generateGamsString.
This feature is available for avid GAMS users who want to see what’s being executed behind the scenes. For more details, see the
Inspecting Generated GAMS String section of the Debugging and Performance page.