Created
July 17, 2016 14:38
-
-
Save pchaigno/76423c97bab73a0c0bee4b27beae5f10 to your computer and use it in GitHub Desktop.
Simple linear program with PuLP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ubuntu:16.04 | |
MAINTAINER Paul Chaignon <[email protected]> | |
RUN apt update | |
RUN apt install -y python-pip glpk-utils coinor-cbc | |
RUN pip install pulp | |
RUN apt build-dep -y python-matplotlib | |
RUN pip install --no-cache-dir pylab | |
ADD . /lp | |
WORKDIR /lp | |
ENTRYPOINT python pl.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pulp | |
from matplotlib.pylab import dot, random | |
def solve_minmax(n, a, b, x_min=None, x_max=None): | |
x = pulp.LpVariable.dicts("x", range(n), x_min, x_max) | |
m = pulp.LpVariable("m") | |
lp_prob = pulp.LpProblem("Minmax Problem", pulp.LpMinimize) | |
lp_prob += pulp.lpSum([m]), "Minimize_the_maximum" | |
for i in range(n): | |
label = "Max_constraint_%d" % i | |
dot_b_x = pulp.lpSum([b[i][j] * x[j] for j in range(n)]) | |
condition = pulp.lpSum([m]) >= a[i] + dot_b_x | |
lp_prob += condition, label | |
lp_prob.writeLP("/tmp/MinmaxProblem.lp") # optional | |
lp_prob.solve(pulp.COIN_CMD()) | |
print "Status:", pulp.LpStatus[lp_prob.status] | |
for v in lp_prob.variables(): | |
print v.name, "=", v.varValue | |
print "Total Cost =", pulp.value(m) | |
if __name__ == "__main__": | |
n = 50 | |
a = 2. * random(n) - 1. | |
b = 2. * random((n, n)) - 1. | |
solve_minmax(n, a, b, -5, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment