optimize weekly schedule for students with constraints in R

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.")
}

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