Interactive Sensitivity Analysis¶
Try me¶
Introduction¶
In this notebook, you can experiment with the graphic representation of a linear programming problem. The objective is to assess the impact that the changes in the problem coefficient have in the optimal solution.
Problem model¶
The problem model is taken from the Production Mix problem:
\(max Z = 300x_{1} + 250x_{2} + 0s_{1} + 0s_{2} + 0s_{3}\)
And the constraints as:
Changes in the objective function coefficient¶
The code cell below allows you to modify the coefficients of the objective function through sliders, to see how these changes modify the slope of the objective function. Depending on the changes, the optimal solution might be at another vertex, so the objective function slider allows to modify the objective variable z to find the optimal value in these cases. Remember, the optimal value will be always the highest value of z where the objective function intersects with the feasibility region.
[1]:
#Import the numpy and pyplot libraries, we set the aliases np and plt so that it is easier to use
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import interactive
#We set the mode inline of matplotlib to get the result at the output of the cell code
%matplotlib inline
#Construct lines, in our coordinate system x represents our decision variable x1 and y represents our decision variable x2
x=np.linspace(0,30,2000) #2000 numbers from 0 to 30
def show_solution(z=6350, c1=300, c2=250):
y1=40 - 2*x # Operator time constraint
y2=15-x/3 # Machining time constraint
plt.figure(1)
#1. Make plot
plt.plot(x, y1, label=r'$2x_{1} + x_{2} \leq 40$', c='orange', ls='dashed') #Plot y1
plt.plot(x, y2, label=r'$x_{1} + 3x_{2} \leq 45$', c='red') #Plot y2
plt.axvline(x=12, label=r'$x_{1} \leq 12$', c='green') #plot y3
#2. Adjust axis
plt.xlim((0, 30))
plt.ylim((0, 45))
plt.xlabel(r'$x_{1}$')
plt.ylabel(r'$x_{2}$')
#3. Fill feasible region
y4=np.minimum(y1, y2) #line representing the maximum between y3 and y2
plt.fill_between(x, y4, 0, where=x<12, color='grey', alpha=0.5) #fill where y5 ys greater than y6
#4 Annotate graph
plt.annotate('A', xy=(0, 0))
plt.annotate('B', xy=(0, 15))
plt.annotate('D',xy=(12, 0))
plt.annotate('E', xy=(12,11))
# 5. Plot objective function
obj_func= (z - c1*x)/c2 # we clear the x2 with the value of z
plt.plot(x, obj_func, label=r'$z = c_1*x_1 + c_2*x_2$', c='black')
# 6. Plot legend
plt.legend(bbox_to_anchor=(1.05,1), loc=2, borderaxespad=0.)
plt.show()
interactive_plot = interactive(show_solution, z=(3000,15000,10), c1=(10,500,10), c2=(100,1000,10))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot
Analysis questions¶
Keep the objective function coefficient of the second variable to 250 and decrease the coefficient of the first variable. What happens to the slope of the objective function? Adjust the value of z until you find again the optimal value of the objective function. What happens to the optimal value of the objective function? Will it be always at the same vertex?
Now, repeat the experiment in the first question and increase the coefficient of the first variable. What happens to the slope of the objective function? Adjust the value of z until you find again the optimal value of the objective function. What happens to the optimal value of the objective function? Do you think it will always be at the same vertex?
Repeat the experiment in the first two questions but now changing the coefficient of the second variable and write down your conclusions.
Changes in the constraint independent terms¶
The code cell below allows you to modify the independent terms of the constraints. The changes are going to change the feasibility region and again, if they are too extreme, the solution might change to another vertex.
[2]:
#Import the numpy and pyplot libraries, we set the aliases np and plt so that it is easier to use
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import interactive
#We set the mode inline of matplotlib to get the result at the output of the cell code
%matplotlib inline
#Construct lines, in our coordinate system x represents our decision variable x1 and y represents our decision variable x2
x=np.linspace(0,50,2000) #2000 numbers from 0 to 30
def show_solution(z=6350, b1=40, b2=45, b3=12):
y_min=x*0 # non negativity constraint
y_max = 250 + 0*x
y1 = b1 - 2*x # Operator time constraint
y2=b2/3-x/3 # Machining time constraint
plt.figure(1)
#1. Make plot
plt.plot(x, y1, label=r'$2x_{1} + x_{2} \leq b_1$', c='orange') #Plot y1
plt.plot(x, y2, label=r'$x_{1} + 3x_{2} \leq b_2$', c='red') #Plot y2
plt.axvline(x=b3, label=r'$x_{1} \leq b_3$', c='green') #plot y3
#2. Adjust axis
plt.xlim((0, 50))
plt.ylim((0, 45))
plt.xlabel(r'$x_{1}$')
plt.ylabel(r'$x_{2}$')
#3. Fill feasible region
y4=np.minimum(y1, y2) #line representing the maximum between y3 and y2
plt.fill_between(x, y4, 0, where=x<b3, color='grey', alpha=0.5) #fill where y5 ys greater than y6
# 5. Plot objective function
obj_func= (z - 300*x)/250 # we clear the x2 with the value of z
plt.plot(x, obj_func, label=r'$z = 300*x_1 + 250*x_2$', c='black')
# 6. Plot legend
plt.legend(bbox_to_anchor=(1.05,1), loc=2, borderaxespad=0.)
plt.show()
interactive_plot = interactive(show_solution, z=(3000,15000,10), b1=(10,100,1), b2=(10,100,1), b3=(0,20,0.1))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot
Analysis questions¶
Keep the Right Hand Side (RHS) coefficients of the second and third constraint unchanged and increase the RHS coefficient of the first constraint. What happens to the feasibility region? Now adjust z so that you find again the optimal value of the objective function. What happens to the optimal value of the objective function? Will it be always at the same vertex?
Keep the Right Hand Side (RHS) coefficients of the second and third constraint unchanged and decrease the RHS coefficient of the first constraint. What happens now to the feasibility region? And what happens to the optimal value of the objective function? Will it be always at the same vertex as before?
Now repeat the experiment in the first two questions but now changing the RHS coefficients of the second and third constraints and write down your conclusions.
Sensitivity analysis wrap-up¶
As you have seen, changing the coefficients of the objective function and the constraints can have a significant impact on the solution of the linear programming problem. In a nutshell, a sensitivity analysis explores the impact of changes of the coefficients of the objective function and the constraints on the solution of the linear programming problem. A solver providing a sensitivity analysis normally provides the following information:
Objective function coefficients: The sensitivity analysis provides the range of values for the coefficients of the objective function for which the current optimal solution remains optimal. Normally, a solver will provide the upper and lower bounds for the coefficients of the objective function. If the coefficients are within these bounds, the current optimal value of decision variables and constraint slack values will remain optimal. If the coefficients are outside these bounds, the current optimal solution will not be optimal anymore. In the graphic representation, this will mean that the optimal solution will no longer be at the same vertex.
Right Hand Side (RHS) coefficients: Similarly, a sensitivity analysis provides the range of values for the Right Hand Side (RHS) coefficients for which the current optimal solution remains optimal. Normally, a solver will provide the upper and lower bounds for the RHS coefficients. If the coefficients are within these bounds, the current optimal value of decision variables and constraint slack will value remain optimal. If the coefficients are outside these bounds, the current optimal solution will not be optimal anymore. Again, in the graphic representation, this will mean that the optimal solution will no longer be at the same vertex.
Sensitivity analysis is a powerful tool to understand how the changes in the coefficients of the objective function and the constraints affect the solution of the linear programming problem. It is important to understand that the sensitivity analysis is a local analysis, meaning that the conclusions are valid only for small changes in the coefficients, and unitary changes that we apply to one and only one coefficient at a time.
The following tables provide the results of a sensitivity analysis applied to the problem at hand in this interactive tutorial:
Decision variable |
value (primal problem solution) |
Reduced Costs (dual problem solution) |
Objective function coefficient |
Objective function lower bound |
Objective function upper bound |
|---|---|---|---|---|---|
x1 |
12 |
0 |
300 |
83.3 |
Infinity |
x2 |
11 |
0 |
250 |
0 |
900 |
Constraint |
Right Hand Side (RHS) |
Slack (primal problem solution) |
Shadow price (dual problem solution) |
RHS lower bound |
RHS upper bound |
|---|---|---|---|---|---|
1 |
40 |
5 |
0 |
35 |
Infinity |
2 |
45 |
0 |
83.3 |
12 |
60 |
3 |
12 |
0 |
216.67 |
0 |
15 |
Now, run the colab and try to set the coefficients of the objective function and the constraints to the values in the upper and lower bound values in the tables. You will see that the optimal solution remains the same while the coefficients are within the bounds. However, if you set the coefficients outside the bounds, the optimal solution will no longer be optimal.
If you set the first coefficient exactly to 83.3 you will notice that the objective function will be parallel to the constraint line. This means that any change below this point will shift the optimal towards the vertex to the right. A similar effect will happen if you set the second coefficient to 900.
On the other hand, if you shift the first constraint to 35, you will notice that now the three constraints intersect at the same point. If you make any changes to \(b_1\) below this point, the optimal solution will no longer be at the same vertex. The same thing happens if you try any RHS lower bound and upper bound in the table.
Do you want to learn more about sensitivity analysis? Check the following tutorial for a more detailed explanation of the algebraic concepts behind sensitivity analysis

