PuLP 是一个用于求解线性规划 (LP) 和混合整数线性规划 (MILP) 问题的 Python 库
pip install pulp
import pulp
prob = pulp.LpProblem("MILP_Problem", pulp.LpMaximize)
x = pulp.LpVariable('x', lowBound=0, cat='Continuous')
y = pulp.LpVariable('y', lowBound=0, cat='Integer')
prob += 3*x + 2*y
prob += x + 2*y <= 100
prob += x + y <= 80
prob += x <= 40
prob.solve()
print("Status:", pulp.LpStatus[prob.status])
print("Optimal Solution:")
for v in prob.variables():
print(v.name, "=", v.varValue)
print("Objective =", pulp.value(prob.objective))
以上代码示例创建了一个简单的混合整数线性规划问题,并使用 PuLP 求解。要调试和优化您的代码,请参考以下建议:
lowBound
和 upBound
)。import pulp
# 使用 GLPK 求解器
prob.solve(pulp.GLPK_CMD())
prob.solve(pulp.PULP_CBC_CMD(msg=True))
pulp.LpStatus
和 pulp.value()
函数检查求解状态和目标值。领取专属 10元无门槛券
手把手带您无忧上云