文章目錄
- 簡單回顧
- 線性規劃LP
- 整數規劃IP
- 0-1規劃
簡單回顧
線性規劃是數學規劃中的一類最簡單規劃問題,常見的線性規劃是一個有約束的,變量范圍為有理數的線性規劃。如:
使用matlab
的linprog
函數即可求解簡單的線性規劃問題,可以參考這篇博客:
MATLAB求解線性規劃(含整數規劃和0-1
規劃)問題
matlab
求解線性規劃LP
問題需要化為最小化問題,所有約束條件必須為≤
類型,限制較多。本文介紹使用python+gurobi
進行求解。
python+gurobi
介紹參考這篇博客:
gurobi最新下載安裝教程 2023.11
線性規劃LP
import gurobipy
from gurobipy import GRB# 創建模型
c = [7, 12]
a = [[9, 4],[4, 5],[3, 10]]
b = [300, 200, 300]
MODEL = gurobipy.Model("Example")# 創建變量
x = MODEL.addVars(2, lb=0, ub=gurobipy.GRB.INFINITY, name='x')# 更新變量環境
MODEL.update()# 創建目標函數
MODEL.setObjective(x.prod(c), gurobipy.GRB.MAXIMIZE)# 創建約束條件
MODEL.addConstrs(x.prod(a[i]) <= b[i] for i in range(3))# 執行線性規劃模型
MODEL.optimize()
print("Obj:", MODEL.objVal)
for v in MODEL.getVars():print(f"{v.VarName}:{round(v.X,3)}")
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 3 rows, 2 columns and 6 nonzeros
Model fingerprint: 0x6b25b35d
Coefficient statistics:Matrix range [3e+00, 1e+01]Objective range [7e+00, 1e+01]Bounds range [0e+00, 0e+00]RHS range [2e+02, 3e+02]
Presolve time: 0.01s
Presolved: 3 rows, 2 columns, 6 nonzerosIteration Objective Primal Inf. Dual Inf. Time0 3.2500000e+30 2.812500e+30 3.250000e+00 0s2 4.2800000e+02 0.000000e+00 0.000000e+00 0sSolved in 2 iterations and 0.01 seconds (0.00 work units)
Optimal objective 4.280000000e+02
Obj: 428.0
x[0]:20.0
x[1]:24.0
最終可得最優解為x = 20, y = 24, 最優值為428。
gurobi
對最大化問題、最小化問題,大于等于和小于等于約束都支持。
整數規劃IP
import gurobipy
from gurobipy import GRB
import numpy as np# 創建模型
c = [3, 2]
a = [[2, 3],[4, 2]]
b = [14, 18]
MODEL = gurobipy.Model("Example")# 創建變量
#x = MODEL.addVars(2, lb=0, ub=gurobipy.GRB.INFINITY, name='x')x1 = MODEL.addVar(vtype=GRB.INTEGER,lb=0,ub=GRB.INFINITY, name='x1')
x2 = MODEL.addVar(vtype=GRB.INTEGER,lb=0,ub=GRB.INFINITY, name='x2')
# 更新變量環境MODEL.update()# 創建目標函數
MODEL.setObjective(c[0]*x1+c[1]*x2, gurobipy.GRB.MAXIMIZE)# 創建約束條件
for i in range(2):MODEL.addConstr(a[i][0]*x1 + a[i][1]*x2 <= b[i])# 執行線性規劃模型
MODEL.optimize()
print("Obj:", MODEL.objVal)
for v in MODEL.getVars():print(f"{v.VarName}:{round(v.X,3)}")
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0x15a6e8bd
Variable types: 0 continuous, 2 integer (0 binary)
Coefficient statistics:Matrix range [2e+00, 4e+00]Objective range [2e+00, 3e+00]Bounds range [0e+00, 0e+00]RHS range [1e+01, 2e+01]
Found heuristic solution: objective 14.0000000
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzeros
Variable types: 0 continuous, 2 integer (0 binary)Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 8 (of 8 available processors)Solution count 1: 14 Optimal solution found (tolerance 1.00e-04)
Best objective 1.400000000000e+01, best bound 1.400000000000e+01, gap 0.0000%
Obj: 14.0
x1:4.0
x2:1.0
可得該整數規劃問題的最優解為x1 = 4, x2 = 1
,最優值為14
。
如果解其對應的松弛問題:
import gurobipy
from gurobipy import GRB
import numpy as np# 創建模型
c = [3, 2]
a = [[2, 3],[4, 2]]
b = [14, 18]
MODEL = gurobipy.Model("Example")# 創建變量
x = MODEL.addVars(2, lb=0, ub=gurobipy.GRB.INFINITY, name='x')# 更新變量環境MODEL.update()# 創建目標函數
MODEL.setObjective(x.prod(c), gurobipy.GRB.MAXIMIZE)# 創建約束條件
MODEL.addConstrs(x.prod(a[i]) <= b[i] for i in range(2))# 執行線性規劃模型
MODEL.optimize()
print("Obj:", MODEL.objVal)
for v in MODEL.getVars():print(f"{v.VarName}:{round(v.X,3)}")
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0x15a42e7d
Coefficient statistics:Matrix range [2e+00, 4e+00]Objective range [2e+00, 3e+00]Bounds range [0e+00, 0e+00]RHS range [1e+01, 2e+01]
Presolve time: 0.01s
Presolved: 2 rows, 2 columns, 4 nonzerosIteration Objective Primal Inf. Dual Inf. Time0 5.0000000e+30 2.750000e+30 5.000000e+00 0s2 1.4750000e+01 0.000000e+00 0.000000e+00 0sSolved in 2 iterations and 0.01 seconds (0.00 work units)
Optimal objective 1.475000000e+01
Obj: 14.75
x[0]:3.25
x[1]:2.5
可以發現對應的解是x1 = 3.25, x2 = 2.5
, 最優值為14.75
。松弛問題的最優解總是優于整數規劃問題的。
0-1規劃
無論是matlab
的linprog
函數還是gurobi,0-1
規劃實際上只需要在整數規劃的基礎上,讓決策變量的定義域在0-1
之間即可。
仍然是上面的同一個問題:
## 0-1規劃import gurobipy
from gurobipy import GRB# 創建模型
c = [3, 2]
a = [[2, 3],[4, 2]]
b = [14, 18]
MODEL = gurobipy.Model("Example")# 創建變量
#x = MODEL.addVars(2, lb=0, ub=gurobipy.GRB.INFINITY, name='x')x1 = MODEL.addVar(vtype=GRB.INTEGER,lb=0,ub=1, name='x1')
x2 = MODEL.addVar(vtype=GRB.INTEGER,lb=0,ub=1, name='x2')
# 更新變量環境MODEL.update()# 創建目標函數
MODEL.setObjective(c[0]*x1+c[1]*x2, gurobipy.GRB.MAXIMIZE)# 創建約束條件
for i in range(2):MODEL.addConstr(a[i][0]*x1 + a[i][1]*x2 <= b[i])# 執行線性規劃模型
MODEL.optimize()
print("Obj:", MODEL.objVal)
for v in MODEL.getVars():print(f"{v.VarName}:{round(v.X,3)}")
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 3 rows, 2 columns and 6 nonzeros
Model fingerprint: 0x6b25b35d
Coefficient statistics:Matrix range [3e+00, 1e+01]Objective range [7e+00, 1e+01]Bounds range [0e+00, 0e+00]RHS range [2e+02, 3e+02]
Presolve time: 0.01s
Presolved: 3 rows, 2 columns, 6 nonzerosIteration Objective Primal Inf. Dual Inf. Time0 3.2500000e+30 2.812500e+30 3.250000e+00 0s2 4.2800000e+02 0.000000e+00 0.000000e+00 0sSolved in 2 iterations and 0.01 seconds (0.00 work units)
Optimal objective 4.280000000e+02
Obj: 428.0
x[0]:20.0
x[1]:24.0
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 3 rows, 6 columns and 18 nonzeros
Model fingerprint: 0x157f6a4a
Coefficient statistics:Matrix range [9e+00, 6e+01]Objective range [6e+00, 1e+01]Bounds range [0e+00, 0e+00]RHS range [6e+01, 2e+02]
Presolve time: 0.01s
Presolved: 3 rows, 6 columns, 18 nonzerosIteration Objective Primal Inf. Dual Inf. Time0 0.0000000e+00 4.187500e+01 0.000000e+00 0s3 2.9842520e+01 0.000000e+00 0.000000e+00 0sSolved in 3 iterations and 0.01 seconds (0.00 work units)
Optimal objective 2.984251969e+01
Obj: 29.84251968503937
x[0]:0.0
x[1]:0.433
x[2]:0.0
x[3]:4.252
x[4]:0.0
x[5]:0.0
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0x15a6e8bd
Variable types: 0 continuous, 2 integer (0 binary)
Coefficient statistics:Matrix range [2e+00, 4e+00]Objective range [2e+00, 3e+00]Bounds range [0e+00, 0e+00]RHS range [1e+01, 2e+01]
Found heuristic solution: objective 14.0000000
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzeros
Variable types: 0 continuous, 2 integer (0 binary)Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 8 (of 8 available processors)Solution count 1: 14 Optimal solution found (tolerance 1.00e-04)
Best objective 1.400000000000e+01, best bound 1.400000000000e+01, gap 0.0000%
Obj: 14.0
x1:4.0
x2:1.0
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0x15a42e7d
Coefficient statistics:Matrix range [2e+00, 4e+00]Objective range [2e+00, 3e+00]Bounds range [0e+00, 0e+00]RHS range [1e+01, 2e+01]
Presolve time: 0.01s
Presolved: 2 rows, 2 columns, 4 nonzerosIteration Objective Primal Inf. Dual Inf. Time0 5.0000000e+30 2.750000e+30 5.000000e+00 0s2 1.4750000e+01 0.000000e+00 0.000000e+00 0sSolved in 2 iterations and 0.01 seconds (0.00 work units)
Optimal objective 1.475000000e+01
Obj: 14.75
x[0]:3.25
x[1]:2.5
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (win64)CPU model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0xdff3d373
Variable types: 0 continuous, 2 integer (0 binary)
Coefficient statistics:Matrix range [2e+00, 4e+00]Objective range [2e+00, 3e+00]Bounds range [1e+00, 1e+00]RHS range [1e+01, 2e+01]
Found heuristic solution: objective 5.0000000Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 1 (of 8 available processors)Solution count 1: 5 Optimal solution found (tolerance 1.00e-04)
Best objective 5.000000000000e+00, best bound 5.000000000000e+00, gap 0.0000%
Obj: 5.0
x1:1.0
x2:1.0
可得0-1規劃的最優解是x1 = x2 = 1
, 最優值 = 5
。
當然0-1規劃的典型應用場景是指派問題、運輸問題、排班問題等。