All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.optaplanner.examples.conferencescheduling.solver.conferenceSchedulingConstraints.drl Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 9.44.0.Final
Show newest version
/*
 * 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.conferencescheduling.solver;
    dialect "java"

import org.optaplanner.core.api.score.buildin.hardmediumsoft.HardMediumSoftScoreHolder;

import org.optaplanner.examples.conferencescheduling.domain.ConferenceConstraintConfiguration;
import org.optaplanner.examples.conferencescheduling.domain.Room;
import org.optaplanner.examples.conferencescheduling.domain.Speaker;
import org.optaplanner.examples.conferencescheduling.domain.Talk;
import org.optaplanner.examples.conferencescheduling.domain.Timeslot;

global HardMediumSoftScoreHolder scoreHolder;

// ############################################################################
// Hard constraints
// ############################################################################

rule "Room unavailable timeslot"
    when
        $talk : Talk(hasUnavailableRoom())
    then
        scoreHolder.penalize(kcontext,
                $talk.getDurationInMinutes());
end

rule "Room conflict"
    when
        $talk1 : Talk(timeslot != null,
                room != null, $room : room,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                room == $room,
                overlapsTime($talk1),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                $talk2.overlappingDurationInMinutes($talk1));
end

rule "Speaker unavailable timeslot"
    when
        $talk : Talk(hasAnyUnavailableSpeaker())
    then
        scoreHolder.penalize(kcontext,
                $talk.getDurationInMinutes());
end

rule "Speaker conflict"
    when
        $speaker : Speaker()
        $talk1 : Talk(timeslot != null,
                hasSpeaker($speaker),
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                hasSpeaker($speaker),
                overlapsTime($talk1),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                $talk2.overlappingDurationInMinutes($talk1));
end

rule "Talk prerequisite talks"
    when
        $talk1 : Talk(timeslot != null)
        $talk2 : Talk(timeslot != null,
                getPrerequisiteTalkSet().contains($talk1),
                !getTimeslot().startsAfter($talk1.getTimeslot()))
    then
        scoreHolder.penalize(kcontext,
                $talk1.getDurationInMinutes() + $talk2.getDurationInMinutes());
end

rule "Talk mutually-exclusive-talks tags"
    when
        $talk1 : Talk(timeslot != null,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                overlappingMutuallyExclusiveTalksTagCount($talk1) > 0,
                overlapsTime($talk1),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
            $talk2.overlappingMutuallyExclusiveTalksTagCount($talk1) * $talk2.overlappingDurationInMinutes($talk1));
end

rule "Consecutive talks pause"
    when
        ConferenceConstraintConfiguration($minimumPause : minimumConsecutiveTalksPauseInMinutes)
        $talk1 : Talk(timeslot != null,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                hasMutualSpeaker($talk1),
                !getTimeslot().pauseExists($talk1.getTimeslot(), $minimumPause),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                $talk1.getDurationInMinutes() + $talk2.getDurationInMinutes());
end

rule "Crowd control"
    when
        $talk : Talk(timeslot != null, $timeslot : timeslot,
                crowdControlRisk > 0)
        Number(this != 1) from accumulate(
            // TODO Analyse if this is better: getTimeslot().getStartDateTime().equals($timeslot.getStartDateTime())
            // because from a crowd control perspective, only the start of the session matters
            // Timeslot.overlaps() is deliberately not used
            $otherTalk : Talk(timeslot == $timeslot,
                crowdControlRisk > 0,
                this != $talk),
            count($otherTalk)
        )
    then
        scoreHolder.penalize(kcontext, $timeslot.getDurationInMinutes());
end


rule "Speaker required timeslot tags"
    when
        $talk : Talk($count : missingSpeakerRequiredTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Speaker prohibited timeslot tags"
    when
        $talk : Talk($count : prevailingSpeakerProhibitedTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk required timeslot tags"
    when
        $talk : Talk($count : missingRequiredTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk prohibited timeslot tags"
    when
        $talk : Talk($count : prevailingProhibitedTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Speaker required room tags"
    when
        $talk : Talk($count : missingSpeakerRequiredRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Speaker prohibited room tags"
    when
        $talk : Talk($count : prevailingSpeakerProhibitedRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk required room tags"
    when
        $talk : Talk($count : missingRequiredRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk prohibited room tags"
    when
        $talk : Talk($count : prevailingProhibitedRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

// ############################################################################
// Medium constraints
// ############################################################################

rule "Published timeslot"
    when
        $talk : Talk(publishedTimeslot != null, timeslot != publishedTimeslot)
    then
        scoreHolder.penalize(kcontext);
end

// ############################################################################
// Soft constraints
// ############################################################################

rule "Published room"
    when
        $talk : Talk(timeslot != null, publishedRoom != null, room != publishedRoom)
    then
        scoreHolder.penalize(kcontext);
end

rule "Theme track conflict"
    when
        $talk1 : Talk(timeslot != null,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                overlappingThemeTrackCount($talk1) > 0,
                overlapsTime($talk1),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                $talk2.overlappingThemeTrackCount($talk1) * $talk2.overlappingDurationInMinutes($talk1));
end

rule "Theme track room stability"
    when
        $talk1 : Talk(timeslot != null,
                $room : room,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                overlappingThemeTrackCount($talk1) > 0,
                getTimeslot().isOnSameDayAs($talk1.getTimeslot()),
                room != $room,
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                $talk2.overlappingThemeTrackCount($talk1)
                * ($talk1.getDurationInMinutes() + $talk2.getDurationInMinutes()));
end

rule "Sector conflict"
    when
        $talk1 : Talk(timeslot != null,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                overlappingSectorCount($talk1) > 0,
                overlapsTime($talk1),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                $talk2.overlappingSectorCount($talk1) * $talk2.overlappingDurationInMinutes($talk1));
end

rule "Audience type diversity"
    when
        $talk1 : Talk(timeslot != null, $timeslot : timeslot,
                $id1 : id)
        // Timeslot.overlaps() is deliberately not used
        $talk2 : Talk(timeslot == $timeslot,
                overlappingAudienceTypeCount($talk1) > 0,
                id > $id1)
    then
        scoreHolder.reward(kcontext,
                $talk2.overlappingAudienceTypeCount($talk1) * $timeslot.getDurationInMinutes());
end

rule "Audience type theme track conflict"
    when
        $talk1 : Talk(timeslot != null,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                overlappingThemeTrackCount($talk1) > 0,
                overlappingAudienceTypeCount($talk1) > 0,
                overlapsTime($talk1),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                ($talk2.overlappingThemeTrackCount($talk1) * $talk2.overlappingAudienceTypeCount($talk1))
                * $talk2.overlappingDurationInMinutes($talk1));
end

rule "Audience level diversity"
    when
        Talk(timeslot != null, $timeslot : timeslot,
                $audienceLevel : audienceLevel,
                $id1 : id)
        // Timeslot.overlaps() is deliberately not used
        Talk(timeslot == $timeslot,
                audienceLevel != $audienceLevel,
                id > $id1)
    then
        scoreHolder.reward(kcontext,
                $timeslot.getDurationInMinutes());
end

rule "Content audience level flow violation"
    when
        $talk1 : Talk(timeslot != null, $timeslot1 : timeslot,
                $audienceLevel1 : audienceLevel)
        $talk2 : Talk(timeslot != null,
                audienceLevel < $audienceLevel1,
                overlappingContentCount($talk1) > 0,
                !getTimeslot().endsBefore($timeslot1))
    then
        scoreHolder.penalize(kcontext,
                $talk2.overlappingContentCount($talk1)
                * ($talk1.getDurationInMinutes() + $talk2.getDurationInMinutes()));
end

rule "Content conflict"
    when
        $talk1 : Talk(timeslot != null,
                $id1 : id)
        $talk2 : Talk(timeslot != null,
                overlappingContentCount($talk1) > 0,
                overlapsTime($talk1),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                $talk2.overlappingContentCount($talk1) * $talk2.overlappingDurationInMinutes($talk1));
end

rule "Language diversity"
    when
        Talk(timeslot != null, $timeslot : timeslot,
                $language1 : language,
                $id1 : id)
        // Timeslot.overlaps() is deliberately not used
        Talk(timeslot == $timeslot,
                language != $language1,
                id > $id1)
    then
        scoreHolder.reward(kcontext,
                $timeslot.getDurationInMinutes());
end

rule "Same day talks"
    when
        $talk1 : Talk(timeslot != null,
                $id1 : id)
        $talk2 :  Talk(timeslot != null,
                overlappingContentCount($talk1) > 0 || overlappingThemeTrackCount($talk1) > 0,
                !getTimeslot().isOnSameDayAs($talk1.getTimeslot()),
                id > $id1)
    then
        scoreHolder.penalize(kcontext,
                ($talk2.overlappingThemeTrackCount($talk1) + $talk2.overlappingContentCount($talk1))
                * ($talk1.getDurationInMinutes() + $talk2.getDurationInMinutes()));
end

rule "Popular talks"
    when
        $talk1 : Talk(timeslot != null,
                room != null, $room1 : room,
                $favoriteCount1 : favoriteCount)
        $talk2 : Talk(timeslot != null,
                room != null,
                favoriteCount < $favoriteCount1,
                getRoom().getCapacity() > $room1.getCapacity())
    then
        scoreHolder.penalize(kcontext,
                $talk1.getDurationInMinutes() + $talk2.getDurationInMinutes());
end


rule "Speaker preferred timeslot tags"
    when
        $talk : Talk($count : missingSpeakerPreferredTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Speaker undesired timeslot tags"
    when
        $talk : Talk($count : prevailingSpeakerUndesiredTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk preferred timeslot tags"
    when
        $talk : Talk($count : missingPreferredTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk undesired timeslot tags"
    when
        $talk : Talk($count : prevailingUndesiredTimeslotTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Speaker preferred room tags"
    when
        $talk : Talk($count : missingSpeakerPreferredRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Speaker undesired room tags"
    when
        $talk : Talk($count : prevailingSpeakerUndesiredRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk preferred room tags"
    when
        $talk : Talk($count : missingPreferredRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end

rule "Talk undesired room tags"
    when
        $talk : Talk($count : prevailingUndesiredRoomTagCount() > 0)
    then
        scoreHolder.penalize(kcontext,
                $count * $talk.getDurationInMinutes());
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy