basic
Class IterativeDeepeningSearch<T>

java.lang.Object
  extended by core.Search<T>
      extended by basic.IterativeDeepeningSearch<T>
Type Parameters:
T - the specific type of all elements in the search domain.
All Implemented Interfaces:
java.lang.Runnable

public class IterativeDeepeningSearch<T>
extends Search<T>

The iterative deepening search algorithm is a blind search able to solve any problem implementing the core.TreeProblem interface. IterativeDeepeningSearch is a subclass of core.Search because it uses recursion rather then the general search algorithm employed in core.DirectedSearch.

The iterative deepening search starts a depth limited search with a depth limit of zero and increases the depth limit slowly until a goal has been found or the maximum depth is reached.
This strategy grants completeness, a time complexity of O(b^n) and a space complexity of O(b*n) (where b is the branching factor and n is the depth of the solution).
Further more the algorithm can find an optimal solution only if the path costs between two states are constant or increase by a function of the depth.
In fact iterative deepening search is the best trade of between breadth first and depth limited search for medium and large search domains.

To avoid duplicate states during the search a HashSet is used, which tries to hash all expanded states in order to inhibit further expansion. In detail there are two important rules for states of type T which allow proper hashing:

In some cases it is necessary to turn of the implicit duplicate handling mechanism. For example there are no duplicate states in the search domain, or special knowledge about the search domain makes customized duplicate handling more feasible.
The following example shows how to implement a duplicate handling mechanism and how to disable the implicit duplicate handling mechanism:
 
 class MyTreeProblem<T> extends TreeProblem<T>{
        private Set<Integer> visited=new HashSet<Integer>();
     
        public List expand(T state){
                List result = new LinkedList();
                for (T newState : super.expand(state) ){
                        if ( visited.add( newState.hashCode() ) ){
                                result.add(newState);
                        }
                }
                return result;
        }
     
 } 
 
 // ...
 
 TreeProblem problem = new MyTreeProblem();
 //Turn of the implicit duplicate handling mechanism
 Search search = IterativeDeepeningSearch(problem, 1000, false);
 

Author:
eden06

Field Summary
protected  java.util.Map<T,java.lang.Integer> hash
          holds the map used for duplicate handling
protected  int maximumDepth
          holds the maximal number of iterations allowed
protected  TreeProblem<T> problem
          holds a reference to the problem to be solved
 
Fields inherited from class core.Search
neededSteps, result
 
Constructor Summary
IterativeDeepeningSearch(TreeProblem<T> problem)
          Create a new IterativeDeepeningSearch with the given problem.
IterativeDeepeningSearch(TreeProblem<T> problem, int maximumDepth)
          Create a new IterativeDeepeningSearch with the given problem, which needs at most the given iterations.
IterativeDeepeningSearch(TreeProblem<T> problem, int maximumDepth, boolean noHash)
          Create a new IterativeDeepeningSearch with the given problem, which needs at most the given iterations and disabled hashing if the noHash flag is true.
 
Method Summary
protected  boolean canPrepare()
          This method checks if the given problem returns a valid initial state.
 int getMaximumDepth()
          Returns the maximal depth to which the search tree will be explored.
 TreeProblem<T> getProblem()
          This method returns the problem, with which this search has been created.
protected  void prepare()
          This method does nothing.
protected  void search()
          This method looks for a goal by iteratively calling a recursive depth limited search with increasing depth limits.
 void setMaximumDepth(int maximumDepth)
          Changes the maximal number of iterations to the given value.
 
Methods inherited from class core.Search
finalize, 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

problem

protected TreeProblem<T> problem
holds a reference to the problem to be solved


maximumDepth

protected int maximumDepth
holds the maximal number of iterations allowed


hash

protected java.util.Map<T,java.lang.Integer> hash
holds the map used for duplicate handling

Constructor Detail

IterativeDeepeningSearch

public IterativeDeepeningSearch(TreeProblem<T> problem)
Create a new IterativeDeepeningSearch with the given problem. This search will explore at most up to a depth of 1000 and uses implicit duplicate handling.

Parameters:
problem - the core.Poblem to be solved

IterativeDeepeningSearch

public IterativeDeepeningSearch(TreeProblem<T> problem,
                                int maximumDepth)
Create a new IterativeDeepeningSearch with the given problem, which needs at most the given iterations. This search will use implicit duplicate handling.

Parameters:
problem - the core.Poblem to be solved
maximumDepth - the maximum exploration depth for the overall search

IterativeDeepeningSearch

public IterativeDeepeningSearch(TreeProblem<T> problem,
                                int maximumDepth,
                                boolean noHash)
Create a new IterativeDeepeningSearch with the given problem, which needs at most the given iterations and disabled hashing if the noHash flag is true.

Parameters:
problem - the core.Poblem to be solved
maximumDepth - the maximum exploration depth for the overall search
noHash - flag indicating that duplicates should not be handled
Method Detail

canPrepare

protected boolean canPrepare()
This method checks if the given problem returns a valid initial state.

Specified by:
canPrepare in class Search<T>
Returns:
true if the search can be prepared
See Also:
Search.canPrepare()

prepare

protected void prepare()
This method does nothing.

Specified by:
prepare in class Search<T>
See Also:
Search.prepare()

search

protected final void search()
This method looks for a goal by iteratively calling a recursive depth limited search with increasing depth limits. It terminates when a goal has been found or the number of expanded nodes does not increase anymore.

Specified by:
search in class Search<T>
See Also:
Search.search()

getProblem

public TreeProblem<T> getProblem()
This method returns the problem, with which this search has been created.

Returns:
the problem to be solved

getMaximumDepth

public int getMaximumDepth()
Returns the maximal depth to which the search tree will be explored.

Returns:
the maximum amount of iterations

setMaximumDepth

public void setMaximumDepth(int maximumDepth)
Changes the maximal number of iterations to the given value.

Parameters:
maximumDepth - the new maximum number of iterations