Copyright (C) 2015-2016, Filipe Brandão
Faculdade de Ciências, Universidade do Porto
Porto, Portugal. All rights reserved. E-mail: [email protected].
PyMPL is a python extension to the AMPL modeling language that adds new statements for evaluating python code within AMPL/GMPL models. PyMPL also includes, among others, procedures for modeling piecewise linear functions, compressed arc-flow graphs for vector packing, sub-tour elimination constraints for TSP, and lot-sizing reformulations (LS-LIB). PyMPL is fully compatible with both python 2 and 3.
- PyMPL documentation: https://github.com/fdabrandao/pympl/wiki
- GiHub repository: https://github.com/fdabrandao/pympl
- BitBucket repository: https://bitbucket.org/fdabrandao/pympl
- Docker repository: https://hub.docker.com/r/fdabrandao/pympl
- PyPI repository: https://pypi.python.org/pypi/PyMPL
Install from the repository:
$ pip install pympl
Or build and install locally:
$ pip install -r requirements.txt
$ pip install . --upgrade
$ cd examples; py.test -v --cov pympl
PyMPL can also be used inside a Docker container that includes a simple web app for an easy usage.
piecewise_linear.mod
# Evaluate python code:
$EXEC{
xvalues = [0, 10, 15, 25, 30, 35, 40, 45, 50, 55, 60, 70]
yvalues = [0, 20, 15, 10, 0, 50, 18, 0, 15, 24, 10, 15]
};
var u >= 0;
# Model a piecewise linear function given a list of pairs (x, y=f(x)):
$PWL[x,y]{zip(xvalues, yvalues)};
maximize obj: 2*x + 15*y;
s.t. A: 3*x + 4*y <= 250;
s.t. B: 7*x - 2*y + 3*u <= 170;
end;
vector_packing.mod
:
# Load a vector packing instance from a file:
$EXEC{
from pyvpsolver import VBP
instance = VBP.from_file("data/instance.vbp")
};
$PARAM[b{I}]{instance.b};
var x{I}, >= 0;
# Generate the arc-flow model:
$VBP_FLOW[Z]{instance.W, instance.w, ["x[{}]".format(i) for i in range(instance.m)]};
# Variable declarations and flow conservation constraints will be created here
minimize obj: Z;
s.t. demand{i in I}: x[i] >= b[i]; # demand constraints
end;
import os
from pympl import PyMPL # import the parser
# Create a parser and pass local and global variables to the model:
parser = PyMPL(locals_=locals(), globals_=globals())`
# Parse a file with PyMPL statements and produce a valid AMPL model:
parser.parse("pympl_model.mod", "ampl_model.mod")
# Call GLPK to solve the model (if the original model uses only valid GMPL statements):
os.system("glpsol --math ampl_model.mod")
# Call AMPL to solve the model:
os.system("ampl ampl_model.mod")
Advanced features:
-
Given a function
f(varname)
that takes a variable name and returns its value:- If any command used implements solution extraction you can use
parser[command_name].extract(f)
to extract the solution; - If any command used implements cut generators you can use
parser[command_name].separate(f)
to generate cutting planes.
- If any command used implements solution extraction you can use
Copyright © 2015-2016 Filipe Brandão < [email protected] >. All rights reserved.