如何使用 PuLP 对 LP 问题实施互斥性?

问题描述 投票:0回答:1

我在 PuLP 中制定了以下 LP 问题公式。我们有一个包含 5 个决策变量的目标函数,每个决策变量都有界限。我们还为每个决策变量设置了 5 个二元变量,以便在前 3 个和后 2 个决策变量之间强制执行互斥性。但是,当我运行它时,PuLP 给我 Status: Infeasible。怎么了?

代码如下:

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)}")
python linear-programming pulp discrete-optimization
1个回答
0
投票

这里有一个提示:想想 P2

bounds
以及它如何与
P2
上的约束相互作用:

P2 = LpVariable("P2", 10, 20)

prob += P2 <= y2*5000000, "P2-Binary-Constraint"

并为

P3
做同样的事情。

然后加上你的互斥性(写对了)

© www.soinside.com 2019 - 2024. All rights reserved.