[英]How to find out the value the pulp LP found when solving?
I was wondering whether or not there is a way to get the results of the LP (linear programme) found when solving your problem.我想知道在解决您的问题时是否有办法获得 LP(线性规划)的结果。 The values i am looking for is not what percentage of each item should be used but the value found for each of your constraints.我正在寻找的值不是应该使用每个项目的百分比,而是为每个约束找到的值。 I have looked in the documentation and the docstrings for the pulp module and haven't found a way to get the values.我查看了纸浆模块的文档和文档字符串,但没有找到获取值的方法。
The site i looked through to check if there was any way:我浏览的网站以检查是否有任何方法:
https://pythonhosted.org/PuLP/pulp.html https://pythonhosted.org/PuLP/pulp.html
Not sure to have fully understood your question.不确定是否完全理解您的问题。 I assume you are asking how to find the value of a pulp constraint .我假设您在问如何找到纸浆约束的值。
Let prob be your pulp linear problem.让prob成为您的纸浆线性问题。
You can get the value of a constraint in 2 ways:您可以通过两种方式获取约束的值:
# iterate over the variables of the constraint and sum their values
# I'm not considering here the possibility that varValue may be None
for constraint in prob.constraints:
constraint_sum = 0
for var, coefficient in prob.constraints[constraint].items():
constraint_sum += var.varValue * coefficient
print(prob.constraints[constraint].name, constraint_sum)
Otherwise directly by using the value attribute, but if the constraint has a RHS you have to pay attention and consider it, since the value of the constraint will consider it.否则直接使用 value 属性,但是如果约束有 RHS 你必须注意并考虑它,因为约束的值会考虑它。
# getting the value of the constraint
for constraint in prob.constraints:
print(prob.constraints[constraint].name, prob.constraints[constraint].value() - prob.constraints[constraint].constant)
Indeed, this is how the value() method is implemented in LpAffineExpression , the superclass of LpConstraint : https://github.com/coin-or/pulp/blob/master/src/pulp/pulp.py事实上,这是()的值的方法是如何在LpAffineExpression实现,LpConstraint的超类: https://github.com/coin-or/pulp/blob/master/src/pulp/pulp.py
def value(self):
s = self.constant
for v,x in self.items():
if v.varValue is None:
return None
s += v.varValue * x
return s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.