So for a project I am working on, I have a list of parts and their dimensions (height, width, and depth) and am trying to minimize the number of unique bags we need to purchase, but there also can’t be too much excess on the bags over the part dimensions (too much waste and the excess baggage may cause the part to no longer fit in the packaging). I have a lot of experience with using Excel’s solver for these types of problems, but there are too many variables and constraints for solver on this problem, so I am trying to use R for optimization for the first time. As far as I know, I am the only one in my company with any knowledge of R so I can’t go to anyone in-house for help.
The following is what I have come up with (with the help of ChatGPT)
# Load necessary libraries
library(ROI)
library(ROI.plugin.glpk)
# Read part dimensions from a CSV file (with named columns 'length', 'width', and 'depth')
part_dimensions <- read.csv(file.choose())
# Check that the data is loaded correctly
print(part_dimensions)
# Number of parts
n_parts <- nrow(part_dimensions)
# Define excess limits for width and depth
min_excess_width <- 2
max_excess_width <- 4
min_excess_depth <- 2 # Add minimum excess depth
max_excess_depth <- 4 # Add maximum excess depth
# Initialize a list to store unique bag sizes
bag_sizes <- list()
# Generate potential bag dimensions based on the constraints for part dimensions
for (i in 1:n_parts) {
part_length <- part_dimensions[i, 'length']
part_width <- part_dimensions[i, 'width']
part_depth <- part_dimensions[i, 'depth']
# Calculate min and max bag lengths, widths, and depths
bag_length_min <- part_length + part_depth + 2
bag_length_max <- part_length + part_depth + 6
bag_width_min <- part_width + min_excess_width
bag_width_max <- part_width + max_excess_width
bag_depth_min <- part_depth + min_excess_depth # Set minimum bag depth
bag_depth_max <- part_depth + max_excess_depth # Set maximum bag depth
# Store the bag sizes in the list
for (l in bag_length_min:bag_length_max) {
for (w in bag_width_min:bag_width_max) {
for (d in bag_depth_min:bag_depth_max) { # Loop through bag depths
bag_sizes[[length(bag_sizes) + 1]] <- c(l, w, d) # Store length, width, depth
}
}
}
}
# Convert bag sizes list to a matrix
bag_sizes <- do.call(rbind, bag_sizes)
n_bags <- nrow(bag_sizes) # Update the number of bags
# Initialize constraints
constraints <- matrix(0, n_parts, n_bags) # Initialize the constraints matrix
# Add constraints for bag dimensions based on part dimensions
for (i in 1:n_parts) {
part_length <- part_dimensions[i, 'length']
part_width <- part_dimensions[i, 'width']
part_depth <- part_dimensions[i, 'depth']
# Define the minimum and maximum bag dimensions
bag_length_min <- part_length + part_depth + 2
bag_length_max <- part_length + part_depth + 6
bag_width_min <- part_width + min_excess_width
bag_width_max <- part_width + max_excess_width
bag_depth_min <- part_depth + min_excess_depth
bag_depth_max <- part_depth + max_excess_depth
for (j in 1:n_bags) {
# Check constraints for bag dimensions
if ((bag_length_min <= bag_sizes[j, 1]) &&
(bag_sizes[j, 1] <= bag_length_max) &&
(bag_width_min <= bag_sizes[j, 2]) &&
(bag_sizes[j, 2] <= bag_width_max) &&
(bag_depth_min <= bag_sizes[j, 3]) && # Check depth
(bag_sizes[j, 3] <= bag_depth_max)) {
constraints[i, j] <- 1 # Assign if the bag can fit the part
}
}
}
# Create the objective to minimize the number of unique bags used
objective <- rep(1, n_bags)
# Create the constraint matrix: Each part must be assigned to exactly one bag
rhs <- rep(1, n_parts) # Each part must be assigned to one bag
dir <- rep("==", n_parts)
# Ensure dimensions are correct for the optimization model
if (length(objective) != n_bags) {
stop("Length of objective does not match number of bags")
}
# Debugging: Print dimensions
cat("Number of bags:", n_bags, "n")
cat("Constraints dimensions: ", dim(constraints), "n")
cat("Objective length:", length(objective), "n")
cat("RHS length:", length(rhs), "n")
cat("Direction length:", length(dir), "n")
# Build the optimization model
opt_model <- OP(
objective = L_objective(objective),
constraints = L_constraint(constraints, dir, rhs),
bounds = V_bound(li = rep(0, n_bags), # Lower bounds for bags (0)
ui = rep(1, n_bags)), # Upper bounds for bags (1 for binary)
types = rep("B", n_bags) # Binary decision variables for bags
)
# Solve the optimization problem
result <- ROI_solve(opt_model)
# Display the results
if (result$status == "optimal") {
cat("Optimal solution found:n")
print(result$solution)
} else {
cat("No optimal solution found.n")
}
I am getting the following error
Error in V_bound(li = rep(0, n_bags), ui = rep(1, n_bags)) :
length of indices must be equal to the length of the corresponding values.
Also, I currently have this set up as a single objective optimization as the constraints are so tight, but I may loosen them in the future requiring a multi-objective approach.
I am so unfamiliar with optimization in R that I don’t even really know where to begin in trying to figure this out. So if anyone has any suggestions, or good resources to refer to that would be very helpful.
TIA
wruss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.