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.
- 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.
pip install gamspyGAMSPy 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.
A 10 MW / 40 MWh battery buys electricity when it is cheap and sells it when it is expensive. Given tomorrow's hourly prices
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.
- Documentation
- Example notebooks and the model library
- API reference
- Coming from GAMS? See GAMSPy for GAMS users.
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.
Ask questions on the GAMS Forum, report bugs and request features in the issue tracker, and see the contributing guide to send changes.
