《使用python线性规划学习总结x.docx》由会员分享,可在线阅读,更多相关《使用python线性规划学习总结x.docx(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、使用python线性规划学习总结x运用 python 线性规划学习总结1.pulp 例子: 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
2、* 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.55Parameter(s) specified in the command line: -cpxlp C:UserstonyAppDataLocalTemp12100-pulp.lp -o C:UserstonyAppDataLocalTemp12100-pulp.solReading problem dat
3、a from C:UserstonyAppDataLocalTemp12100-pulp.lp.1 row, 4 columns, 3 non-zeros8 lines were readGLPK Simplex Optimizer, v4.551 row, 4 columns, 3 non-zerosPreprocessing.1 row, 3 columns, 3 non-zerosScaling. A: min|aij| = 3.050e+000max|aij| = 6.100e+000ratio = 2.000e+000Problem data seem to be well scal
4、edConstructing initial basis.Size of triangular part is 10: obj =0.000000000e+000infeas = 7.900e+000 (0)* 1: obj =0.000000000e+000infeas = 0.000e+000 (0)OPTIMAL LP SOLUTION FOUNDTime used: 0.0 secsMemory used: 0.0 Mb (36952 bytes)Writing basic solution to C:UserstonyAppDataLocalTemp12100-pulp.sol.1
5、_dummy=Nonex=0.0y=0.0z=1.29508运用 python-pymprog 模块解决线性规划问题 原文 今日在学习数模的过程中遇到了一些线性规划的问题,老师举荐的是 lingo 这款软件,然而并不想因此再新学一门语言。于是找到了找到了一个 python 下解决线性规划问题的模块Pymprog。- 安装 本人运用的 Windows 下的 Python 环境 Anaconda,运用 conda 应当是找不到这个模块的,干脆运用 pip 安装即可。pip install pymprog• 1- 简洁的例子 如解决如下规划问题:maximize15 x + 10 y #
6、目标函数S.T. x <=3# 约束条件 1y<=4# 约束条件 2 x +y<=5# 约束条件 3 x >=0, y >=0 # x,y 非零1 2 3 4 5 • 6 #coding: utf-8from pymprog import *begin(bike production)x, y = var(x, y) # 变量maximize(15 * x + 10 * y, profit) # 目标函数x <= 3 # 约束条件 1y <= 4 # 约束条件 2x + y <= 5 # 约束条件 3solve()1 2 3 4 5 6
7、 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,
8、 profit)Max profit: 15 * x + 10 * y>>> x <= 30 <= x <= 3 continuous>>> y <= 40 <= y <= 4 continuous>>> x + y <= 5R1: x + y <= 5>>> solve()GLPK Simplex Optimizer, v4.601 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