Пример #1
0
 public void SolveMaze(string mazeName)
 {
     if (IsMazeExists(mazeName))
     {
         if (!IsSolutionExists(mazeName))
         {
             new Thread(() =>
             {
                 Maze3d maze3d           = m_mazes[mazeName];
                 ISearchable maze        = new SearchableMaze3d(maze3d);
                 AState startState       = maze.GetStartState();
                 ASearchingAlgorithm bfs = new BreadthFirstSearch();
                 Solution solution       = bfs.Solve(maze);
                 m_solutions.Add(mazeName, solution);
                 controller.output("solution for " + mazeName + " is ready");
             }).Start();
         }
         else
         {
             controller.output("solution for " + mazeName + " is ready");
         }
     }
     else
     {
         throw new Exception("maze " + mazeName + " dosen't exist!");
     }
 }
Пример #2
0
        public void Solve(string mazename, string algorithm)
        {
            if (!(m_controller as MyController).mazes.ContainsKey(mazename))
            {
                (m_controller as MyController).M_view.Output("maze name does not exists");
                return;
            }

            SearchableMaze3d sm = new SearchableMaze3d(((m_controller as MyController).mazes[mazename] as Maze3d));

            if (algorithm.ToLower().Equals("bfs"))
            {
                BreadthFirstSearch bs    = new BreadthFirstSearch();
                Solution           bssol = bs.Solve(sm);
                (m_controller as MyController).bfssolutions.Add(mazename, bssol);
                (m_controller as MyController).M_view.Output("BFS solution for " + mazename + " is ready");
            }
            else if (algorithm.ToLower().Equals("dfs"))
            {
                DepthFirstSearch ds    = new DepthFirstSearch();
                Solution         dssol = ds.Solve(sm);
                (m_controller as MyController).dfssolutions.Add(mazename, dssol);
                (m_controller as MyController).M_view.Output("DFS solution for " + mazename + " is ready");
            }
            else
            {
                (m_controller as MyController).M_view.Output("Wrong searching algorithm");
            }
        }
Пример #3
0
        /// <summary>
        /// Thread solving of the maze - to be implemented in a thread
        /// </summary>
        /// <param name="searchingAlgorithm">Chosen search algorithm</param>
        /// <param name="mazeName">Maze name</param>
        private void solve(ISearchingAlgorithm searchingAlgorithm, string mazeName)
        {
            ISearchable maze;
            Solution    mazeSolution;

            if (!solutionExists(mazeName))
            {
                try
                {
                    maze = new SearchableMaze3d(m_mazesDictionary[mazeName]);
                }
                catch (Exception)
                {
                    return;
                }
                m_stoppingList.Add(searchingAlgorithm);
                mazeSolution = searchingAlgorithm.Solve(maze);
                m_stoppingList.Remove(searchingAlgorithm);
                List <string[]> getSolutionCoordinate = mazeSolution.getSolutionCoordinates();
                m_solutionsDictionary[mazeName] = mazeSolution;
                mCurrentSolution = mazeSolution;
                isSolutionExists = true;
                saveSolutionDictionary();
                saveMazeDictionary();
            }
            else // Solution exists
            {
                MessageBox.Show(("The solution exists for the maze named: \n" + mazeName), "Solution Exists");
            }
        }
Пример #4
0
        /// <summary>
        /// solve the maze in new Thread
        /// </summary>
        /// <param name="name">mame of the thread</param>
        /// <param name="new_alg">alg of solving</param>
        private void SolveMazeInNewThread(string name, ASearchingAlgorithm new_alg)
        {
            Maze             m_from_dic    = m_DicOfMazes[name];
            Maze3d           m_3d          = m_from_dic as Maze3d;
            SearchableMaze3d maze3dS       = new SearchableMaze3d(m_3d);
            Solution         maze_solution = new_alg.Solve(maze3dS); // solve the maze

            IfSolutionExist(name);                                   // if the solution exist with identical name - remove it.
            m_solutions.Add(name, maze_solution);                    // add to dictionary of solutions
        }
Пример #5
0
        /// <summary>
        /// Solves maze in a new thread in the thread pool
        /// </summary>
        /// <param name="mazeName"></param>
        /// <param name="algorithm"></param>
        private void solveMazeInThreadPool(string mazeName, string algorithm)
        {
            ThreadPool.SetMaxThreads(m_numberOfThreadsInThreadPool, m_numberOfThreadsInThreadPool);
            ThreadPool.QueueUserWorkItem(
                new WaitCallback((state) =>
            {
                if (m_generatedMazes.ContainsKey(mazeName))
                {
                    Maze3d maze = m_generatedMazes[mazeName];
                    SearchableMaze3d searchableMaze = new SearchableMaze3d(maze);

                    if (algorithm == "BFS")
                    {
                        BFS bfs = new BFS();   //**
                        setStoppableObjects(bfs);
                        Solution bfsSolution = bfs.Solve(searchableMaze);
                        try
                        {
                            AddToMazeSolutions(mazeName, bfsSolution);
                            AddToMazeToSolution(maze, bfsSolution);
                            ModelChanged("Solution for " + mazeName + " is ready");
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else if (algorithm == "DFS")
                    {
                        DFS dfs = new DFS();
                        setStoppableObjects(dfs);
                        Solution dfsSolution = dfs.Solve(searchableMaze);

                        try
                        {
                            AddToMazeSolutions(mazeName, dfsSolution);
                            AddToMazeToSolution(maze, dfsSolution);
                            ModelChanged("Solution for " + mazeName + " is ready");
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                else
                {
                    ModelChanged("Maze named " + mazeName + " does'nt exist");
                }
            })
                );
        }
        public void TestIsASolution()
        {
            //arrange
            var              mg    = new MyMaze3dGenerator();
            Maze3d           maze  = (Maze3d)mg.generate(7, 7, 4);
            BFS              bfs   = new BFS();
            SearchableMaze3d sMaze = new SearchableMaze3d(maze);
            Solution         sol   = bfs.Solve(sMaze);

            //act
            bfs.Solve(sMaze);

            //assert
            Assert.IsNotNull(sol);
        }
Пример #7
0
        /// <summary>
        /// rin in thread pool the solution
        /// </summary>
        /// <param name="maze_name">name of the maze</param>
        /// <param name="alg">algorithem to solve the maze</param>
        private void RunInThreadPoolOfSolution(Maze maze, string name)
        {
            ThreadPool.QueueUserWorkItem
            (
                new WaitCallback((state) =>
            {
                ASearchingAlgorithm new_alg = checkAlg();
                Maze m_from_dic             = m_DicOfMazes[name];
                Maze3d m_3d = m_from_dic as Maze3d;
                SearchableMaze3d maze3dS = new SearchableMaze3d(m_3d);
                Solution maze_solution   = new_alg.Solve(maze3dS); // solve the maze

                m_solutions.Add(name, maze_solution);              // add to dictionary of solutions
                m_mazeAndSolutions.Add(maze, maze_solution);
                ModelChanged("SolutionIsReady", name);
            })
            );
        }
Пример #8
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());
        }
Пример #9
0
        private static void testSearchAlgorithms()
        {
            ArrayList s = new ArrayList();

            s.Add(5);
            s.Add(5);
            s.Add(4);
            MyMaze3dGenerator mg   = new MyMaze3dGenerator();
            AMaze             maze = mg.generate(s);

            maze.Print();
            Console.WriteLine("DFS solution path:");
            SearchableMaze3d sm    = new SearchableMaze3d((maze as Maze3d));
            DepthFirstSearch ds    = new DepthFirstSearch();
            Solution         dssol = ds.Solve(sm);

            dssol.PrintSolution();
            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("for BFS solution press any key");
            Console.ReadKey();
            Console.WriteLine("BFS solution path:");
            BreadthFirstSearch bs    = new BreadthFirstSearch();
            Solution           bssol = bs.Solve(sm);

            bssol.PrintSolution();
            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("for further information about DFS solution press any key");
            Console.ReadKey();
            Console.WriteLine("num of nodes developed in DFS: " + ds.getNumberOfNodes());
            Console.WriteLine("time it took to find solution(ms) in DFS: " + ds.GetSolvingTime());
            Console.WriteLine();
            Console.WriteLine("for further information about BFS solution press any key");
            Console.ReadKey();
            Console.WriteLine();
            Console.WriteLine("num of nodes developed in BFS: " + bs.getNumberOfNodes());
            Console.WriteLine("time it took to find solution(ms) in BFS: " + bs.GetSolvingTime());
        }
Пример #10
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();
        }
Пример #11
0
        public void MazeSolver(Stream rw)
        {
            //StreamReader fromClient = new StreamReader(stream);
            //StreamWriter toClient = new StreamWriter(stream);


            //string command = fromClient.ReadLine();
            // string[] splited = command.Split('|');

            //  string mazeString = /*splited[0];*/fromClient.ReadLine();
            //  Console.WriteLine("client sent me: " + mazeString);
            //  string algorithm = /*splited[1];*/"BFS";
            //
            //  byte[] mazeByteArr = Encoding.ASCII.GetBytes(mazeString);
            //  Maze3d maze;
            //  using (MemoryStream memStream = new MemoryStream())
            //  {
            //      BinaryFormatter binForm = new BinaryFormatter();
            //      memStream.Write(mazeByteArr, 0, mazeByteArr.Length);
            //      memStream.Seek(0, SeekOrigin.Begin);
            //      maze = (Maze3d)binForm.Deserialize(memStream);
            //  }

            IFormatter formatter = new BinaryFormatter();
            Maze3d     maze      = (Maze3d)formatter.Deserialize(rw);
            string     solString;

            if (m_MazeToSolution.ContainsKey(maze))
            {
                Solution sol = m_MazeToSolution[maze];

                formatter.Serialize(rw, sol);
                //solString = convertSolutionToString(sol);
                //toClient.WriteLine(solString);
                //toClient.Flush();
            }

            SearchableMaze3d searchableMaze = new SearchableMaze3d(maze);
            string           algorithm      = "BFS";

            if (algorithm == "BFS")
            {
                BFS bfs = new BFS(); //**

                Solution bfsSolution = bfs.Solve(searchableMaze);
                //solString = convertSolutionToString(bfsSolution);
                // toClient.WriteLine(solString);
                // toClient.Flush();
                formatter.Serialize(rw, bfsSolution);
                try
                {
                    AddToMazeToSolution(maze, bfsSolution);
                }
                catch (Exception)
                {
                }
            }
            else if (algorithm == "DFS")
            {
                DFS dfs = new DFS();

                Solution dfsSolution = dfs.Solve(searchableMaze);
                // solString = convertSolutionToString(dfsSolution);
                // toClient.WriteLine(solString);
                // toClient.Flush();
                formatter.Serialize(rw, dfsSolution);

                try
                {
                    AddToMazeToSolution(maze, dfsSolution);
                }
                catch (Exception)
                {
                }
            }
        }