Exemplo n.º 1
0
        /// <summary>
        /// Part 2
        /// testing the search algorithms
        /// </summary>
        private static void testSearchAlgorithms()
        {
            MyMaze3dGenerator smg  = new MyMaze3dGenerator();
            Maze        maze3d     = smg.generate(20, 20, 10);
            ISearchable maze       = new SearchableMaze3d((Maze3d)maze3d);
            AState      startState = maze.GetStartState();

            Console.WriteLine("**************************************");
            Console.WriteLine("*******TESTING SEARCH ALGORITHMS******");
            Console.WriteLine("**************************************");
            Console.WriteLine("maze to solve:");
            Console.WriteLine();
            maze3d.printMapKeys();
            maze3d.print();
            Console.WriteLine();
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
            Console.WriteLine();
            Console.WriteLine("TEST 1: testing Breadth First Search algorithm");
            Console.WriteLine("**************************************");
            Console.WriteLine();

            ASearchingAlgorithm bfs      = new BreadthFirstSearch();
            Solution            solution = bfs.Solve(maze);

            if (solution.IsSolutionExists())
            {
                Console.WriteLine("Solution found:");
                Console.WriteLine("***************");
                Console.WriteLine();
                Console.WriteLine("Path:");

                foreach (AState state in solution.GetSolutionPath())
                {
                    state.PrintState();
                }
            }
            else
            {
                Console.WriteLine("No Solution found!");
            }

            Console.WriteLine("");
            Console.WriteLine(string.Format("Moves to goals state: {0}", solution.GetSolutionSteps()));
            Console.WriteLine(string.Format("Nodes generated: {0}", bfs.GetNumberOfGeneratedNodes()));
            Console.WriteLine(string.Format("Solving time (miliseconds): {0}", bfs.GetSolvingTimeMiliseconds()));

            Console.WriteLine("Press any key to continue");
            Console.WriteLine();
            Console.ReadKey();


            Console.WriteLine("TEST 2: testing Depth First Search algorithm");
            Console.WriteLine("**************************************");
            Console.WriteLine();
            ASearchingAlgorithm dfs       = new DepthFirstSearch();
            Solution            solution2 = dfs.Solve(maze);

            if (solution2.IsSolutionExists())
            {
                Console.WriteLine("Solution found:");
                Console.WriteLine("***************");
                Console.WriteLine();
                Console.WriteLine("Path:");

                foreach (AState state in solution2.GetSolutionPath())
                {
                    state.PrintState();
                }
            }
            else
            {
                Console.WriteLine("No Solution found!");
            }

            Console.WriteLine("");
            Console.WriteLine(string.Format("Moves to goals state: {0}", solution2.GetSolutionSteps()));
            Console.WriteLine(string.Format("Nodes generated: {0}", dfs.GetNumberOfGeneratedNodes()));
            Console.WriteLine(string.Format("Solving time (miliseconds): {0}", dfs.GetSolvingTimeMiliseconds()));

            Console.ReadLine();
        }
Exemplo n.º 2
0
        private static void testSearchAlgorithms()
        {
            ASearchingAlgorithm BFS = new BreadthFirstSearch();
            ASearchingAlgorithm DFS = new DepthFirstSearch();
            AMazeGenerator      g   = new MyMaze3dGenerator();
            Maze        maze        = g.generate(5, 5, 4);
            ISearchable search      = new SearchableMaze3d(maze);

            Console.WriteLine("****************Part 2 - Tests***************");
            Console.WriteLine("The Start State: {0}", maze.getStartPosition());
            Console.WriteLine("The Goal State: {0}", maze.getGoalPosition());
            Console.WriteLine();
            //BFS
            maze.print();
            Console.WriteLine("***************The Solve by BFS:***************");
            Solution s_bfs = BFS.Solve(search);

            s_bfs.printSolution();
            Console.WriteLine();

            //DFS
            // maze.print();
            Console.WriteLine("press any key to continue to DFS...");
            Console.ReadLine();
            Console.WriteLine("***************The Solve by DFS:***************");
            Solution s_dfs = DFS.Solve(search);

            s_dfs.printSolution();

            Console.WriteLine();
            Console.WriteLine("****************Results***************");
            Console.WriteLine("The Numbers of Nodes in BFS solution: {0} Nodes", s_bfs.numOfNodesInSolution());
            Console.WriteLine("The Numbers of Nodes in DFS solution: {0} Nodes", s_dfs.numOfNodesInSolution());
            Console.WriteLine("The Numbers of Generated Nodes in BFS: {0} Nodes", BFS.GetNumberOfGeneratedNodes());
            Console.WriteLine("The Numbers of Generated Nodes in DFS: {0} Nodes", DFS.GetNumberOfGeneratedNodes());
            Console.WriteLine("Time that take to find solution in BFS: {0} Milliseconds", BFS.GetSolvingTimeMiliseconds());
            Console.WriteLine("Time that take to find solution in DFS: {0} Milliseconds", DFS.GetSolvingTimeMiliseconds());
        }