Package org.jacop.search
Class SimpleSolutionListener<T extends Var>
- java.lang.Object
-
- org.jacop.search.SimpleSolutionListener<T>
-
- Type Parameters:
T- type of variable being used in search.
- All Implemented Interfaces:
SolutionListener<T>
- Direct Known Subclasses:
CPvizNetworkFlow.NetListener,CrossWord.PrintListener,OneSolution,Optimize.ResultListener,PrintOutListener,RestartSearch.CostListener,SimpleImprovementSearch.CostListener,Solve.CostListener
public class SimpleSolutionListener<T extends Var> extends java.lang.Object implements SolutionListener<T>
It defines a simple solution listener which should be used if some basic functionality of search when a solution is encountered are required.- Version:
- 4.8
-
-
Field Summary
Fields Modifier and Type Field Description (package private) booleanalwaysUpdateToMostRecentSolutionSolutionListener<T>[]childrenSolutionListenersIt contains children of the solution listener.private static booleandebugIt specifies if the debugging information should be printed.protected intnoSolutionsSolutionListener<? extends Var>parentSolutionListenerIf this search is a slave search than each solution within this search must be connected to a solution of the master search.int[]parentSolutionNoIf this search is a slave search than each solution within this search must be connected to a solution of the master search.(package private) booleanrecordSolutionsintsolutionLimitIt specifies the number of solutions we want to find.Domain[][]solutionsT[]varsIt is executed right after consistency of the current search node.
-
Constructor Summary
Constructors Constructor Description SimpleSolutionListener()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanassignSolution(Store store)It assigns the last found solution to the store.booleanassignSolution(Store store, int number)It imposes the constraints, so the last found solution is enforced.booleanexecuteAfterSolution(Search<T> search, SelectChoicePoint<T> select)It is executed by search after a solution is found.intfindSolutionMatchingParent(int parentNo)For a given master solution finds any solution within that listener which matches the master solution.intgetParentSolution(int childSolutionNo)Domain[]getSolution(int no)It returns the solution number no.Domain[][]getSolutions()It returns all solutions.T[]getVariables()It returns null if no solution was recorded, or the variables for which the solution(s) was recorded.booleanisRecordingSolutions()It specifies if the solution listener is recording solutions or not.voidprintAllSolutions()It prints all the solutions.voidrecordSolution()It records a solution.voidrecordSolutions(boolean status)It records all solutions so they can be later retrieved and used.PrimitiveConstraint[]returnSolution()It returns a collection of constraints which represent the last found solution.PrimitiveConstraint[]returnSolution(int number)It returns the solution with the given number (value 0 denotes the first solution) as a set of primitive constraints.voidsearchAll(boolean status)It searches for all solutions, but they do not have to be recorded as this is decided by another parameter.voidsetChildrenListeners(SolutionListener<T> child)It sets the child listener for this solution listener.voidsetChildrenListeners(SolutionListener<T>[] children)It sets the children listeners for this solution listener.voidsetParentSolutionListener(SolutionListener<? extends Var> parent)It allows to inform sub-search of what is the current number of the solution in master search.voidsetSolutionLimit(int limit)It sets the solution limit.voidsetSolutionsNo(int no)voidsetVariables(T[] vs)booleansolutionLimitReached()It checks if the sufficient number of solutions was found.intsolutionsNo()It returns number of solutions found while using this choice point selector.java.lang.StringtoString()It returns the string representation of the last solution.
-
-
-
Field Detail
-
debug
private static final boolean debug
It specifies if the debugging information should be printed.- See Also:
- Constant Field Values
-
vars
public T extends Var[] vars
It is executed right after consistency of the current search node. The return code specifies if the search should continue or exit.
-
alwaysUpdateToMostRecentSolution
boolean alwaysUpdateToMostRecentSolution
-
solutionLimit
public int solutionLimit
It specifies the number of solutions we want to find.
-
noSolutions
protected int noSolutions
-
recordSolutions
boolean recordSolutions
-
solutions
public Domain[][] solutions
-
parentSolutionListener
public SolutionListener<? extends Var> parentSolutionListener
If this search is a slave search than each solution within this search must be connected to a solution of the master search. The parentSolutionListener is a solution listener of the master search.
-
parentSolutionNo
public int[] parentSolutionNo
If this search is a slave search than each solution within this search must be connected to a solution of the master search. This array stores for each solution recorded by this solution listener the solution number of the master slave.
-
childrenSolutionListeners
public SolutionListener<T extends Var>[] childrenSolutionListeners
It contains children of the solution listener.
-
-
Method Detail
-
getVariables
public T[] getVariables()
It returns null if no solution was recorded, or the variables for which the solution(s) was recorded.- Specified by:
getVariablesin interfaceSolutionListener<T extends Var>- Returns:
- list of variables
-
solutionLimitReached
public boolean solutionLimitReached()
Description copied from interface:SolutionListenerIt checks if the sufficient number of solutions was found.- Specified by:
solutionLimitReachedin interfaceSolutionListener<T extends Var>- Returns:
- true if the limit of found solutions has been reached.
-
setSolutionLimit
public void setSolutionLimit(int limit)
Description copied from interface:SolutionListenerIt sets the solution limit.- Specified by:
setSolutionLimitin interfaceSolutionListener<T extends Var>- Parameters:
limit- the maximal number of solutions we are interested in.
-
setParentSolutionListener
public void setParentSolutionListener(SolutionListener<? extends Var> parent)
Description copied from interface:SolutionListenerIt allows to inform sub-search of what is the current number of the solution in master search.- Specified by:
setParentSolutionListenerin interfaceSolutionListener<T extends Var>- Parameters:
parent- solution listener used by a master search.
-
getSolutions
public Domain[][] getSolutions()
Description copied from interface:SolutionListenerIt returns all solutions. Each solution is in a separate array.- Specified by:
getSolutionsin interfaceSolutionListener<T extends Var>- Returns:
- first dimension is indexed by solution, second dimension is indexed by a variable.
-
getSolution
public Domain[] getSolution(int no)
It returns the solution number no. The first solution has an index 1.- Specified by:
getSolutionin interfaceSolutionListener<T extends Var>- Parameters:
no- it obtains the solution with a given index.- Returns:
- array containing assignments to search variables.
-
solutionsNo
public int solutionsNo()
It returns number of solutions found while using this choice point selector.- Specified by:
solutionsNoin interfaceSolutionListener<T extends Var>- Returns:
- the number of solutions.
-
setSolutionsNo
public void setSolutionsNo(int no)
-
recordSolutions
public void recordSolutions(boolean status)
It records all solutions so they can be later retrieved and used.- Specified by:
recordSolutionsin interfaceSolutionListener<T extends Var>- Parameters:
status- true if we are interested in recording all solutions, false otherwise.
-
searchAll
public void searchAll(boolean status)
It searches for all solutions, but they do not have to be recorded as this is decided by another parameter.- Specified by:
searchAllin interfaceSolutionListener<T extends Var>- Parameters:
status- true if we are interested in search for all solutions, false otherwise.
-
recordSolution
public void recordSolution()
It records a solution. It uses the current value of the search variables (they must be all grounded) as well as the current number of the solution in master search (if there is one).
-
executeAfterSolution
public boolean executeAfterSolution(Search<T> search, SelectChoicePoint<T> select)
Description copied from interface:SolutionListenerIt is executed by search after a solution is found.- Specified by:
executeAfterSolutionin interfaceSolutionListener<T extends Var>- Parameters:
search- the search which have found a solution.select- the select choice point heuristic- Returns:
- false forces the search to keep looking for a solution, true then the search will accept a solution.
-
assignSolution
public boolean assignSolution(Store store)
It assigns the last found solution to the store. If the function returns false that means that for some reason the solution which was supposed to be a solution is not. It can be caused by a number of issues, starting with wrongly implemented plugins, wrongly implemented consistency or satisfied function of the constraint.- Parameters:
store- the store in the context of which the search took place.- Returns:
- true if the store is consistent after assigning a solution, false otherwise.
-
assignSolution
public boolean assignSolution(Store store, int number)
Description copied from interface:SolutionListenerIt imposes the constraints, so the last found solution is enforced.- Specified by:
assignSolutionin interfaceSolutionListener<T extends Var>- Parameters:
store- store in which the solution is enforced.number- the number of the solution to be enforced.- Returns:
- true if the store is consistent after enforcing a solution, false otherwise.
-
setVariables
public void setVariables(T[] vs)
-
toString
public java.lang.String toString()
Description copied from interface:SolutionListenerIt returns the string representation of the last solution.- Specified by:
toStringin interfaceSolutionListener<T extends Var>- Overrides:
toStringin classjava.lang.Object
-
returnSolution
public PrimitiveConstraint[] returnSolution()
Description copied from interface:SolutionListenerIt returns a collection of constraints which represent the last found solution.- Specified by:
returnSolutionin interfaceSolutionListener<T extends Var>- Returns:
- the set of constraints which imposed enforce the last found solution.
-
returnSolution
public PrimitiveConstraint[] returnSolution(int number)
It returns the solution with the given number (value 0 denotes the first solution) as a set of primitive constraints.- Parameters:
number- the solution number (0 denotes the first solution).- Returns:
- set of primitive constraint which if imposed will enforce given solution.
-
findSolutionMatchingParent
public int findSolutionMatchingParent(int parentNo)
Description copied from interface:SolutionListenerFor a given master solution finds any solution within that listener which matches the master solution.- Specified by:
findSolutionMatchingParentin interfaceSolutionListener<T extends Var>- Parameters:
parentNo- solution number of the parent for which we search matching solution.- Returns:
- -1 if no solution was found, otherwise the index of the solution.
-
setChildrenListeners
public void setChildrenListeners(SolutionListener<T>[] children)
Description copied from interface:SolutionListenerIt sets the children listeners for this solution listener.- Specified by:
setChildrenListenersin interfaceSolutionListener<T extends Var>- Parameters:
children- an array containing children listeners.
-
setChildrenListeners
public void setChildrenListeners(SolutionListener<T> child)
Description copied from interface:SolutionListenerIt sets the child listener for this solution listener.- Specified by:
setChildrenListenersin interfaceSolutionListener<T extends Var>- Parameters:
child- the child listener.
-
isRecordingSolutions
public boolean isRecordingSolutions()
Description copied from interface:SolutionListenerIt specifies if the solution listener is recording solutions or not.- Specified by:
isRecordingSolutionsin interfaceSolutionListener<T extends Var>- Returns:
- true if all solutions are recorded, false if only the last one is recorded.
-
printAllSolutions
public void printAllSolutions()
Description copied from interface:SolutionListenerIt prints all the solutions.- Specified by:
printAllSolutionsin interfaceSolutionListener<T extends Var>
-
getParentSolution
public int getParentSolution(int childSolutionNo)
- Specified by:
getParentSolutionin interfaceSolutionListener<T extends Var>
-
-