# 使用python线性规划学习总结

1.pulp

https://blog.csdn.net/cyuhong/article/details/50865909

https://www.cnblogs.com/shizhenqiang/p/8274806.html

2.python-pymprog
3.scipy.optimize.linprog

1.pulp试试这个模块

from pulp import *
# 设置对象
f  = LpProblem('lptest', LpMinimize)
# 设置三个变量，并设置变量最小取值
x = LpVariable('x', lowBound = 0)
y = LpVariable('y', lowBound = 0)
z = LpVariable('z', lowBound = 0)

# 载入约束变量
f += 3.05 * x + 4.05 * y + 6.1 * z >= 7.9

# 求解
GLPK().solve(f)

# 显示结果
for i in f.variables():
print(i.name + "=" + str(i.varValue))


GLPSOL: GLPK LP/MIP Solver, v4.55
Parameter(s) specified in the command line:
--cpxlp C:\Users\tony\AppData\Local\Temp\12100-pulp.lp -o C:\Users\tony\AppData\Local\Temp\12100-pulp.sol
1 row, 4 columns, 3 non-zeros
GLPK Simplex Optimizer, v4.55
1 row, 4 columns, 3 non-zeros
Preprocessing...
1 row, 3 columns, 3 non-zeros
Scaling...
A: min|aij| = 3.050e+000  max|aij| = 6.100e+000  ratio = 2.000e+000
Problem data seem to be well scaled
Constructing initial basis...
Size of triangular part is 1
0: obj =  0.000000000e+000  infeas = 7.900e+000 (0)
*     1: obj =  0.000000000e+000  infeas = 0.000e+000 (0)
OPTIMAL LP SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.0 Mb (36952 bytes)
Writing basic solution to 'C:\Users\tony\AppData\Local\Temp\12100-pulp.sol'...
1

__dummy=None
x=0.0
y=0.0
z=1.29508

# 使用python-pymprog模块解决线性规划问题

## - 安装

pip install pymprog
• 1

## - 简单的例子

maximize  15 x + 10 y         # 目标函数
S.T.
x         <=  3  # 约束条件1
y  <=  4  # 约束条件2
x +    y  <=  5  # 约束条件3
x >=0, y >=0     # x,y非零
• 1
• 2
• 3
• 4
• 5
• 6
#coding: utf-8
from pymprog import *
begin('bike production')
x, y = var('x, y') # 变量
maximize(15 * x + 10 * y, 'profit') # 目标函数
x <= 3 # 约束条件1
y <= 4 # 约束条件2
x + y <= 5 # 约束条件3
solve()
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9

>>> from pymprog import *
>>> begin('bike production')
model('bikes production') is the default model.
>>> x, y = var('x, y') # create variables
>>> x, y # take a look at them
(0 <= x continuous, 0 <= y continuous)
>>> maximize(15*x + 10*y, 'profit')
Max profit: 15 * x + 10 * y
>>> x <= 3
0 <= x <= 3 continuous
>>> y <= 4
0 <= y <= 4 continuous
>>> x + y <= 5
R1: x + y <= 5
>>> solve()
GLPK Simplex Optimizer, v4.60
1 row, 2 columns, 2 non-zeros
*     0: obj =  -0.000000000e+00 inf =   0.000e+00 (2)
*     2: obj =   6.500000000e+01 inf =   0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120