public void TestBestFirstGraphSearch() { int size = 9; int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); NPuzzleState <int[]> initial = NPuzzleUtils.GenerateInitState(size); Assert.True(NPuzzleUtils.AcceptableState(initial.State)); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> npuzzle = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initial); Assert.AreEqual(npuzzle.InitialState, initial); Assert.AreEqual(npuzzle.GoalState, goal); //Heuristics.HeuristicFunction<NPuzzleState<int[]>,int, int> handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; try { Assert.AreEqual(npuzzle.InitialState, initial); // Search.BestFirstGraphSearch<int[],int,int> bfgs = new Search.BestFirstGraphSearch<int[],int,int>(npuzzle, handler); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> initialNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(npuzzle.InitialState); // Search.PriorityQueue<int, SearchTreeNode.Node<NPuzzleState<int[]>,int,int>> frontier = new Search.PriorityQueue<int, SearchTreeNode.Node<NPuzzleState<int[]>,int,int>>(); Search.BestFirstGraphSearch <NPuzzleState <int[]>, int, int> bfgs = new Search.BestFirstGraphSearch <NPuzzleState <int[]>, int, int>(npuzzle, handler); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = bfgs.Search(); List <int> solution = node.Solution(); Console.WriteLine("Printing solution:"); solution.ForEach(delegate(int a) { Console.Write("{0} ", a); }); } catch (NPuzzleUtils.InvalidProblemException ex) { System.Console.WriteLine("There is an InvalidProblemException here doode"); System.Console.WriteLine(ex.Message); throw ex; } catch (NPuzzleUtils.InvalidProblemPropertyException ex) { throw ex; } catch (System.NullReferenceException ex) { Console.WriteLine(ex); } }
// private Heuristics.Heurfun<C, Node<E,A,C>> hf; // private PriorityQueue<C, Node<E,A,C>> frontier; // //! TODO: Is there a better way to include the use //! of a heuristic function for AStar graph searches? //! Generally, it seems like it would be preferable //! to pass in a heuristic function, and add the //! result of the heuristic function to the //! pathCost of reaching the node from the root //! node, like: // //! delegate (Node<E,A,C> node) //! { //! return node.pathCost + hf(node); //! } // //! Problem is, the compiler throws an error because //! both values are of type C, and there is no way //! to add such values at this point in time. Generally //! C should represent a numerical value of some kind //! , and hence the addition operator should work. //! But the compiler doesn't know this. So what type //! restriction can we impose on C? // //! For now, simply /*! * * * @param {Heuristics.Heurfun<Cost, Node<State, Action, Cost>>} hf - a heuristic function which */ public AStarGraphSearch(AbstractProblem <E, A, C> p, Heuristics.Heurfun <C, Node <E, A, C> > hf) : base(p, hf) { }
// private Heuristics.Heurfun<C, Node<E, A, C>> heurfun; //private Heuristics.HeuristicFunction<E,A,C> heurfun; public BestFirstGraphSearch(AbstractProblem <E, A, C> p, Heuristics.Heurfun <C, Node <E, A, C> > hf /*, PriorityQueue<C, Node<E,A,C>> f*/) { frontier = new PriorityQueue <C, Node <E, A, C> >(hf); // f problem = p; //heurfun = hf; }
public PriorityQueue(Heuristics.Heurfun <Key, Value> hf) { frontier = new SortedList <Key, List <Value> >(); keys = frontier.Keys; heurfun = hf; }
public void RemoveIncumbent() { Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler); int size = 9; SearchTreeNode.Node <NPuzzleState <int[]>, int, int>[] nodeArray = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> [100]; SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node; for (int i = 0; i < 100; i++) { NPuzzleState <int[]> istate = NPuzzleUtils.GenerateInitState(size); node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(istate); nodeArray[i] = node; int heur = NPuzzleHeuristics.ManhattanDistance(node); frontier.Append(node); } for (int i = 0; i < 100; i++) { frontier.RemoveIncumbent(nodeArray[i]); Assert.False(frontier.InPriorityQueue(nodeArray[i])); } }
public void TestAppendToo() { Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler); int size = 9; SearchTreeNode.Node <NPuzzleState <int[]>, int, int>[] nodeArray = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> [100]; SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node; for (int i = 0; i < 100; i++) { NPuzzleState <int[]> istate = NPuzzleUtils.GenerateInitState(size); node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(istate); nodeArray[i] = node; //int heur = NPuzzleHeuristics.ManhattanDistance(node); frontier.Append(node); } for (int i = 0; i < 100; i++) { int heur = NPuzzleHeuristics.ManhattanDistance(nodeArray[i]); Assert.True(frontier.InPriorityQueue(nodeArray[i])); } int j = 0; int[] heurArray = new int[100]; while (frontier.Count() > 0) { node = frontier.Pop(); heurArray[j] = NPuzzleHeuristics.ManhattanDistance(node); j++; } for (j = 0; j < 99; j++) { Assert.True(heurArray[j] <= heurArray[j + 1]); } }
private Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > GetPriorityQueueToo() { Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler); int size = 9; SearchTreeNode.Node <NPuzzleState <int[]>, int, int>[] nodeArray = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> [100]; SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node; for (int i = 0; i < 100; i++) { NPuzzleState <int[]> istate = NPuzzleUtils.GenerateInitState(size); node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(istate); nodeArray[i] = node; //int heur = NPuzzleHeuristics.ManhattanDistance(node); frontier.Append(node); } return(frontier); }
private Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > GetPriorityQueue() { // Heuristics.HeuristicFunction<NPuzzleState<int[]>,int,int> handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler); //SortedList<int, List<SearchTreeNode.NPuzzleNode<int[],int,int> > > frontier = new SortedList<int, List<SearchTreeNode.NPuzzleNode<int[],int,int> > >(); List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > nodes = TestNode.CreateNodesForTesting(); //! md = 12 int[] s7 = { 6, 4, 3, 1, 9, 5, 8, 7, 2 }; NPuzzleState <int[]> s7State = new NPuzzleState <int[]>(s7); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node7 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> (s7State); nodes.Add(node7); //! 2 + 2 + 1 + 1 + 2 = 8 int[] s8 = { 1, 6, 3, 8, 4, 5, 7, 2, 9 }; NPuzzleState <int[]> s8State = new NPuzzleState <int[]>(s8); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node8 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> (s8State); nodes.Add(node8); foreach (var node in nodes) { //int md = handler(node); frontier.Append(node); } return(frontier); }