Find the maximum solution to
\(Z=4x+3y\)
Suppose that the objective function is subject to the following constraints:
\(x≥0\) \(y≥2\) \(2y≤25−x\) \(4y≤2x−8\) \(y≤2x−5\)
## Warning: package 'lpSolve' was built under R version 3.6.2
# Set coefficients of the objective function
f.obj <- c(4, 3)
# Create constraint matrix
f.con = matrix(c(1,2,
-2,4,
-2,1), nrow=3, byrow = TRUE)
# Set unequality signs
f.dir <- c("<=",
">=",
">=")
# Set right hand side coefficients
f.rhs <- c(25,
-8,
-5)
# Final value (z)
lp("max", f.obj, f.con, f.dir, f.rhs)## Success: the objective function is 55
## [1] 7 9
## [1] 1.5 -2.0
## [1] 1e+30 8e+00
# Dual Values (first dual of the constraints and then dual of the variables)
# Duals of the constraints and variables are mixed
lp("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)$duals## [1] 2 0 -1 0 0
## [1] 2.500e+00 -1.000e+30 -2.375e+01 -1.000e+30 -1.000e+30
## [1] 1.00e+30 1.00e+30 1.25e+01 1.00e+30 1.00e+30
Solution: The maximum z value (and thus, the optimum) that can be obtained while satisfying the given constraints is 55, where x = 7 and y = 9. The sensitivity coefficients go from 1.5 and -2.0 to 1e+30 and 8e+00. The shadow/dual prices of the constraints are 2, 0 and -1, while for the decision variables are 0 and 0, respectively. The shadow/dual prices lower limits of the constraints are 2.500e+00, -1.000e+30 and -2.375e+01, while for the decision variables are -1.000e+30 and -1.000e+30, respectively. Finally, the shadow/dual prices upper limits of the constraints are 1.00e+30, 1.00e+30 and 1.25e+01, while for the decision variables are 1.00e+30 and 1.00e+30, respectively.
Let say you are working as consultant for a boutique car manufacturer, producing luxury cars. They run on one-month (30 days) cycles, we have one cycle to show we can provide value. There is one robot, 2 engineers and one detailer in the factory. The detailer has some holiday off, so only has 21 days available.The 2 cars need different time with each resource:
\(CarA=x\) \(CarB=y\) \(Robot=3x+4y≥30\) \(Engineer=5x+6y≥60\) \(Detailer=1.5x+3y≥21\)
Car A provides $ 30,000 profit, whilst Car B offers $45,000 profit. At the moment, they produce 4 of each car per month, for $300,000 profit. Not bad at all, but we think we can do better for them.
# Set the coefficients of the objective function
obj.func <- c(30000, 45000)
# Create constraint matrix
constr <- matrix(c(3,4,
5,6,
1.5,3),
nrow = 3,
byrow = TRUE)
# Create right hand side or the result for the constraints
rhs <- c(30, 60, 21)
# Create the direction of the constraints
direc <- c("<=","<=","<=")
# Find the optimum solution
optimum <- lp(direction="max",
objective.in = obj.func,
const.mat = constr,
const.dir = direc,
const.rhs = rhs,
all.int = T)
str(optimum)## List of 28
## $ direction : int 1
## $ x.count : int 2
## $ objective : num [1:2] 30000 45000
## $ const.count : int 3
## $ constraints : num [1:4, 1:3] 3 4 1 30 5 6 1 60 1.5 3 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:4] "" "" "const.dir.num" "const.rhs"
## .. ..$ : NULL
## $ int.count : int 2
## $ int.vec : int [1:2] 1 2
## $ bin.count : int 0
## $ binary.vec : int 0
## $ num.bin.solns : int 1
## $ objval : num 330000
## $ solution : num [1:2] 2 6
## $ presolve : int 0
## $ compute.sens : int 0
## $ sens.coef.from : num 0
## $ sens.coef.to : num 0
## $ duals : num 0
## $ duals.from : num 0
## $ duals.to : num 0
## $ scale : int 196
## $ use.dense : int 0
## $ dense.col : int 0
## $ dense.val : num 0
## $ dense.const.nrow: int 0
## $ dense.ctr : num 0
## $ use.rw : int 0
## $ tmp : chr "Nobody will ever look at this"
## $ status : int 0
## - attr(*, "class")= chr "lp"
## [1] 0
# Display the optimum values for W and B
solution <- optimum$solution
names(solution) <- c("x","y")
print(solution)## x y
## 2 6
# Check the value of objective function at optimum point
print(paste("Total Profit: ",
optimum$objval,
sep = ""))## [1] "Total Profit: 330000"
Solution : The total production of Car A = 2 and Car B = 6 to get maximum profit 330.000
Let say you would like to make some sausages and you have the following ingredients available:
Assume that you will make 2 types of sausage:
Economy (>40% Chicken) Premium (>60% Chicken) *One sausage is 50 grams (0.05 kg)
According to government regulations of Indonesia: The most starch you can use in your sausages is 25%. You have a contract with a butcher, and have already purchased 23 kg Chicken, that must go in your sausages. *You have a demand for 350 economy sausages and 500 premium sausages.
So, please figure out how to optimize the cost effectively to blend your sausages.
Please visualize a contour plot of the following function:
\(f(x,y)=x^2+y^2+3xy\)
How coordinate descent works How Steepest descent works How Newton Method works How Conjugate Gradient works
f <- function(x, y) {x^2 + y^2 + 3*x*y}
n <- 30
xpts <- seq(-1.5, 1.5, len = n)
ypts <- seq(-1.5, 1.5, len = n)
gr <- expand.grid(x = xpts, y = ypts)
feval <- with(gr, matrix(f(x, y), nrow = n, ncol = n))
par(mar = c(5, 4, 1, 1))
contour(xpts, ypts, feval, nlevels = 20, xlab = "x", ylab = "y")
points(-1, -1, pch = 19, cex = 2)
abline(h = -1)## $minimum
## [1] 1.499924
##
## $objective
## [1] -1.25
the minimum value is obtain 1.499924 and value of objective -1.25