org.drools.planner.examples.curriculumcourse.solver.curriculumCourseScoreRules.drl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of drools-planner-examples Show documentation
Show all versions of drools-planner-examples Show documentation
Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule
engine powered score calculation. This is the drools-planner-examples module which contains examples on how to use
Drools Planner.
/*
* Copyright 2010 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.planner.examples.curriculumcourse.solver;
dialect "java"
import org.drools.planner.core.score.calculator.HardAndSoftConstraintScoreCalculator;
import org.drools.planner.core.score.constraint.IntConstraintOccurrence;
import org.drools.planner.core.score.constraint.ConstraintType;
import org.drools.planner.examples.curriculumcourse.domain.Course;
import org.drools.planner.examples.curriculumcourse.domain.Curriculum;
import org.drools.planner.examples.curriculumcourse.domain.CurriculumCourseSchedule;
import org.drools.planner.examples.curriculumcourse.domain.Day;
import org.drools.planner.examples.curriculumcourse.domain.Lecture;
import org.drools.planner.examples.curriculumcourse.domain.Period;
import org.drools.planner.examples.curriculumcourse.domain.Room;
import org.drools.planner.examples.curriculumcourse.domain.Teacher;
import org.drools.planner.examples.curriculumcourse.domain.Timeslot;
import org.drools.planner.examples.curriculumcourse.domain.UnavailablePeriodConstraint;
global HardAndSoftConstraintScoreCalculator scoreCalculator;
// ############################################################################
// Hard constraints
// ############################################################################
// Lectures: A missing or extra lecture of a course.
// This hard constraint is implemented a requirement by the initializer
// Conflicts: Two conflicting lectures in the same period.
// Three conflicting lectures count as 3 violations: one for each pair.
rule "conflictingLecturesInSamePeriod"
when
$leftCourse : Course($leftCourseId : id, $leftTeacher : teacher, $leftCurriculumList : curriculumList)
// Lectures of the same course can also be conflicting
$rightCourse : Course(id >= $leftCourseId, $rightTeacher : teacher, $rightCurriculumList : curriculumList)
eval($leftTeacher == $rightTeacher)
or exists Curriculum(this memberOf $leftCurriculumList, this memberOf $rightCurriculumList)
$leftLecture : Lecture(course == $leftCourse, $period : period)
$rightLecture : Lecture(course == $rightCourse, period == $period, this != $leftLecture)
then
insertLogical(new IntConstraintOccurrence("conflictingLecturesInSamePeriod",
ConstraintType.NEGATIVE_HARD, 1,
$leftLecture, $rightLecture));
end
// RoomOccupancy: Two lectures in the same room at the same period.
// Any extra lecture in the same period and room counts as one more violation.
rule "roomOccupancy"
when
$leftLecture : Lecture($leftId : id, $period : period, room != null, $room : room)
// $leftLecture has lowest id of the period+room combo
not Lecture(period == $period, room == $room, id < $leftId)
// rightLecture has the same period
$rightLecture : Lecture(period == $period, room == $room, id > $leftId, $rightId : id)
then
insertLogical(new IntConstraintOccurrence("roomOccupancy", ConstraintType.NEGATIVE_HARD,
1,
$leftLecture, $rightLecture));
end
// Availabilities: Each lecture in a period unavailable for that course.
rule "unavailablePeriodConstraint"
when
$unavailablePeriodConstraint : UnavailablePeriodConstraint($course : course, $period : period)
$lecture : Lecture(course == $course, period == $period)
then
insertLogical(new IntConstraintOccurrence("unavailablePeriodConstraint", ConstraintType.NEGATIVE_HARD,
1,
$lecture));
end
// ############################################################################
// Soft constraints
// ############################################################################
// RoomCapacity: For each lecture, the number of students that attend the course must be less or equal
// than the number of seats of all the rooms that host its lectures.
// Each student above the capacity counts as 1 point of penalty.
rule "roomCapacity"
when
$room : Room($capacity : capacity)
$lecture : Lecture(room == $room, studentSize > $capacity, $studentSize : studentSize)
then
insertLogical(new IntConstraintOccurrence("roomCapacity", ConstraintType.NEGATIVE_SOFT,
($studentSize - $capacity),
$room, $lecture));
end
// MinimumWorkingDays: The lectures of each course must be spread into a minimum number of days.
// Each day below the minimum counts as 5 points of penalty.
rule "minimumWorkingDays"
when
$course : Course($minWorkingDaySize : minWorkingDaySize)
$dayCount : Number(intValue < $minWorkingDaySize) from accumulate(
$day : Day()
and exists Lecture(course == $course, day == $day),
count($day)
)
// An uninitialized schedule should have no constraints broken
exists Lecture(course == $course)
then
insertLogical(new IntConstraintOccurrence("minimumWorkingDays", ConstraintType.NEGATIVE_SOFT,
(($minWorkingDaySize - $dayCount.intValue()) * 5),
$course));
end
// CurriculumCompactness: Lectures belonging to a curriculum should be adjacent
// to each other (i.e., in consecutive periods).
// For a given curriculum we account for a violation every time there is one lecture not adjacent
// to any other lecture within the same day.
// Each isolated lecture in a curriculum counts as 2 points of penalty.
rule "curriculumCompactness"
when
$curriculum : Curriculum()
$lecture : Lecture(curriculumList contains $curriculum,
$day : day, $timeslotIndex : timeslotIndex
)
not Lecture(curriculumList contains $curriculum,
day == $day, timeslotIndex == ($timeslotIndex - 1)
)
not Lecture(curriculumList contains $curriculum,
day == $day, timeslotIndex == ($timeslotIndex + 1)
)
then
insertLogical(new IntConstraintOccurrence("curriculumCompactness", ConstraintType.NEGATIVE_SOFT,
2,
$lecture, $curriculum));
end
// RoomStability: All lectures of a course should be given in the same room.
// Each distinct room used for the lectures of a course, but the first, counts as 1 point of penalty.
rule "roomStability"
when
$course : Course()
$roomCount : Number(intValue > 1) from accumulate(
$room : Room()
and exists Lecture(course == $course, room == $room),
count($room)
)
then
insertLogical(new IntConstraintOccurrence("roomStability", ConstraintType.NEGATIVE_SOFT,
($roomCount.intValue() - 1),
$course));
end
// ############################################################################
// Calculate score
// ############################################################################
// Accumulate hard constraints
rule "hardConstraintsBroken"
salience -1 // Do the other rules first (optional, for performance)
when
$hardTotal : Number() from accumulate(
IntConstraintOccurrence(constraintType == ConstraintType.NEGATIVE_HARD, $weight : weight),
sum($weight) // Vote for http://jira.jboss.com/jira/browse/JBRULES-1075
)
then
scoreCalculator.setHardConstraintsBroken($hardTotal.intValue());
end
// Accumulate soft constraints
rule "softConstraintsBroken"
salience -1 // Do the other rules first (optional, for performance)
when
$softTotal : Number() from accumulate(
IntConstraintOccurrence(constraintType == ConstraintType.NEGATIVE_SOFT, $weight : weight),
sum($weight) // Vote for http://jira.jboss.com/jira/browse/JBRULES-1075
)
then
scoreCalculator.setSoftConstraintsBroken($softTotal.intValue());
end