org.drools.planner.core.heuristic.selector.variable.PlanningValueSelector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of drools-planner-core Show documentation
Show all versions of drools-planner-core Show documentation
Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule
engine powered score calculation. This is the drools-planner-core module which contains metaheuristic
algorithms.
/*
* Copyright 2011 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.core.heuristic.selector.variable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.drools.planner.core.domain.variable.PlanningVariableDescriptor;
import org.drools.planner.core.domain.variable.PlanningValueSorter;
import org.drools.planner.core.phase.AbstractSolverPhaseScope;
import org.drools.planner.core.phase.event.SolverPhaseLifecycleListenerAdapter;
import org.drools.planner.core.score.director.ScoreDirector;
/**
* Determines the order in which the planning values of 1 planning entity class are selected for an algorithm
*/
public class PlanningValueSelector extends SolverPhaseLifecycleListenerAdapter {
private PlanningVariableDescriptor planningVariableDescriptor;
private PlanningValueSelectionOrder selectionOrder = PlanningValueSelectionOrder.ORIGINAL;
private PlanningValueSelectionPromotion selectionPromotion = PlanningValueSelectionPromotion.NONE; // TODO
private boolean roundRobinSelection = false; // TODO
private ScoreDirector scoreDirector;
private Random workingRandom;
private Collection cachedPlanningValues = null;
public PlanningValueSelector(PlanningVariableDescriptor planningVariableDescriptor) {
this.planningVariableDescriptor = planningVariableDescriptor;
}
public void setSelectionOrder(PlanningValueSelectionOrder selectionOrder) {
this.selectionOrder = selectionOrder;
}
public void setSelectionPromotion(PlanningValueSelectionPromotion selectionPromotion) {
this.selectionPromotion = selectionPromotion;
}
public void setRoundRobinSelection(boolean roundRobinSelection) {
this.roundRobinSelection = roundRobinSelection;
}
// ************************************************************************
// Worker methods
// ************************************************************************
@Override
public void phaseStarted(AbstractSolverPhaseScope solverPhaseScope) {
validateConfiguration();
scoreDirector = solverPhaseScope.getScoreDirector();
workingRandom = solverPhaseScope.getWorkingRandom();
initSelectedPlanningValueList(solverPhaseScope);
}
private void validateConfiguration() {
if (selectionOrder == PlanningValueSelectionOrder.INCREASING_STRENGTH) {
PlanningValueSorter valueSorter = planningVariableDescriptor.getValueSorter();
if (!valueSorter.isSortStrengthSupported()) {
throw new IllegalStateException("The selectionOrder (" + selectionOrder
+ ") can not be used on PlanningEntity ("
+ planningVariableDescriptor.getPlanningEntityDescriptor().getPlanningEntityClass().getName()
+ ")'s planningVariable (" + planningVariableDescriptor.getVariableName()
+ ") that has no support for strength sorting. Check the @PlanningVariable annotation.");
}
}
}
private void initSelectedPlanningValueList(AbstractSolverPhaseScope solverPhaseScope) {
if (planningVariableDescriptor.isPlanningValuesCacheable()) {
Collection planningValues = planningVariableDescriptor.extractPlanningValues(
solverPhaseScope.getWorkingSolution(), null);
cachedPlanningValues = applySelectionOrder(planningValues);
} else {
cachedPlanningValues = null;
}
}
@Override
public void phaseEnded(AbstractSolverPhaseScope solverPhaseScope) {
cachedPlanningValues = null;
}
public Iterator iterator(Object planningEntity) {
if (cachedPlanningValues != null) {
return cachedPlanningValues.iterator();
} else {
Collection planningValues = planningVariableDescriptor.extractPlanningValues(
scoreDirector.getWorkingSolution(), planningEntity);
planningValues = applySelectionOrder(planningValues);
return planningValues.iterator();
}
}
private Collection applySelectionOrder(Collection workingPlanningValues) {
switch (selectionOrder) {
case ORIGINAL:
return workingPlanningValues;
case RANDOM:
List