Exemplo n.º 1
0
        public void testAStarSearch()
        {
            // added to narrow down bug report filed by L.N.Sudarshan of
            // Thoughtworks and Xin Lu of UCI

            // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
            // {2,0,5,6,4,8,3,7,1});
            // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
            // {0,8,7,6,5,4,3,2,1});
            EightPuzzleBoard board = new EightPuzzleBoard(new int[]
                                                          { 7, 1, 8, 0, 4, 6, 2, 3, 5 });
            //EightPuzzleBoard board = new EightPuzzleBoard(new int[]
            //{ 1, 0, 2, 3, 4, 5, 6, 7, 8 });

            IProblem <EightPuzzleBoard, IAction>          problem = new BidirectionalEightPuzzleProblem(board);
            ISearchForActions <EightPuzzleBoard, IAction> search
                = new AStarSearch <EightPuzzleBoard, IAction>(
                      new GraphSearch <EightPuzzleBoard, IAction>(),
                      EightPuzzleFunctions.createManhattanHeuristicFunction());
            SearchAgent <EightPuzzleBoard, IAction> agent
                = new SearchAgent <EightPuzzleBoard, IAction>(problem, search);

            Assert.AreEqual(23, agent.getActions().Size());
            Assert.AreEqual("1133", // "926" GraphSearchReduced Frontier
                            agent.getInstrumentation().getProperty("nodesExpanded"));
            Assert.AreEqual("676",  // "534" GraphSearchReduced Frontier
                            agent.getInstrumentation().getProperty("queueSize"));
            Assert.AreEqual("677",  // "535" GraphSearchReduced Frontier
                            agent.getInstrumentation().getProperty("maxQueueSize"));
        }
        public void testGenerateCorrect3Successors()
        {
            ICollection <IAction> actions = CollectionFactory.CreateQueue <IAction>(EightPuzzleFunctions.getActions(board));

            Assert.AreEqual(3, actions.Size());

            // test first successor
            EightPuzzleBoard expectedFirst = new EightPuzzleBoard(new int[] { 1, 2,
                                                                              0, 3, 4, 5, 6, 7, 8 });
            EightPuzzleBoard actualFirst = (EightPuzzleBoard)EightPuzzleFunctions.getResult(board, actions.Get(0));

            Assert.AreEqual(expectedFirst, actualFirst);
            Assert.AreEqual(EightPuzzleBoard.UP, actions.Get(0));

            // test second successor
            EightPuzzleBoard expectedSecond = new EightPuzzleBoard(new int[] { 1,
                                                                               2, 5, 3, 4, 8, 6, 7, 0 });
            EightPuzzleBoard actualSecond = (EightPuzzleBoard)EightPuzzleFunctions.getResult(board, actions.Get(1));

            Assert.AreEqual(expectedSecond, actualSecond);
            Assert.AreEqual(EightPuzzleBoard.DOWN, actions.Get(1));

            // test third successor
            EightPuzzleBoard expectedThird = new EightPuzzleBoard(new int[] { 1, 2,
                                                                              5, 3, 0, 4, 6, 7, 8 });
            EightPuzzleBoard actualThird = (EightPuzzleBoard)EightPuzzleFunctions.getResult(board, actions.Get(2));

            Assert.AreEqual(expectedThird, actualThird);
            Assert.AreEqual(EightPuzzleBoard.LEFT, actions.Get(2));
        }
        public void testGenerateCorrectWhenGapMovedRightward()
        {
            board.moveGapLeft();// gives { 1, 2, 5, 3, 0, 4, 6, 7, 8 }
            Assert.AreEqual(new EightPuzzleBoard(new int[] { 1, 2, 5, 3, 0, 4,
                                                             6, 7, 8 }), board);

            ICollection <IAction> actions = CollectionFactory.CreateQueue <IAction>(EightPuzzleFunctions.getActions(board));

            Assert.AreEqual(4, actions.Size());

            EightPuzzleBoard expectedFourth = new EightPuzzleBoard(new int[] { 1,
                                                                               2, 5, 3, 4, 0, 6, 7, 8 });
            EightPuzzleBoard actualFourth = (EightPuzzleBoard)EightPuzzleFunctions.getResult(board, actions.Get(3));

            Assert.AreEqual(expectedFourth, actualFourth);
            Assert.AreEqual(EightPuzzleBoard.RIGHT, actions.Get(3));
        }
Exemplo n.º 4
0
 static void eightPuzzleAStarDemo()
 {
     System.Console.WriteLine("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
     try
     {
         IProblem <EightPuzzleBoard, IAction> problem = new BidirectionalEightPuzzleProblem(random1);
         ISearchForActions <EightPuzzleBoard, IAction>
         search = new AStarSearch <EightPuzzleBoard, IAction>(
             new GraphSearch <EightPuzzleBoard, IAction>(), EightPuzzleFunctions.createMisplacedTileHeuristicFunction());
         SearchAgent <EightPuzzleBoard, IAction> agent = new SearchAgent <EightPuzzleBoard, IAction>(problem, search);
         printActions(agent.getActions());
         printInstrumentation(agent.getInstrumentation());
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Exemplo n.º 5
0
        public void testGreedyBestFirstSearchReducedFrontier()
        {
            // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
            // {2,0,5,6,4,8,3,7,1});
            // EightPuzzleBoard extreme = new EightPuzzleBoard(new int[]
            // {0,8,7,6,5,4,3,2,1});
            EightPuzzleBoard board = new EightPuzzleBoard(new int[] { 7, 1, 8, 0, 4, 6, 2, 3, 5 });

            IProblem<EightPuzzleBoard, IAction> problem = new BidirectionalEightPuzzleProblem(board);
            QueueBasedSearch<EightPuzzleBoard, IAction> search = new GreedyBestFirstSearch<EightPuzzleBoard, IAction>
                    (new GraphSearchReducedFrontier<EightPuzzleBoard, IAction>(), EightPuzzleFunctions.createManhattanHeuristicFunction());

            SearchAgent<EightPuzzleBoard, IAction> agent = new SearchAgent<EightPuzzleBoard, IAction>(problem, search);
            Assert.AreEqual(49, agent.getActions().Size());
            Assert.AreEqual("197", agent.getInstrumentation().getProperty("nodesExpanded"));
            Assert.AreEqual("140", agent.getInstrumentation().getProperty("queueSize"));
            Assert.AreEqual("141", agent.getInstrumentation().getProperty("maxQueueSize"));

        }
Exemplo n.º 6
0
 static void eightPuzzleGreedyBestFirstManhattanDemo()
 {
     System.Console.WriteLine("\nEightPuzzleDemo Greedy Best First Search (ManhattanHeursitic)-->");
     try
     {
         IProblem <EightPuzzleBoard, IAction> problem = new BidirectionalEightPuzzleProblem(boardWithThreeMoveSolution);
         ISearchForActions <EightPuzzleBoard, IAction>
         search = new GreedyBestFirstSearch <EightPuzzleBoard, IAction>(
             new GraphSearch <EightPuzzleBoard, IAction>(),
             EightPuzzleFunctions.createManhattanHeuristicFunction());
         SearchAgent <EightPuzzleBoard, IAction> agent = new SearchAgent <EightPuzzleBoard, IAction>(problem, search);
         printActions(agent.getActions());
         printInstrumentation(agent.getInstrumentation());
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Exemplo n.º 7
0
 static void eightPuzzleSimulatedAnnealingDemo()
 {
     System.Console.WriteLine("\nEightPuzzleDemo Simulated Annealing  Search -->");
     try
     {
         IProblem <EightPuzzleBoard, IAction> problem = new BidirectionalEightPuzzleProblem(random1);
         SimulatedAnnealingSearch <EightPuzzleBoard, IAction>
         search = new SimulatedAnnealingSearch <EightPuzzleBoard, IAction>(
             EightPuzzleFunctions.createManhattanHeuristicFunction());
         SearchAgent <EightPuzzleBoard, IAction> agent = new SearchAgent <EightPuzzleBoard, IAction>(problem, search);
         printActions(agent.getActions());
         System.Console.WriteLine("Search Outcome=" + search.getOutcome());
         System.Console.WriteLine("Final State=\n" + search.getLastSearchState());
         printInstrumentation(agent.getInstrumentation());
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Exemplo n.º 8
0
        public void testHeuristicCalculation()
        {
            IToDoubleFunction <Node <EightPuzzleBoard, IAction> > h =
                EightPuzzleFunctions.createMisplacedTileHeuristicFunction();
            EightPuzzleBoard board = new EightPuzzleBoard(new int[] { 2, 0, 5, 6,
                                                                      4, 8, 3, 7, 1 });

            Assert.AreEqual(6.0, h.applyAsDouble(new Node <EightPuzzleBoard, IAction>(board)), 0.001);

            board = new EightPuzzleBoard(new int[] { 6, 2, 5, 3, 4, 8, 0, 7, 1 });
            Assert.AreEqual(5.0, h.applyAsDouble(new Node <EightPuzzleBoard, IAction>(board)), 0.001);

            board = new EightPuzzleBoard(new int[] { 6, 2, 5, 3, 4, 8, 7, 0, 1 });
            Assert.AreEqual(6.0, h.applyAsDouble(new Node <EightPuzzleBoard, IAction>(board)), 0.001);

            board = new EightPuzzleBoard(new int[] { 8, 1, 2, 3, 4, 5, 6, 7, 0 });
            Assert.AreEqual(1.0, h.applyAsDouble(new Node <EightPuzzleBoard, IAction>(board)), 0.001);

            board = new EightPuzzleBoard(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
            Assert.AreEqual(0.0, h.applyAsDouble(new Node <EightPuzzleBoard, IAction>(board)), 0.001);
        }