'Optaplanner school timetable multiple student groups at the same room at same time

I am trying to figure out how to allow multiple student groups(some configured number) to attend lesson on the same subject in the same room, at the same time.

I think I should use group by, but can't understand how to count those and penalize if greater than defined number.



Solution 1:[1]

return forEach(Lesson.class)
.groupBy(Lesson::getTimeslot, Lesson::getRoom, sum(Lesson::getStudentCount)
.filter((timeslot, room, studentTotal) -> studentTotal > room.capacity)
.penalize("room capacity", ONE_HARD,
        (timeslot, room, studentTotal) -> studentTotal - room.capacity);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Geoffrey De Smet