当前位置:   article > 正文

python实例 优化目标函数_python求解线性规划问题,后发现了scipy模块,optimize,新手希望大神能写个实例,例子如下:!python曲线拟合...

线性规划问题目标函数有常数项吗

python求解线性规划问题,后发现了scipy模块,optimize,新手希望大神能写个实例,例子如下:!python曲线拟合

用python线性规划教程2020-10-10 09:44:52人已围观

线性规划习题

手边没有matlab和lingo(我也不会用)

方法一:安装VB或者打开excel VBA 编辑界面下输入代码如下

Option Explicit

Dim x1 As Integer

Dim x2 As Integer

Dim x3 As Integer

Dim x As Integer

Dim xmax As Integer

Dim x11 As Integer

Dim x22 As Integer

Dim x33 As Integer

Private Sub CommandButton1_Click()

xmax = 0

For x1 = 0 To 4

For x2 = 0 To 5

For x3 = 0 To 6

x = 3 * x1 4 * x2 x3

If ((x1 2 * x2 x3) <= 10) And ((x1 x2 2 * x3) <= 6) And ((3 * x1 x2 2 * x3) <= 12) Then

If x >= xmax Then

xmax = x

x11 = x1

x22 = x2

x33 = x3

End If

End If

Next x3

Next x2

Next x1

MsgBox (x11)

MsgBox (x22)

MsgBox (x33)

End Sub

最终得到最佳答案:x1=2 x2=4 x3=0时 zm=3x1 4x2 x3=22

方法二:使用lingo

一般地,使用LINGO 求解运筹学问题可以分为以下两个步骤来完成:

1)根据实际问题,建立数学模型,即使用数学建模的方法建立优化模型

2)根据优化模型,利用LINGO 来求解模型。主要是根据LINGO 软件,把数学模型转译成计算机语言,借助于计算机来求解。

例:在线性规划中的应用max Z =5 X1 3 X2 6X3,

s.t. X1 2 X2 X3 ≤18

2 X1 X2 3 X3 =16

X1 X2 X3 =10

X1 ,X2 ≥0 , X3 为自由变量

应用LINGO 来求解该模型,只需要在 lingo窗口中输入以下信息即可:

max=5·x1 3·x2 6·x3 ;

x1 2·x2 x3 <=18 ;

2·x1 x2 3·x3 =16 ;

x1 x2 x3 =10 ;

@free( x3) ;

然后按运行按钮,得到模型最优解,具体如下:

Objective value: 46.00000

Variable Value

Reduced Cost

x1 14.00000 0.000000   x2 0.000000 1.000000   x3 -4 .000000 0.000000

由此可知,当 x1 =14 , x2 =0 , x3 =-4 时,模型得到最优值,且最优值为 46。

说明:在利用LINGO 求解线性规划时,如自变量都为非负的话,在LINGO 中输入的信息和模型基本相同;如自变量为自由变量,可以使用函数 @free来把系统默认的非负变量定义自由变量,如实例一中的 x3。

python求解线性规划问题,后发现了scipy模块,optimize,新手希望大神能写个实例,例子如下:

scipy做线性规划不是便,推荐用pulp来做,这个模块不属于python的内置模块,需要先安装,pip install pulp

from pulp import *

# 设置对象

prob = LpProblem('myProblem', LpMinimize)

# 设置三个变量,并设置变量最小取值

x1 = LpVariable('x1', 0)

x2 = LpVariable('x2', 0)

x3 = LpVariable('x3', 0)

x4 = LpVariable('x4')

# 载入目标函数,默认是求最小值,因此这次对原目标函数乘以-1

prob = 3*x1 - 4*x2 2*x3 -5*x4

# 载入约束变量

prob = 4*x1 - x2 2*x3 -x4 == -2

prob = x1 x2 -x3 2*x4 <= 14

prob = -2*x1 3*x2 x3 -x4 >= 2

# 求解

status = prob.solve()

# 显示结果

for i in prob.variables():

print(i.name "=" str(i.varValue))

计算结果为:

x1=0.0

x2=2.0

x3=4.0

x4=8.0

python有能解决层次线性规划的库吗?

约件没时间细看了,根据你的代了一下,没有语法错误了! !分; sets: month/1..12/: it, dt, st, rt, ht; endsets !目标函数; MAX=@sum(month(I): 5400*DT(I) 2400.110*HT(I)); !约束条件; @for(month(I): Rt(I) - Dt(I) >=20; It(。

Python scipy库线性规划如何让变量取整数

scipy做线性规划很方便,推荐用pulp来做,模块不属于python的内置模块要先安装,pip install pulp

from pulp import *

# 设置对象

prob = LpProblem('myProblem', LpMinimize)

# 设个变量,并设置变量最小取值

x1 = LpVariable('x1', 0)

x2 = LpVariable('x2', 0)

x3 = LpVariable('x3', 0)

x4 = LpVariable('x4')

# 载入目标函数,默认是求最小值,因此这次对原目标函数乘以-1

prob = 3*x1 - 4*x2 2*x3 -5*x4

# 载入约束变量

prob = 4*x1 - x2 2*x3 -x4 == -2

prob = x1 x2 -x3 2*x4 <= 14

prob = -2*x1 3*x2 x3 -x4 >= 2

# 求解

Python里面的scipy库如何计算线性规划问题呢?

这一篇文章介绍了如何做,但是你的例子,考以下,应该很简能写出来了http://jingyan.baidu.com/article/e2284b2b5800e6e2e6118d97.html

Python有哪些可以做带约束的二次线性规划的包

APM Python

- APM Python is free optimization software through a web service.

Nonlinear Programming problem are sent to the APMonitor server and

results are returned to the local Python script. A web-interface

automatically loads to help visualize solutions, in particular dynamic

optimization problems that include differential and algebraic equations.

Default solvers include APOPT, BPOPT, and IPOPT. Pre-configured modes

include optimization, parameter estimation, dynamic simulation, and

nonlinear control.

Coopr - The Coopr software project integrates a variety of Python optimization-related packages.

CVOXPT

- CVXOPT is a free software package for convex optimization based on

the Python programming language. It can be used with the interactive

Python interpreter, on the command line by executing Python scripts, or

integrated in other software via Python extension modules. Its main

purpose is to make the development of software for convex optimization

applications straightforward by building on Pythons extensive standard

library and on the strengths of Python as a high-level programming

language.

OpenOpt

(license: BSD) contains connections to tens of solvers and has some own

Python-written ones, e.g. nonlinear solver with specifiable accuracy: interalg, graphic output of convergence and some more numerical optimization "MUST HAVE" features. Also OpenOpt can solve FuncDesigner

problems with automatic differentiation, that usually work faster and

gives more precise results than finite-differences derivatives

approximation.

PuLP

- PuLP is an LP modeler written in python. PuLP can generate MPS or LP

files and call GLPK, COIN CLP/CBC, CPLEX, and GUROBI to solve linear

problems.

Pyomo

- The Python Optimization Modeling Objects (Pyomo) package is an open

source tool for modeling optimization applications in Python. Pyomo can

be used to define symbolic problems, create concrete problem instances,

and solve these instances with standard solvers. Pyomo provides a

capability that is commonly associated with algebraic modeling languages

such as AMPL, AIMMS, and GAMS, but Pyomo's modeling objects are

embedded within a full-featured high-level programming language with a

rich set of supporting libraries. Pyomo leverages the capabilities of

the Coopr software library, which integrates Python packages for

defining optimizers, modeling optimization applications, and managing

computational experiments.

scipy.optimize - some solvers written or connected by SciPy developers.

pyOpt

- pyOpt is a package for formulating and solving nonlinear constrained

optimization problems in an efficient, reusable and portable manner

(license: LGPL).

Python有哪些可以做带约束的二次线性规划的包

线性规划立足于求满足所有约束条件的最优解,而在 实际问题中,可能存在相互矛盾的约束条件.目标规划可 以在相互矛盾的约束条件下找到满意解.

Python有哪些可以做带约束的二次线性规划的包

线性规划立足于求满足所有约束条件的最优解,而在 实际问题中,可能存在相互矛盾的约束条件.目标规划可 以在相互矛盾的约束条件下找到满意解.

版权声明:本站所有文章皆为原创,欢迎转载或转发,请保留网站地址和作者信息。

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

闽ICP备14008679号