org.optaplanner.constraint.streams.bavet.BavetConstraintSessionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optaplanner-constraint-streams-bavet Show documentation
Show all versions of optaplanner-constraint-streams-bavet Show documentation
OptaPlanner solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains implementation of Constraint streams (Bavet).
package org.optaplanner.constraint.streams.bavet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.optaplanner.constraint.streams.bavet.common.AbstractNode;
import org.optaplanner.constraint.streams.bavet.common.BavetAbstractConstraintStream;
import org.optaplanner.constraint.streams.bavet.common.NodeBuildHelper;
import org.optaplanner.constraint.streams.bavet.uni.ForEachUniNode;
import org.optaplanner.constraint.streams.common.inliner.AbstractScoreInliner;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.api.score.stream.Constraint;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.score.definition.ScoreDefinition;
public final class BavetConstraintSessionFactory> {
private final SolutionDescriptor solutionDescriptor;
private final List> constraintList;
public BavetConstraintSessionFactory(SolutionDescriptor solutionDescriptor,
List> constraintList) {
this.solutionDescriptor = solutionDescriptor;
this.constraintList = constraintList;
}
// ************************************************************************
// Node creation
// ************************************************************************
public BavetConstraintSession buildSession(boolean constraintMatchEnabled,
Solution_ workingSolution) {
ScoreDefinition scoreDefinition = solutionDescriptor.getScoreDefinition();
AbstractScoreInliner scoreInliner = AbstractScoreInliner.buildScoreInliner(scoreDefinition,
constraintMatchEnabled);
Score_ zeroScore = scoreDefinition.getZeroScore();
Set> constraintStreamSet = new LinkedHashSet<>();
Map constraintWeightMap = new HashMap<>(constraintList.size());
for (BavetConstraint constraint : constraintList) {
Score_ constraintWeight = constraint.extractConstraintWeight(workingSolution);
// Filter out nodes that only lead to constraints with zero weight.
// Note: Node sharing happens earlier, in BavetConstraintFactory#share(Stream_).
if (!constraintWeight.equals(zeroScore)) {
// Relies on BavetConstraintFactory#share(Stream_) occurring for all constraint stream instances
// to ensure there are no 2 equal ConstraintStream instances (with different child stream lists).
constraint.collectActiveConstraintStreams(constraintStreamSet);
constraintWeightMap.put(constraint, constraintWeight);
}
}
NodeBuildHelper buildHelper = new NodeBuildHelper<>(constraintStreamSet, constraintWeightMap, scoreInliner);
// Build constraintStreamSet in reverse order to create downstream nodes first
// so every node only has final variables (some of which have downstream node method references).
List> reversedConstraintStreamList = new ArrayList<>(constraintStreamSet);
Collections.reverse(reversedConstraintStreamList);
for (BavetAbstractConstraintStream constraintStream : reversedConstraintStreamList) {
constraintStream.buildNode(buildHelper);
}
List nodeList = buildHelper.destroyAndGetNodeList();
Map, ForEachUniNode
© 2015 - 2025 Weber Informatics LLC | Privacy Policy