Search code examples
droolsoptaplanner

Optaplanner: Restricting Courses to certain rooms


I'm currently basing off the curriculum example of optaplanner and I'm trying to assign courses to certain rooms only. Like CS101 can only be assigned to Lab1 and not TheatreRoom1.

I'm current trying to restrict courses to be in rooms and don't know how to do this

Have any ideas on how to achieve this? My current rule is the following.

rule "courseAllowedInRoom"
    when
        $room: Room()
        Lecture($course:course)
        eval($course.cantBeInRoom($room))
    then
        scoreHolder.addHardConstraintMatch(kcontext, -1);
end

I've verified via unit tests that Course.cantbeInRoom seems to be outputting the correct boolean but i still get Lectures in rooms that should conflict.


Solution

  • Use the Penalty pattern

    class CourseRoomRestrictionPenalty {
      Course c;
      Room r;
    }
    
    rule "CourseRoomRestrictionPenalty"
    when
        CourseRoomRestrictionPenalty($c : course, $r : room)
        Lecture(course == $c, room != $r)
    then
        ...addHard...(..., -1);
    end