Linear Program (LP)

   使用Gurobi optimizer求解线性规划(LP)问题…

$$ max f(X) = 2x_1 + 3x_2 $$
   s.t.
$$ x_1 + 2x_2 \leq 8 $$
$$ 4x_1 \leq 16 $$
$$ 4x_2 \leq 12 $$
$$ x_1, x_2 \geq 0 $$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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)>

$$ minf(X) = -3x_1 + x_2 + x_3 $$
   s.t.
$$ x_1 - 2x_2 + x_3 \leq 11 $$
$$ -4x_1 + x_2 + 2x_3 \geq 3 $$
$$ -2x_1 + x_3 = 1 $$
$$ x_1, x_2, x_3 \geq 0 $$

   Note:

  • Add decision variables to a model in a loop.
  • Build a linear expression by using quicksum() function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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)>

$$ minf(X) = 2x_1 + 3x_2 + 5x_3 + 2x_4 + 3x_5 $$
   s.t.
$$ x_1 + x_2 + 2x_3 + x_4 + 3x_5 \geq 4 $$
$$ 2x_1 - x_2 + 3x_3 + x_4 + x_5 \geq 3 $$
$$ x_1, x_2, x_3, x_4, x_5 \geq 0 $$

   Note:

  • Add decision variables to a model by using Model.addVars() function.
  • Build a linear expression by using quicksum() function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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)
有何不可 wechat
Subscribe to my blog by scanning my wechat account~
书山有路勤为径,学海无涯苦作舟~