求解时pulp LP找到的值怎么求出来?
How to find out the value the pulp LP found when solving?
我想知道在解决你的问题时是否有办法得到LP(线性规划)的结果。我正在寻找的值不是应该使用每个项目的百分比,而是为每个约束找到的值。我查看了纸浆模块的文档和文档字符串,但没有找到获取值的方法。
我查过的网站有没有办法:
不确定是否完全理解您的问题。我假设您在问 如何找到纸浆约束的值。
让 prob 成为你的纸浆线性问题。
您可以通过两种方式获取约束的值:
# 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)
否则直接使用value属性,但是如果constraint有RHS就得注意考虑了,因为constraint的value会考虑。
# getting the value of the constraint
for constraint in prob.constraints:
print(prob.constraints[constraint].name, prob.constraints[constraint].value() - prob.constraints[constraint].constant)
事实上,LpAffineExpression 中的 value() 方法是这样实现的,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
我想知道在解决你的问题时是否有办法得到LP(线性规划)的结果。我正在寻找的值不是应该使用每个项目的百分比,而是为每个约束找到的值。我查看了纸浆模块的文档和文档字符串,但没有找到获取值的方法。
我查过的网站有没有办法:
不确定是否完全理解您的问题。我假设您在问 如何找到纸浆约束的值。
让 prob 成为你的纸浆线性问题。
您可以通过两种方式获取约束的值:
# 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)
否则直接使用value属性,但是如果constraint有RHS就得注意考虑了,因为constraint的value会考虑。
# getting the value of the constraint
for constraint in prob.constraints:
print(prob.constraints[constraint].name, prob.constraints[constraint].value() - prob.constraints[constraint].constant)
事实上,LpAffineExpression 中的 value() 方法是这样实现的,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