Getting started

Installation

The AMPL API can function as an add-on to any existing AMPL installation. If you do not yet have an AMPL installation on the computer where you will be working with the API, see our demo page or trial page to download a working version that can be installed quickly.

In order to install the Python API you just need to run

python -m pip install amplpy

Note: For Windows, Linux, and macOS, the amplpy package comes with binary wheels for Python 2.7 and 3.x. Please make sure that you are using the latest version of pip before installing amplpy (upgrade using pip install pip --upgrade or python -m pip install pip --upgrade). If a binary wheel for your platform is not available, a C++ compiler and python development libraries will be required.

Initial test

To begin, open a command/terminal window and clone our GitHub repository:

git clone git@github.com:ampl/amplpy.git

To complete an initial test, run firstexample with

python amplpy/examples/firstexample.py <solver>

where optionally <solver> is the name of a solver that has been installed with AMPL. (If a solver is not specified, AMPL’s default choice will be used.) This will solve several small diet problems and then display the optimal amounts of the foods from the last solution. Alternatively, you can download the examples directly from https://github.com/ampl/amplpy/tree/master/examples.

Note that the folder containing the AMPL executable should be in the system search path. Otherwise, the error message “AMPL could not be started” will be shown. If the AMPL installation directory is not in the system search path, you can add it passing a new Environment to AMPL as follows:

from amplpy import AMPL, Environment
ampl = AMPL(Environment('full path to the AMPL installation directory'))

Note that you may need to use raw strings (e.g., r’C:\ampl\ampl.mswin64’) or escape the slashes (e.g., ‘C:\\ampl\\ampl.mswin64’) if the path includes backslashes.

Development

Import the amplpy modude with

import amplpy

Together with your existing AMPL implementation, this will provide the full object library and access to all AMPL functions. Please make sure that the folder containing the AMPL executable is in the system search path.

Deployment

To deploy AMPL API applications to users who do not have their own AMPL installations, include with your application the AMPL executable (ampl or ampl.exe), and the amplpy package. Note that the folder containing the AMPL executable should be in the system search path.