Skip to main content

GAMSPy

Write optimization models in Python the way you write them on paper.
Indexed algebra over sets, generated by the GAMS execution engine, solved by the solver of your choice.

PyPI version Downloads Supported versions License: MIT Documentation Status Open in Colab

Building and solving a battery arbitrage model with GAMSPy

Why GAMSPy

  • Models that read like math. Declare variables and constraints over sets once (storage[t] = level[t] == level[t - 1] + charge[t] - discharge[t]) instead of building scalar expressions in Python loops.
  • Model generation outside Python. GAMSPy hands the algebra to the GAMS execution engine, which has generated large industrial models for decades, so model size is not bound by Python loop speed.
  • Swap solvers without touching the model. LP, MIP, NLP, MINLP, QCP and MCP models run on CPLEX, CONOPT, PATH and other bundled solvers, or on HiGHS, Gurobi, Knitro, Xpress, BARON, MOSEK and more with gamspy install solver <name>.
  • Fits your Python stack. Load data from lists, NumPy arrays or pandas DataFrames and get results back as DataFrames.
  • Machine learning inside optimization. Embed trained PyTorch networks and decision trees, random forests and gradient boosting as constraints.
  • Built-in formulations. Piecewise-linear functions, indicator and logical constraints, and stochastic programming with SDDP (beta).
  • Solve anywhere. Run the same model locally, on GAMS Engine or the NEOS Server, or turn it into an interactive GAMS MIRO app.

Installation

pip install gamspy

GAMSPy requires Python 3.11 or newer. The GAMS runtime, a set of default solvers and a demo license come with it, so the example below runs right after installation.

Your first model

A 10 MW / 40 MWh battery buys electricity when it is cheap and sells it when it is expensive. Given tomorrow's hourly prices pₜ, choose charging cₜ and discharging dₜ to maximize profit, while the stored energy ℓₜ follows a 95% efficiency each way:

maximize    Σₜ pₜ (dₜ − cₜ)
subject to  ℓₜ = ℓₜ₋₁ + 0.95 cₜ − dₜ / 0.95     ∀t
            0 ≤ cₜ, dₜ ≤ 10,   0 ≤ ℓₜ ≤ 40      ∀t

In GAMSPy, the math maps line by line to code:

import gamspy as gp
import numpy as np

night = [52, 48, 45, 44, 47, 58, 75, 82]
day = [61, 34, 12, -5, -8, 3, 21, 46]
evening = [78, 112, 135, 118, 92, 74, 63, 55]

m = gp.Container()

t = gp.Set(m, records=[f"{h:02}:00" for h in range(24)])
price = gp.Parameter(m, domain=t, records=np.array(night + day + evening))

charge = gp.Variable(m, domain=t, type="positive")
discharge = gp.Variable(m, domain=t, type="positive")
level = gp.Variable(m, domain=t, type="positive")
charge.up[t] = 10
discharge.up[t] = 10
level.up[t] = 40

storage = gp.Equation(m, domain=t)
storage[t] = level[t] == level[t - 1] + 0.95 * charge[t] - discharge[t] / 0.95

arbitrage = gp.Model(
    m,
    equations=[storage],
    problem="LP",
    sense="max",
    objective=gp.Sum(t, price[t] * (discharge[t] - charge[t])),
)
arbitrage.solve(solver="CPLEX")

bars = "".join("▁▂▃▄▅▆▇█"[round(v / 40 * 7)] for v in level.toDense())
print(f"Profit:  €{arbitrage.objective_value:,.0f}")
print(f"Battery: {bars}")
Profit:  €5,016
Battery: ▁▃▅▆█▇▅▃▁▁▃▄▆████▆▄▂▁▁▁▁

The battery fills up overnight, sells into the morning peak, recharges while solar pushes midday prices below zero, and empties into the evening peak. To use another solver, change the name: "HIGHS", "IPOPT" or any other installed solver solves the same model. level[t - 1] is the previous hour; before the first hour it is zero, so the battery starts empty. A year of hourly prices uses the same model: only the data changes.

Continue with the quick start guide or run it in Colab.

Learn more

License

The GAMSPy Python source is MIT-licensed. Solving uses the bundled GAMS runtime and solvers, which need a license:

  • A demo license for small models is included.
  • Free licenses are available for personal non-commercial use and for academic use (the academic one includes commercial solvers without size limits). Get one at the GAMS Portal; see free licenses for the terms.
  • Commercial and production use needs a commercial license: contact sales@gams.com.

See the licensing guide for installation and exceptions.

Community and contributing

Ask questions on the GAMS Forum, report bugs and request features in the issue tracker, and see the contributing guide to send changes.

Metadata

Release files for gamspy 1.30.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gamspy 1.30.2
File Size Uploaded Provenance Actions
gamspy-1.30.2.tar.gz 381.6 kB With provenance Details for gamspy-1.30.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gamspy 1.30.2
File Interpreter ABI Platform Actions
gamspy-1.30.2-py3-none-any.whl Python 3 none any Details for gamspy-1.30.2-py3-none-any.whl

Total release size: 826.3 kB

Release files / gamspy-1.30.2.tar.gz

Download URL gamspy-1.30.2.tar.gz
Size 381.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e79e1b3cef5a89db2f9b95463f5827205e210822f1d00e745178d20400a7b75e
BLAKE2b-256 checksum
How to use checksums
1da162e5982eccca44a29107768c1bcf3fa82c46be2a1b8921aab90e942307f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / gamspy-1.30.2-py3-none-any.whl

Download URL gamspy-1.30.2-py3-none-any.whl
Size 444.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
650f4bb052b4d54ec7e9d7d9500f384b8afebde47ef8b6d9bd4a44e248e9a07c
BLAKE2b-256 checksum
How to use checksums
a9e83f16ef04be3f292fff7a7137e5de37c4babe80df528544105dc60c4408a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for gamspy 1.30.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for gamspy-1.30.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.30.2 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page