org.drools.planner.examples.manners2009.solver.manners2009ScoreRules.drl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of drools-planner-examples Show documentation
Show all versions of drools-planner-examples Show documentation
Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule
engine powered score calculation. This is the drools-planner-examples module which contains examples on how to use
Drools Planner.
/*
* Copyright 2010 JBoss Inc
*
* 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.drools.planner.examples.manners2009.solver;
dialect "java"
import org.drools.planner.core.score.calculator.SimpleScoreCalculator;
import org.drools.planner.core.score.constraint.UnweightedConstraintOccurrence;
import org.drools.planner.core.score.constraint.ConstraintType;
import org.drools.planner.examples.manners2009.domain.Gender;
import org.drools.planner.examples.manners2009.domain.Guest;
import org.drools.planner.examples.manners2009.domain.Hobby;
import org.drools.planner.examples.manners2009.domain.HobbyPractician;
import org.drools.planner.examples.manners2009.domain.Job;
import org.drools.planner.examples.manners2009.domain.JobType;
import org.drools.planner.examples.manners2009.domain.Manners2009;
import org.drools.planner.examples.manners2009.domain.Seat;
import org.drools.planner.examples.manners2009.domain.SeatDesignation;
import org.drools.planner.examples.manners2009.domain.Table;
global SimpleScoreCalculator scoreCalculator;
// ############################################################################
// Hard constraints
// ############################################################################
// Maintain a boy-girl-boy-girl seating arrangement is obsolete because it's build into the moves
// 1 democrat and 1 republican = 2 politians at each table but NOT two of the same kind
// 2 doctors at each table but NOT two of the same kind
// 2 sports stars at each table but NOT two of the same kind
// 2 programmers at each table but NOT two of the same kind
// 2 socialites at each table
// 2 teachers at each table
rule "twoSameJobTypePerTable"
when
$jobType : JobType()
$table : Table()
not (
SeatDesignation(guestJobType == $jobType, seatTable == $table, $firstJob : guestJob)
and SeatDesignation(guestJobType == $jobType, seatTable == $table, $secondJob : guestJob)
and (eval($jobType == JobType.SOCIALITE) or eval($jobType == JobType.TEACHER) or eval($firstJob != $secondJob))
)
then
insertLogical(new UnweightedConstraintOccurrence("twoSameJobTypePerTable",
$jobType, $table));
end
// This is to avoid score traps. The score function should for example give an insentive
// to put it 1 doctor at a table without doctors even though 2 doctors are needed.
// This extra rule can be avoided by weighting the broken constrainst of the other rule
rule "atLeastOneJobTypePerTableScoreGuider"
when
$table : Table()
$jobType : JobType()
not SeatDesignation(guestJobType == $jobType, seatTable == $table)
then
insertLogical(new UnweightedConstraintOccurrence("tableWithoutJobTypeShouldBePunishedExtra",
$jobType, $table));
end
// Each person must share a hobby with his/her left neighbour
// (so also the same or another hobby with his/her right neighbour)
rule "leftHasHobbyInCommon"
when
$leftSeat : Seat()
$rightSeat : Seat(leftSeat == $leftSeat)
$leftDesignation : SeatDesignation($leftGuest : guest, seat == $leftSeat)
$rightDesignation : SeatDesignation($rightGuest : guest, seat == $rightSeat)
not (
HobbyPractician(guest == $leftGuest, $leftHobby : hobby)
and HobbyPractician(guest == $rightGuest, hobby == $leftHobby)
)
then
insertLogical(new UnweightedConstraintOccurrence("leftHasHobbyInCommon",
$leftDesignation, $rightDesignation));
end
// ############################################################################
// Calculate score
// ############################################################################
rule "hardConstraintsBroken"
when
$occurrenceCount : Number() from accumulate(
$unweightedConstraintOccurrence : UnweightedConstraintOccurrence(),
count($unweightedConstraintOccurrence)
)
then
scoreCalculator.setScore(- $occurrenceCount.intValue());
end