Created
April 14, 2015 02:44
-
-
Save dragstar328/19c36156db0310f45d44 to your computer and use it in GitHub Desktop.
LP by openopt + glpk
This file contains 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
# 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