Quick Start Guide#


This quick start guide is designed to provide you with a concise overview of GAMSPy and its key features. By the end of this guide, you’ll have a solid understanding of how to create basic mathematical models using GAMSPy. For more advanced features, we recommend exploring our comprehensive user guide and the extensive model library.

While not mandatory, having a basic understanding of Python programming and familiarity with the Pandas library. will be helpful in following this tutorial.

A Transportation Problem#

In this guide, we’ll delve into an example of the transportation problem. This classic scenario involves managing supplies from various plants to meet demands at multiple markets for a single commodity. Additionally, we have the unit costs associated with shipping the commodity from plants to markets. The fundamental economic question here is:

How can we optimize the shipment quantities between each plant and market to minimize the total transport cost?

The problem’s algebraic representation typically takes the following format:

Indices (Sets):

  • \(i\) = plants

  • \(j\) = markets

Given Data (Parameters):

  • \(a_i\) = supply of commodity at plant \(i\) (in cases)

  • \(b_j\) = demand for commodity at market \(j\) (in cases)

  • \(c_{ij}\) = cost per unit of shipment between plant \(i\) and market \(j\)

Decision Variables:

  • \(x_{ij}\) = amount of commodity to ship from plant \(i\) to market \(j\) where \(x_{ij} \ge 0\) for all \(i,j\)


  • Observe supply limit at plant \(i\): \(\sum_j x_{ij} \le a_i \: \forall i\)

  • Satisfy demand at market \(j\): \(\sum_i x_{ij} \ge b_j \: \forall j\)

  • Objective Function: Minimize \(\sum_i \sum_j c_{ij} \cdot x_{ij}\)


Before we dive into the optimization process, let’s handle our data using the Pandas library. We’ll begin by organizing the necessary information, which we will subsequently feed into our optimization model.

import pandas as pd

capacities = pd.DataFrame(
    [["seattle", 350], ["san-diego", 600]], columns=["city", "capacity"]

seattle 350
san-diego 600
demands = pd.DataFrame(
    [["new-york", 325], ["chicago", 300], ["topeka", 275]], columns=["city", "demand"]

new-york 325
chicago 300
topeka 275
distances = pd.DataFrame(
        ["seattle", "new-york", 2.5],
        ["seattle", "chicago", 1.7],
        ["seattle", "topeka", 1.8],
        ["san-diego", "new-york", 2.5],
        ["san-diego", "chicago", 1.8],
        ["san-diego", "topeka", 1.4],
    columns=["from", "to", "distance"],
).set_index(["from", "to"])

from to
seattle new-york 2.5
chicago 1.7
topeka 1.8
san-diego new-york 2.5
chicago 1.8
topeka 1.4
freight_cost = 90

Symbol Declaration#

In line with our systematic breakdown of the transportation problem into sets, parameters, variables, and constraints, we will adopt a similar approach to define the problem as a GAMSPy Model. To do so, it is essential to import the gamspy library initially.

from gamspy import Container, Set, Parameter, Variable, Equation, Model, Sum, Sense


Before we proceed further, let’s create a Container to encapsulate all the relevant information for our GAMSPy Model. This Container acts as a centralized hub, gathering essential data, sets, parameters, variables, and constraints, providing a clear structure for our optimization problem.

m = Container()


Sets serve as the fundamental building blocks of a GAMSPy Model, directly corresponding to the indices in the algebraic representations of models. In our transportation problem context, we have defined the following indices:

  • \(i\) = plants

  • \(j\) = markets

For detailed guidance on using sets, please refer to the set section of our user guide.

There a two ways to declare sets:

  1. Separate declaration and data assignment

  2. Combine declaration and data assignment

Separate declaration and data assignment#

i = Set(container=m, name="i", description="plants")

Combine declaration and data assignment#

j = Set(container=m, name="j", description="markets", records=demands.index)

The effect of using the above Set statements is that we declared two sets, namely \(i\) and \(j\). Additionally, we provided descriptions to elaborate on their meaning, enhancing the readability of our Model. Lastly, we assigned members to the sets, establishing a clear connection between the abstract sets and their real-world counterparts.

\(i\) = {Seattle, San Diego}

\(j\) = {New York, Chicago, Topeka}

To verify the content of a set, you can use <set name>.records.

uni element_text
0 seattle
1 san-diego
uni element_text
0 new-york
1 chicago
2 topeka


Declaring parameters involves using Parameter. Each parameter is assigned a name and a description. Note that parameter \(a_i\) is indexed by \(i\). To accommodate these indices, we include the domain attribute, pointing to the corresponding set.

It is worth mentioning that, similar to sets, you have the flexibility to either combine or separate the declaration and data assignment steps. For convenience, we will proceed by combining the declaration and data assignment.

a = Parameter(
    description="supply of commodity at plant i (in cases)",
city value
0 seattle 350.0
1 san-diego 600.0
b = Parameter(
    description="demand for commodity at market j (in cases)",
city value
0 new-york 325.0
1 chicago 300.0
2 topeka 275.0
c = Parameter(
    domain=[i, j],
    description="cost per unit of shipment between plant i and market j",

The cost per unit of shipment between plant \(i\) and market \(j\) is derived from the distance between \(i\) and \(j\) and can be calculated as follows:

\(c_{ij} = \frac{90 \cdot d_{ij}}{1000}\),

where \(d_{ij}\) denotes the distance between \(i\) and \(j\).

We have two options to calculate \(c_{ij}\) and assign the data to the GAMSPy parameter:

  1. Python assignment - calculation in Python, e.g., using Pandas and <parameter name>.setRecords()

  2. GAMSPy assignment - calculation in GAMSPy

Python Assignment#

cost = freight_cost * distances / 1000
from to
seattle new-york 0.225
chicago 0.153
topeka 0.162
san-diego new-york 0.225
chicago 0.162
topeka 0.126
from to value
0 seattle new-york 0.225
1 seattle chicago 0.153
2 seattle topeka 0.162
3 san-diego new-york 0.225
4 san-diego chicago 0.162
5 san-diego topeka 0.126

GAMSPy Assignment#

For the direct assignment we need to declare a new Parameter denoting the distances between \(i\) and \(j\).

d = Parameter(
    domain=[i, j],
    description="distance between plant i and market j",
from to value
0 seattle new-york 2.5
1 seattle chicago 1.7
2 seattle topeka 1.8
3 san-diego new-york 2.5
4 san-diego chicago 1.8
5 san-diego topeka 1.4
c[i, j] = freight_cost * d[i, j] / 1000
i j value
0 seattle new-york 0.225
1 seattle chicago 0.153
2 seattle topeka 0.162
3 san-diego new-york 0.225
4 san-diego chicago 0.162
5 san-diego topeka 0.126

Further information on the usage of parameters can be found in our parameter section of the user guide.


GAMSPy variables are declared using Variable. Each Variable is assigned a name, a domain if necessary, a type, and, optionally, a description.

x = Variable(
    domain=[i, j],
    description="amount of commodity to ship from plant i to market j",

This statement results in the declaration of a shipment variable for each (i,j) pair.

More information on variables can be found in the variable section of our user guide.


A GAMSPy Equation must be declared and defined in two separate statements. The format of the declaration is the same as for other GAMSPy symbols. First comes the keyword, Equation in this case, followed by the name, domain and text. The transportation problem has two constraints:

Supply: observe supply limit at plant \(i\): \(\sum_j x_{ij} \le a_i \: \forall i\)

Demand: satisfy demand at market \(j\): \(\sum_i x_{ij} \ge b_j \: \forall j\)

supply = Equation(
    container=m, name="supply", domain=i, description="observe supply limit at plant i"
demand = Equation(
    container=m, name="demand", domain=j, description="satisfy demand at market j"

The components of an Equation definition are: 1. The Python variable of the Equation being defined 2. The domain (optional) 3. Domain restricting conditions (optional) 4. A = sign 5. Left hand side expression 6. Relational operator (==, <=, >=) 7. The right hand side expression.

The Equation definition for the supply constraint of the transportation problem is implemented as follows:

supply[i] = Sum(j, x[i, j]) <= a[i]

Using the same logic as above, we can define the demand equation as follows:

demand[j] = Sum(i, x[i, j]) >= b[j]

More information on equations is given in the equation section of our user guide.


The objective function of a GAMSPy Model does not require a separate Equation declaration. You can assign the objective expression to a Python variable or use it directly in the Model() statement of the next section.

obj = Sum((i, j), c[i, j] * x[i, j])


A GAMSPy Model() consolidates constraints, an objective function, a sense (minimize, maximize, and feasibility), and a problem type. It also possesses a name and is associated with a Container.

To define our transportation problem as a GAMSPy Model, we assign it to a Python variable, link it to our Container (populated with symbols and data), name it “transport”, specify the equations, set the problem type as linear program (LP), specify the sense of the objective function (Sense.MIN), and point to the objective expression.

GAMSPy allows two alternatives to assign equations to a Model: 1. Using a list of equations, 2. Retrieving all equations by calling m.getEquations().

Using a List of Equations#

Using a list of equations is especially useful if you want to define multiple GAMSPy Models with a subset of the equations in your Container. For the transportation problem this can be done as follows:

transport = Model(
    equations=[supply, demand],

Retrieving all Equations#

Using m.getEquations() is especially convenient if you want to include all equations of your Container to be associated with your model. For the transportation problem this can be done as follows:

transport_2 = Model(

More information on the usage of a GAMSPy Model can be found in the model section of our user guide.


Upon defining the GAMSPy Model, it’s ready for being solved. The solve() statement triggers the generation of the specific model instance, creates suitable data structures for the solver, and invokes the solver. To view solver output in the console, the sys library can be used, passing the output=sys.stdout attribute to transport.solve().

import sys

--- Job _job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.gms Start 03/26/24 16:48:43 46.3.0 cc87b1db LEX-LEG x86 64bit/Linux
--- Applying:
--- GAMS Parameters defined
    Restart /tmp/tmpcnjoku9m/_restart_eb49fd27-0ed0-4f36-947d-8d7bf4708fa6.g00
    Input /tmp/tmpcnjoku9m/_job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.gms
    Output /tmp/tmpcnjoku9m/_job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.lst
    Save /tmp/tmpcnjoku9m/_save_eb49fd27-0ed0-4f36-947d-8d7bf4708fa6.g00
    ScrDir /tmp/tmpcnjoku9m/225a/
    SysDir /home/muhammet/anaconda3/envs/py38/lib/python3.8/site-packages/gamspy_base/
    CurDir /tmp/tmpcnjoku9m/
    LogOption 3
    LogFile /tmp/tmpcnjoku9m/_job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.log
    Trace /tmp/tmpcnjoku9m/trace.txt
    License /home/muhammet/anaconda3/envs/py38/lib/python3.8/site-packages/gamspy_base/user_license.txt
    TraceOpt 3
    PreviousWork 1
Licensee: Other License                                  S240124|0002CO-GEN
          GAMS Software GmbH, Braunschweig Office                   DCE2908
          License Admin: Muhammet Soytuerk, msoyturk@gams.com
          Other time limited license
          The evaluation period of the license will expire on Jul 22, 2024
Processor information: 1 socket(s), 12 core(s), and 16 thread(s) available
GAMS 46.3.0   Copyright (C) 1987-2024 GAMS Development. All rights reserved
--- Starting continued compilation
--- Workfile was generated under GAMS version LEX430-430
--- _job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.gms(3) 3 Mb
--- GDXin=/tmp/tmpcnjoku9m/_gdx_in_eb49fd27-0ed0-4f36-947d-8d7bf4708fa6.gdx
--- GDX File ($gdxIn) /tmp/tmpcnjoku9m/_gdx_in_eb49fd27-0ed0-4f36-947d-8d7bf4708fa6.gdx
--- _job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.gms(66) 3 Mb
--- Starting execution: elapsed 0:00:00.001
--- Generating LP model transport
--- _job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.gms(152) 4 Mb
---   6 rows  7 columns  19 non-zeroes
--- Range statistics (absolute non-zero finite values)
--- RHS       [min, max] : [ 2.750E+02, 6.000E+02] - Zero values observed as well
--- Bound     [min, max] : [        NA,        NA] - Zero values observed as well
--- Matrix    [min, max] : [ 1.260E-01, 1.000E+00]
--- Executing CPLEX (Solvelink=2): elapsed 0:00:00.002

IBM ILOG CPLEX   46.3.0 cc87b1db Mar 19, 2024          LEG x86 64bit/Linux

--- GAMS/CPLEX licensed for continuous and discrete problems.
--- GMO setup time: 0.00s
--- GMO memory 0.50 Mb (peak 0.50 Mb)
--- Dictionary memory 0.00 Mb
--- Cplex link memory 0.00 Mb (peak 0.00 Mb)
--- Starting Cplex

Version identifier: | 2022-11-28 | 9160aff4d
CPXPARAM_Advance                                 0
CPXPARAM_Simplex_Display                         2
CPXPARAM_MIP_Display                             4
CPXPARAM_MIP_Pool_Capacity                       0
CPXPARAM_MIP_Tolerances_AbsMIPGap                0
Tried aggregator 1 time.
LP Presolve eliminated 0 rows and 1 columns.
Reduced LP has 5 rows, 6 columns, and 12 nonzeros.
Presolve time = 0.00 sec. (0.00 ticks)

Iteration      Dual Objective            In Variable           Out Variable
     1              73.125000    x(seattle,new-york) demand(new-york) slack
     2             119.025000     x(seattle,chicago)  demand(chicago) slack
     3             153.675000    x(san-diego,topeka)   demand(topeka) slack
     4             153.675000  x(san-diego,new-york)  supply(seattle) slack

--- LP status (1): optimal.
--- Cplex Time: 0.00sec (det. 0.01 ticks)

Optimal solution found
Objective:          153.675000

--- Reading solution for model transport
--- Executing after solve: elapsed 0:00:00.009
--- _job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.gms(214) 4 Mb
--- GDX File (execute_unload) /tmp/tmpcnjoku9m/_gdx_out_eb49fd27-0ed0-4f36-947d-8d7bf4708fa6.gdx
*** Status: Normal completion
--- Job _job_897cbf3f-e2ae-49aa-9f21-ad803d2c05b0.gms Stop 03/26/24 16:48:43 elapsed 0:00:00.009
Solver Status Model Status Objective Num of Equations Num of Variables Model Type Solver Solver Time
0 Normal OptimalGlobal 153.675 6 7 LP CPLEX 0

Retrieving Results#

Variable Values#

The values of the variables in the solution can be retrieved using using <variable name>.records. The level specifies the shipment quantities \(x_{ij}\). Other variable attributes are the marginal values, lower and upper bounds, and the variable’s scaling factor.

x.records.set_index(["i", "j"])
level marginal lower upper scale
i j
seattle new-york 50.0 0.000 0.0 inf 1.0
chicago 300.0 0.000 0.0 inf 1.0
topeka 0.0 0.036 0.0 inf 1.0
san-diego new-york 275.0 0.000 0.0 inf 1.0
chicago 0.0 0.009 0.0 inf 1.0
topeka 275.0 0.000 0.0 inf 1.0

Objective Value#

The optimal objective function value can be accessed by <model name>.objective_value.