Coder Social home page Coder Social logo

fdabrandao / pympl Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 3.0 1020 KB

Mathematical Programming Toolbox for AMPL/GMPL

Home Page: https://pypi.python.org/pypi/PyMPL

License: Other

Shell 9.27% Python 79.28% AMPL 8.07% CSS 0.08% JavaScript 0.33% HTML 2.79% Dockerfile 0.18%
optimization ampl optimization-tools modeling-language

pympl's Introduction

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: [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.

Coverage Status

Useful links

Setup

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.

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

[Folder with 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 < [email protected] >. All rights reserved.

pympl's People

Contributors

fdabrandao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.