求解后显示约束值
Show constraints values after solving
我有这个简单的产品组合 L.P :
from pulp import *
x = pulp.LpVariable("x", lowBound=0)
y = pulp.LpVariable("y", lowBound=0)
problem = pulp.LpProblem("A simple max problem", pulp.LpMaximize)
problem += 300*x + 250*y, "The objective function"
# Constraints
problem += 1.4*x + y <= 35000, "1st constraint"
problem += 0.51*x + y <= 17000, "2nd constraint"
problem += x <= 22500, "3rd constraint"
problem += y <= 15000, "4th constraint"
problem.solve()
print "Optimal Result:"
for variable in problem.variables():
print variable.name, "=", variable.varValue
print "Total net max profit:"
print value(problem.objective)
Optimal Result:
x = 20224.719
y = 6685.3933
Total net max profit:
7738764.025
如何显示“已填充”约束计算出的解决方案? :
让我们采用第一个约束,我需要进行此计算并显示它,我将 x 和 y 替换为最佳值:
1.4*20224.719 + 6685.3933 = 34999,9999
我需要的是输出这样的东西,列出每个约束“左侧计算”:
{"1st constraint solution" : 34999,9999 ,
"2nd constraint solution" : 16999,9999 ,
"3rd constraint" solution" :20224.719 ,
"4th constraint" solution" : 6685.3933 }
等...
在此示例中,值非常接近,但情况并非总是如此...
我认为它叫做“左侧”
非常感谢,我找不到任何关于此的文档,是否有任何代码准备好执行此操作?
欢迎来到本站。
我已经扩充了您的示例(下方)以展示如何访问 pulp
中的约束值和松弛。您的示例似乎是用 python 的早期版本(3.0 之前)编写的,打印语句中缺少括号就证明了这一点。如果可能的话,您应该转向更现代的安装。
为了在 pulp
中找到这些功能,我发现有时 dox 很好,但很多时候,我只是潜入一个交互式环境 (ipython
) 并使用 auto-complete查看提供的内容并进行探索的功能。 :) 您命名了您的约束,因此下面的名称与名称配合得很好。它应该可以在没有名称的情况下正常工作,并且您可以从字典中打印出约束值,即表达式。
代码
from pulp import *
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)
problem = pulp.LpProblem("A simple max problem", LpMaximize)
problem += 300*x + 250*y, "The objective function"
# Constraints
problem += 1.4*x + y <= 35000, "1st constraint"
problem += 0.51*x + y <= 17000, "2nd constraint"
problem += x <= 22500, "3rd constraint"
problem += y <= 15000, "4th constraint"
problem.solve()
print ("Optimal Result:")
for variable in problem.variables():
print (variable.name, "=", variable.varValue)
print ("Total net max profit:")
print (value(problem.objective))
constraints = problem.constraints
print(f'The constraints are held in a {type(constraints)}')
for name in constraints.keys():
value = constraints.get(name).value()
slack = constraints.get(name).slack
print(f'constraint {name} has value: {value:0.2e} and slack: {slack:0.2e}')
产量:
...
Optimal Result:
x = 20224.719
y = 6685.3933
Total net max profit:
7738764.025
The constraints are held in a <class 'collections.OrderedDict'>
constraint 1st_constraint has value: -1.00e-04 and slack: -0.00e+00
constraint 2nd_constraint has value: -1.00e-05 and slack: -0.00e+00
constraint 3rd_constraint has value: -2.28e+03 and slack: 2.28e+03
constraint 4th_constraint has value: -8.31e+03 and slack: 8.31e+03
我有这个简单的产品组合 L.P :
from pulp import *
x = pulp.LpVariable("x", lowBound=0)
y = pulp.LpVariable("y", lowBound=0)
problem = pulp.LpProblem("A simple max problem", pulp.LpMaximize)
problem += 300*x + 250*y, "The objective function"
# Constraints
problem += 1.4*x + y <= 35000, "1st constraint"
problem += 0.51*x + y <= 17000, "2nd constraint"
problem += x <= 22500, "3rd constraint"
problem += y <= 15000, "4th constraint"
problem.solve()
print "Optimal Result:"
for variable in problem.variables():
print variable.name, "=", variable.varValue
print "Total net max profit:"
print value(problem.objective)
Optimal Result:
x = 20224.719
y = 6685.3933
Total net max profit:
7738764.025
如何显示“已填充”约束计算出的解决方案? :
让我们采用第一个约束,我需要进行此计算并显示它,我将 x 和 y 替换为最佳值:
1.4*20224.719 + 6685.3933 = 34999,9999
我需要的是输出这样的东西,列出每个约束“左侧计算”:
{"1st constraint solution" : 34999,9999 ,
"2nd constraint solution" : 16999,9999 ,
"3rd constraint" solution" :20224.719 ,
"4th constraint" solution" : 6685.3933 }
等...
在此示例中,值非常接近,但情况并非总是如此...
我认为它叫做“左侧” 非常感谢,我找不到任何关于此的文档,是否有任何代码准备好执行此操作?
欢迎来到本站。
我已经扩充了您的示例(下方)以展示如何访问 pulp
中的约束值和松弛。您的示例似乎是用 python 的早期版本(3.0 之前)编写的,打印语句中缺少括号就证明了这一点。如果可能的话,您应该转向更现代的安装。
为了在 pulp
中找到这些功能,我发现有时 dox 很好,但很多时候,我只是潜入一个交互式环境 (ipython
) 并使用 auto-complete查看提供的内容并进行探索的功能。 :) 您命名了您的约束,因此下面的名称与名称配合得很好。它应该可以在没有名称的情况下正常工作,并且您可以从字典中打印出约束值,即表达式。
代码
from pulp import *
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)
problem = pulp.LpProblem("A simple max problem", LpMaximize)
problem += 300*x + 250*y, "The objective function"
# Constraints
problem += 1.4*x + y <= 35000, "1st constraint"
problem += 0.51*x + y <= 17000, "2nd constraint"
problem += x <= 22500, "3rd constraint"
problem += y <= 15000, "4th constraint"
problem.solve()
print ("Optimal Result:")
for variable in problem.variables():
print (variable.name, "=", variable.varValue)
print ("Total net max profit:")
print (value(problem.objective))
constraints = problem.constraints
print(f'The constraints are held in a {type(constraints)}')
for name in constraints.keys():
value = constraints.get(name).value()
slack = constraints.get(name).slack
print(f'constraint {name} has value: {value:0.2e} and slack: {slack:0.2e}')
产量:
...
Optimal Result:
x = 20224.719
y = 6685.3933
Total net max profit:
7738764.025
The constraints are held in a <class 'collections.OrderedDict'>
constraint 1st_constraint has value: -1.00e-04 and slack: -0.00e+00
constraint 2nd_constraint has value: -1.00e-05 and slack: -0.00e+00
constraint 3rd_constraint has value: -2.28e+03 and slack: 2.28e+03
constraint 4th_constraint has value: -8.31e+03 and slack: 8.31e+03