optaplannerconstraint-satisfactionresource-scheduling

In OptaPlanner, how does one restrict the number of times a fact is assigned to an entity?


Using course scheduling as an example, say an instructor can only teach n courses. To enforce this, my thought is to find all courses that are being taught by a given instructor, and increase badness by the negative difference, by half if below. How would I go about doing that (getting all courses that are taught by a given professor)?


Solution

  • If you introduce a List<Course> courseList bi-directional variable (section 6.2. Bi-directional Variable (Inverse Relation Shadow Variable) in the Instructor class, you'll have access to all his courses:

    @PlanningEntity
    public class Course {
        @PlanningVariable(valueRangeProviderRefs = {"instructorRange"})
        private Instructor instructor;
    }
    
    @PlanningEntity 
    public class Instructor {
        @InverseRelationShadowVariable(sourceVariableName = "instructor")
        private List<Course> courseList;
    }
    

    As for scoring, penalise hard score when the instructor has too many courses assigned and soft score when there aren't enough of them.