What is GAMSPy?#
GAMSPy combines the high-performance GAMS 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. This allows creating full pipelines in Python by using your favorite packages for preprocessing (e.g. data cleaning) and postprocessing (e.g. visualization) along with GAMSPy.
Why is GAMSPy fast?#
GAMSPy delegates the expensive assignment and solve statements to the GAMS execution system. Set-based operations
are at the core of the GAMSPy and GAMS execution system. For example, in many optimization libraries what you
would write your equations as given in the Other Libraries block below:
import other_library as ol
I = ['i1', 'i2']
J = ['j1', 'j2']
a = ol.Parameter()
x = ol.Variable()
b = ol.Parameter()
objective = sum(
a[i,j] * x[i, j]
for i in I
for j in J
) >= b[i,j]
import gamspy as gp
m = gp.Container()
i = gp.Set(m, records=['i1', 'i2'])
j = gp.Set(m, records=['j1', 'j2'])
a = gp.Parameter(m)
x = gp.Variable(m)
b = gp.Parameter(m)
objective = gp.Sum((i,j), a[i,j] * x[i,j]) >= b[i,j]
With the approach of other libraries, you iterate over all items of I and J. This approach has certain disadvantages:
It can get pretty verbose for long statements with many loops which decreases the readability.
The performance might suffer severely (depending on the implementation) if there are lots of items to iterate through since Python loops are known to be very slow.
Meanwhile GAMSPy implementation employs set-based operations. This results in:
Concise and easier to read definitions.
GAMSPy definitions closely resemble mathematical notation that you would put in a paper making it easier to correctly code mathematical equations.
Great performance since the actual operation is performed by GAMS using robust and highly optimized low level code.
See also
Performance in Optimization Models: A Comparative Analysis of GAMS, Pyomo, GurobiPy, and JuMP
Model Instances vs. Mathematical Models#
Creating robust, readable, and maintainable models is an art rooted in algebraic formulation. The ability to express mathematical models in a language that retains the essence of algebraic notation and is machine-processable is paramount.
With this goal in mind GAMSPy has been developed to be able to generate mathematical models instead of model instances. Think of a mathematical model as a pure representation of mathematical symbols, devoid of specific data. In contrast, a model instance is the unrolled and constant folded representation of a model with its actual data. In a model instance sum expressions are resolved into its individual components and equation domains are resolved to individual scalar equations.
Mathematical Model
Model Instance
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.
Sparsity#
One key aspect of any modeling language is how it handles sparse multidimensional data structures. Many optimization problems are subject to a particular structure in which the data cube has a lot of zeros and only a few non-zeros, a characteristic referred to as sparsity. In optimization problems, it is often necessary to account for complex mappings of indices to subsets.
While you might be used to taking on the full responsibility to make sure only the relevant combinations of indices go into your variable definition in the Python modeling world, we especially focused on transferring the convenience and mindset of GAMS into Python by designing GAMSPy. Thus, GAMSPy automatically takes care of generating variables only for the relevant combinations of indices based on the algebraic formulation. This feature is particularly useful when working with a large multidimensional index space, where generating all possible combinations of indices would be computationally expensive and unnecessary. GAMSPy quietly handles this task in the background, allowing us to focus on the formulation of the model.
Which backends GAMSPy support?#
GAMSPy currently supports three backends: your local GAMS installation, GAMS Engine and NEOS Server. The default backend is the local GAMS installation that comes with GAMSPy. With GAMS Engine backend, you can run hundreds of jobs in the cloud in parallel to speed up your pipeline. One can also send their jobs to NEOS Server which is a free service for solving optimization problems. Make sure you understand the NEOS Terms of Use before you use this backend.
GAMSPy is also integrated with GAMS MIRO which allows you to create fully interactive applications. Extensive visualization options of GAMS MIRO support you to make decisions based on optimization.
Interoperability of GAMSPy#
A mathematical optimization model written with GAMSPy can be converted to GAMS or LaTeX format. GAMSPy to GAMS conversion might be useful to do fast prototyping in GAMSPy and converting the model to GAMS to use certain features of GAMS (e.g. EMP models) that are missing in GAMSPy. GAMSPy to LaTeX conversion can be convenient to put your equations into an academic paper or to verify the equations you have written match with what you think mathematically.