赞
踩
Linux环境下,要是用python的pulp进行线性规划
需要先安装
1、pulp
直接终端输入:$ easy_install pulp
(注:若无easy_install 则终端输入 $ sudo apt-get install python-setuptools)
2、调试安装glpk
最常见的是:直接终端输入$ sudo apt-get install glpk
若不行:点击打开链接
尔后就可以模仿着写出python代码
- from pulp import *
- #对象
- sum = LpProblem('money', LpMinimize)
- x11 = LpVariable('x11', lowBound = 0)
- x12 = LpVariable('x12', lowBound = 0)
- x13 = LpVariable('x13', lowBound = 0)
- x14 = LpVariable('x14', lowBound = 0)
- x15 = LpVariable('x15', lowBound = 0)
- x16 = LpVariable('x16', lowBound = 0)
- x10 = LpVariable('x10', lowBound = 0)
-
- y11 = LpVariable('y11', lowBound = 0)
- y12 = LpVariable('y12', lowBound = 0)
- y13 = LpVariable('y13', lowBound = 0)
- y14 = LpVariable('y14', lowBound = 0)
- y15 = LpVariable('y15', lowBound = 0)
- y16 = LpVariable('y16', lowBound = 0)
- y10 = LpVariable('y10', lowBound = 0)
-
- x22 = LpVariable('x22', lowBound = 0)
- x23 = LpVariable('x23', lowBound = 0)
- x24 = LpVariable('x24', lowBound = 0)
- x25 = LpVariable('x25', lowBound = 0)
- x26 = LpVariable('x26', lowBound = 0)
- x20 = LpVariable('x20', lowBound = 0)
-
- y22 = LpVariable('y22', lowBound = 0)
- y23 = LpVariable('y23', lowBound = 0)
- y24 = LpVariable('y24', lowBound = 0)
- y25 = LpVariable('y25', lowBound = 0)
- y26 = LpVariable('y26', lowBound = 0)
- y20 = LpVariable('y20', lowBound = 0)
-
- x33 = LpVariable('x33', lowBound = 0)
- x34 = LpVariable('x34', lowBound = 0)
- x35 = LpVariable('x35', lowBound = 0)
- x36 = LpVariable('x36', lowBound = 0)
- x30 = LpVariable('x30', lowBound = 0)
-
- y33 = LpVariable('y33', lowBound = 0)
- y34 = LpVariable('y34', lowBound = 0)
- y35 = LpVariable('y35', lowBound = 0)
- y36 = LpVariable('y36', lowBound = 0)
- y30 = LpVariable('y30', lowBound = 0)
-
- x44 = LpVariable('x44', lowBound = 0)
- x45 = LpVariable('x45', lowBound = 0)
- x46 = LpVariable('x46', lowBound = 0)
- x40 = LpVariable('x40', lowBound = 0)
-
- y44 = LpVariable('y44', lowBound = 0)
- y45 = LpVariable('y45', lowBound = 0)
- y46 = LpVariable('y46', lowBound = 0)
- y40 = LpVariable('y40', lowBound = 0)
-
-
- x55 = LpVariable('x55', lowBound = 0)
- x56 = LpVariable('x56', lowBound = 0)
- x50 = LpVariable('x50', lowBound = 0)
-
- y55 = LpVariable('y55', lowBound = 0)
- y56 = LpVariable('y56', lowBound = 0)
- y50 = LpVariable('y50', lowBound = 0)
-
- x66 = LpVariable('x66', lowBound = 0)
- x60 = LpVariable('x60', lowBound = 0)
-
- y66 = LpVariable('y66', lowBound = 0)
- y60 = LpVariable('y60', lowBound = 0)
-
- s1 = LpVariable('s1', lowBound = 0)
- s2 = LpVariable('s2', lowBound = 0)
- s3 = LpVariable('s3', lowBound = 0)
- s4 = LpVariable('s4', lowBound = 0)
- s5 = LpVariable('s5', lowBound = 0)
- s6 = LpVariable('s6', lowBound = 0)
- s0 = LpVariable('s0', lowBound = 0)
-
- # 载入约束变量
- sum += 15*x11+15.3*x12+15.5*x13+15.7*x14+15.9*x15+16.1*x16
- +16*y11+16.3*y12+16.5*y13+16.7*y14+16.9*y15+17.1*y16
- +14*x22+14.3*x23+14.5*x24+14.7*x25+14.9*x26
- +15*y22+15.3*y23+15.5*y24+15.7*y25+15.9*y26
- +13.5*x33+13.8*x34+14*x35+14.2*x36
- +14.5*y33+14.8*y34+15*y35+15.2*y36
- +13*x44+13.3*x45+13.5*x46
- +14*y44+14.3*y45+14.5*y46
- +13*x55+13.3*x56
- +14*y55+14.3*y56
- +13.5*x66
- +14.5*y66
- sum += x11+x12+x13+x14+x15+x16+x10 ==60
- sum += y11+y12+y13+y14+y15+y16+y10 ==10
- sum += x22+x23+x24+x25+x26+x20 ==50
- sum += y22+y23+y24+y25+y26+y20 ==10
- sum += x33+x34+x35+x36+x30 ==90
- sum += y33+y34+y35+y36+y30 ==20
- sum += x44+x45+x46+x40 ==100
- sum += y44+y45+y46+y40 ==40
- sum += x55+x56+x50 ==100
- sum += y55+y56+y50 ==40
- sum += x66+x60 ==80
- sum += y66+y60 ==40
- sum += s1+s2+s3+s4+s5+s6+s0 ==103
- sum += x11+y11+s1 == 104
- sum += x12+x22+y12+y22+s2 == 75
- sum += x13+x23+x33+y13+y23+y33+s3 == 115
- sum += x14+x24+x34+x44+y14+y24+y34+y44+s4 == 160
- sum += x15+x25+x35+x45+x55+y15+y25+y35+y45+y55+s5 == 103
- sum += x16+x26+x36+x46+x56+x66+y16+y26+y36+y46+y56+y66+s6 == 150
- sum += x10+x20+x30+x40+x50+x60+y10+y20+y30+y40+y50+y60+s0 == 36
-
- # 求解
- GLPK().solve(sum)
-
- # 显示结果
- for i in sum.variables():
- print(i.name + "=" + str(i.varValue))
问题:
总结不足:初涉python,代码过于冗长
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。