https://github.com/fdabrandao/pympl
Mathematical Programming Toolbox for AMPL/GMPL
https://github.com/fdabrandao/pympl
ampl modeling-language optimization optimization-tools
Last synced: about 2 months ago
JSON representation
Mathematical Programming Toolbox for AMPL/GMPL
- Host: GitHub
- URL: https://github.com/fdabrandao/pympl
- Owner: fdabrandao
- License: other
- Created: 2015-08-25T02:57:56.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-04-07T22:08:20.000Z (about 6 years ago)
- Last Synced: 2025-03-27T22:23:27.641Z (2 months ago)
- Topics: ampl, modeling-language, optimization, optimization-tools
- Language: Python
- Homepage: https://pypi.python.org/pypi/PyMPL
- Size: 996 KB
- Stars: 14
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: COPYING
Awesome Lists containing this project
README
## PyMPL: A Mathematical Programming Toolbox
Copyright (C) 2015-2016, Filipe Brandão
Faculdade de Ciências, Universidade do Porto
Porto, Portugal. All rights reserved. E-mail: .---
[PyMPL](https://github.com/fdabrandao/pympl) is a python extension to the AMPL modeling language that adds new [statements](https://github.com/fdabrandao/pympl/wiki/STMTS) for evaluating python code within AMPL/GMPL models. PyMPL also includes, among others, procedures for modeling [piecewise linear functions](https://github.com/fdabrandao/pympl/wiki/STMTS_SOS), [compressed arc-flow graphs](https://github.com/fdabrandao/pympl/wiki/STMTS_VPSolver) for vector packing, [sub-tour elimination constraints](https://github.com/fdabrandao/pympl/wiki/STMTS_TSP) for TSP, and [lot-sizing reformulations](https://github.com/fdabrandao/pympl/wiki/STMTS_LSLIB) (LS-LIB). PyMPL is fully compatible with both python 2 and 3.

[](https://travis-ci.org/fdabrandao/pympl)
[](https://coveralls.io/github/fdabrandao/pympl)### Useful links
* PyMPL documentation:
* GiHub repository:
* BitBucket repository:
* Docker repository:
* PyPI repository:### Setup
Install from the [repository](https://pypi.python.org/pypi/PyMPL):
```bash
$ 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](https://github.com/fdabrandao/pympl/wiki/Docker-container) that includes a simple web app for an easy usage.
### Examples
``piecewise_linear.mod``
```ampl
# 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``:
```ampl
# 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 hereminimize obj: Z;
s.t. demand{i in I}: x[i] >= b[i]; # demand constraints
end;
```### PyMPL Parser
```python
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")
```[[Folder with examples](https://github.com/fdabrandao/pympl/tree/master/examples)]
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.***
Copyright © 2015-2016 [Filipe Brandão](http://www.dcc.fc.up.pt/~fdabrandao/) < [[email protected]](mailto:[email protected]) >. All rights reserved.