Optimization Using R: Concepts, Origins, Applications, and Case Studies
Source: Dev.to
Origins of Optimization
The field of mathematical optimization can be traced back to the 17th century when mathematicians such as Newton and Leibniz laid the foundations of calculus. Early optimization problems involved finding maxima or minima of functions, which later grew to include more complex, multi‑variable constraints.
By the early 20th century, optimization gained prominence through operations research, especially during World War II when countries needed to allocate limited military resources effectively. Linear programming was formalized by George Dantzig in 1947 with the introduction of the simplex method, revolutionizing the way industries approached resource allocation.
As computing power increased, optimization moved from theoretical mathematics to widespread practical use. With programming languages like R, optimization became accessible to analysts, data scientists, and researchers across domains.
What Is Optimization?
Optimization refers to the process of choosing the best possible solution from a set of available alternatives. It involves:
- Objective function – e.g., maximize profit, minimize cost
- Decision variables – inputs we manipulate
- Constraints – limitations such as resources or time
- Optimal solution – satisfies constraints and yields the best outcome
Optimization problems generally fall under two types:
- Unconstrained Optimization – decision variables can assume any value within their domain.
- Constrained Optimization / Linear Programming (LP) – limitations or restrictions are imposed on variables.
R supports both through functions like optim() and packages such as lpSolve and lpSolveAPI.
Unconstrained Optimization in R
Unconstrained optimization identifies the minima or maxima of functions without limitations. R provides the built‑in optim() function for solving such problems.
Example
# Define the objective function
f <- function(x) 4 * (x[1] - 1)^2 + 7 * (x[2] - 3)^2 + 30
# Starting point
c0 <- c(1, 1)
# Run optimization
result <- optim(c0, f)
# Display results
result$par # Optimal parameters
result$value # Minimum value of the function
result$convergence # 0 indicates successful optimization
The output reveals optimal parameters close to (1, 3), a minimum function value of 30, and a convergence status of 0 (successful).
Introduction to Linear Programming
Linear Programming (LP) focuses on optimizing a linear objective subject to linear constraints. It is widely used in resource allocation, production planning, transportation, energy management, and scheduling.
LP problems follow this structure:
- Objective function – maximize or minimize
- Decision variables – e.g.,
y1, y2, … - Constraints – linear inequalities or equalities
- Non‑negativity –
y1 ≥ 0, y2 ≥ 0
Packages like lpSolve make LP implementation in R simple and intuitive.
Real‑Life Applications of Optimization
1. Supply Chain & Logistics
- Warehouse location selection
- Delivery route planning
- Inventory management
- Transportation cost minimization
2. Manufacturing
- Production scheduling
- Waste reduction
- Machine utilization maximization
3. Finance
- Portfolio construction balancing return, risk, and market constraints (quadratic programming)
4. Agriculture
- Planting strategies considering land area, crop prices, resource constraints, and labor
5. Healthcare
- Nurse scheduling
- Patient wait‑time reduction
- Hospital bed allocation
Case Study 1: Product Mix Optimization
A company produces two products, A and B, sold at $25 and $20 respectively. Resources and time constraints are:
- Available resources: 1800 units
- Time available: 8 hours (480 minutes)
- Product A requires 20 resource units; B requires 12
- Both require 4 minutes of production time
Objective: Maximize revenue
[ \text{Revenue} = 25y_1 + 20y_2 ]
Constraints
[ \begin{aligned} 20y_1 + 12y_2 &\le 1800 \ 4y_1 + 4y_2 &\le 480 \ y_1, y_2 &\ge 0 \end{aligned} ]
Solution in R (lpSolve)
library(lpSolve)
objective.in <- c(25, 20) # Coefficients of the objective
const.mat <- matrix(c(20, 12, 4, 4), nrow = 2, byrow = TRUE)
rhs <- c(1800, 480)
dir <- c("<=", "<=")
optimum <- lp("max", objective.in, const.mat, dir, rhs)
# Results
optimum$solution # Production quantities (y1, y2)
optimum$objval # Maximum revenue
Optimal production
- Product A: 45 units
- Product B: 75 units
- Maximum revenue: $2,625
Case Study 2: Agricultural Optimization Problem
A farmer has 75 acres of land and wants to plant wheat (x) and barley (y). The LP model incorporates planting cost, profit per bushel, yield per acre, and storage constraints.
Objective: Maximize profit
[ \text{Profit} = 143x + 60y ]
Constraints
[ \begin{aligned} 120x + 210y &\le 15000 \quad \text{(planting cost)}\ 110x + 30y &\le 4000 \quad \text{(storage limit)}\ x + y &\le 75 \quad \text{(land limit)}\ x, y &\ge 0 \end{aligned} ]
Solution in R (lpSolveAPI)
library(lpSolveAPI)
lprec <- make.lp(0, 2) # 0 constraints, 2 decision variables
lp.control(lprec, sense = "max") # Maximization problem
set.objfn(lprec, c(143, 60)) # Objective coefficients
add.constraint(lprec, c(120, 210), "<=", 15000) # Planting cost
add.constraint(lprec, c(110, 30), "<=", 4000) # Storage limit
add.constraint(lprec, c(1, 1), "<=", 75) # Land limit
solve(lprec) # Solve the model
get.variables(lprec) # Optimal acres (x, y)
Optimal acres
- Wheat: 21.875 acres
- Barley: 53.125 acres
- Maximum profit: $6,315.63
Why Use R for Optimization?
- Built‑in functions (
optim()) and robust libraries (lpSolve, lpSolveAPI, ROI, nloptr, etc.) - Easy visualization of constraints and solutions (e.g.,
ggplot2) - Seamless integration with statistical modeling and data manipulation pipelines
- Open‑source, cross‑platform, and supported by a large community
Conclusion
Optimization plays a pivotal role in modern decision‑making across industries—from manufacturing and logistics to agriculture and finance. With R, implementing optimization models becomes more accessible and practical, thanks to its mathematical capabilities and intuitive packages. By understanding core concepts, exploring real‑life examples, and practicing with case studies, you can start solving your own optimization problems with confidence. Whether you’re maximizing profit, minimizing cost, or allocating constrained resources, optimization in R empowers you to make smarter and more efficient decisions.