当前位置:   article > 正文

用python-pulp解决线性规划问题_pulp glpk

pulp glpk

Linux环境下,要是用python的pulp进行线性规划

需要先安装

1、pulp

直接终端输入:$ easy_install pulp

(注:若无easy_install  则终端输入 $ sudo apt-get install python-setuptools)

2、调试安装glpk

最常见的是:直接终端输入$ sudo apt-get install glpk

若不行:点击打开链接


尔后就可以模仿着写出python代码

  1. from pulp import *
  2. #对象
  3. sum = LpProblem('money', LpMinimize)
  4. x11 = LpVariable('x11', lowBound = 0)
  5. x12 = LpVariable('x12', lowBound = 0)
  6. x13 = LpVariable('x13', lowBound = 0)
  7. x14 = LpVariable('x14', lowBound = 0)
  8. x15 = LpVariable('x15', lowBound = 0)
  9. x16 = LpVariable('x16', lowBound = 0)
  10. x10 = LpVariable('x10', lowBound = 0)
  11. y11 = LpVariable('y11', lowBound = 0)
  12. y12 = LpVariable('y12', lowBound = 0)
  13. y13 = LpVariable('y13', lowBound = 0)
  14. y14 = LpVariable('y14', lowBound = 0)
  15. y15 = LpVariable('y15', lowBound = 0)
  16. y16 = LpVariable('y16', lowBound = 0)
  17. y10 = LpVariable('y10', lowBound = 0)
  18. x22 = LpVariable('x22', lowBound = 0)
  19. x23 = LpVariable('x23', lowBound = 0)
  20. x24 = LpVariable('x24', lowBound = 0)
  21. x25 = LpVariable('x25', lowBound = 0)
  22. x26 = LpVariable('x26', lowBound = 0)
  23. x20 = LpVariable('x20', lowBound = 0)
  24. y22 = LpVariable('y22', lowBound = 0)
  25. y23 = LpVariable('y23', lowBound = 0)
  26. y24 = LpVariable('y24', lowBound = 0)
  27. y25 = LpVariable('y25', lowBound = 0)
  28. y26 = LpVariable('y26', lowBound = 0)
  29. y20 = LpVariable('y20', lowBound = 0)
  30. x33 = LpVariable('x33', lowBound = 0)
  31. x34 = LpVariable('x34', lowBound = 0)
  32. x35 = LpVariable('x35', lowBound = 0)
  33. x36 = LpVariable('x36', lowBound = 0)
  34. x30 = LpVariable('x30', lowBound = 0)
  35. y33 = LpVariable('y33', lowBound = 0)
  36. y34 = LpVariable('y34', lowBound = 0)
  37. y35 = LpVariable('y35', lowBound = 0)
  38. y36 = LpVariable('y36', lowBound = 0)
  39. y30 = LpVariable('y30', lowBound = 0)
  40. x44 = LpVariable('x44', lowBound = 0)
  41. x45 = LpVariable('x45', lowBound = 0)
  42. x46 = LpVariable('x46', lowBound = 0)
  43. x40 = LpVariable('x40', lowBound = 0)
  44. y44 = LpVariable('y44', lowBound = 0)
  45. y45 = LpVariable('y45', lowBound = 0)
  46. y46 = LpVariable('y46', lowBound = 0)
  47. y40 = LpVariable('y40', lowBound = 0)
  48. x55 = LpVariable('x55', lowBound = 0)
  49. x56 = LpVariable('x56', lowBound = 0)
  50. x50 = LpVariable('x50', lowBound = 0)
  51. y55 = LpVariable('y55', lowBound = 0)
  52. y56 = LpVariable('y56', lowBound = 0)
  53. y50 = LpVariable('y50', lowBound = 0)
  54. x66 = LpVariable('x66', lowBound = 0)
  55. x60 = LpVariable('x60', lowBound = 0)
  56. y66 = LpVariable('y66', lowBound = 0)
  57. y60 = LpVariable('y60', lowBound = 0)
  58. s1 = LpVariable('s1', lowBound = 0)
  59. s2 = LpVariable('s2', lowBound = 0)
  60. s3 = LpVariable('s3', lowBound = 0)
  61. s4 = LpVariable('s4', lowBound = 0)
  62. s5 = LpVariable('s5', lowBound = 0)
  63. s6 = LpVariable('s6', lowBound = 0)
  64. s0 = LpVariable('s0', lowBound = 0)
  65. # 载入约束变量
  66. sum += 15*x11+15.3*x12+15.5*x13+15.7*x14+15.9*x15+16.1*x16
  67. +16*y11+16.3*y12+16.5*y13+16.7*y14+16.9*y15+17.1*y16
  68. +14*x22+14.3*x23+14.5*x24+14.7*x25+14.9*x26
  69. +15*y22+15.3*y23+15.5*y24+15.7*y25+15.9*y26
  70. +13.5*x33+13.8*x34+14*x35+14.2*x36
  71. +14.5*y33+14.8*y34+15*y35+15.2*y36
  72. +13*x44+13.3*x45+13.5*x46
  73. +14*y44+14.3*y45+14.5*y46
  74. +13*x55+13.3*x56
  75. +14*y55+14.3*y56
  76. +13.5*x66
  77. +14.5*y66
  78. sum += x11+x12+x13+x14+x15+x16+x10 ==60
  79. sum += y11+y12+y13+y14+y15+y16+y10 ==10
  80. sum += x22+x23+x24+x25+x26+x20 ==50
  81. sum += y22+y23+y24+y25+y26+y20 ==10
  82. sum += x33+x34+x35+x36+x30 ==90
  83. sum += y33+y34+y35+y36+y30 ==20
  84. sum += x44+x45+x46+x40 ==100
  85. sum += y44+y45+y46+y40 ==40
  86. sum += x55+x56+x50 ==100
  87. sum += y55+y56+y50 ==40
  88. sum += x66+x60 ==80
  89. sum += y66+y60 ==40
  90. sum += s1+s2+s3+s4+s5+s6+s0 ==103
  91. sum += x11+y11+s1 == 104
  92. sum += x12+x22+y12+y22+s2 == 75
  93. sum += x13+x23+x33+y13+y23+y33+s3 == 115
  94. sum += x14+x24+x34+x44+y14+y24+y34+y44+s4 == 160
  95. sum += x15+x25+x35+x45+x55+y15+y25+y35+y45+y55+s5 == 103
  96. sum += x16+x26+x36+x46+x56+x66+y16+y26+y36+y46+y56+y66+s6 == 150
  97. sum += x10+x20+x30+x40+x50+x60+y10+y20+y30+y40+y50+y60+s0 == 36
  98. # 求解
  99. GLPK().solve(sum)
  100. # 显示结果
  101. for i in sum.variables():
  102. print(i.name + "=" + str(i.varValue))


问题:




总结不足:初涉python,代码过于冗长


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/844155
推荐阅读
相关标签
  

闽ICP备14008679号