org.drools.planner.examples.travelingtournament.solver.smart.smartTravelingTournamentScoreRules.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.travelingtournament.smart.solver;
dialect "java"
import org.drools.planner.core.score.buildin.hardandsoft.HardAndSoftScoreHolder;
import org.drools.planner.core.score.constraint.UnweightedConstraintOccurrence;
import org.drools.planner.core.score.constraint.ConstraintType;
import org.drools.planner.examples.travelingtournament.domain.Match;
import org.drools.planner.examples.travelingtournament.domain.Day;
import org.drools.planner.examples.travelingtournament.domain.Team;
import org.drools.planner.examples.travelingtournament.domain.TravelingTournament;
import org.drools.planner.examples.travelingtournament.domain.solver.Hop;
global HardAndSoftScoreHolder scoreHolder;
// ############################################################################
// Hard constraints
// ############################################################################
// multipleMatchesPerTeamPerDay is obsolete because it is always 0
rule "fourConsecutiveHomeMatches"
when
$m : Match($homeTeam : homeTeam, $day1 : day)
Match(homeTeam == $homeTeam, eval(day.getIndex() == $day1.getIndex() + 1))
Match(homeTeam == $homeTeam, eval(day.getIndex() == $day1.getIndex() + 2))
Match(homeTeam == $homeTeam, eval(day.getIndex() == $day1.getIndex() + 3))
then
insertLogical(new UnweightedConstraintOccurrence("fourConsecutiveHomeMatches", $m));
end
rule "fourConsecutiveAwayMatches"
when
$m : Match($awayTeam : awayTeam, $day1 : day)
Match(awayTeam == $awayTeam, eval(day.getIndex() == $day1.getIndex() + 1))
Match(awayTeam == $awayTeam, eval(day.getIndex() == $day1.getIndex() + 2))
Match(awayTeam == $awayTeam, eval(day.getIndex() == $day1.getIndex() + 3))
then
insertLogical(new UnweightedConstraintOccurrence("fourConsecutiveAwayMatches", $m));
end
rule "matchRepeater"
when
$m : Match($homeTeam : homeTeam, $awayTeam : awayTeam, $day1 : day)
Match(homeTeam == $awayTeam, awayTeam == $homeTeam, eval(day.getIndex() == $day1.getIndex() + 1))
then
insertLogical(new UnweightedConstraintOccurrence("matchRepeater", $m));
end
// ############################################################################
// Soft constraints
// ############################################################################
rule "startToAwayHop"
when
Match($toTeam : homeTeam, $team : awayTeam, $day : day)
not Day(eval(index == $day.getIndex() - 1))
then
insertLogical(new Hop($team, $team, $toTeam));
end
rule "homeToAwayHop"
when
Match($team : homeTeam, $day1 : day)
Match($toTeam : homeTeam, awayTeam == $team, eval(day.getIndex() == $day1.getIndex() + 1))
then
insertLogical(new Hop($team, $team, $toTeam));
end
rule "awayToAwayHop"
when
Match($fromTeam : homeTeam, $team : awayTeam, $day1 : day)
Match($toTeam : homeTeam, awayTeam == $team, eval(day.getIndex() == $day1.getIndex() + 1))
then
insertLogical(new Hop($team, $fromTeam, $toTeam));
end
rule "awayToHomeHop"
when
Match($fromTeam : homeTeam, $team : awayTeam, $day1 : day)
Match(homeTeam == $team, eval(day.getIndex() == $day1.getIndex() + 1))
then
insertLogical(new Hop($team, $fromTeam, $team));
end
rule "awayToEndHop"
when
Match($fromTeam : homeTeam, $team : awayTeam, $day : day)
not Day(eval(index == $day.getIndex() + 1))
then
insertLogical(new Hop($team, $fromTeam, $team));
end
// ############################################################################
// Calculate score
// ############################################################################
rule "hardConstraintsBroken"
salience -1 // Do the other rules first (optional, for performance)
when
$occurrenceCount : Number() from accumulate(
$unweightedConstraintOccurrence : UnweightedConstraintOccurrence(),
count($unweightedConstraintOccurrence)
)
then
scoreHolder.setHardConstraintsBroken($occurrenceCount.intValue());
end
rule "softConstraintsBroken"
salience -1 // Do the other rules first (optional, for performance)
when
$totalDistance : Number() from accumulate(
Hop($distance : distance),
sum($distance)
)
then
scoreHolder.setSoftConstraintsBroken($totalDistance.intValue());
end