Skip to content

Instantly share code, notes, and snippets.

@pchaigno
Created July 17, 2016 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pchaigno/76423c97bab73a0c0bee4b27beae5f10 to your computer and use it in GitHub Desktop.
Save pchaigno/76423c97bab73a0c0bee4b27beae5f10 to your computer and use it in GitHub Desktop.
Simple linear program with PuLP
FROM ubuntu:16.04
MAINTAINER Paul Chaignon <paul.chaignon@gmail.com>
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
#!/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