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

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.0
File Size Uploaded Provenance Actions
gamspy-1.30.0.tar.gz 377.8 kB With provenance Details for gamspy-1.30.0.tar.gz

Built distribution (wheel)

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

Total release size: 812.6 kB

Release files / gamspy-1.30.0.tar.gz

Download URL gamspy-1.30.0.tar.gz
Size 377.8 kB
Tags Source
SHA-256 checksum
How to use checksums
aa64140086f510f7d62141220a9dfe24b0b6f2a7371e62a2f1ecb65e4f1fe54b
BLAKE2b-256 checksum
How to use checksums
7a7f05a015cdf6fe42ad4e19bb3f3a53fbe101bac15cbefbc612f587d3281545
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 12, 2026.

Transparency log

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

Download URL gamspy-1.30.0-py3-none-any.whl
Size 434.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
362c584c79b3ba80fb3067bf998e1c7cf2c54bcb58a61275e2a8438784fc9f5b
BLAKE2b-256 checksum
How to use checksums
aae0f8cd21547141f654286cc5283d133a407aad7c1c10c3a02774d0c722bb0b
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 12, 2026.

Transparency log

Security for gamspy 1.30.0

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.0.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 12, 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.0 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