core
Class BestFirstSearch<T>

java.lang.Object
  extended by core.Search<T>
      extended by core.DirectedSearch<T>
          extended by core.BestFirstSearch<T>
Type Parameters:
T - the specific type of all elements of the search domain.
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
AStarSearch, GreedySearch, UniformCostSearch

public abstract class BestFirstSearch<T>
extends DirectedSearch<T>

This subclass of DirectedSearch is the abstraction of all best first search algorithms like greedy search or A* search.
The basic idea behind this search algorithm is, to expand the promising states at first. To estimate how promising a state is a specific evaluation function f is used. Each subclass has a different implementation for this function, but all use the core.HeuristicProblem interface.
(For details on the evaluation function lock in to the concrete subclasses of BestFirstSearch.)

This search algorithm has two modes for evaluation of the states in the search space.
The first mode evaluates each state when the state is enqueued. (This is the default behavior.) The second mode evaluates each state when ever a state is popped from the queue. You can create a best first search with this behavior with the following code:
BestFirstSearch search = new MyBestFirstSearch(problem, true);
So what is the different between these two modes and when should which be used?
The first mode is only applicable if there are no two paths from the initial state to a state b, so that f(b) would evaluate to two different values. For examples look into the documentations of the subclasses. This simple constraint allows the search to use a java.util.PriorityQueue which grants high performance.
The second mode is applicable in all cases (also in cases where the evaluation of a state my change over time). But this generality costs a lot of performance during the search, because we have to use the util.SortedQueue instead.


If you want to implement your own best first search algorithm you only have to inherit from this class and implement the evaluation function f according to the following rules:
All subclasses should provide at least one constructor, where the user can decide whether to disable automatic duplicate handling and whether to update the states in the queue.
Note: No subclass of BestFirstSearch should hide on of these configurations to the user.

Author:
eden06
See Also:
GreedySearch, UniformCostSearch, AStarSearch

Field Summary
protected  HeuristicProblem<T> heuristicProblem
          holds a reference to the problem to be solved of the actual type
protected  java.util.Queue<T> queue
          holds the instance of the queue used during the search.
 
Fields inherited from class core.DirectedSearch
hash, problem
 
Fields inherited from class core.Search
neededSteps, result
 
Constructor Summary
BestFirstSearch(HeuristicProblem<T> problem)
          This method creates a new BestFirstSearch.
BestFirstSearch(HeuristicProblem<T> problem, boolean update)
          This method creates a new BestFirstSearch.
BestFirstSearch(HeuristicProblem<T> problem, boolean noHash, boolean update)
          This method creates a new BestFirstSearch.
 
Method Summary
protected  boolean add(T state)
          This method adds a new state to the queue.
protected  void clear()
          This method removes all elements from the queue.
protected  boolean empty()
          This method tells whether the queue is empty or not.
abstract  java.lang.Double f(T state)
          This method is the hook for evaluating states during insertion into the queue.
 HeuristicProblem<T> getProblem()
          This method returns the problem, with which this search has been created.
protected  T pop()
          This method returns the best state according to the function value.
 
Methods inherited from class core.DirectedSearch
branchedNodes, canPrepare, finalize, prepare, search
 
Methods inherited from class core.Search
getResult, initialize, initialized, neededSteps, run, running, stop
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

heuristicProblem

protected HeuristicProblem<T> heuristicProblem
holds a reference to the problem to be solved of the actual type


queue

protected java.util.Queue<T> queue
holds the instance of the queue used during the search.

Constructor Detail

BestFirstSearch

public BestFirstSearch(HeuristicProblem<T> problem)
This method creates a new BestFirstSearch. All search parameters will be set to the default values.

Parameters:
problem - the HeuristicProblem to be solved

BestFirstSearch

public BestFirstSearch(HeuristicProblem<T> problem,
                       boolean update)
This method creates a new BestFirstSearch. According to the given update flag the search will use either a queue which is able to update the ordering or not.
Note: The performance is significantly improved, if the queue must not update its elements.

Parameters:
problem - the HeuristicProblem to be solved
update - flag indicating whether the states in the queue should be updated or not.

BestFirstSearch

public BestFirstSearch(HeuristicProblem<T> problem,
                       boolean noHash,
                       boolean update)
This method creates a new BestFirstSearch. According to the given flags the search will turn off the internal duplicate handling mechanism and uses a queue which updates its elements or not.

 HeuristicProblem problem;
 Search s1,s2,s3,s4;
 s1 = new BestFirstSearch(problem,false,false); 
 s2 = new BestFirstSearch(problem,true ,false);
 s3 = new BestFirstSearch(problem,false,true );
 s4 = new BestFirstSearch(problem,true ,true );
 
s1 will be a search with duplicate handling and a queue without updates.
s2 will be a search without duplicate handling and a queue without updates.
s3 will be a search with duplicate handling and a queue which updates its elements.
s4 will be a search without duplicate handling and a queue which updates its elements.

Note: The performance is significantly improved, if the queue must not update its elements.

Parameters:
problem - the HeuristicProblem to be solved
noHash - flag indicating that duplicates should not be handled
update - flag indicating whether the states in the queue should be updated or not.
Method Detail

getProblem

public HeuristicProblem<T> getProblem()
Description copied from class: DirectedSearch
This method returns the problem, with which this search has been created.

Overrides:
getProblem in class DirectedSearch<T>
Returns:
the problem to be solved
See Also:
DirectedSearch.getProblem()

add

protected boolean add(T state)
This method adds a new state to the queue.

Specified by:
add in class DirectedSearch<T>
Parameters:
state - to be added to the queue
Returns:
true only if this state has been added.
See Also:
DirectedSearch.add(java.lang.Object)

clear

protected void clear()
This method removes all elements from the queue.

Specified by:
clear in class DirectedSearch<T>
See Also:
DirectedSearch.clear()

empty

protected boolean empty()
This method tells whether the queue is empty or not.

Specified by:
empty in class DirectedSearch<T>
Returns:
true only if the queue is empty.
See Also:
DirectedSearch.empty()

pop

protected T pop()
This method returns the best state according to the function value.
Mathematically speaking if pop() returned a state a then there is no state b in the queue with f(b)<f(a)

Specified by:
pop in class DirectedSearch<T>
Returns:
the first element of the queue
See Also:
DirectedSearch.pop()

f

public abstract java.lang.Double f(T state)
This method is the hook for evaluating states during insertion into the queue.
This method should return a value indicating how good a state is.
This means if state a is better than state b then f(a) should be smaller f(b).

Parameters:
state - to be evaluated
Returns:
the function value