org.optaplanner.examples.cheaptime.solver.cheapTimeConstraints.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 all versions of optaplanner-examples
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.cheaptime.solver;
dialect "java"
import java.util.List;
import java.util.ArrayList;
import org.optaplanner.core.api.score.buildin.hardmediumsoftlong.HardMediumSoftLongScoreHolder;
import org.optaplanner.examples.cheaptime.domain.CheapTimeSolution;
import org.optaplanner.examples.cheaptime.domain.Machine;
import org.optaplanner.examples.cheaptime.domain.MachineCapacity;
import org.optaplanner.examples.cheaptime.domain.PeriodPowerPrice;
import org.optaplanner.examples.cheaptime.domain.Task;
import org.optaplanner.examples.cheaptime.domain.TaskAssignment;
import org.optaplanner.examples.cheaptime.domain.TaskRequirement;
import org.optaplanner.examples.cheaptime.score.CheapTimeCostCalculator;
import org.optaplanner.examples.cheaptime.optional.score.drools.IdleCost;
import org.optaplanner.examples.cheaptime.optional.score.drools.PeriodWrapper;
import org.optaplanner.examples.cheaptime.optional.score.drools.MachinePeriodPart;
global HardMediumSoftLongScoreHolder scoreHolder;
// Setup
rule "period"
salience 3
when
CheapTimeSolution($globalPeriodRangeFrom : globalPeriodRangeFrom, $globalPeriodRangeTo : globalPeriodRangeTo)
then
for (int i = $globalPeriodRangeFrom; i < $globalPeriodRangeTo; i++) {
insert(new PeriodWrapper(i));
}
end
// Introduce MachinePeriodPart to score efficiently
rule "machinePeriodPart"
salience 2
when
CheapTimeSolution($resourceList : resourceList)
PeriodWrapper($period : period);
$machine : Machine()
$taskAssignmentList : ArrayList() from collect (
TaskAssignment(
machine == $machine,
startPeriod <= $period,
endPeriod > $period)
)
then
insertLogical(new MachinePeriodPart($machine, $period, $resourceList.size(), $taskAssignmentList));
end
// ############################################################################
// Hard constraints
// ############################################################################
// Start time limits: each task must start between its earliest start and latest start limit.
rule "startTimeLimitsFrom"
when
$taskAssignment : TaskAssignment($task : task, $startPeriod : startPeriod < $task.startPeriodRangeFrom)
then
scoreHolder.addHardConstraintMatch(kcontext, $startPeriod - $task.getStartPeriodRangeFrom());
end
rule "startTimeLimitsTo"
when
$taskAssignment : TaskAssignment($task : task, $startPeriod : startPeriod >= $task.startPeriodRangeTo)
then
scoreHolder.addHardConstraintMatch(kcontext, $task.getStartPeriodRangeTo() - $startPeriod);
end
// Maximum capacity: the maximum capacity for each resource for each machine must not be exceeded.
rule "maximumCapacity" // resourceInShortTotal is calculated in MachinePeriodPart constructor
when
MachinePeriodPart($resourceInShortTotal : resourceInShortTotal < 0)
then
scoreHolder.addHardConstraintMatch(kcontext, $resourceInShortTotal);
end
// Startup and shutdown: each machine must be active in the periods during which it has assigned tasks.
// Between tasks it is allowed to be idle to avoid startup and shutdown costs.
// -> no rule is required because it's implemented in MachinePeriodPart
// ############################################################################
// Medium constraints
// ############################################################################
// Machine power cost: Each active or idle machine consumes power,
// which infers a power cost (depending on the power price during that time).
// Machine startup and shutdown cost: Every time a machine starts up or shuts down, an extra cost is inflicted.
rule "calculateIdleCost"
salience 1
when
MachinePeriodPart($machine : machine, $period : period, active == true)
exists MachinePeriodPart(machine == $machine, period == $period - 1, active == false)
exists MachinePeriodPart(machine == $machine, period < $period - 1, active == true)
accumulate (
MachinePeriodPart(machine == $machine, $activePeriod : period < $period - 1, active == true);
$lastActivePeriod : max($activePeriod)
)
$idlePeriodPowerPriceList : ArrayList() from collect (
PeriodPowerPrice(
period > (Integer) $lastActivePeriod,
period < $period)
)
then
long idleCost = 0;
for (Object obj : $idlePeriodPowerPriceList) {
PeriodPowerPrice idlePeriodPowerPrice = (PeriodPowerPrice)obj;
idleCost += CheapTimeCostCalculator.multiplyTwoMicros($machine.getPowerConsumptionMicros(), idlePeriodPowerPrice.getPowerPriceMicros());
}
insertLogical(new IdleCost($machine, $period, idleCost));
end
rule "machinePowerCostActive"
when
MachinePeriodPart($machine : machine, $period : period, active == true)
$periodPowerPrice : PeriodPowerPrice(period == $period)
then
scoreHolder.addMediumConstraintMatch(kcontext,
- CheapTimeCostCalculator.multiplyTwoMicros($machine.getPowerConsumptionMicros(), $periodPowerPrice.getPowerPriceMicros()));
end
rule "machinePowerCostOffToActive"
when
MachinePeriodPart($machine : machine, $period : period, active == true)
IdleCost(machine == $machine, activePeriodAfterIdle == $period, $idleCost : cost >= $machine.spinUpDownCostMicros)
then
scoreHolder.addMediumConstraintMatch(kcontext, - $machine.getSpinUpDownCostMicros());
end
rule "machinePowerCostIdleToActive"
when
MachinePeriodPart($machine : machine, $period : period, active == true)
IdleCost(machine == $machine, activePeriodAfterIdle == $period, $idleCost : cost < $machine.spinUpDownCostMicros)
then
scoreHolder.addMediumConstraintMatch(kcontext, - $idleCost);
end
rule "firstBoot"
when
MachinePeriodPart($machine : machine, $period : period, active == true)
not MachinePeriodPart(machine == $machine, period < $period, active == true);
then
scoreHolder.addMediumConstraintMatch(kcontext, - $machine.getSpinUpDownCostMicros());
end
// Task power cost: Each task consumes power too, which infers a power cost (depending on the power price during its time).
rule "taskPowerCost"
when
PeriodWrapper($period : period)
TaskAssignment(startPeriod <= $period, endPeriod > $period, $task : task)
$periodPowerPrice : PeriodPowerPrice(period == $period)
then
scoreHolder.addMediumConstraintMatch(kcontext,
- CheapTimeCostCalculator.multiplyTwoMicros($task.getPowerConsumptionMicros(), $periodPowerPrice.getPowerPriceMicros()));
end
// ############################################################################
// Soft constraints
// ############################################################################
// Start early: prefer starting a task sooner rather than later.
rule "startEarly"
when
TaskAssignment($startPeriod : startPeriod != null)
then
scoreHolder.addSoftConstraintMatch(kcontext, - $startPeriod);
end