'Show constraints values after solving
I have this simple product mix 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
How can I display the "filled" constraints calculated solutions ? :
Let's take the 1st constraint, I need to do this calculation and to display it, I replace x and y by the optimal values:
1.4*20224.719 + 6685.3933 = 34999,9999
What I need is to output is something like this ,listing each constraints "left side calculation":
{"1st constraint solution" : 34999,9999 ,
"2nd constraint solution" : 16999,9999 ,
"3rd constraint" solution" :20224.719 ,
"4th constraint" solution" : 6685.3933 }
etc ...
In this example, values are pretty close, but It's not always the case ...
I think It's called ' the left side' Thanks a lot, I can't find any doc about this, is there any code ready for doing this?
Solution 1:[1]
Welcome to the site.
I've augmented your example (below) to show how to access the constraint values and slack in pulp. Your example appears to be written in earlier version (pre 3.0) of python as evidenced by lack of parens in print statement. You should move to a more modern installation, if at all possible.
To find these functions in pulp I find that sometimes the dox are good, but often times, I just dive into an interactive environment (ipython) and use the auto-complete functionality to see what is offered and go exploring. :) You named your constraints so this below works well with the names. It should work fine without the names as well as you could just print the constraint value out of the dictionary, which is the expression.
Code
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}')
Yields:
...
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AirSquid |
