如何在纸浆中安装不同的求解器?

发布于 2025-01-20 13:40:39 字数 1556 浏览 1 评论 0原文

我有一个课程项目,要求我们使用松弛变量的价值以及问题中的成本降低。我已经提出并解决了问题,但是当我安装纸浆时,它仅下载并安装了一个求解器glpk_cmd。我认为该求解器正在使我的成本降低,而松弛值则显示为“无”,因为朋友可以运行它并用我的代码获得值。如何在纸浆中安装另一个求解器?我使用Anaconda安装Python,因此PIP对我不起作用。 '''

# Problem 1
from pulp import *
# Create LP
prob_1 = LpProblem("DoorProduction", LpMinimize)
solver.solver = getSolver('CPLEX_CMD')

#assign variables
M1 = LpVariable("Machine_1",0,350) #hours for machine 1
M2 = LpVariable("Machine_2",0,300) #hours for machine 2
O1 = LpVariable("Overtime_1",0) #overtime hours for machine 1
O2 = LpVariable("Overtime_2",0) #overtime hours for machine 2
S = LpVariable("Standard_Doors",25)
H = LpVariable("High_Security_Doors",25)
X = LpVariable("Max_Security_Doors",10)

# Objective Function
prob_1 += 30 * M1 + 40 * M2 + 65 * O1 + 90 * O2

# Constraints
prob_1 += 3 * S + 7 * H + 9 * X == M1 + O1, "Machine_One"
prob_1 += 4 * S + 5 * H + 7 * X == M2 + O2, "Machine_Two"
prob_1 += 540 * S + 760 * H + 950 * X >= 45000, "Revenue"

# Solve the problem
prob_1.solve(pulp.solvers.PULP_CBC_CMD(msg=True))
print("Status:", LpStatus[prob_1.status])

# Print results and variable values
for v in prob_1.variables():
    print(v.name, "=", v.varValue, "\trc =", v.dj)
print()

for name, c in prob_1.constraints.items():
    print(name, ":\t", c.pi, "\tslack =", c.slack)
print()

print("Optimal objective value", value(prob_1.objective))

revenue = 540 * S.varValue + 760 * H.varValue + 950 * X.varValue
profit = revenue - value(prob_1.objective)

print()
print("Revenue =",revenue)
print("Profit =", profit)

'''

I have a project for a class that asks us to use the value of the slack variables and the reduced costs in a question. I have formulated and solved the problem, but when I installed pulp, it only downloaded and installed one solver, GLPK_CMD. I think this solver is making my reduced costs and slack values show as "None" because a friend could run it and get values with my code. How do I install another solver in pulp? I used anaconda to install python, so pip doesn't work for me.
'''

# Problem 1
from pulp import *
# Create LP
prob_1 = LpProblem("DoorProduction", LpMinimize)
solver.solver = getSolver('CPLEX_CMD')

#assign variables
M1 = LpVariable("Machine_1",0,350) #hours for machine 1
M2 = LpVariable("Machine_2",0,300) #hours for machine 2
O1 = LpVariable("Overtime_1",0) #overtime hours for machine 1
O2 = LpVariable("Overtime_2",0) #overtime hours for machine 2
S = LpVariable("Standard_Doors",25)
H = LpVariable("High_Security_Doors",25)
X = LpVariable("Max_Security_Doors",10)

# Objective Function
prob_1 += 30 * M1 + 40 * M2 + 65 * O1 + 90 * O2

# Constraints
prob_1 += 3 * S + 7 * H + 9 * X == M1 + O1, "Machine_One"
prob_1 += 4 * S + 5 * H + 7 * X == M2 + O2, "Machine_Two"
prob_1 += 540 * S + 760 * H + 950 * X >= 45000, "Revenue"

# Solve the problem
prob_1.solve(pulp.solvers.PULP_CBC_CMD(msg=True))
print("Status:", LpStatus[prob_1.status])

# Print results and variable values
for v in prob_1.variables():
    print(v.name, "=", v.varValue, "\trc =", v.dj)
print()

for name, c in prob_1.constraints.items():
    print(name, ":\t", c.pi, "\tslack =", c.slack)
print()

print("Optimal objective value", value(prob_1.objective))

revenue = 540 * S.varValue + 760 * H.varValue + 950 * X.varValue
profit = revenue - value(prob_1.objective)

print()
print("Revenue =",revenue)
print("Profit =", profit)

'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文