org.optaplanner.examples.examination.solver.examinationConstraints.drl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optaplanner-examples Show documentation
Show all versions of optaplanner-examples Show documentation
OptaPlanner solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains the examples which demonstrate how to use it in a normal Java application.
/*
* Copyright 2010 Red Hat, Inc. and/or its affiliates.
*
* 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.optaplanner.examples.examination.solver;
dialect "java"
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScoreHolder;
import org.optaplanner.examples.examination.domain.Exam;
import org.optaplanner.examples.examination.domain.Examination;
import org.optaplanner.examples.examination.domain.ExaminationConstraintConfiguration;
import org.optaplanner.examples.examination.domain.Period;
import org.optaplanner.examples.examination.domain.PeriodPenalty;
import org.optaplanner.examples.examination.domain.PeriodPenaltyType;
import org.optaplanner.examples.examination.domain.Room;
import org.optaplanner.examples.examination.domain.RoomPenalty;
import org.optaplanner.examples.examination.domain.RoomPenaltyType;
import org.optaplanner.examples.examination.domain.Student;
import org.optaplanner.examples.examination.domain.Topic;
import org.optaplanner.examples.examination.domain.solver.TopicConflict;
global HardSoftScoreHolder scoreHolder;
// ############################################################################
// Hard constraints
// ############################################################################
// Two exams in the same period which share students.
rule "conflictingExamsInSamePeriod"
when
$topicConflict : TopicConflict($leftTopic : leftTopic, $rightTopic : rightTopic)
$leftExam : Exam(topic == $leftTopic, $period : period, period != null)
$rightExam : Exam(topic == $rightTopic, period == $period)
then
scoreHolder.penalize(kcontext, $topicConflict.getStudentSize());
end
// More time required during a period than available in that period.
rule "periodDurationTooShort"
when
$exam : Exam(topicDuration > periodDuration, period != null)
then
scoreHolder.penalize(kcontext, $exam.getTopicStudentSize());
end
// More seating required during a period in a room than available in that room.
rule "roomCapacityTooSmall"
when
$period : Period()
$room : Room($capacity : capacity)
accumulate(
Exam(period == $period, room == $room, $studentSize : topicStudentSize);
$totalStudentSize : sum($studentSize);
$totalStudentSize > $capacity
)
then
scoreHolder.penalize(kcontext, $capacity - $totalStudentSize);
end
// Period hard constraints
rule "periodPenaltyExamCoincidence"
when
$periodPenalty : PeriodPenalty(
periodPenaltyType == PeriodPenaltyType.EXAM_COINCIDENCE,
$leftTopic : leftTopic,
$rightTopic : rightTopic
)
$leftExam : Exam(topic == $leftTopic, $leftPeriod : period, period != null)
$rightExam : Exam(topic == $rightTopic, period != $leftPeriod, period != null)
// Left and right don't share a student because those are filtered out in ExaminationImporter
then
scoreHolder.penalize(kcontext, $leftTopic.getStudentSize() + $rightTopic.getStudentSize());
end
rule "periodPenaltyExclusion"
when
$periodPenalty : PeriodPenalty(
periodPenaltyType == PeriodPenaltyType.EXCLUSION,
$leftTopic : leftTopic,
$rightTopic : rightTopic
)
$leftExam : Exam(topic == $leftTopic, $leftPeriod : period, period != null)
$rightExam : Exam(topic == $rightTopic, period == $leftPeriod)
then
scoreHolder.penalize(kcontext, $leftTopic.getStudentSize() + $rightTopic.getStudentSize());
end
rule "periodPenaltyAfter"
when
$periodPenalty : PeriodPenalty(
periodPenaltyType == PeriodPenaltyType.AFTER,
$leftTopic : leftTopic,
$rightTopic : rightTopic
)
$leftExam : Exam(topic == $leftTopic, $leftPeriodIndex : periodIndex, period != null)
$rightExam : Exam(topic == $rightTopic, $leftPeriodIndex <= periodIndex, period != null)
then
scoreHolder.penalize(kcontext, $leftTopic.getStudentSize() + $rightTopic.getStudentSize());
end
// Room hard constraints
rule "roomPenaltyExclusive"
when
$roomPenalty : RoomPenalty(
roomPenaltyType == RoomPenaltyType.ROOM_EXCLUSIVE,
$topic : topic
)
$leftExam : Exam(topic == $topic, $room : room, room != null, $period : period, period != null)
$rightExam : Exam(room == $room, period == $period, topic != $topic, $otherTopic : topic)
then
scoreHolder.penalize(kcontext, $topic.getStudentSize() + $otherTopic.getStudentSize());
end
// ############################################################################
// Soft constraints
// ############################################################################
// Two exams in a row which share students
rule "twoExamsInARow"
when
$topicConflict : TopicConflict($leftTopic : leftTopic, $rightTopic : rightTopic)
$leftExam : Exam(topic == $leftTopic, $leftDayIndex : dayIndex, $leftPeriodIndex : periodIndex, period != null)
$rightExam : Exam(topic == $rightTopic, dayIndex == $leftDayIndex,
Math.abs($leftPeriodIndex - periodIndex) == 1)
then
scoreHolder.penalize(kcontext, $topicConflict.getStudentSize());
end
// TODO check if merging twoExamsInARow and twoExamsInADay can be a noticeable performance boost
// Two exams in a day which share students
rule "twoExamsInADay"
when
$topicConflict : TopicConflict($leftTopic : leftTopic, $rightTopic : rightTopic)
$leftExam : Exam(topic == $leftTopic, $leftDayIndex : dayIndex, $leftPeriodIndex : periodIndex, period != null)
$rightExam : Exam(topic == $rightTopic, dayIndex == $leftDayIndex,
Math.abs($leftPeriodIndex - periodIndex) > 1)
then
scoreHolder.penalize(kcontext, $topicConflict.getStudentSize());
end
// Exams which share students have to few periods between them
rule "periodSpread"
when
$constraintConfiguration : ExaminationConstraintConfiguration()
$topicConflict : TopicConflict($leftTopic : leftTopic, $rightTopic : rightTopic)
$leftExam : Exam(topic == $leftTopic, $leftPeriodIndex : periodIndex, period != null)
$rightExam : Exam(topic == $rightTopic, period != null,
Math.abs($leftPeriodIndex - periodIndex) < ($constraintConfiguration.getPeriodSpreadLength() + 1))
then
scoreHolder.penalize(kcontext, $topicConflict.getStudentSize());
end
// Several exams in the same room and period have different durations
rule "mixedDurations"
when
$leftExam : Exam($leftId : id, period != null, $period : period, room != null, $room : room,
$leftTopicDuration : topicDuration)
// 4 mixed durations of 100, 150, 200 and 200 should only result in 2 penalties (for 100&150 and 100&200)
// leftExam has lowest id of the period+room combo
not Exam(period == $period, room == $room, id < $leftId)
// rightExam has a different duration
$rightExam : Exam(period == $period, room == $room, id > $leftId,
topicDuration != $leftTopicDuration,
$rightId : id, $rightTopicDuration : topicDuration
)
// rightExam has the lowest id of the period+room+rightDuration combo
not Exam(period == $period, room == $room, id < $rightId,
topicDuration == $rightTopicDuration
)
then
scoreHolder.penalize(kcontext);
end
// Larger Exams towards the beginning of the examination session
rule "frontLoad"
when
$exam : Exam(topicFrontLoadLarge == true, periodFrontLoadLast == true)
then
scoreHolder.penalize(kcontext);
end
// Period Penalty
rule "periodPenalty"
when
$period : Period(penalty != 0)
$exam: Exam(period == $period)
then
scoreHolder.penalize(kcontext, $period.getPenalty());
end
// Room Penalty
rule "roomPenalty"
when
$room : Room(penalty != 0)
$exam: Exam(room == $room)
then
scoreHolder.penalize(kcontext, $room.getPenalty());
end