用默认求解器没问题,使用CPLEX就会出错,不知道咋回事
from pulp import *
# 1. 建立问题
model = LpProblem("钢材生产问题", LpMinimize)
# 2. 建立变量
ingredients = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7']
ingMass = LpVariable.dicts("Ingr", ingredients, lowBound = 0, cat='Continuous')
# 3. 设置目标函数
costs = {
'x1': 16,
'x2': 10,
'x3': 8,
'x4': 9,
'x5': 48,
'x6': 60,
'x7': 53
}
model += lpSum([costs[item] * ingMass[item] for item in ingredients]), "总生产成本"
# 4. 施加约束
carbonPercent = {
'x1': 0.008,
'x2': 0.007,
'x3': 0.0085,
'x4': 0.004,
'x5': 0,
'x6': 0,
'x7': 0
}
NiPercent = {
'x1': 0.18,
'x2': 0.032,
'x3': 0,
'x4': 0,
'x5': 1,
'x6': 0,
'x7': 0
}
CrPercent = {
'x1': 0.12,
'x2': 0.011,
'x3': 0,
'x4': 0,
'x5': 0,
'x6': 1,
'x7': 0
}
MoPercent = {
'x1': 0,
'x2': 0.001,
'x3': 0,
'x4': 0,
'x5': 0,
'x6': 0,
'x7': 1
}
model += lpSum([ingMass[item] for item in ingredients]) == 1000, "质量约束"
model += lpSum([carbonPercent[item]*ingMass[item] for item in ingredients]) >= 6.5, "碳最小占比"
model += lpSum([carbonPercent[item]*ingMass[item] for item in ingredients]) <= 7.5, "碳最大占比"
model += lpSum([NiPercent[item]*ingMass[item] for item in ingredients]) >= 30, "镍最小占比"
model += lpSum([NiPercent[item]*ingMass[item] for item in ingredients]) <= 35, "镍最大占比"
model += lpSum([CrPercent[item]*ingMass[item] for item in ingredients]) >= 10, "铬最小占比"
model += lpSum([CrPercent[item]*ingMass[item] for item in ingredients]) <= 12, "铬最大占比"
model += lpSum([MoPercent[item]*ingMass[item] for item in ingredients]) >= 11, "钼最小占比"
model += lpSum([MoPercent[item]*ingMass[item] for item in ingredients]) <= 13, "钼最大占比"
model += ingMass['x1'] <= 75, "废料1可用量"
model += ingMass['x2'] <= 250, "废料2可用量"
# 5. 求解
model.solve(CPLEX())
# 6. 打印结果
print(model)
print("求解状态:", LpStatus[model.status])
for v in model.variables():
print(v.name, "=", v.varValue)
print("最优总成本 = ", value(model.objective))
希望可以求解决
你安装的社区版吗请问