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

org.optaplanner.examples.projectjobscheduling.solver.projectJobSchedulingConstraints.drl Maven / Gradle / Ivy

/*
 * 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.projectjobscheduling.solver;
    dialect "java"

import org.optaplanner.core.api.score.buildin.bendable.BendableScoreHolder;

import org.optaplanner.examples.projectjobscheduling.domain.Allocation;
import org.optaplanner.examples.projectjobscheduling.domain.ExecutionMode;
import org.optaplanner.examples.projectjobscheduling.domain.Job;
import org.optaplanner.examples.projectjobscheduling.domain.JobType;
import org.optaplanner.examples.projectjobscheduling.domain.Project;
import org.optaplanner.examples.projectjobscheduling.domain.ResourceRequirement;
import org.optaplanner.examples.projectjobscheduling.domain.resource.Resource;
import org.optaplanner.examples.projectjobscheduling.solver.score.capacity.NonrenewableResourceCapacityTracker;
import org.optaplanner.examples.projectjobscheduling.solver.score.capacity.RenewableResourceCapacityTracker;
import org.optaplanner.examples.projectjobscheduling.solver.score.capacity.ResourceCapacityTracker;
import org.optaplanner.examples.projectjobscheduling.solver.score.drools.RenewableResourceUsedDay;

global BendableScoreHolder scoreHolder;

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

rule "nonrenewableResourceCapacity"
    when
        $resource : Resource(renewable == false, $capacity : capacity)
        accumulate(
            ResourceRequirement(resource == $resource,
                    $executionMode : executionMode,
                    $requirement : requirement)
            and Allocation(executionMode == $executionMode);
            $used : sum($requirement);
            $used > $capacity
        )
    then
        scoreHolder.addHardConstraintMatch(kcontext, 0, $capacity - $used);
end

rule "renewableResourceUsedDay"
        salience 1 // Do these rules first (optional, for performance)
    when
        ResourceRequirement(resourceRenewable == true, $executionMode : executionMode, $resource : resource)
        Allocation(executionMode == $executionMode,
                $startDate : startDate, $endDate : endDate)
    then
        for (int i = $startDate; i < $endDate; i++) {
            insertLogical(new RenewableResourceUsedDay($resource, i));
        }
end

rule "renewableResourceCapacity"
    when
        RenewableResourceUsedDay($resource : resource, $capacity : resourceCapacity, $usedDay : usedDay)
        accumulate(
            ResourceRequirement(resource == $resource,
                    $executionMode : executionMode,
                    $requirement : requirement)
            and Allocation(executionMode == $executionMode, $usedDay >= startDate, $usedDay < endDate);
            $used : sum($requirement);
            $used > $capacity
        )
    then
        scoreHolder.addHardConstraintMatch(kcontext, 0, $capacity - $used);
end

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

rule "totalProjectDelay"
    when
        Allocation(jobType == JobType.SINK, endDate != null, $endDate : endDate,
               $criticalPathEndDate : projectCriticalPathEndDate)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, 0,  $criticalPathEndDate - $endDate);
end


rule "totalMakespan"
    when
        accumulate(
            Allocation(jobType == JobType.SINK, $endDate : endDate);
            $maxProjectEndDate : max($endDate)
        )
    then
        scoreHolder.addSoftConstraintMatch(kcontext, 1, - (Integer) $maxProjectEndDate);
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy