使用python线性规划学习总结

 

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非零

 

#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()

 

产生结果如下:

>>> 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值