user7953955
user7953955

Reputation:

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. 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

Upvotes: 0

Views: 2883

Answers (1)

abc
abc

Reputation: 11929

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.
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.

    # 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

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

Upvotes: 4

Related Questions