In this lab we will focus on sensitivity analysis.
Sensitivity analysis is the study of how the uncertainty in the
output of a mathematical model or system (numerical or otherwise) can be
apportioned to different sources of uncertainty in its inputs. We will
use the lpSolveAPI R-package as we did in the previous
lab.
In this lab, we will learn how to run a sensitivity analysis on the marketing use case covered so far.
Remember to always set your working directory to the source file location. Go to ‘Session’, scroll down to ‘Set Working Directory’, and click ‘To Source File Location’. Read carefully the below and follow the instructions to complete the tasks and answer any questions. Submit your work to RPubs as detailed in previous notes.
For your assignment you may be using different data sets than what is included here. Always read carefully the instructions on Sakai. Tasks/questions to be completed/answered are highlighted in larger bolded fonts and numbered according to their particular placement in the task section.
In order to conduct the sensitivity analysis, we will need to
download again the lpSolveAPI package unless you have it
already installed in your R environment
# Require will load the package only if not installed
# Dependencies = TRUE makes sure that dependencies are install
if(!require("lpSolveAPI",quietly = TRUE))
install.packages("lpSolveAPI",dependencies = TRUE, repos = "https://cloud.r-project.org")
library("lpSolveAPI")
We will revisit and solve again the marketing case discussed in class (also part of previous lab).
# We start with `0` constraint and `2` decision variables. The object name `lpmark` is discretionary.
lpmark <- make.lp(0,2)
# Define type of optimization as maximum and dump the screen output into a `dummy` variable
dump = lp.control(lpmark, sense="max")
# Set the objective function coefficients
set.objfn(lpmark, c(275.691,48.341))
Add all constraints to the model.
add.constraint(lpmark, c(1,1),"<=", 350000)
add.constraint(lpmark, c(1,0),">=", 15000)
add.constraint(lpmark, c(0,1),">=", 15000)
add.constraint(lpmark, c(2,-1),"<=", 0)
add.constraint(lpmark, c(1,0),">=", 0)
add.constraint(lpmark, c(0,1),">=", 0)
Now, view the problem setting in tabular/matrix form. This is a good checkpoint to confirm that our contraints have been properly set.
lpmark
## Model name:
## C1 C2
## Maximize 275.691 48.341
## R1 1 1 <= 350000
## R2 1 0 >= 15000
## R3 0 1 >= 15000
## R4 2 -1 <= 0
## R5 1 0 >= 0
## R6 0 1 >= 0
## Kind Std Std
## Type Real Real
## Upper Inf Inf
## Lower 0 0
# solve
solve(lpmark)
## [1] 0
Next we get the optimum results.
# display the objective function optimum value
get.objective(lpmark)
## [1] 43443517
# display the decision variables optimum values
get.variables(lpmark)
## [1] 116666.7 233333.3
For the sensitivity part we will add two new code sections to obtain the sensitivity results. ….
# display sensitivity to coefficients of objective function.
get.sensitivity.obj(lpmark)
## $objfrom
## [1] 48.3410 -137.8455
##
## $objtill
## [1] 1.00000e+30 2.75691e+02
objfrom. Explain in coincise manner what
the sensitivity results represent in reference to the marketing
model.The sensitivity results represent the number amount that can be changed (maximum of 48.3410 and minimum of -137.8455) within the coefficients of the radio and TV When the changes occur, the optimum values for sales will be able to remain constant.
# display sensitivity to right hand side constraints.
# There will be a total of m+n values where m is the number of contraints and n is the number of decision variables
get.sensitivity.rhs(lpmark)
## $duals
## [1] 124.12433 0.00000 0.00000 75.78333 0.00000 0.00000 0.00000
## [8] 0.00000
##
## $dualsfrom
## [1] 4.50e+04 -1.00e+30 -1.00e+30 -3.05e+05 -1.00e+30 -1.00e+30 -1.00e+30
## [8] -1.00e+30
##
## $dualstill
## [1] 1.00e+30 1.00e+30 1.00e+30 6.55e+05 1.00e+30 1.00e+30 1.00e+30 1.00e+30
duals. Explain in coincise manner what
the two non-zero sensitivity results represent. Distinguish the
binding/non-binding constraints, the surplus/slack, and marginal
values.