Package search
Class GraphSearcher<State,Node extends SearchTreeNode<Node,State>,Frontier extends FrontierStructure<Node>>
java.lang.Object
search.GraphSearcher<State,Node,Frontier>
- Type Parameters:
State
- Type representing elements of the search space.Node
- Type representing nodes in the search tree. Each node typically contains a reference to a State element.Frontier
- Type representing the (entire) search frontier (open set).
- Direct Known Subclasses:
BreadthFirstSearcher
,PriorityQueueSearcher
public class GraphSearcher<State,Node extends SearchTreeNode<Node,State>,Frontier extends FrontierStructure<Node>> extends Object
Topmost class encapsulating graph search. The
search
method
implements the Graph-Search algorithm of Russell and Norvig (2nd
ed., Figure 3.7, p. 77), with customizable behavior provided as
constructor arguments.-
Constructor Summary
Constructors Constructor Description GraphSearcher(Supplier<GoalChecker<Node>> goalCheckerFactory, Supplier<? extends Frontier> frontierFactory, Function<Frontier,ExploredSet<Node>> exploredSetFactory, Function<State,Node> initializer)
The constructor parameters encode the particlar behavior which distinguishes different search algorithms. -
Method Summary
Modifier and Type Method Description void
debugExpansion(Node node)
This method prints a debugging message when a tree node is generated from another node extracted from the frontier for expansion.void
debugFrontier(Frontier f)
Print debugging information about the frontier.void
debugFrontierAddition(Node node)
This method prints a debugging message when a tree node is added to the frontier.void
debugFrontierExhausted(GoalChecker<Node> checker)
This method prints a debugging message when the frontier is emptied (which is usually an error situation).void
debugFrontierNonaddition(Node node)
This method prints a debugging message when a tree node is not added to the frontier.void
debugFrontierRemoval(Node node)
This method prints a debugging message when a tree node is removed from the frontier for expansion.void
debugGoalFound(Node node)
void
debugInitialNode(Node node)
This method prints a debugging message about the initial tree node of a search.boolean
getDebug()
Returns whether execution of thesearch(State)
method is printing debugging messages.long
getLastAddedToFrontier()
Returns the number of frontier nodes which were added to the last search.long
getLastExpandedFromFrontier()
Returns the number of frontier nodes which were expanded in the last search.long
getLastNotAddedToFrontier()
Returns the number of frontier nodes which were generated from an expanded node, but not added to the last search.long
getLastUnexpandedInFrontier()
Returns the number of frontier nodes which were expanded in the last search.Node
search(State initial)
Executes a search beginning from a particular search space element.void
setDebug(boolean debug)
Controls whether execution of thesearch(State)
method should print debugging messages.boolean
solvable(State initial)
Convenience method for when we care only about whether a solution exists, and not what it is.
-
Constructor Details
-
GraphSearcher
public GraphSearcher(Supplier<GoalChecker<Node>> goalCheckerFactory, Supplier<? extends Frontier> frontierFactory, Function<Frontier,ExploredSet<Node>> exploredSetFactory, Function<State,Node> initializer)The constructor parameters encode the particlar behavior which distinguishes different search algorithms.- Parameters:
goalCheckerFactory
- Theget
method of this object must return a predicate on tree nodes used to tell if they are goal nodes.frontierFactory
- Theget
method of this object returns a new, empty Frontier instance.exploredSetFactory
- Structure used to manage adding elements to the frontier, in particular for avoiing duplication.initializer
- Creates an initial tree node from a search space element.
-
-
Method Details
-
search
Executes a search beginning from a particular search space element.- Parameters:
initial
- The starting element- Returns:
- The tree node corresponding to a goal element of the search space which is returned from this search
- Throws:
SearchFailureException
- When the search does not lead to a goal state
-
solvable
Convenience method for when we care only about whether a solution exists, and not what it is.- Parameters:
initial
- The starting element- Returns:
- true if
search(State)
would return a final node with the same initial state, otherwise false
-
debugInitialNode
This method prints a debugging message about the initial tree node of a search.- Parameters:
node
- The tree node in question.
-
debugFrontierRemoval
This method prints a debugging message when a tree node is removed from the frontier for expansion.- Parameters:
node
- The tree node in question.
-
debugExpansion
This method prints a debugging message when a tree node is generated from another node extracted from the frontier for expansion.- Parameters:
node
- The tree node in question.
-
debugFrontierAddition
This method prints a debugging message when a tree node is added to the frontier.- Parameters:
node
- The tree node in question. The default message printed in the version of this method defined in this class does not actually use this argument, since the tree node is already printed in the default version of thedebugExpansion(Node)
method.
-
debugFrontierNonaddition
This method prints a debugging message when a tree node is not added to the frontier.- Parameters:
node
- The tree node in question
-
debugFrontierExhausted
This method prints a debugging message when the frontier is emptied (which is usually an error situation).- Parameters:
checker
- TheGoalChecker
instance used in this search. This instance may be storing expanded nodes (to avoid revisiting them), and so may have additional information we wish to view. However the default implementation of this method in this parent class does not use this argument.
-
debugGoalFound
-
debugFrontier
Print debugging information about the frontier. By default, invokesdebugDisplayFrontier
. -
getLastAddedToFrontier
public long getLastAddedToFrontier()Returns the number of frontier nodes which were added to the last search.- Returns:
- -1 if no search has been executed
-
getLastNotAddedToFrontier
public long getLastNotAddedToFrontier()Returns the number of frontier nodes which were generated from an expanded node, but not added to the last search.- Returns:
- -1 if no search has been executed
-
getLastExpandedFromFrontier
public long getLastExpandedFromFrontier()Returns the number of frontier nodes which were expanded in the last search.- Returns:
- -1 if no search has been executed
-
getLastUnexpandedInFrontier
public long getLastUnexpandedInFrontier()Returns the number of frontier nodes which were expanded in the last search.- Returns:
- -1 if no search has been executed
-
setDebug
public void setDebug(boolean debug)Controls whether execution of thesearch(State)
method should print debugging messages. -
getDebug
public boolean getDebug()Returns whether execution of thesearch(State)
method is printing debugging messages.
-