使用Gurobi optimizer求解…
m
a
x
f
(
X
)
=
2
x
1
+
3
x
2
max f(X) = 2x_1 + 3x_2
maxf(X)=2x1+3x2
s.t.
x
1
+
2
∗
x
2
≤
8
x_1 + 2*x_2 \leq 8
x1+2∗x2≤8
4
∗
x
1
≤
16
4*x_1 \leq16
4∗x1≤16
4
∗
x
2
≤
12
4*x_2 \leq 12
4∗x2≤12
x
1
,
x
2
≥
0
x_1, x_2 \geq 0
x1,x2≥0
# Import lib
from gurobipy import *
# Create model
model_1 = Model()
# Add decision variables to a model
x1 = model_1.addVar(lb=0, vtype=GRB.CONTINUOUS, name="x1")
x2 = model_1.addVar(lb=0, vtype=GRB.CONTINUOUS, name="x2")
# Set objective function
f1 = 2*x1 + 3*x2
model_1.setObjective(f1, GRB.MAXIMIZE)
# Add constraints to a model
ct1 = model_1.addConstr(lhs=x1+2*x2, sense=GRB.LESS_EQUAL, rhs=8, name='ct1')
ct2 = model_1.addConstr(lhs=4*x1, sense=GRB.LESS_EQUAL, rhs=16, name="ct2")
ct2 = model_1.addConstr(lhs=4*x2, sense=GRB.LESS_EQUAL, rhs=12, name="ct3")
# Optimize
model_1.optimize()
# Print data
if model_1.status == GRB.Status.OPTIMAL:
print('='*25)
# Print objVal
print('==> objVal:' ,model_1.objVal)
# Print var value
print('==> x1:', x1, 'x2:', x2)
Academic license - for non-commercial use only
Optimize a model with 3 rows, 2 columns and 4 nonzeros
Coefficient statistics:
Matrix range [1e+00, 4e+00]
Objective range [2e+00, 3e+00]
Bounds range [0e+00, 0e+00]
RHS range [8e+00, 2e+01]
Presolve removed 2 rows and 0 columns
Presolve time: 0.01s
Presolved: 1 rows, 2 columns, 2 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 1.6000000e+01 2.000000e+00 0.000000e+00 0s
1 1.4000000e+01 0.000000e+00 0.000000e+00 0s
Solved in 1 iterations and 0.02 seconds
Optimal objective 1.400000000e+01
=========================
==> objVal: 14.0
==> x1: <gurobi.Var x1 (value 4.0)> x2: <gurobi.Var x2 (value 2.0)>
m
i
n
f
(
X
)
=
−
3
x
1
+
x
2
+
x
3
minf(X) = -3x_1 + x_2 + x_3
minf(X)=−3x1+x2+x3
s.t.
x
1
−
2
x
2
+
x
3
≤
11
x_1 - 2x_2 + x_3 \leq 11
x1−2x2+x3≤11
−
4
x
1
+
x
2
+
2
x
3
≥
3
-4x_1 + x_2 + 2x_3 \geq 3
−4x1+x2+2x3≥3
−
2
x
1
+
x
3
=
1
-2x_1 + x_3 = 1
−2x1+x3=1
x
1
,
x
2
,
x
3
≥
0
x_1, x_2, x_3 \geq 0
x1,x2,x3≥0
Note:
- Add decision variables to a model in a loop.
- Build a linear expression by using
quicksum()
function.
# Create model
model_2 = Model()
# Add decision variables to a model in a loop
x_names = ['x1', 'x2', 'x3']
x = {}
for x_name in x_names:
x[x_name] = model_2.addVar(name=x_name)
# Set objective function
f2 = -3*x['x1'] + x['x2'] + x['x3']
model_2.setObjective(f2, GRB.MINIMIZE)
# Add constraints to a model
model_2.addConstr(lhs=x['x1']-2*x['x2']+x['x3'], sense=GRB.LESS_EQUAL, rhs=11)
model_2.addConstr(lhs=-4*x['x1']+x['x2']+2*x['x3'], sense=GRB.GREATER_EQUAL, rhs=3)
model_2.addConstr(lhs=-2*x['x1']+x['x3'], sense=GRB.EQUAL, rhs=1)
# Optimize
model_2.optimize()
# Print data
if model_2.status == GRB.Status.OPTIMAL:
print('='*25)
#
print('==> Objective function:', f2)
# Print ObjVal
print('==> ObjVal:', model_2.ObjVal)
# Pirnt variable value
print('==> x1:',x['x1'], 'x2:',x['x2'], 'x3:',x['x3'])
Optimize a model with 3 rows, 3 columns and 8 nonzeros
Coefficient statistics:
Matrix range [1e+00, 4e+00]
Objective range [1e+00, 3e+00]
Bounds range [0e+00, 0e+00]
RHS range [1e+00, 1e+01]
Presolve removed 3 rows and 3 columns
Presolve time: 0.01s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 -2.0000000e+00 0.000000e+00 0.000000e+00 0s
Solved in 0 iterations and 0.01 seconds
Optimal objective -2.000000000e+00
=========================
==> Objective function: <gurobi.LinExpr: -3.0 x1 + x2 + x3>
==> ObjVal: -2.0
==> x1: <gurobi.Var x1 (value 4.0)> x2: <gurobi.Var x2 (value 1.0)> x3: <gurobi.Var x3 (value 9.0)>
m
i
n
f
(
X
)
=
2
x
1
+
3
x
2
+
5
x
3
+
2
x
4
+
3
x
5
minf(X) = 2x_1 + 3x_2 + 5x_3 + 2x_4 + 3x_5
minf(X)=2x1+3x2+5x3+2x4+3x5
s.t.
x
1
+
x
2
+
2
x
3
+
x
4
+
3
x
5
≥
4
x_1 + x_2 + 2x_3 + x_4 + 3x_5 \geq 4
x1+x2+2x3+x4+3x5≥4
2
x
1
−
x
2
+
3
x
3
+
x
4
+
x
5
≥
3
2x_1 - x_2 + 3x_3 + x_4 + x_5 \geq 3
2x1−x2+3x3+x4+x5≥3
x
1
,
x
2
,
x
3
,
x
4
,
x
5
≥
0
x_1, x_2, x_3, x_4, x_5 \geq 0
x1,x2,x3,x4,x5≥0
Note:
- Add decision variables to a model by using
Model.addVars()
function. - Build a linear expression by using
quicksum()
function.
# Creatw model
model_3 = Model(name='model_3')
# Add decision variables to a model by using Model.addVars()
x_vector = [('x1'), ('x2'), ('x3'), ('x4'), ('x5')]
x = model_3.addVars(x_vector, name=x_vector)
# Set objective function
f3 = quicksum([ 2*x['x1'], 3*x['x2'], 5*x['x3'], 2*x['x4'], 3*x['x5'] ])
model_3.setObjective(f3)
# Add constraints to a model
model_3.addConstr(lhs=quicksum([ 1*x['x1'], 1*x['x2'], 2*x['x3'], 1*x['x4'], 3*x['x5'] ]) , sense=GRB.GREATER_EQUAL, rhs=4)
model_3.addConstr(lhs=quicksum([ 2*x['x1'], -1*x['x2'], 3*x['x3'], 1*x['x4'], 1*x['x5'] ]) , sense=GRB.GREATER_EQUAL, rhs=3)
# OPtimize
model_3.optimize()
# Print data
if model_3.status == GRB.Status.OPTIMAL:
print('='*25)
#
print('==> Objective function:', model_3.getObjective())
# ObjVal
print('==> ObjVal:', model_3.ObjVal)
# Print optimal variable value
for v in model_3.getVars():
print('==> ' , (v.varName, v.x) )
Optimize a model with 2 rows, 5 columns and 10 nonzeros
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [2e+00, 5e+00]
Bounds range [0e+00, 0e+00]
RHS range [3e+00, 4e+00]
Presolve removed 0 rows and 1 columns
Presolve time: 0.01s
Presolved: 2 rows, 4 columns, 8 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 0.0000000e+00 1.750000e+00 0.000000e+00 0s
2 5.0000000e+00 0.000000e+00 0.000000e+00 0s
Solved in 2 iterations and 0.01 seconds
Optimal objective 5.000000000e+00
=========================
==> Objective function: <gurobi.LinExpr: 2.0 x1 + 3.0 x2 + 5.0 x3 + 2.0 x4 + 3.0 x5>
==> ObjVal: 5.0
==> ('x1', 1.0)
==> ('x2', 0.0)
==> ('x3', 0.0)
==> ('x4', 0.0)
==> ('x5', 1.0)