Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Introduction to GAMSPy in Python

A Transportation Problem

https://gamspy.readthedocs.io/en/latest/user/index.html
A Transportation Problem:
How can we optimize the shipment quantities between each plant and market to minimize the total transport cost?
What is GAMSPy?
• GAMSPy combines the high-performance GAMS (General Algebraic Modeling System) execution system with the
flexible Python language, creating a powerful mathematical optimization package.
• It acts as a bridge between the expressive Python language and the robust GAMS system, allowing you to create
complex mathematical models effortlessly.
• GAMSPy is able to generate mathematical models instead of model instances. Especially for complex models with
many variables and equations, a model instance can become hard to manage. Therefore, GAMSPy leverages the idea
of a standalone, data independent, and indexed representation of a mathematical model which is very close to the
original mathematical algebraic formulation.
!pip install gamspy
import gamspy

print(gamspy.utils.getInstalledSolvers())
print(gamspy.utils.getAvailableSolvers())

['CONOPT', 'CONVERT', 'CPLEX', 'NLPEC', 'PATH', 'SBB’]

['BARON', 'CBC', 'CONOPT', 'CONOPT3', 'CONVERT', 'COPT',


'CPLEX', 'DICOPT', 'GUROBI', 'HIGHS', 'IPOPT', 'IPOPTH',
'KNITRO', 'MINOS', 'MOSEK', 'MPSGE', 'NLPEC', 'PATH',
'SBB', 'SCIP', 'SHOT', 'SNOPT', 'XPRESS']
Data:
• 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.
Data:
• 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
Data:
Data:
Data:
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, Problem, Options
Container:
• 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:
• 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

• i = Set(container=m, name="i",
description="plants", records=capacities.index)

• j = Set(container=m, name="j",
description="markets", records=demands.index)
Sets:
Parameters:
• Declaring parameters involves using `Parameter`. Each parameter
is assigned a name and a description.
• Note that parameter `aᵢ` is indexed by `i`.
• To accommodate these indices, we include the `domain` attribute,
pointing to the corresponding set.
Parameters:
Parameters:
Parameters:
Parameters:
Variables:
• GAMSPy variables are declared using `Variable`. Each
`Variable` is assigned a name, a domain if necessary, a type, and,
optionally, a description.
Equations:
• 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`: `∑ⱼxᵢⱼ≤aᵢ ∀i`
• Demand: satisfy demand at market `j`: `∑ᵢxᵢⱼ≥bⱼ ∀j`
Equations:
Objective:
• 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.
Model:
• 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()`.
Model:
Solve:
• 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()`.

• iteration_limit  Iteration limit of solver


• job_time_limit  Elapsed time limit in
seconds
• time_limit  Wall-clock time limit for solver
Retrieving Results:
• Variable Values: • Objective Value:
• The values of the variables in the solution can be retrieved • The optimal objective function value can be accessed
using using `<variable name>.records`. by `<model name>.objective_value`.
• The level specifies the shipment quantities `xᵢⱼ`.
• Other variable attributes are the marginal values, lower and
upper bounds, and the variable’s scaling factor.
Solve using NEOS OPTIMIZATION SERVER:
Thank you!

You might also like