如何使用PuLP对LP问题强制执行互斥



我在PuLP中制作了以下LP问题公式。我们有一个目标函数,它有5个决策变量,每个变量都有界。我们还为每个决策变量设置了5个二进制变量,以加强前3个决策变量和后2个决策变量之间的互斥性。然而,当我运行它时,PuLP会给我状态:不可行。它怎么了?

这是代码:

from pulp import *
# Define the problem as a maximization problem
prob = LpProblem("AVB-Problem", LpMaximize)
# Define the decision variables
P2 = LpVariable("P2", 10, 20)
P3 = LpVariable("P3", 20, 30)
P4 = LpVariable("P4", 30, None)
P6 = LpVariable("P6", 15, 30)
P7 = LpVariable("P7", 30, None)
y2 = LpVariable("y2", cat="Binary")
y3 = LpVariable("y3", cat="Binary")
y4 = LpVariable("y4", cat="Binary")
y6 = LpVariable("y6", cat="Binary")
y7 = LpVariable("y7", cat="Binary")
# Define the objective function
prob += 1*P2 + 1.5*P3 + 1.7*P4 + 2*P6 + 2.5*P7, "Z"
# Define the constraints
prob += P2 + P3 + P4 + P6 + P7 <= 500000, "Total-Constraint"
prob += P2 <= y2*10000000000, "P2-Binary-Constraint"
prob += P3 <= y3*10000000000, "P3-Binary-Constraint"
prob += P4 <= y4*10000000000, "P4-Binary-Constraint"
prob += P6 <= y6*10000000000, "P6-Binary-Constraint"
prob += P7 <= y7*10000000000, "P7-Binary-Constraint"
prob += y2 + y3 + y4 == 1, "Mutual Exclusivity 1"
prob += y6 + y7 == 1, "Mutual Exclusivity 2"
# Solve the problem
prob.solve(PULP_CBC_CMD(msg=1))
# Print the solution status
print(f"Status: {LpStatus[prob.status]}")
# Print the optimal solution and the optimal value of the objective function
for v in prob.variables():
print(f"{v.name}: {v.varValue}")
print(f"Optimal Value of the Objective Function: {value(prob.objective)}")

这里有一个提示:想想P2边界,以及它如何与P2:上的约束相互作用

P2 = LpVariable("P2", 10, 20)
prob += P2 <= y2*5000000, "P2-Binary-Constraint"

并对P3执行相同操作。

然后加入你的相互排他性(写得正确)

相关内容

  • 没有找到相关文章

最新更新