Vectors
Matrices
Systems of Linear Equations
A retail company operates three warehouses that distribute products to three sales regions. Management wants to evaluate current inventory levels, shipping patterns, and determine whether existing inventory can satisfy customer demand.
You have been provided the following datasets:
inventory.csv
demand.csv
shipments.csv
inventory <- read.csv("C:/Users/rbron/OneDrive/A New R Class/605/inventory.csv")
demand <- read.csv("C:/Users/rbron/OneDrive/A New R Class/605/demand.csv")
shipments <- read.csv("C:/Users/rbron/OneDrive/A New R Class/605/shipments.csv")
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Create inventory matrix
inventory <- matrix(
c(1200, 900, 800,
1000, 1100, 700,
900, 700, 1000),
nrow = 3,
byrow = TRUE
)
rownames(inventory) <- c("Warehouse A", "Warehouse B", "Warehouse C")
colnames(inventory) <- c("Region1", "Region2", "Region3")
inventory
## Region1 Region2 Region3
## Warehouse A 1200 900 800
## Warehouse B 1000 1100 700
## Warehouse C 900 700 1000
# Calculate total inventory by region
total_inventory <- colSums(inventory)
total_inventory
## Region1 Region2 Region3
## 3100 2700 2500
Output:
Region1 Region2 Region3
3100 2700 2500
Region 1 has the most inventory with 3,100 units, followed by Region 2 with 2,700 units and Region 3 with 2,500 units.
# Create demand vector
demand <- c(2800, 2400, 2500)
names(demand) <- c("Region1", "Region2", "Region3")
# Calculate surplus or shortage
surplus_shortage <- total_inventory - demand
surplus_shortage
## Region1 Region2 Region3
## 300 300 0
Output:
Region1 Region2 Region3
300 300 0
Regions 1 and 2 each have a surplus of 300 units. Region 3 has neither a surplus nor a shortage because its inventory exactly matches current demand.
most_at_risk <- names(which.min(surplus_shortage))
most_at_risk
## [1] "Region3"
Output:
"Region3"
Region 3 is the most at risk of a future stock shortage because its available inventory exactly matches current demand, leaving no extra inventory as a buffer. Regions 1 and 2 each have a surplus of 300 units, giving them some protection if demand increases unexpectedly. From a supply chain perspective, management should pay particular attention to Region 3 because even a small increase in demand could result in a shortage. Management could also consider whether some of the excess inventory in Regions 1 and 2 could be strategically reallocated. However, moving inventory would also involve transportation and handling costs, so those costs should be considered before making a decision.
# Create shipment matrix
shipments <- matrix(
c(300, 400, 500,
400, 350, 250,
500, 300, 200),
nrow = 3,
byrow = TRUE
)
rownames(shipments) <- c("Warehouse A", "Warehouse B", "Warehouse C")
colnames(shipments) <- c("Region1", "Region2", "Region3")
shipments
## Region1 Region2 Region3
## Warehouse A 300 400 500
## Warehouse B 400 350 250
## Warehouse C 500 300 200
# Total shipments by warehouse
warehouse_totals <- rowSums(shipments)
warehouse_totals
## Warehouse A Warehouse B Warehouse C
## 1200 1000 1000
# Total shipments by region
region_totals <- colSums(shipments)
region_totals
## Region1 Region2 Region3
## 1200 1050 950
Warehouse totals:
Warehouse A Warehouse B Warehouse C
1200 1000 1000
Regional totals:
Region1 Region2 Region3
1200 1050 950
largest_shipper <- names(which.max(warehouse_totals))
largest_shipper
## [1] "Warehouse A"
Output:
"Warehouse A"
Warehouse A ships the most products, with 1,200 units.
region_most <- names(which.max(region_totals))
region_most
## [1] "Region1"
Output:
"Region1"
Region 1 receives the most products, with 1,200 units.
# Create grouped bar chart
par(mar = c(5, 5, 4, 10))
barplot(
shipments,
beside = TRUE,
main = "Shipments by Warehouse and Region",
xlab = "Region",
ylab = "Number of Products",
col = c("lightblue", "lightgreen", "lightpink"),
ylim = c(0, 550)
)
# Add legend outside the plotting area
legend(
"topright",
inset = c(-0.30, 0),
legend = rownames(shipments),
fill = c("lightblue", "lightgreen", "lightpink"),
bty = "n",
xpd = TRUE
)
Figure 1: Shipments by Warehouse and Region
The visualization shows the shipment volumes from each warehouse to each region. Warehouse A has the largest total shipment volume at 1,200 units. Region 1 receives the most products overall, with 1,200 units, while Region 3 receives the fewest at 950 units. Warehouse C sends the largest shipment to Region 1 at 500 units, while Warehouse A sends the largest shipment to Region 3 at 500 units. The shipment patterns show that warehouses contribute different amounts to each region and that Warehouse A plays an important role in the company’s overall distribution network.
The system of equations is:
x + y + z = 18
2x + y = 26
y + 2z = 22
The system can be written as:
\[Ax=b\]
where:
A =
[ 1 1 1 ]
[ 2 1 0 ]
[ 0 1 2 ]
x =
[ x ]
[ y ]
[ z ]
b =
[ 18 ]
[ 26 ]
[ 22 ]
In R:
# Coefficient matrix
A <- matrix(
c(1, 1, 1,
2, 1, 0,
0, 1, 2),
nrow = 3,
byrow = TRUE
)
# Variable vector
x <- c("x", "y", "z")
# Constants vector
b <- c(18, 26, 22)
A
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 2 1 0
## [3,] 0 1 2
x
## [1] "x" "y" "z"
b
## [1] 18 26 22
# Check the determinant of the coefficient matrix
det(A)
## [1] 0
# Attempt to solve the system
solution <- tryCatch(
solve(A, b),
error = function(e) {
paste("No solution:", e$message)
}
)
solution
## [1] "No solution: Lapack routine dgesv: system is exactly singular: U[3,3] = 0"
The system does not produce a valid solution because the equations are inconsistent. The determinant of the coefficient matrix is zero, indicating that the matrix is singular.
The second and third equations are:
2x + y = 26
y + 2z = 22
Adding these equations gives:
2x + 2y + 2z = 48
Dividing by 2 gives:
x + y + z = 24
However, the first equation states:
x + y + z = 18
Therefore, the equations contradict each other and the system has no solution as written.
Because there is no solution, the verification demonstrates the contradiction.
# Add equations 2 and 3
equation_sum <- c(2, 2, 2)
constant_sum <- 26 + 22
# Divide both sides by 2
implied_coefficients <- equation_sum / 2
implied_total <- constant_sum / 2
implied_coefficients
## [1] 1 1 1
implied_total
## [1] 24
Output:
[1] 1 1 1
[1] 24
This demonstrates:
x + y + z = 24
But the first equation requires:
x + y + z = 18
Therefore, the system has no solution as written.
The current resource allocation model cannot determine how many truckloads each warehouse should ship because the three equations are inconsistent. The second and third equations imply that the total number of truckloads must be 24, while the first equation states that only 18 truckloads are available. Management should therefore review the transportation capacity or replenishment requirements before using the model to make a shipping decision.
The inventory analysis provides additional information for planning. Warehouse A has the largest inventory with 2,900 units, followed by Warehouse B with 2,800 units and Warehouse C with 2,600 units. Warehouse A also ships the most products, with 1,200 units. At the regional level, Region 3 is the most vulnerable because its inventory exactly matches demand, leaving no surplus buffer. These results suggest that Warehouse A may have an important role in supporting future shipments, particularly if additional inventory is needed for regions at greater risk of shortages.
Although the current system cannot provide a valid allocation, systems of linear equations are useful in supply chain planning because they allow management to consider multiple constraints simultaneously. Once the equations are corrected, the model could help determine how available transportation capacity should be distributed among warehouses while meeting business requirements.