
com.thesett.aima.search.impl.BaseQueueSearch Maven / Gradle / Ivy
Show all versions of search Show documentation
/*
* Copyright The Sett Ltd, 2005 to 2014.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.thesett.aima.search.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Queue;
import com.thesett.aima.search.QueueBasedSearchMethod;
import com.thesett.aima.search.RepeatedStateFilter;
import com.thesett.aima.search.SearchNode;
import com.thesett.aima.search.SearchNotExhaustiveException;
import com.thesett.aima.search.Traversable;
import com.thesett.aima.search.spi.QueueSearchAlgorithm;
import com.thesett.aima.search.spi.QueueSearchState;
import com.thesett.common.util.logic.UnaryPredicate;
/**
* BaseQueueSearch provides a base implementation for deriving new searches from by providing different queue
* implementations. For example using a LIFO queue generates depth first search and a FIFO queue generates breadth
* first. Heuristic searches can easily be implemented by using priority queues with ordering based on a heuristic
* evaluation. Uniform cost search can be implement by using a priority queue with an ordering based on the path cost.
*
* The actual search algorithm is factored out of this class and provided by classes that implement the
* {@link QueueSearchAlgorithm} interface. Roughly speaking all queue search algorithms are quite similar; they procede
* by extracting the head item from the queue buffer and testing to see if it is a goal state, if it is then the search
* terminates on that {@link SearchNode}, otherwise the accessible states from the present search node are expanded into
* the queue and the search continues with the next element. See the search algorithm implementations for the finer
* details of each. This class uses the {@link MaxStepsAlgorithm} by default.
*
*
Depending on the queue implementation the search nodes will be extracted from it in different orders and this is
* what determines the order in which elements are examined by the search algorithms. The queue implementation must be
* provided by concrete sub-classes of this base class.
*
*
Different kinds of search node can be used to capture different information about the states being searched over
* and this in turn can be used by the queue implementations and ordering functions to control the way in which the
* search proceeds. The most obvious example is to extend the basic search node with a heuristic one that computes
* heuristics for the states and then to provide an ordering over heuristics (Greedy, or A-Star for example) to control
* the search.
*
*
As specified in the {@link QueueBasedSearchMethod} interface this class provides a way to plug in a repeated state
* filter.
*
*
These four aspects of the search routine, the basic algorithm, the search node type, the queue and the rejected
* state pool can all be combined independently of one another to provide an array of different queue based search
* techniques in a very flexible manner. It is necessary though to understand each of the components separately in order
* to be able to combine them together successfully. Implementations of many standard search routines are provided in
* this package by the combination of plug-in elements, consult the implementations for the details.
*
*
Generally speaking if the queue should become empty without a goal state being found then the search algorithm
* should return null and terminate. However, it should only do this if the search space is known to have been searched
* completely. Searches that terminate prematurely and know that there are still unsearched states to work with should
* terminate by throwing a {@link SearchNotExhaustiveException} or a specific sub-class of it. Returning with null must
* be reserved for the case where it is known that no solutions exist so there is no point in searching any further.
*
*
This class implements {@link QueueSearchState} interface. This is a call-back interface that provides a limited
* view onto this class to the {@link QueueSearchAlgorithm} implementations so that they can call its
* {@link #enqueueStartStates} method to initialize the queue ready for searching. This was done for the sake of hiding
* the full implementation of this class from the search algorithms.
*
*
CRC Card
* Responsibilities Collaborations
* Combine implementations of queue, search node, repeated state filter and basic algorithm into a search.
* {@link SearchNode}, {@link RepeatedStateFilter}, {@link QueueSearchAlgorithm}, {@link QueueSearchState}
* Set up the start states for a search.
* Set a maximum number of search steps allowable.
* Reset a search algorithm.
*
*
* @author Rupert Smith
*/
public abstract class BaseQueueSearch