Although R is not the best tool to solve LP problems, for the ones who already know and use R, I solve one the problems that I solved in my IE 202 Introduction to Modeling and Optimization class by using R's lpSolve package.
You can see the markdown file in RPubs - Document
Willy Wonka’s Candy Company
QUESTION
. Willy Wonka’s Candy Company produces three types of candy: Wonka Bars, Bottle Caps and Giant Sweet Tarts. In order to produce the different type of candies, Willy can run three different production processes as described below. Each process involves blending different types of sugars in the Magical Factories Mixer.
• Process 1: Running Process 1 for one hour:
Costs: $5
Requires: Two barrels of sugar type A and three barrels of sugar type B
Output: Two Wonka Bars and one packet of Bottle Caps.
• Process 2: Running Process 2 for one hour:
Costs: $4
Requires: One barrel of sugar type A and three barrels of sugar type B
Output: Three packets of Bottle Caps.
• Process 3: Running Process 3 for one hour:
Costs: $1
Requires: Two barrels of sugar type B and three packets of Bottle Caps
Output: Two Wonka Bars and one packet of Giant Sweet tarts.
Each week we can purchase 200 barrels of sugar type A at $2 per barrel, and 300 barrels of sugar type B at $3 per barrel. Assume that they can sell everything that they can produce. Wonka Bars are sold at $9 per bar, Bottle Caps are sold at $10 per packet, and Giant Sweet Tarts are sold at $24 per packet. Assume that 100 hours of mixing are available.
install.packages("lpSolve")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
library(lpSolve)
Aim is maximazing profit
Decision Variable x_i= number of hours process i runs, i = 1, 2, 3. Model
max 9(2x1 + 2x3)+ 10(x1 + 3x2 − 3x3) + 24x3− (5x1 + 4x2 + x3)− 2(2x1 + x2)− 3(3x1 + 3x2 + 2x3)
s.t. 2x1 + x2 ≤ 200 (Sugar type A)
3x1 + 3x2 + 2x3 ≤ 300 (Sugar type B)
x1 + x2 + x3 ≤ 100 (Time limitation)
x1 + 3x2 ≥ 3x3 (Production of BC is larger than its consumption) x1 ≥ 0 x2 ≥ 0 x3 ≥ 0
Constraints LHS coefficients can be represented with a matrix as followed
constraints.LHS<- matrix(c(2,1,0,
3,3,2,
1,1,1,
1,3,-3), nrow=4, ncol = 3, byrow=TRUE)
RHS of constraints
RHS <- c(200, 300,100, 0)
Directions of the constraints, except nonnegativity of the variables
c_direction<- c("<=","<=","<=", ">=")
Objective function coefficients
objective.fxn<- c(10,15,5)
Optimal Solution
optimal<- lp(direction = "max",
objective.in = objective.fxn,
const.mat = constraints.LHS,
const.dir = c_direction,
const.rhs = RHS,
all.int = F,
compute.sens = TRUE)
optimal
## Success: the objective function is 1500
optimal[["objval"]]
## [1] 1500
Comments