I have to plan a week of classes with students. I have six students that have eight different classes at different times. Not all students have to attend all classes and some classes can overlap in time, as long as no student should be in both classes. I have to find the combinations of schedules that fulfill certain specific constraints. For example students a and b must share at least two afternoons in Class 1, Class 2 is only visited by student c and can only take part on two afternoons, and Class 3 is only offered on a specific day but all students must attend. How can I find combinations of classes for all six students that adhere to the constrains using R?
What I have found so far:
- This paper, of which I understand almost nothing
- the
ompr
package, but since I have zero background in scheduling algorithms, I don’t really know how to start or how to translate the words they use to my scenario - ChatGPT gives me code that kind of looks nice but that invents functions and thus does not run. I add it below, for inspiration.
- this question which I don’t really understand and which remains open.
I think I would already benefit from some explication as to how my scenario relates to machines, jobs, items and batches, to the Knapsack-problem.
Any help is greatly appreciated!
# non-working code from ChatGPT
library(ompr)
library(ROI)
library(lpSolve)
# Define students, classes, and time slots
students <- c("A", "B", "C", "D", "E", "F")
classes <- c("Class1", "Class2", "Class3", "Class4", "Class5", "Class6", "Class7", "Class8")
time_slots <- c("Monday_Morning", "Monday_Afternoon", "Monday_Evening",
"Tuesday_Morning", "Tuesday_Afternoon", "Tuesday_Evening",
"Wednesday_Morning", "Wednesday_Afternoon", "Wednesday_Evening",
"Thursday_Morning", "Thursday_Afternoon", "Thursday_Evening",
"Friday_Morning", "Friday_Afternoon", "Friday_Evening")
# Define decision variables
decision_variables <- array(
MIPVariable( # !!this function is not in the namespace of any package that is loaded!!
i = students,
j = classes,
k = time_slots,
type = "binary"
),
dim = c(length(students), length(classes), length(time_slots))
)
# Define constraints
constraints <- list()
# Constraint: Each student attends at most one class per time slot
for (i in students) {
for (k in time_slots) {
constraints[[paste0("OneClassPerTimeSlot_", i, "_", k)]] <-
sum(decision_variables[i, , k]) <= 1
}
}
# Constraint: Student A and B must share at least two afternoons in Class 1
constraints[["SharedAfternoons_A_B_Class1"]] <-
sum(decision_variables[c("A", "B"), "Class1", c("Monday_Afternoon", "Tuesday_Afternoon", "Wednesday_Afternoon", "Thursday_Afternoon", "Friday_Afternoon")]) >= 2
# Constraint: Class 2 is only visited by student C and can only take part on two afternoons
constraints[["Class2_OnlyStudentC_Limit"]] <-
sum(decision_variables["C", "Class2", c("Monday_Afternoon", "Tuesday_Afternoon", "Wednesday_Afternoon", "Thursday_Afternoon", "Friday_Afternoon")]) <= 2
# Constraint: Class 3 is only offered on Wednesday morning and all students must attend
constraints[["Class3_OfferedWednesdayMorning"]] <-
sum(decision_variables[, "Class3", "Wednesday_Morning"]) == length(students)
# Define the model
model <- MIPModel() %>%
add_variables(decision_variables) %>%
add_constraints(constraints)
# Solve the model
solution <- solve_model(model, with_ROI(solver = "lp_solve"))
# Print the solution
if (solution$status == "optimal") {
print(solution$objective_value)
print(solution$variable_value)
} else {
print("No feasible solution found.")
}