Skip to content

Instantly share code, notes, and snippets.

@dragstar328
Created April 14, 2015 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragstar328/19c36156db0310f45d44 to your computer and use it in GitHub Desktop.
Save dragstar328/19c36156db0310f45d44 to your computer and use it in GitHub Desktop.
LP by openopt + glpk
# coding:utf-8
import FuncDesigner as fd
from openopt import LP
"""
max. 400x + 300y
sub. 60x+40y <= 3800
20x+30y <= 2100
20x+10y <= 1200
"""
# variables
x, y = fd.oovars(2)
# problem
f = 400*x + 300*y
# startpoirt
startpoint = {x: 0, y: 0}
# subjects
const = []
const.append(60*x+40*y <= 3800)
const.append(20*x+30*y <= 2100)
const.append(20*x+10*y <= 1200)
# create instance
p = LP(f, startpoint, constraints=const, goal="max")
# solve by glpk
r = p.solve("glpk")
# solution
x_opt = r(x, y)
print "Solution: x=%d y=%d" % (x_opt[0], x_opt[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment