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

org.optaplanner.examples.meetingscheduling.solver.meetingSchedulingConstraints.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.meetingscheduling.solver;
    dialect "java"

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

import org.optaplanner.examples.meetingscheduling.domain.Attendance;
import org.optaplanner.examples.meetingscheduling.domain.Meeting;
import org.optaplanner.examples.meetingscheduling.domain.MeetingAssignment;
import org.optaplanner.examples.meetingscheduling.domain.MeetingSchedule;
import org.optaplanner.examples.meetingscheduling.domain.Person;
import org.optaplanner.examples.meetingscheduling.domain.PreferredAttendance;
import org.optaplanner.examples.meetingscheduling.domain.RequiredAttendance;
import org.optaplanner.examples.meetingscheduling.domain.Room;
import org.optaplanner.examples.meetingscheduling.domain.TimeGrain;

global HardMediumSoftScoreHolder scoreHolder;

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

rule "Room conflict"
    when
        $leftAssignment : MeetingAssignment(room != null, $leftId : id, $room : room)
        $rightAssignment : MeetingAssignment(room == $room, calculateOverlap($leftAssignment) > 0, id > $leftId)
    then
        scoreHolder.penalize(kcontext, $rightAssignment.calculateOverlap($leftAssignment));
end

// TODO Might be more efficient if a MeetingAssignment knows timeGrainList.size()
// TODO Can be obsolete if MeetingAssignment uses @ValueRangeProvider from entity for timeGrainRange
rule "Don't go in overtime"
    when
        MeetingAssignment(startingTimeGrain != null, $lastTimeGrainIndex : getLastTimeGrainIndex())
        not TimeGrain(grainIndex == $lastTimeGrainIndex)
    then
        scoreHolder.penalize(kcontext, $lastTimeGrainIndex);
end

rule "Required attendance conflict"
    when
        RequiredAttendance($leftId : id, $person : person, $leftMeeting : meeting)
        RequiredAttendance(person == $person, $rightMeeting : meeting, id > $leftId)
        $leftAssignment : MeetingAssignment(meeting == $leftMeeting, startingTimeGrain != null)
        $rightAssignment : MeetingAssignment(meeting == $rightMeeting, calculateOverlap($leftAssignment) > 0)
    then
        scoreHolder.penalize(kcontext, $rightAssignment.calculateOverlap($leftAssignment));
end

rule "Required room capacity"
    when
        MeetingAssignment(requiredCapacity > roomCapacity, $requiredCapacity : requiredCapacity, $roomCapacity : roomCapacity)
    then
        scoreHolder.penalize(kcontext, $requiredCapacity - $roomCapacity);
end

rule "Start and end on same day"
    when
        MeetingAssignment(startingTimeGrain != null, $firstTimeGrain : startingTimeGrain, $lastTimeGrainIndex : getLastTimeGrainIndex())
        $lastTimeGrain : TimeGrain(grainIndex == $lastTimeGrainIndex, $firstTimeGrain.getDay() != getDay())
    then
        scoreHolder.penalize(kcontext);
end

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

rule "Required and preferred attendance conflict"
    when
        RequiredAttendance($person : person, $leftMeeting : meeting)
        PreferredAttendance(person == $person, $rightMeeting : meeting)
        $leftAssignment : MeetingAssignment(meeting == $leftMeeting, startingTimeGrain != null)
        $rightAssignment : MeetingAssignment(meeting == $rightMeeting, calculateOverlap($leftAssignment) > 0)
    then
        // Fairness not implemented (see tennis example)
        scoreHolder.penalize(kcontext);
end

rule "Preferred attendance conflict"
    when
        PreferredAttendance($leftId : id, $person : person, $leftMeeting : meeting)
        PreferredAttendance(person == $person, $rightMeeting : meeting, id > $leftId)
        $leftAssignment : MeetingAssignment(meeting == $leftMeeting, startingTimeGrain != null)
        $rightAssignment : MeetingAssignment(meeting == $rightMeeting, calculateOverlap($leftAssignment) > 0)
    then
        // Fairness not implemented (see tennis example)
        scoreHolder.penalize(kcontext);
end

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

rule "Do all meetings as soon as possible"
    when
        MeetingAssignment(startingTimeGrain != null, $lastTimeGrainIndex : lastTimeGrainIndex)
    then
        scoreHolder.penalize(kcontext, $lastTimeGrainIndex);
end

rule "One TimeGrain break between two consecutive meetings"
    when
        MeetingAssignment(startingTimeGrain != null,  $leftEnd : getLastTimeGrainIndex())
        MeetingAssignment(startingTimeGrain != null, $leftEnd == startingTimeGrain.getGrainIndex() - 1)
    then
        scoreHolder.penalize(kcontext);
end

rule "Overlapping meetings"
    when
        $leftAssignment : MeetingAssignment(startingTimeGrain != null)
        $rightAssignment : MeetingAssignment(startingTimeGrain != null, meeting.getId() < $leftAssignment.getMeeting().getId(),
            meeting != $leftAssignment.getMeeting())
    then
        if ($leftAssignment.calculateOverlap($rightAssignment) != 0) {
            scoreHolder.penalize(kcontext);
        }
end

rule "Assign larger rooms first"
    when
        MeetingAssignment(room != null, $roomCapacity : room.getCapacity())
        Room($roomCapacity < capacity, $otherRoomCapacity : capacity) // TODO BUG
    then
        scoreHolder.penalize(kcontext, $otherRoomCapacity - $roomCapacity);
end

rule "Room stability"
    when
        Attendance($person : person, $leftMeeting : meeting)
        Attendance(person == $person, meeting != $leftMeeting, $rightMeeting : meeting)
        MeetingAssignment(meeting == $leftMeeting, startingTimeGrain != null, $leftStartTimeGrain : startingTimeGrain, $leftRoom : room)
        MeetingAssignment(meeting == $rightMeeting, startingTimeGrain != null, $leftStartTimeGrain.getGrainIndex() < startingTimeGrain.getGrainIndex(),
            $leftRoom != room, startingTimeGrain.getGrainIndex() - $leftMeeting.getDurationInGrains() - $leftStartTimeGrain.getGrainIndex() <= 2)
    then
        scoreHolder.penalize(kcontext);
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy