使用python线性规划学习总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jacke121/article/details/79954541

1.pulp

例子:https://blog.csdn.net/MartaYang/article/details/71439074

          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
Reading problem data from 'C:\Users\tony\AppData\Local\Temp\12100-pulp.lp'...
1 row, 4 columns, 3 non-zeros
8 lines were read
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模块解决线性规划问题

原文https://blog.csdn.net/Gardenia_Hello/article/details/70466079

今天在学习数模的过程中遇到了一些线性规划的问题,老师推荐的是lingo这款软件,然而并不想因此再新学一门语言。于是找到了找到了一个python下解决线性规划问题的模块——Pymprog。

- 安装

本人使用的Windows下的Python环境Anaconda,使用conda应该是找不到这个模块的,直接使用pip安装即可。

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
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页