目录
1、概述
2、案例分析
1、概述
我们首先先分享一点干活:线性规划和整数规划的若干建模技巧特别棒。然后就是介绍整数规划的完整知识点。最后呢,我们就是给出一个案例上代码和结果。
2、案例分析
(1)Pulp库实现
import numpy as np
import pulp as pl
def main():
ProbLp=pl.LpProblem("ProbLp",sense=pl.LpMaximize)
print(ProbLp.name)
x1=pl.LpVariable('x1',lowBound=0,upBound=None,cat='Integer')
x2=pl.LpVariable('x2',lowBound=0,upBound=2,cat='integer')
ProbLp+=(x1+4*x2)
ProbLp+=(-2*x1+3*x2<=3)
ProbLp+=(x1+2*x2<=8)
ProbLp.solve()
print("Shan Status:", pl.LpStatus[ProbLp.status]) # 输出求解状态
for v in ProbLp.variables():
print(v.name, "=", v.varValue) # 输出每个变量的最优值
print("F(x) =", pl.value(ProbLp.objective)) # 输出最优解的目标函数值
if __name__ =='__main__':
main()
ProbLp
Welcome to the CBC MILP Solver
Version: 2.10.3
Build Date: Dec 15 2019
command line - C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\pulp\apis\..\solverdir\cbc\win\64\cbc.exe C:\Users\ADMINI~1\AppData\Local\Temp\62f3e078b0f04e78b386e3120bbb215a-pulp.mps max timeMode elapsed branch printingOptions all solution C:\Users\ADMINI~1\AppData\Local\Temp\62f3e078b0f04e78b386e3120bbb215a-pulp.sol
At line 2 NAME MODEL
At line 3 ROWS
At line 7 COLUMNS
At line 16 RHS
At line 19 BOUNDS
At line 22 ENDATA
Problem MODEL has 2 rows, 2 columns and 4 elements
Coin0008I MODEL read with 0 errors
Option for timeMode changed from cpu to elapsed
Continuous objective value is 12 - 0.00 seconds
Cgl0004I processed model has 2 rows, 2 columns (1 integer (0 of which binary)) and 4 elements
Cbc0012I Integer solution of -12 found by DiveCoefficient after 0 iterations and 0 nodes (0.01 seconds)
Cbc0001I Search completed - best objective -12, took 0 iterations and 0 nodes (0.01 seconds)
Cbc0035I Maximum depth 0, 0 variables fixed on reduced cost
Cuts at root node changed objective from -12 to -12
Probing was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Gomory was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Knapsack was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Clique was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
MixedIntegerRounding2 was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
FlowCover was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
TwoMirCuts was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
ZeroHalf was tried 0 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)
Result - Optimal solution found
Objective value: 12.00000000
Enumerated nodes: 0
Total iterations: 0
Time (CPU seconds): 0.01
Time (Wallclock seconds): 0.01
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.05 (Wallclock seconds): 0.05
Shan Status: Optimal
x1 = 4.0
x2 = 2.0
F(x) = 12.0
Process finished with exit code 0
(2)cvxpy实现
import numpy as np
import cvxpy as cp
def main():
n=2
C=np.array([1,4])
A=np.array([[-2,3],[1,2]])
B=np.array([3,8])
x=cp.Variable(n,integer=True)
objective=cp.Maximize((cp.sum(C*x)))
constraints=[0<=x,a*x<=b] #明确约束条件
prob=cp.problem(objective,constraints)
results=prob.solve(solve=cp.CPLEX)
print(prob.value) #目标函数值
print(x.value)
if __name__ =='__main__':
main()