Problem set: I have 3 different groups that need to be assigned to 6 different locations.
Objective: I need to minimize the unused space in each location.
Constraints:
For each location j, the sum of individuals assigned to that location from all groups should be less than or equal to the location capacity of j. (e.g. Each location has a capacity which cannot be exceeded.)
The total number of Group A, Group B, and Group C assigned to each location should not exceed the corresponding total numbers available. (e.g. Each group has a size that cannot be exceeded.)
The groups cannot mix. For example, Group A cannot be in the same camp as Group B or Group C, Group B cannot be in the same location as Group A or Group C, and so on.
The issue I am having is with the third constraint.
Below is the R script that works for the first two constraints:
library(lpSolve)
# Define the data
n_groups <- 3
n_locations <- 6
n_group_a <- 130
n_group_b <- 40
n_group_c <- 120
l_capacities <- c(60, 50, 40, 30, 20, 10)
# Define the decision variables
vars <- matrix(0, nrow = n_groups * n_locations, ncol = 1)
for (i in 1:n_groups) {
for (j in 1:n_locations) {
vars[(i-1)*n_locations+j] <- 1
}
}
# Maximize the used spaces
obj <- rep(1, n_locations*n_groups)
# Define the constraints
rhs <- c(l_capacities, n_group_a, n_group_b, n_group_c)
dir <- c(rep("<=", n_locations + n_groups))
con <- matrix(0, nrow = n_locations + n_groups, ncol = n_groups * n_locations)
# Constraint 1: For each location j, the sum of individuals assigned to that location from all groups should be less than or equal to the location capacity of j.
for (j in 1:n_locations) {
for (i in 1:n_groups) {
con[j, (i-1)*n_locations+j] <- 1
}
}
# Constraint 2: The total number of Group A, Group B, and Group C assigned to each location should not exceed the corresponding total numbers available.
for (j in 1:n_groups) {
con[n_locations+j, seq((j-1)*n_locations+1, j*n_locations)] <- c(rep(1, n_locations))#, rep(0, 5 * n_groups))
# con[n_locations+j, seq((j-1)*n_locations+1, j*n_locations)] <- c(rep(1, n_locations))#, rep(0, 5 * n_groups))
# con[n_locations+n_groups+j, seq((j-1)*n_groups+1, j*n_groups)] <- c(rep(1, n_groups)) #c(rep(0, n_groups), rep(1, n_groups), rep(0, n_groups))
# con[n_locations+2*n_groups+j, seq((j-1)*n_groups+1, j*n_groups)] <- c(rep(0, 2 * n_groups), rep(1, n_groups))
}
# Solve the problem
result <- lp(direction = "max", objective.in = obj, const.mat = con, const.dir = dir, const.rhs = rhs)
# Print the optimal assignment
assignment <- matrix(result$solution[1:(n_groups*n_locations)], nrow = n_locations)
rownames(assignment) <- paste0("Location ", 1:n_locations)
colnames(assignment) <- c("Group A", "Group B", "Group C")
print(assignment)
I am having difficulty building the third constraint. Below is the code I have been playing with but would like some help adjusting (currently gives me an "subscript out of bounds" error):
# Constraint 3: Each individual can only be assigned to one location.
# for (i in 1:n_groups) {
# con[n_locations+2*n_groups+i, seq((i-1)*n_locations+1, i*n_locations)] <- rep(1, n_locations)
# }
Any help appreciated. Thanks!