Issues with multi-objective optimization in R [closed]

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

New contributor

wruss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật