等式约束
constraint_eq = [
lambda x: x[0] + x[1] + x[2] - 1
]
不等式约束
constraint_ueq = [
lambda x: 210 * x[0] + 300 * x[1] + 100 * x[2] + 130 * x[3] + 260 * x[4] - 600,
lambda x: x[2] + x[3] - 1,
lambda x: -x[0] + x[4]
]
不等式约束
'''constraint_ueq = [
lambda x: x[0] + 2 * x[1] - x[2] - 2,
lambda x: x[0] + 4 * x[1] + x[2] - 4,
lambda x: x[0] + x[1] - 3,
lambda x: 4 * x[1] + x[2] - 6
]'''
'''def cons_ueq1(x):
return [x[0]+2*x[1]-x[2]-2]
cons1=cons_ueq1
def cons_ueq2(x):
return [x[0] + 4 * x[1] + x[2] - 4]
cons2=cons_ueq2
def cons_ueq3(x):
return [x[0]+x[1]-3]
cons3=cons_ueq3
def cons_ueq4(x):
return [4*x[1]+x[2]-6]
cons4=cons_ueq4'''
ga = GA(func=object_func, n_dim=5, size_pop=200, max_iter=800, lb=[0, 0, 0, 0, 0],
ub=[1, 1, 1, 1, 1], constraint_eq=constraint_eq, constraint_ueq=constraint_ueq, precision=[1, 1, 1, 1, 1])
#ga = GA(func=object_func, n_dim=3, size_pop=200, max_iter=800, lb=[0, 0, 0, ],
ub=[1, 1, 1], constraint_eq=constraint_eq, constraint_ueq=constraint_ueq, precision=[1, 1, 1])
ga = GA(func=object_func, n_dim=3, size_pop=200, max_iter=800, lb=[0, 0, 0],
ub=[1,1,1],constraint_ueq=[cons1,cons2,cons3,cons4], precision=[1, 1, 1])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', -best_y)
ga = GA(func=object_func, n_dim=3, size_pop=200, max_iter=800, lb=[0, 0, 0],
ub=[1,1,1],constraint_ueq=[cons1,cons2,cons3,cons4], precision=[1, 1, 1])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', -best_y)
(2)结果
案例1
best_x: [1. 0. 0. 1. 1.]
best_y: [410.]
Process finished with exit code 0
案例2
best_x: [1. 0. 1.]
best_y: [8.]
4、PuLp实现
(1)代码
import numpy as np
import pulp # 导入 pulp 库
主程序
def main():
InvestLP = pulp.LpProblem("Invest decision problem", sense=pulp.LpMaximize) # 定义问题,求最大值
x1 = pulp.LpVariable('A', cat='Binary') # 定义 x1,A 项目
x2 = pulp.LpVariable('B', cat='Binary') # 定义 x2,B 项目
x3 = pulp.LpVariable('C', cat='Binary') # 定义 x3,C 项目
x4 = pulp.LpVariable('D', cat='Binary') # 定义 x4,D 项目
x5 = pulp.LpVariable('E', cat='Binary') # 定义 x5,E 项目
InvestLP += (150x1 + 210x2 + 60x3 + 80x4 + 180*x5) # 设置目标函数 f(x)
InvestLP += (210x1 + 300x2 + 100x3 + 130x4 + 260*x5 <= 600) # 不等式约束
InvestLP += (x1 + x2 + x3 == 1) # 等式约束
InvestLP += (x3 + x4 <= 1) # 不等式约束
InvestLP += (x5 - x1 <= 0) # 不等式约束
'''InvestLP + =(3x1-2x2+5*x3)
InvestLP +=(x1+2*x2-x3<=2)
InvestLP +=(x1+4*x2+x3<=4)
InvestLP +=(x1+x2<=3)
InvestLP +=(4*x2+x3<=6)'''
InvestLP.solve()
print(InvestLP.name) # 输出求解状态
print("Status youcans:", pulp.LpStatus[InvestLP.status]) # 输出求解状态
for v in InvestLP.variables():
print(v.name, "=", v.varValue) # 输出每个变量的最优值
print("Max f(x) =", pulp.value(InvestLP.objective)) # 输出最优解的目标函数值
return
if name == 'main':
main()
(2)结果
案例1
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\ADMINI1\AppData\Local\Temp\db61c307138c48e1a387c3ed1a44019d-pulp.mps max timeMode elapsed branch printingOptions all solution C:\Users\ADMINI1\AppData\Local\Temp\db61c307138c48e1a387c3ed1a44019d-pulp.sol
At line 2 NAME MODEL
At line 3 ROWS
At line 9 COLUMNS
At line 37 RHS
At line 42 BOUNDS
At line 48 ENDATA
Problem MODEL has 4 rows, 5 columns and 12 elements
Coin0008I MODEL read with 0 errors
Option for timeMode changed from cpu to elapsed
Continuous objective value is 410 - 0.00 seconds
Cgl0004I processed model has 4 rows, 5 columns (5 integer (5 of which binary)) and 12 elements
Cutoff increment increased from 1e-05 to 9.9999
Cbc0038I Initial state - 0 integers unsatisfied sum - 0
Cbc0038I Solution found of -410
Cbc0038I Before mini branch and bound, 5 integers at bound fixed and 0 continuous
Cbc0038I Mini branch and bound did not improve solution (0.01 seconds)
Cbc0038I After 0.01 seconds - Feasibility pump exiting with objective of -410 - took 0.00 seconds
Cbc0012I Integer solution of -410 found by feasibility pump after 0 iterations and 0 nodes (0.01 seconds)
Cbc0001I Search completed - best objective -410, 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 -410 to -410
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: 410.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.02 (Wallclock seconds): 0.02
Invest_decision_problem
Status youcans: Optimal
A = 1.0
B = 0.0
C = 0.0
D = 1.0
E = 1.0
Max f(x) = 410.0
Process finished with exit code 0
案例2
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\ADMINI1\AppData\Local\Temp\550a3c1b17d243fb96401a6fb553ed11-pulp.mps max timeMode elapsed branch printingOptions all solution C:\Users\ADMINI1\AppData\Local\Temp\550a3c1b17d243fb96401a6fb553ed11-pulp.sol
At line 2 NAME MODEL
At line 3 ROWS
At line 9 COLUMNS
At line 29 RHS
At line 34 BOUNDS
At line 38 ENDATA
Problem MODEL has 4 rows, 3 columns and 10 elements
Coin0008I MODEL read with 0 errors
Option for timeMode changed from cpu to elapsed
Continuous objective value is 8 - 0.00 seconds
Cgl0004I processed model has 0 rows, 0 columns (0 integer (0 of which binary)) and 0 elements
Cbc3007W No integer variables - nothing to do
Cuts at root node changed objective from -8 to -1.79769e+308
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: 8.00000000
Enumerated nodes: 0
Total iterations: 0
Time (CPU seconds): 0.00
Time (Wallclock seconds): 0.01
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.01 (Wallclock seconds): 0.01
最后
🍅 硬核资料:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
🍅 技术互助:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
🍅 面试题库:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
🍅 知识体系:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。