AMPL Modules for Python#
AMPL and all Solvers are now available as Python Packages:
# Install Python API for AMPL
$ python -m pip install amplpy --upgrade
# Install solvers (e.g., HiGHS and Gurobi)
$ python -m amplpy.modules install highs 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
Python API (amplpy) Documentation
Modules Available#
List of modules available:
Open-source solvers:
highs
,cbc
,coin
(includes: CBC, Couenne, Ipopt, Bonmin),open
(includes all open-source solvers)NEOS Server:
gokestrel
(kestrel client)Commercial solvers:
baron
,conopt
,copt
,cplex
,gurobi
,knitro
,lgo
,lindoglobal
,loqo
,minos
,mosek
,octeract
,snopt
,xpress
AMPL Plugins:
amplgsl
(amplgsl docs),plugins
(amplplugins docs)
Commands#
usage#
List usage instructions:
$ python -m amplpy.modules usage
install#
Install modules:
$ python -m amplpy.modules install <solver 1> <solver 2> ...
Example:
$ python -m amplpy.modules install highs gurobi
update#
Update AMPL or solver versions:
$ python -m amplpy.modules install <base> <solver 1> <solver 2> ... --upgrade
Example:
$ python -m amplpy.modules install base highs gurobi --upgrade
Note
Modules are not automatically updated with amplpy.
activate#
Activate a license (e.g., free https://ampl.com/ce license):
$ python -m amplpy.modules activate <license-uuid>
uninstall#
Uninstall modules:
$ python -m amplpy.modules uninstall <solver 1> <solver 2> ...
Example:
$ python -m amplpy.modules uninstall gurobi highs
installed#
List installed modules:
$ python -m amplpy.modules installed
Example:
$ python -m amplpy.modules installed
You have the following modules installed:
base
gurobi
highs
available#
List modules available to be installed:
$ python -m amplpy.modules available
You can install any of the following modules:
amplgsl
baron
base
cbc
coin
conopt
copt
cplex
gcg
gecode
gokestrel
gurobi
highs
ilogcp
knitro
lgo
lindoglobal
loqo
minos
mosek
octeract
open
plugins
scip
snopt
xpress
path#
Value to append to the environment variable PATH to access modules:
$ python -m amplpy.modules path
Example:
export PATH=$PATH:`python -m amplpy.modules path`
find#
Find the path to a file in any module:
$ python -m amplpy.modules find <file name>
Example:
$ python -m amplpy.modules find gurobi
requirements#
Generate requirements.txt
for the modules currently installed:
$ python -m amplpy.modules requirements
--index-url https://pypi.ampl.com
--extra-index-url https://pypi.org/simple
ampl_module_base==20221226
ampl_module_gurobi==20221228
ampl_module_highs==20221228
run#
Run command in the same environment as the modules
$ python -m amplpy.modules run <command>
Example:
$ python -m amplpy.modules run ampl -v
AMPL Version 20221013 (Darwin-20.6.0, 64-bit)
Demo license with maintenance expiring 20240131.
ampl:
Programmatically#
All of this is also available from Python programmatically:
>>> from amplpy import modules
>>> modules.installed()
['base', 'gurobi', 'highs']
>>> modules.available()
['baron', 'cbc', 'lindoglobal', 'xpress', 'gurobi', 'lgo', 'open', 'minos', 'octeract', 'copt', 'plugins', 'amplgsl', 'loqo', 'conopt', 'base', 'cplex', 'snopt', 'coin', 'knitro', 'highs', 'gokestrel']
>>> modules.install('cplex')
>>> modules.installed()
['base', 'cplex', 'gurobi', 'highs']
>>> modules.uninstall('cplex')
>>> modules.installed()
['base', 'gurobi', 'highs']
How to Use Modules#
Install Modules#
# Install Python API for AMPL
$ python -m pip install amplpy --upgrade
# Install solvers (e.g., HiGHS, Gurobi, COIN-OR)
$ python -m amplpy.modules install highs gurobi coin
# Activate your license (e.g., free https://ampl.com/ce license)
$ python -m amplpy.modules activate <license-uuid>
Using from AMPL#
Installed modules are loaded by default when amplpy is loaded.
from amplpy import AMPL
ampl = AMPL() # instantiate AMPL object
ampl.option["solver"] = "highs" # use the solver highs
In case you have another AMPL installation and want to ensure that modules are used, you can do that as follows:
from amplpy import AMPL, modules
modules.load() # load all modules
ampl = AMPL() # instantiate AMPL object
Using from Pyomo#
Even though the NL format was invented for connecting solvers to AMPL, it has been adopted by other systems such as Pyomo. Pyomo is an open-source modeling tool written in Python that is compatible with solvers that read .nl files, which means it works with all AMPL solvers. You can use AMPL solvers with Pyomo as follows:
Install modules for HiGHS, CBC, Couenne, Bonmin, Ipopt, SCIP, and GCG:
!pip install amplpy pyomo -q
!python -m amplpy.modules install coin highs scip gcg -q # Install HiGHS, CBC, Couenne, Bonmin, Ipopt, SCIP, and GCG
Use the solver modules from Pyomo:
from amplpy import modules
import pyomo.environ as pyo
solver_name = "highs" # "highs", "cbc", "couenne", "bonmin", "ipopt", "scip", or "gcg".
solver = pyo.SolverFactory(solver_name+"nl", executable=modules.find(solver_name), solve_io="nl")
model = pyo.ConcreteModel()
model.x = pyo.Var([1,2], domain=pyo.NonNegativeReals)
model.OBJ = pyo.Objective(expr = 2*model.x[1] + 3*model.x[2])
model.Constraint1 = pyo.Constraint(expr = 3*model.x[1] + 4*model.x[2] >= 1)
solver.solve(model, tee=True)
Note that Pyomo is typically substantially slower than AMPL due to being written completely in Python (e.g., not performing the model instantiation using heavily optimized C code like AMPL does), and that it may also not be able to take advantage of all functionalities of our solver drivers (especially the ones built with the new MP interface); the solver performance when used from AMPL also tends to be superior due to AMPL’s presolver that is typically able to reduce model sizes substantially. Nevertheless, feel free to use any of our solvers with other modeling tools.
Note
Pyomo is an independent open-source project. It is not developed or maintained by AMPL. We can just provide support related to deployment using our solver drivers. For any Pyomo issues not related to our solvers please check the support channels at https://www.pyomo.org/.