[运筹优化]Gurobi之Python调用实例——简单整数规划

[运筹优化]Gurobi之Python调⽤实例——简单整数规划
⼀个:
\begin{equation}
\begin{aligned}
& max & \quad x+y+2z \
& s.t. & \
过氧化酶& \quad & x+2y+3z \le 4 \
& \quad & x+y \ge 1 \
李薇日记
& \quad & x,y,z \quad binary
\end{aligned}
\end{equation}
Python代码:
from gurobipy import*
try:
# Create a new model
m = Model("mip1")
# Create variables
x = m.addVar(vtype=GRB.BINARY, name="x")
y = m.addVar(vtype=GRB.BINARY, name="y")
z = m.addVar(vtype=GRB.BINARY, name="z")
御书院# Set objective
m.setObjective(x + y +2* z, GRB.MAXIMIZE)
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x +2* y +3* z <=4,"c0")
# Add constraint: x + y >= 1
m.addConstr(x + y >=1,"c1")
m.optimize()
for v Vars():
print('%s %g'%(v.varName, v.x))
print('Obj: %g'% m.objVal)
except GurobiError as e:
print('Error code '+)+": "+str(e))
except AttributeError:
print('Encountered an attribute error')
输出结果:
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Variable types: 0 continuous, 3 integer (3 binary)
助产学Coefficient statistics:
Matrix range    [1e+00, 3e+00]非法侵入(01)
Objective range  [1e+00, 2e+00]
Bounds range    [1e+00, 1e+00]
RHS range        [1e+00, 4e+00]
Found heuristic solution: objective 2.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.02s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.02 seconds
Thread count was 1 (of 4 available processors)
Solution count 2: 3 2
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000% x 1
y 0
z 1石云生
Obj: 3

本文发布于:2024-09-22 10:04:57,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/536897.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:整数   运筹   输出   优化
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议