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

org.optaplanner.examples.machinereassignment.solver.machineReassignmentConstraints.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 2011 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.machinereassignment.solver;
    dialect "java"

import org.optaplanner.core.api.score.buildin.hardsoftlong.HardSoftLongScoreHolder;

import org.optaplanner.examples.machinereassignment.domain.MachineReassignment;
import org.optaplanner.examples.machinereassignment.domain.MrBalancePenalty;
import org.optaplanner.examples.machinereassignment.domain.MrMachineCapacity;
import org.optaplanner.examples.machinereassignment.domain.MrGlobalPenaltyInfo;
import org.optaplanner.examples.machinereassignment.domain.MrLocation;
import org.optaplanner.examples.machinereassignment.domain.MrMachine;
import org.optaplanner.examples.machinereassignment.domain.MrNeighborhood;
import org.optaplanner.examples.machinereassignment.domain.MrProcess;
import org.optaplanner.examples.machinereassignment.domain.MrProcessAssignment;
import org.optaplanner.examples.machinereassignment.domain.MrResource;
import org.optaplanner.examples.machinereassignment.domain.MrService;
import org.optaplanner.examples.machinereassignment.domain.solver.MrServiceDependency;
import org.optaplanner.examples.machinereassignment.solver.drools.MrMachineTransientUsage;
import org.optaplanner.examples.machinereassignment.solver.drools.MrMachineUsage;
import org.optaplanner.examples.machinereassignment.solver.drools.MrServiceMovedProcessesCount;

global HardSoftLongScoreHolder scoreHolder;

rule "machineUsage"
        salience 1 // Do these rules first (optional, for performance)
    when
        $machineCapacity : MrMachineCapacity($machine : machine, $resource : resource)
        accumulate(
            $processAssignment : MrProcessAssignment(machine == $machine);
            $usageTotal : sum($processAssignment.getUsage($resource))
        )
    then
        insertLogical(new MrMachineUsage($machineCapacity, $usageTotal));
end
rule "machineTransientUsage"
        salience 1 // Do these rules first (optional, for performance)
    when
        $machineCapacity : MrMachineCapacity(transientlyConsumed == true, $machine : machine, $resource : resource)
        accumulate(
            $processAssignment : MrProcessAssignment(originalMachine == $machine, moved == true);
            $usageTotal : sum($processAssignment.getUsage($resource))
        )
    then
        insertLogical(new MrMachineTransientUsage($machineCapacity, $usageTotal));
end

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

// Capacity constraints + Transient usage constraints
rule "maximumCapacityNonTransientlyConsumed"
    when
        $machineUsage : MrMachineUsage(transientlyConsumed == false, maximumAvailable < 0,
                $maximumAvailable : maximumAvailable)
    then
        scoreHolder.addHardConstraintMatch(kcontext, $maximumAvailable);
end
rule "maximumCapacityTransientlyConsumed"
    when
        $machineUsage : MrMachineUsage(transientlyConsumed == true,
                $maximumAvailable : maximumAvailable, $resource : resource, $machine : machine)
        MrMachineTransientUsage(resource == $resource, machine == $machine,
                $maximumAvailable < usage, $transientUsage : usage)
    then
        scoreHolder.addHardConstraintMatch(kcontext, $maximumAvailable - $transientUsage);
end

// Conflict constraints
rule "serviceConflict"
    when
        $leftProcessAssignment : MrProcessAssignment(machine != null, $service : service, $machine : machine, $leftId : id)
        $rightProcessAssignment : MrProcessAssignment(service == $service, machine == $machine, id > $leftId)
    then
        scoreHolder.addHardConstraintMatch(kcontext, -1);
end

// Spread constraints
rule "serviceLocationSpread"
    when
        $service : MrService($locationSpread : locationSpread)
        accumulate(
            $location : MrLocation()
            and exists MrProcessAssignment(service == $service, location == $location);
            $spreadCount : count($location);
            $spreadCount < $locationSpread
        )
    then
        scoreHolder.addHardConstraintMatch(kcontext, $spreadCount - $locationSpread);
end

// Dependency constraints
rule "serviceDependency"
    when
        $serviceDependency : MrServiceDependency($fromService : fromService, $toService : toService)
        $processAssignment : MrProcessAssignment(machine != null, service == $fromService, $neighborhood : neighborhood)
        not MrProcessAssignment(service == $toService, neighborhood == $neighborhood)
    then
        scoreHolder.addHardConstraintMatch(kcontext, -1);
end

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

// Load cost
rule "loadCost"
    when
        $machineUsage : MrMachineUsage(safetyAvailable < 0,
                $safetyAvailable : safetyAvailable, $loadCostWeight : loadCostWeight)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, $safetyAvailable * $loadCostWeight);
end

// Balance cost
rule "balanceCost"
    when
        $balancePenalty : MrBalancePenalty($originResource : originResource, $targetResource : targetResource,
                $multiplicand : multiplicand, $weight : weight)
        MrMachineUsage(resource == $originResource, maximumAvailable > 0,
                $machine : machine, $originAvailable : maximumAvailable)
        MrMachineUsage(resource == $targetResource, machine == $machine,
                maximumAvailable < $originAvailable * $multiplicand,
                $targetAvailable : maximumAvailable)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, ($targetAvailable - ($originAvailable * $multiplicand)) * $weight);
end

// Process move cost
rule "processMoveCost"
    when
        MrGlobalPenaltyInfo(processMoveCostWeight > 0, $processMoveCostWeight : processMoveCostWeight)
        $processAssignment : MrProcessAssignment(moved == true, processMoveCost > 0, $processMoveCost : processMoveCost)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, - $processMoveCost * $processMoveCostWeight);
end

// Service move cost
rule "serviceMovedProcessesCount"
        salience 1 // Do these rules first (optional, for performance)
    when
        $service : MrService()
        accumulate(
            $processAssignment : MrProcessAssignment(service == $service, moved == true);
            $movedProcessesCount : count($processAssignment)
        )
    then
        insertLogical(new MrServiceMovedProcessesCount($service, $movedProcessesCount.intValue()));
end

rule "serviceMoveCost"
    when
        MrGlobalPenaltyInfo(serviceMoveCostWeight > 0, $serviceMoveCostWeight : serviceMoveCostWeight)
        $serviceMovedProcessesCount : MrServiceMovedProcessesCount($service : service, $serviceId : serviceId,
                $movedProcessesCount : movedProcessesCount)
        not MrServiceMovedProcessesCount(movedProcessesCount > $movedProcessesCount)
        not MrServiceMovedProcessesCount(movedProcessesCount == $movedProcessesCount, serviceId < $serviceId)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, - $movedProcessesCount * $serviceMoveCostWeight);
end

// Machine move cost
rule "machineMoveCost"
    when
        MrGlobalPenaltyInfo(machineMoveCostWeight > 0, $machineMoveCostWeight : machineMoveCostWeight)
        $processAssignment : MrProcessAssignment(moved == true, machineMoveCost > 0, $machineMoveCost : machineMoveCost)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, - $machineMoveCost * $machineMoveCostWeight);
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy