(* :Title: Uninformed Search Algorithms *) (* :Authors: Raza Ahmed and Brian L. Evans *) (* :Summary: This contains the search algorithms which rely on the heuristics or the evaluation functions. *) (* :Context: HeuristicSearch`UninformedSearch` *) (* :PackageVersion: $Revision: 1.9 $ *) (* :Copyright: Copyright 1996 by Regents of the University of California Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. *) (* :History: *) (* :Keywords: *) (* :Source: *) (* :Warning: *) (* :Mathematica Version: 2.0 *) (* :Limitation: *) (* :Discussion: *) (* :Functions: *) (* B E G I N P A C K A G E *) BeginPackage[ "HeuristicSearch`UninformedSearch`", { "HeuristicSearch`GeneralSearch`" } ]; (* U S A G E I N F O R M A T I O N *) BreadthFirstSearch::usage = "BreadthFirstSearch[problem] returns a state satisfying the goal criteria after going down the search tree through the successor states top to bottom."; DepthFirstSearch::usage = "DepthFirstSearch[problem] returns a state satisfying the goal criteria after going down the search tree through the successor states bottom to top."; (* E N D U S A G E I N F O R M A T I O N *) Begin[ "`Private`" ] (* this is the part of the algorithm common to both breadth first and *) (* depth first search *) generalSearch[_, {}] = {}; generalSearch[combiner_, problem_, optionlist_] := Module[ {evaluationfunction, goal, goalFactor, keeplooping = True, listofrules, node = {}, nodes = {}, successorfunction, tempnode}, {goalFactor, evaluationfunction, successorfunction, listofrules} = { CostReductionFactor, EvaluationFunction, SuccessorFunction, TransformationRules} /. optionlist; goal = GoalFunction[problem, goalFactor, evaluationfunction]; AppendTo[nodes, problem]; While [ keeplooping, node = First[nodes]; nodes = Rest[nodes]; tempnode = successorfunction[node, listofrules] //. List -> IdentityFunction; If [ GoalTest[node, goal, evaluationfunction], keeplooping = False]; If [ node =!= tempnode, nodes = Flatten[ combiner[nodes, Flatten[successorfunction[node, listofrules]]]]]; If [ Length[nodes] == 0, keeplooping = False ] ]; node ] (* It does the mapping of the argument onto itself *) IdentityFunction[problem__] := problem; (* The algorithm uses the generalSearch to search through the tree *) (* Newly explored nodes are appended at the end of the search list *) BreadthFirstSearch[problem_, options___] := generalSearch[Append, problem, Join[{options}, Options[BreadthFirstSearch]]] Options[ BreadthFirstSearch ] = { CostReductionFactor -> 1.2, EvaluationFunction -> LeafCount, SuccessorFunction -> RuleBasedSuccessorFunction, TransformationRules -> {} }; (* The algorithm uses the generalSearch to search through the tree *) (* Newly explored nodes are appended at beginning of the search list *) DepthFirstSearch[problem_, options___] := generalSearch[Prepend, problem, Join[{options}, Options[DepthFirstSearch]]] Options[ DepthFirstSearch ] = { CostReductionFactor -> 1.2, EvaluationFunction -> LeafCount, SuccessorFunction -> RuleBasedSuccessorFunction, TransformationRules -> {} }; Protect[ BreadthFirstSearch, DepthFirstSearch ]; End[]; EndPackage[];