# ETL
library(tidyverse)
library(lpSolve)47 Get Started with lpSolve
As usual, we start by uploading the libraries that we’re going to use.
48 Introduction
Let’s introduce the lpSolve package.
The lpSolve package in R is used for solving linear programming (LP) problems, which are optimization problems where the objective function and constraints are linear.
In supply chain management, linear programming can be applied to various problems to optimize operations, reduce costs, and improve efficiency. Here are some popular examples of applications using the lpSolve package in supply chain management:
Transportation Problem:
Objective: Minimize transportation costs.
Description: Determine the most cost-effective way to transport goods from multiple suppliers to multiple consumers, given supply and demand constraints.
Application: Use
lpSolveto set up a linear programming model where the objective function represents total transportation costs, and constraints ensure that supply meets demand.
Production Planning:
Objective: Minimize production costs while meeting demand.
Description: Decide on the quantity of different products to produce, considering constraints such as resource availability, production capacity, and demand forecasts.
Application: Formulate an linear programming (LP) model where the objective function minimizes costs (e.g., raw materials, labor), and constraints include production capacity and demand requirements.
Inventory Management:
Objective: Minimize holding and shortage costs.
Description: Optimize inventory levels to balance costs associated with holding inventory and potential stock outs.
Application: Use
lpSolveto model the trade-offs between holding costs and shortage costs, subject to constraints like storage capacity and service level requirements.
Supply Chain Network Design:
Objective: Minimize total cost of the supply chain network.
Description: Design a supply chain network that includes decisions about the location of warehouses and distribution centers, and the flow of goods through the network.
Application: Create an LP model to minimize costs associated with facility operations, transportation, and inventory, subject to constraints like facility capacity and geographic coverage.
Resource Allocation:
Objective: Maximize profit or minimize cost by allocating resources efficiently.
Description: Allocate limited resources (e.g., budget, manpower) across various activities or projects to achieve the best outcome.
Application: Formulate an LP model to optimize resource allocation, subject to constraints like budget limits and resource availability.
In this section we’re going to present a basic and gradual tutorial to illustrate how to use it.
49 Defining the Problem
To solve a linear programming problem, you need to define 3 elements :
the objective function.
the constraints.
and their constraint directions and right-hand side values.
49.1 Objective Function
It represents the coefficients of our objective function as a numeric vector.
# create objective function
objective.function.coefficients <- c(0.15, 0.40) # Example: Maximize 0.15x1 + 0.40x2
# display
objective.function.coefficients[1] 0.15 0.40
49.2 Constraint Matrix
We create a matrix representing the coefficients of our decision variables in the constraints. Each row corresponds to a constraint.
The 2 rows of our matrix are related to :
0.20x1 + 0.70x2 <= 100
x1 + x2 <= 200
# create constraint matrix
constraint.matrix <- matrix(c(0.20, 0.70, 1, 1),
ncol = 2,
byrow = TRUE)
# display
constraint.matrix [,1] [,2]
[1,] 0.2 0.7
[2,] 1.0 1.0
49.3 Constraint Directions
A character vector indicating the direction of each constraint (e.g., “<=”, “>=”, “=”).
Those constraints are related to each row of the constraint matrix.
# create constraint
constraint.directions <- c("<=", "<=")
# display
constraint.directions[1] "<=" "<="
49.4 Right-Hand Side (RHS) Values
A numeric vector containing the right-hand side values of the constraints.
Here :
0.20x1 + 0.70x2 must be inferior or equal to 100.
x1 + x2 must be inferior or equal to 200.
# create right-hand side
constraint.rhs <- c(100, 200)
# display
constraint.rhs[1] 100 200
50 Solving the Problem
Now, we use the lp() function to solve the linear programming problem.
We need to specify whether we are minimizing or maximizing.
The result is a list of 29 elements. We will especially look at 3 of them :
- Optimal Values of Decision Variables.
- Optimal objective value (based on the optimal values of the Decision Variables).
- Status of the solution.
# For maximization
result <- lp("max",
objective.function.coefficients,
constraint.matrix,
constraint.directions,
constraint.rhs)
# For minimization
# result <- lp("min",
# objective.function.coefficients,
# constraint.matrix,
# constraint.directions,
# constraint.rhs)
# display
resultSuccess: the objective function is 60
51 Interpreting the Results
The result object contains various information about the solution.
51.1 Optimal Values of Decision Variables
They are the optimum values of the variables x1 and x2 which meet the defined constraints.
Here we find : x1 = 80 and x2 =120.
result$solution[1] 80 120
51.2 Optimal Objective Value
It indicates the final score (maximum or minimum) of the solution that we found.
In our example, we wanted to maximize the function : 0.15x1 + 0.40x2
So, considering the optimal values x1 = 80 and x2 =120, we obtain 0.15*80 + 0.4*120 = 60
result$objval[1] 60
51.3 Status of the Solution
A value of 0 indicates an optimal solution.
result$status[1] 0
52 Full example
We have looked at the methodology through a detailed approach.
Now we can write a more compact code to solve our problem. This code contains 4 steps :
(define the) Problem we want to solve.
Create objective function | constraint matrix | constraint direction and RHS.
Solve the problem.
Print results.
#--------------------------
# Problem we want to solve
#--------------------------
# Maximize 0.15x1 + 0.40x2
# Subject to:
# 0.20x1 + 0.70x2 <= 100
# x1 + x2 <= 200
#--------------------------
# Create objective function | constraint matrix | constraint direction and RHS
#--------------------------
objective.function.coefficients <- c(0.15, 0.40)
constraint.matrix <- matrix(c(0.20, 0.70, 1, 1), ncol = 2, byrow = TRUE)
constraint.directions <- c("<=", "<=")
constraint.rhs <- c(100, 200)
#--------------------------
# Solve the problem
#--------------------------
result <- lp("max",
objective.function.coefficients,
constraint.matrix,
constraint.directions,
constraint.rhs)
#--------------------------
# Print results
#--------------------------
print(paste("Optimal objective value:", result$objval))[1] "Optimal objective value: 60"
print(paste("Optimal solution (x1, x2):", result$solution[1], result$solution[2]))[1] "Optimal solution (x1, x2): 80 120"
print(paste("Status of the solution:", result$status))[1] "Status of the solution: 0"