# fdabrandao/pympl

Mathematical Programming Toolbox for AMPL/GMPL
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
 Failed to load latest commit information. docs examples pympl scripts .gitignore .gitmodules .travis.yml CHANGELOG.md COPYING Dockerfile LICENSE MANIFEST.in README.md requirements.txt setup.py virtualenv.sh webapp.sh

## PyMPL: A Mathematical Programming Toolbox

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.

### Setup

Install from the repository:

\$ pip install pympl

Or build and install locally:

\$ pip install -r requirements.txt
\$ 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.

### Examples

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;

### PyMPL Parser

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")