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

javax.constraints.impl.search.goal.GoalCheckMaxNumberOfSolutions Maven / Gradle / Ivy

Go to download

JCP Standard JSR331 “Java Constraint Programming API”. It is used for Modeling and Solving Constraint Satisfaction and Optimization Problems using Java and off-the-shelf Constraint/Linear Solvers

There is a newer version: 2.3.1
Show newest version
//=============================================
// J A V A  C O M M U N I T Y  P R O C E S S
// 
// J S R  3 3 1
// 
// Common Implementation
// 
//============================================= 
package javax.constraints.impl.search.goal;

import javax.constraints.Solver;

/**
 * This goal is used inside solveAll() to stop or continue the search.
 * It checks if the maximal number of solutions has been reached.
 * If yes, it returns null ("do nothing") and search will be stopped.
 * If no, it returns goalFail() and the search will be continued.
 */
public class GoalCheckMaxNumberOfSolutions extends Goal {

	public GoalCheckMaxNumberOfSolutions(Solver solver) {
		super(solver, "check max solutions");
	}

	public Goal execute() throws Exception {
		trace();
		SolverWithGoals solver = getSolver();
		int max = solver.getMaxNumberOfSolutions();
		if (max <= 0) {
			//solver.backtrack();
			return null;
		}
		if (solver.getSolutions() == null || solver.getNumberOfSolutions() == max) {
			solver.log("Search interrupted by the MaxNumberOfSolutions: " + max);
			solver.backtrack();
			return null; // finish search
		}
		//solver.backtrack();
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy