Here we use R’s lpSolve package to solve the optimization problem presented by John Foreman in Chapter 4 of his book, Data Smart [http://www.wiley.com/WileyCDA/WileyTitle/productCd-111866146X.html]. As per the teaching objectives of the book, the author develops a solution using Excel only and, in this case, its add-in optimization tool, Solver.
The problem posed is to optimize the allocation of orange juice orders among a pool of suppliers worldwide, to keep costs to a minimum, yet still meeting taste, color, acidity, supply and contractual constraints.
There are 11 potential suppliers for each of the three months (Jan - March) for which orders are placed. Our objective function has 33 variables, each variable representing an order quantity placed with each vendor across each of the three months. We identify and express 41 constraints for the problem.
The solution to the objective function is found to be M$1.23 and is in agreement with that returned by Excel and Solver.
library(lpSolve)
# Read source data
objF <- read.csv("objF.csv") # contains the objective function
objF <- objF[["objF"]] # converts objF to a vector
supply <- read.csv("OJ.csv")
# Create constraint matrix
constraintMat <- as.matrix(supply[1:41,1:33]) # 41 rows, 33 variables
# Create constraintDir
constraintDir <- as.vector(supply[["Eqn"]]) # separates out the constraint function e.g. ">=", "<=", ...
# Create constraintRHS
constraintRHS <- as.vector(supply[["RHS"]]) # separates out the constraint values
# Run the linear programming solver.
optBlend <- lp(direction = 'min', objective.in = objF, const.mat = constraintMat,
const.dir = constraintDir, const.rhs = constraintRHS)
optBlend # returns the minimum of the objective function
## Success: the objective function is 1227560
optBlend$solution # returns the order quantities placed, 1000 gals/month, with each supplier
## [1] 0.000000 13.500000 240.000000 0.000000 0.000000 60.899123
## [7] 0.000000 0.000000 35.026316 174.561404 76.013158 0.000000
## [13] 0.000000 240.000000 0.000000 75.510995 12.434211 0.000000
## [19] 114.246575 132.328767 0.000000 25.479452 0.000000 0.000000
## [25] 280.000000 111.166667 8.489005 0.000000 0.000000 53.753425
## [31] 132.644917 35.438596 78.507390
It is convenient to have the ability to run linear programming optimizations in R. lpSolve does not specify a limit to the number of variables which can be presented to the solver, rather memory is cited as the limiting factor. By contrast, Excel’s Solver is reportedly limited to 200 or fewer variables for most installations.
Other tools are available and may be better suited for heavy duty optimization problems with high variable counts. I have used FICO XPRESS Optimization Suite [http://www.fico.com/en/products/fico-xpress-optimization-suite#overview] in the past and found it to be excellent - powerful and easy to use. The author highlighted two good commercial solutions based on his experience. One is the cloud-based Gurobi [http://http://www.gurobi.com/] and the other is the enterprise-focused CPLEX [http://www-01.ibm.com/software/commerce/optimization/cplex-optimizer/], from IBM.
Happy optimizing!