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.1

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.1
File Size Uploaded Provenance Actions
gamspy-1.30.1.tar.gz 380.1 kB With provenance Details for gamspy-1.30.1.tar.gz

Built distribution (wheel)

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

Total release size: 823.0 kB

Release files / gamspy-1.30.1.tar.gz

Download URL gamspy-1.30.1.tar.gz
Size 380.1 kB
Tags Source
SHA-256 checksum
How to use checksums
94f0216c8749dd41db58fac0570dfc5ea59f1180b057fc8ac322cf031fa1f066
BLAKE2b-256 checksum
How to use checksums
5fe36ff0ba1f10ad6a108954fd642483923acb83f5d4cc249a7b0a1faecc4ca8
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 20, 2026.

Transparency log

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

Download URL gamspy-1.30.1-py3-none-any.whl
Size 442.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71c242a56b125873c3ddf78c55c4d204c1a7037b967952a1826f9dd6f439fde9
BLAKE2b-256 checksum
How to use checksums
a0644906023423a3c9bb0e65527c6e90c1097cfdbc2c4e6238d3f425efacfb3c
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 20, 2026.

Transparency log

Security for gamspy 1.30.1

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.1.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 20, 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.1 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