GAMSPy uses a hierarchical structure to build optimization models:
- Container: A
Container acts as a centralized hub that gathers all sets, parameters, variables, and equations. - Sets: Define the indices (e.g., plants, markets) for the model. They can be declared separately or combined with data assignment.
- Parameters: Indexed data (e.g., supply, demand, costs). They use a
domain attribute to link to specific Set objects. - Variables: The decision variables (e.g., shipment quantities) defined with a
domain and a type (e.g., "Positive"). - Equations: Mathematical constraints. They require a declaration (name, domain) and a definition (the algebraic relationship).
- Model: Consolidates equations, an objective function, a
Sense (e.g., Sense.MIN), and a problem type (e.g., "LP").
from gamspy import Container, Set, Parameter, Variable, Equation, Model, Sum, Sense
m = Container()
# Define Sets
i = Set(container=m, name="i", records=["seattle", "san-diego"])
j = Set(container=m, name="j", records=["new-york", "chicago"])
# Define Parameters
a = Parameter(container=m, name="a", domain=i, records=["seattle", 350])
# Define Variables
x = Variable(container=m, name="x", domain=[i, j], type="Positive")
# Define Equations
supply = Equation(container=m, name="supply", domain=i)
supply[i] = Sum(j, x[i, j]) <= a[i]
# Define Model
obj = Sum((i, j), x[i, j])
transport = Model(m, name="transport", equations=[supply], problem="LP", sense=Sense.MIN, objective=obj)