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

org.drools.planner.examples.travelingtournament.solver.simple.simpleTravelingTournamentScoreRules.drl Maven / Gradle / Ivy

/*
 * 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.simple.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
// ############################################################################

rule "multipleMatchesPerTeamPerDay"
    when
        $team : Team()
        $m : Match( $id : id, homeTeam == $team, $day : day )
                || $m : Match( $id : id, awayTeam == $team, $day : day )
        exists Match( id > $id, homeTeam == $team, day == $day )
                || exists Match(id > $id, awayTeam == $team, day == $day )
    then
        insertLogical(new UnweightedConstraintOccurrence("multipleMatchesPerTeamPerDay", $m));
end

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 : Double() from accumulate(
            Hop($distance : distance),
            sum($distance)
        )
    then
        scoreHolder.setSoftConstraintsBroken($totalDistance.intValue());
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy