Skip to main content

Python API for AMPL

Project description

AMPLPY: Python API for AMPL

# Install Python API for AMPL
$ python -m pip install amplpy --upgrade

# Install solver modules (e.g., HiGHS, CBC, Gurobi)
$ python -m amplpy.modules install highs cbc gurobi

# Activate your license (e.g., free https://ampl.com/ce license)
$ python -m amplpy.modules activate <license-uuid>

# Import in Python
$ python
>>> from amplpy import AMPL
>>> ampl = AMPL() # instantiate AMPL object
# Minimal example:
from amplpy import AMPL
import pandas as pd
ampl = AMPL()
ampl.eval(r"""
    set A ordered;
    param S{A, A};
    param lb default 0;
    param ub default 1;
    var w{A} >= lb <= ub;
    minimize portfolio_variance:
        sum {i in A, j in A} w[i] * S[i, j] * w[j];
    s.t. portfolio_weights:
        sum {i in A} w[i] = 1;
""")
tickers, cov_matrix = # ... pre-process data in Python
ampl.set["A"] = tickers
ampl.param["S"] = pd.DataFrame(cov_matrix, index=tickers, columns=tickers)
ampl.solve(solver="gurobi", gurobi_options="outlev=1")
assert ampl.solve_result == "solved"
sigma = ampl.get_value("sqrt(sum {i in A, j in A} w[i] * S[i, j] * w[j])")
print(f"Volatility: {sigma*100:.1f}%")
# ... post-process solution in Python

[Documentation] [AMPL Modules for Python] [Available on Google Colab] [AMPL Community Edition]

amplpy is an interface that allows developers to access the features of AMPL from within Python. For a quick introduction to AMPL see Quick Introduction to AMPL.

In the same way that AMPL’s syntax matches naturally the mathematical description of the model, the input and output data matches naturally Python lists, sets, dictionaries, pandas and numpy objects.

All model generation and solver interaction is handled directly by AMPL, which leads to great stability and speed; the library just acts as an intermediary, and the added overhead (in terms of memory and CPU usage) depends mostly on how much data is sent and read back from AMPL, the size of the expanded model as such is irrelevant.

With amplpy you can model and solve large scale optimization problems in Python with the performance of heavily optimized C code without losing model readability. The same model can be deployed on applications built on different languages by just switching the API used.

Documentation

Repositories:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

amplpy-0.14.0.tar.gz (2.2 MB view hashes)

Uploaded Source

Built Distributions

amplpy-0.14.0-cp312-cp312-win_amd64.whl (2.5 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

amplpy-0.14.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.7 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

amplpy-0.14.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (5.8 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

amplpy-0.14.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.7 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

amplpy-0.14.0-cp312-cp312-macosx_10_9_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

amplpy-0.14.0-cp312-cp312-macosx_10_9_universal2.whl (1.8 MB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

amplpy-0.14.0-cp311-cp311-win_amd64.whl (2.5 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

amplpy-0.14.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

amplpy-0.14.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (5.8 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

amplpy-0.14.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

amplpy-0.14.0-cp311-cp311-macosx_10_9_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

amplpy-0.14.0-cp311-cp311-macosx_10_9_universal2.whl (1.8 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

amplpy-0.14.0-cp310-cp310-win_amd64.whl (2.5 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

amplpy-0.14.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

amplpy-0.14.0-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (5.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

amplpy-0.14.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

amplpy-0.14.0-cp310-cp310-macosx_10_9_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

amplpy-0.14.0-cp310-cp310-macosx_10_9_universal2.whl (1.8 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

amplpy-0.14.0-cp39-cp39-win_amd64.whl (2.5 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

amplpy-0.14.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

amplpy-0.14.0-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (5.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

amplpy-0.14.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

amplpy-0.14.0-cp39-cp39-macosx_10_9_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

amplpy-0.14.0-cp39-cp39-macosx_10_9_universal2.whl (1.8 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

amplpy-0.14.0-cp38-cp38-win_amd64.whl (2.5 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

amplpy-0.14.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

amplpy-0.14.0-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (5.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

amplpy-0.14.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

amplpy-0.14.0-cp38-cp38-macosx_10_9_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

amplpy-0.14.0-cp38-cp38-macosx_10_9_universal2.whl (1.8 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

amplpy-0.14.0-cp37-cp37m-win_amd64.whl (2.5 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

amplpy-0.14.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

amplpy-0.14.0-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (5.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ppc64le

amplpy-0.14.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.6 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

amplpy-0.14.0-cp37-cp37m-macosx_10_9_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

amplpy-0.14.0-cp36-cp36m-win_amd64.whl (2.8 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

amplpy-0.14.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

amplpy-0.14.0-cp36-cp36m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (5.7 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ppc64le

amplpy-0.14.0-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.6 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ARM64

amplpy-0.14.0-cp36-cp36m-macosx_10_9_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page