AMPL Python API

raw JSON →
0.16.0 verified Mon Apr 27 auth: no python

amplpy is the official Python API for AMPL, a powerful algebraic modeling language for optimization. Version 0.16.0 provides a Pythonic interface to define models, solve them with AMPL's solvers, and manipulate results. It is maintained by AMPL Inc. and follows a regular release cadence aligned with AMPL updates.

pip install amplpy
error ModuleNotFoundError: No module named 'amplpy'
cause amplpy is not installed, or installed in a different Python environment.
fix
Run pip install amplpy in the correct environment (e.g., virtualenv).
error RuntimeError: AMPL executable not found. Please set the AMPL path
cause AMPL is not installed or not in PATH, and amplpy cannot locate the AMPL executable.
fix
Set the AMPL path via environment variable AMPLBIN or pass Environment(ampl_bin='/path/to/ampl') to the AMPL constructor.
error TypeError: No matching signature found
cause Using numpy or pandas data types that are not supported by AMPL's C++ interface; often due to using pandas DataFrames with non-string column names.
fix
Convert column names to strings and ensure data types are basic (int, float, str).
breaking In amplpy 0.13.0 and later, the `amplpy.AMPL` constructor no longer accepts `ampl_bin` or `format` arguments; use `Environment` to set AMPL path.
fix Use `Environment(ampl_bin='/path/to/ampl')` and pass to `AMPL(environment=env)`.
gotcha When using AMPL DataFrame with pandas DataFrames, the column names may be automatically converted to strings; mixing types can cause silent data loss.
fix Ensure all column names in pandas DataFrames are strings before converting to AMPL DataFrame.
deprecated The method `getData(name)` is deprecated in favor of `get_data(name)` (underscore convention) since version 0.12.0.
fix Replace `getData('x')` with `get_data('x')`.

Create an AMPL instance, evaluate a simple model, solve it, and print the objective value.

from amplpy import AMPL, DataFrame
ampl = AMPL()
ampl.eval('var x; maximize obj: x; subject to c: x <= 1;')
ampl.solve()
print('Objective:', ampl.getValue('obj'))