The Battle of Hoth
1. Library
library(lpSolve)
## Warning: package 'lpSolve' was built under R version 4.2.2
2. Linear Optimisation
2.1 Coefficients of Objective Function
<- c(75, 50, 25)
obj_coeff_hoth obj_coeff_hoth
## [1] 75 50 25
2.2 Constaints Coefficients
Set matrix corresponding to coefficients of constraints by rows.
<- matrix(c(6, 4, 4,
constraints_coeff_hoth 1, 1.5, 0,
4, 2, 1,
0, 0, 2,
-2, 1, 0),
nrow = 5, byrow = TRUE)
constraints_coeff_hoth
## [,1] [,2] [,3]
## [1,] 6 4.0 4
## [2,] 1 1.5 0
## [3,] 4 2.0 1
## [4,] 0 0.0 2
## [5,] -2 1.0 0
Set inequalities signs.
<- c("<=",
constraints_dir_hoth "<=",
"<=",
"<=",
"<=")
constraints_dir_hoth
## [1] "<=" "<=" "<=" "<=" "<="
Set right hand side constraints.
<- c(84,
constraints_rhs_hoth 21,
30,
10,
0)
constraints_rhs_hoth
## [1] 84 21 30 10 0
2.3 Linear Solution
<- lp(direction = "max",
optimum_hoth objective.in = obj_coeff_hoth,
const.mat = constraints_coeff_hoth,
const.dir = constraints_dir_hoth,
const.rhs = constraints_rhs_hoth,
compute.sens = TRUE)
optimum_hoth
## Success: the objective function is 671.875
The final values
$solution optimum_hoth
## [1] 3.125 6.250 5.000
$objval optimum_hoth
## [1] 671.875
2.4 Solution Evaluation
Reduced costs.
$sens.coef.from optimum_hoth
## [1] -100.000 37.500 21.875
$sens.coef.to optimum_hoth
## [1] 1.00e+02 6.25e+01 1.00e+30
<- c("At-At", "Snow Speeder", "Tauntaun")
product_hoth_name <- optimum_hoth$sens.coef.to
coeff_hoth_max <- optimum_hoth$sens.coef.from
coeff_hoth_min
<- obj_coeff_hoth
coeff_hoth
<- data.frame(product_hoth_name,
solution_hoth_reduced_cost
coeff_hoth,
coeff_hoth_max,
coeff_hoth_min) solution_hoth_reduced_cost
## product_hoth_name coeff_hoth coeff_hoth_max coeff_hoth_min
## 1 At-At 75 1.00e+02 -100.000
## 2 Snow Speeder 50 6.25e+01 37.500
## 3 Tauntaun 25 1.00e+30 21.875
Shadow prices.
$duals optimum_hoth
## [1] 0.0000 0.0000 21.8750 1.5625 6.2500 0.0000 0.0000 0.0000
<- c("Assembly", "Finishing", "Equipping",
constraint_hoth_name "Feeding", "Mix")
<- constraints_rhs_hoth
constraints_hoth <- optimum_hoth$duals[1:5]
shadow_price_hoth
<- data.frame(constraint_hoth_name,
solution_hoth_shadow_price
constraints_hoth,
shadow_price_hoth) solution_hoth_shadow_price
## constraint_hoth_name constraints_hoth shadow_price_hoth
## 1 Assembly 84 0.0000
## 2 Finishing 21 0.0000
## 3 Equipping 30 21.8750
## 4 Feeding 10 1.5625
## 5 Mix 0 6.2500
3. Integer Optimisation
3.1 Integer Solution
<- lp(direction = "max",
optimum_hoth_int objective.in = obj_coeff_hoth,
const.mat = constraints_coeff_hoth,
const.dir = constraints_dir_hoth,
const.rhs = constraints_rhs_hoth,
int.vec = 1:5,
compute.sens = TRUE)
optimum_hoth_int
## Success: the objective function is 650
The final values
$solution optimum_hoth_int
## [1] 4 5 4
$objval optimum_hoth_int
## [1] 650
3.2 Solution Evaluation
Reduced costs.
$sens.coef.from optimum_hoth_int
## [1] -100 0 0
$sens.coef.to optimum_hoth_int
## [1] 1e+30 1e+30 1e+30
<- c("At-At", "Snow Speeder", "Tauntaun")
product_hoth_int_name <- optimum_hoth_int$sens.coef.to
coeff_hoth_int_max <- optimum_hoth_int$sens.coef.from
coeff_hoth_int_min
<- obj_coeff_hoth
coeff_hoth_int
<- data.frame(product_hoth_int_name,
solution_hoth_int_reduced_cost
coeff_hoth_int,
coeff_hoth_int_max,
coeff_hoth_int_min) solution_hoth_int_reduced_cost
## product_hoth_int_name coeff_hoth_int coeff_hoth_int_max coeff_hoth_int_min
## 1 At-At 75 1e+30 -100
## 2 Snow Speeder 50 1e+30 0
## 3 Tauntaun 25 1e+30 0
Shadow prices.
$duals optimum_hoth_int
## [1] 0 0 25 0 0 -25 0 0
<- c("Assembly", "Finishing", "Equipping",
constraint_hoth_int_name "Feeding", "Mix")
<- constraints_rhs_hoth
constraints_hoth_int <- optimum_hoth_int$duals[1:5]
shadow_price_hoth_int
<- data.frame(constraint_hoth_name,
solution_hoth_int_shadow_price
constraints_hoth_int,
shadow_price_hoth_int) solution_hoth_int_shadow_price
## constraint_hoth_name constraints_hoth_int shadow_price_hoth_int
## 1 Assembly 84 0
## 2 Finishing 21 0
## 3 Equipping 30 25
## 4 Feeding 10 0
## 5 Mix 0 0
May the force be with you