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

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

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

import org.optaplanner.examples.pas.domain.AdmissionPart;
import org.optaplanner.examples.pas.domain.Bed;
import org.optaplanner.examples.pas.domain.BedDesignation;
import org.optaplanner.examples.pas.domain.Department;
import org.optaplanner.examples.pas.domain.DepartmentSpecialism;
import org.optaplanner.examples.pas.domain.Equipment;
import org.optaplanner.examples.pas.domain.Gender;
import org.optaplanner.examples.pas.domain.GenderLimitation;
import org.optaplanner.examples.pas.domain.Night;
import org.optaplanner.examples.pas.domain.Patient;
import org.optaplanner.examples.pas.domain.PatientAdmissionSchedule;
import org.optaplanner.examples.pas.domain.PreferredPatientEquipment;
import org.optaplanner.examples.pas.domain.RequiredPatientEquipment;
import org.optaplanner.examples.pas.domain.Room;
import org.optaplanner.examples.pas.domain.RoomEquipment;
import org.optaplanner.examples.pas.domain.RoomSpecialism;
import org.optaplanner.examples.pas.domain.Specialism;

global HardMediumSoftScoreHolder scoreHolder;

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

// Two patients in the same bed for a number of nights.
rule "sameBedInSameNight"
    when
        $leftBedDesignation : BedDesignation(
                bed != null, $bed : bed,
                $leftFirst : firstNightIndex, $leftLast : lastNightIndex,
                $leftId : id)
        $rightBedDesignation : BedDesignation(
                bed == $bed,
                $leftFirst <= lastNightIndex, $leftLast >= firstNightIndex,
                $rightFirst : firstNightIndex, $rightLast : lastNightIndex,
                id > $leftId)
    then
        scoreHolder.addHardConstraintMatch(kcontext,
                -1000 * (1 + Math.max($leftLast, $rightLast) - Math.max($leftFirst, $rightFirst)));
end

// Not yet applicable: RoomMaintenance constraint

// Gender limitation: Female in a male only room
rule "femaleInMaleRoom"
    when
        $bedDesignation : BedDesignation(roomGenderLimitation == GenderLimitation.MALE_ONLY,
                patientGender == Gender.FEMALE)
    then
        // Note: the original spec classified this as a soft constraint
        scoreHolder.addHardConstraintMatch(kcontext, -50 * $bedDesignation.getAdmissionPartNightCount());
end
// Gender limitation: Male in a female only room
rule "maleInFemaleRoom"
    when
        $bedDesignation : BedDesignation(roomGenderLimitation == GenderLimitation.FEMALE_ONLY,
                patientGender == Gender.MALE)
    then
        // Note: the original spec classified this as a soft constraint
        scoreHolder.addHardConstraintMatch(kcontext, -50 * $bedDesignation.getAdmissionPartNightCount());
end
// Gender limitation: Different genders in the same room when the room doesn't allow it
rule "differentGenderInSameGenderRoomInSameNight"
    when
        $leftBedDesignation : BedDesignation(roomGenderLimitation == GenderLimitation.SAME_GENDER,
                bed != null, $room : room, $leftGender : patientGender,
                $leftFirst : firstNightIndex, $leftLast : lastNightIndex,
                $leftId : id)
        $rightBedDesignation : BedDesignation(
                room == $room, patientGender != $leftGender,
                $leftFirst <= lastNightIndex, $leftLast >= firstNightIndex,
                $rightFirst : firstNightIndex, $rightLast : lastNightIndex,
                id > $leftId)
    then
        scoreHolder.addHardConstraintMatch(kcontext,
                -1000 * (1 + Math.max($leftLast, $rightLast) - Math.max($leftFirst, $rightFirst)));
end

// Department's minimumAge constraint
rule "departmentMinimumAge"
    when
        $department : Department(minimumAge != null, $minimumAge : minimumAge)
        $bedDesignation : BedDesignation(department == $department, patientAge < $minimumAge)
    then
        // Note: the original spec classified this as a soft constraint
        scoreHolder.addHardConstraintMatch(kcontext, -100 * $bedDesignation.getAdmissionPartNightCount());
end
// Department's maximumAge constraint
rule "departmentMaximumAge"
    when
        $department : Department(maximumAge != null, $maximumAge : maximumAge)
        $bedDesignation : BedDesignation(department == $department, patientAge > $maximumAge)
    then
        // Note: the original spec classified this as a soft constraint
        scoreHolder.addHardConstraintMatch(kcontext, -100 * $bedDesignation.getAdmissionPartNightCount());
end

// Not yet applicable: Patient requiredMaximumRoomCapacity constraint

// RequiredPatientEquipment constraint
rule "requiredPatientEquipment"
    when
        $requiredPatientEquipment : RequiredPatientEquipment($patient : patient, $equipment : equipment)
        $bedDesignation : BedDesignation(patient == $patient, $room : room, bed != null)
        not RoomEquipment(room == $room, equipment == $equipment)
    then
        // Note: the original spec classified this as a soft constraint
        scoreHolder.addHardConstraintMatch(kcontext, -50 * $bedDesignation.getAdmissionPartNightCount());
end

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

// Assign every patient to a bed, unless the dataset is overconstrained
rule "assignEveryPatientToABed"
    when
        $bedDesignation : BedDesignation(bed == null)
    then
        scoreHolder.addMediumConstraintMatch(kcontext, - $bedDesignation.getAdmissionPartNightCount());
end

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

// Patient preferredMaximumRoomCapacity
rule "preferredMaximumRoomCapacity"
    when
        $bedDesignation : BedDesignation(patientPreferredMaximumRoomCapacity != null,
                patientPreferredMaximumRoomCapacity < roomCapacity, bed != null)
    then
        // TODO this would be better for the problem, though the official spec does not do it
        // 8 * ($bedDesignation.getPatientPreferredMaximumRoomCapacity() - $bedDesignation.getRoomCapacity())
        // * $bedDesignation.getAdmissionPartNightCount(),
        scoreHolder.addSoftConstraintMatch(kcontext, -8 * $bedDesignation.getAdmissionPartNightCount());
end

// DepartmentSpecialism constraint
rule "departmentSpecialism"
    when
        $bedDesignation : BedDesignation($specialism : admissionPartSpecialism, $department : department, bed != null)
        not DepartmentSpecialism(department == $department, specialism == $specialism)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -10 * $bedDesignation.getAdmissionPartNightCount());
end

// RoomSpecialism constraint
rule "roomSpecialismNotExists"
    when
        $bedDesignation : BedDesignation(admissionPartSpecialism != null, $specialism : admissionPartSpecialism,
            $room : room, bed != null)
        not RoomSpecialism(room == $room, specialism == $specialism)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -20 * $bedDesignation.getAdmissionPartNightCount());
end
rule "roomSpecialismNotFirstPriority"
    when
        $bedDesignation : BedDesignation(admissionPartSpecialism != null, $specialism : admissionPartSpecialism,
            $room : room, bed != null)
        RoomSpecialism(priority > 1, room == $room, specialism == $specialism, $priority : priority)
    then
        scoreHolder.addSoftConstraintMatch(kcontext,
                -10 * ($priority - 1) * $bedDesignation.getAdmissionPartNightCount());
end

// PreferredPatientEquipment constraint
rule "preferredPatientEquipment"
    when
        $preferredPatientEquipment : PreferredPatientEquipment($patient : patient, $equipment : equipment)
        $bedDesignation : BedDesignation(patient == $patient, $room : room, bed != null)
        not RoomEquipment(room == $room, equipment == $equipment)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -20 * $bedDesignation.getAdmissionPartNightCount());
end

// Do not change bed in an AdmissionPart on different Nights constraint is built-in




© 2015 - 2024 Weber Informatics LLC | Privacy Policy