All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thesett.aima.search.util.Searches Maven / Gradle / Ivy

Go to download

Search code developed from 'Artificial Intelligence a Modern Approach', Prentice Hall.

There is a newer version: 0.9.97
Show newest version
/*
 * 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.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.thesett.aima.search.QueueBasedSearchMethod;
import com.thesett.aima.search.SearchMethod;
import com.thesett.aima.search.SearchNode;
import com.thesett.aima.search.SearchNotExhaustiveException;
import com.thesett.aima.search.Traversable;
import com.thesett.common.util.SequenceIterator;

/**
 * Searches provides static helper methods to simplify common operations using search methods.
 *
 * 

*
CRC Card
Responsibilities Collaborations *
Provide iterator over all solutions to a search space. {@link QueueBasedSearchMethod} *
* * @author Rupert Smith */ public class Searches { /** * Provides an iterator over a search method, that returns successive search solutions on demand. * * @param The traversable state type of the search. * @param method The search method to iterate over. * * @return An iterator over all solution states to the search. */ public static Iterator allSolutions(SearchMethod method) { //return new Filterator, T>(allSolutionNodes(method), new ExtractSearchNode()); // Take a final reference to the search method to use from within the inner class. final SearchMethod search = method; return new SequenceIterator() { /** * Generates the next element in the search. * * @return The next solution from the search if one is available, or null if the search is * complete. */ public T nextInSequence() { try { return search.search(); } catch (SearchNotExhaustiveException e) { // SearchNotExhaustiveException means that the search has completed within its designed parameters // without exhausting the search space. Consequently there are no more solutions to find, // the exception can be ignored and the sequence correctly terminated. e = null; return null; } } }; } /** * Finds the set of all goals of a search. * * @param The traversable state type of the search. * @param method The search method to find all goals of. * * @return A set of all goals found. */ public static Set setOf(SearchMethod method) { Set result = new HashSet(); findAll(result, method); return result; } /** * Finds a bag of all goals of a search. * * @param The traversable state type of the search. * @param method The search method to find all goals of. * * @return A bag of all goals found. */ public static Collection bagOf(SearchMethod method) { Collection result = new ArrayList(); findAll(result, method); return result; } /** * Provides an iterator over a search method, that returns successive search solutions on demand. * * @param The operator type of the search. * @param The traversable state type of the search. * @param method The search method to iterate over. * * @return An iterator over all solutions to the search. */ public static > Iterator> allSolutionPaths( QueueBasedSearchMethod method) { // Take a final reference to the search method to use from within the inner class. final QueueBasedSearchMethod search = method; return new SequenceIterator>() { /** * Generates the next element in the search. * * @return The next solution from the search if one is available, or null if the search is * complete. */ public SearchNode nextInSequence() { try { return search.findGoalPath(); } catch (SearchNotExhaustiveException e) { // SearchNotExhaustiveException means that the search has completed within its designed parameters // without exhausting the search space. Consequently there are no more solutions to find, // the exception can be ignored and the sequence correctly terminated. e = null; return null; } } }; } /** * Finals all solutions to a search and inserts them into the specified collection. * * @param The traversable state type of the search. * @param result The collection to build up the results in. * @param method The search to run. */ private static void findAll(Collection result, SearchMethod method) { for (Iterator i = allSolutions(method); i.hasNext();) { T nextSoltn = i.next(); result.add(nextSoltn); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy