/// <summary> /// Solves the maze DFS. /// </summary> /// <param name="name">The name.</param> /// <returns>Solution</returns> public Solution <Position> solveMazeDFS(string name) { modelData.mutexDfs.WaitOne(); Solution <Position> solution = null; // Check if the maze exist. if (modelData.Mazes.ContainsKey(name)) { ISearchable <Position> mazeObjectAdapter = new MazeAdapter(modelData.Mazes[name]); ISearcher <Position> DFS = new Dfs <Position>(); // Check if the solution exist. if (modelData.DfsSolutions.ContainsKey(name)) { solution = modelData.DfsSolutions[name]; } else { // Calculate the solution. solution = DFS.Search(mazeObjectAdapter); modelData.DfsSolutions.Add(name, solution); } } State <Position> .StatePool.Clear(); modelData.mutexDfs.ReleaseMutex(); return(solution); }
/// <summary> /// add a solution to a maze. /// </summary> /// <param name="name">the name of the maze</param> /// <param name="searcher">the searcher algo</param> private void AddSolution(string name, int searcher) { // the solution. Solution <State <Position> > solution = new Solution <State <Position> >(); int nodesEv = 0; ISearchable <Position> srMaze = new MazeSearchable(singleMazes[name]); // bfs algo. if (searcher == 0) { ISearcher <Position> bfs = new Bfs <Position>(); solution = bfs.Search(srMaze); nodesEv = bfs.GetNumberOfNodesEvaluated(); } // dfs algo. else { ISearcher <Position> dfs = new Dfs <Position>(); solution = dfs.Search(srMaze); nodesEv = dfs.GetNumberOfNodesEvaluated(); } string strSol = " "; // backtrace the maze solution. for (int i = solution.SolLst.Count - 1; i > 0; i--) { // went down. if (solution.SolLst[i].StateType.Row > solution.SolLst[i - 1].StateType.Row) { strSol += "2"; } // went up. else if (solution.SolLst[i].StateType.Row < solution.SolLst[i - 1].StateType.Row) { strSol += "3"; } // went left. else if (solution.SolLst[i].StateType.Col > solution.SolLst[i - 1].StateType.Col) { strSol += "0"; } // went right. else if (solution.SolLst[i].StateType.Col < solution.SolLst[i - 1].StateType.Col) { strSol += "1"; } } // the solution in json. JObject sol = new JObject { { "Name", name }, { "Solution", strSol }, { "NodesEvaluated", nodesEv } }; string Jsol = JsonConvert.SerializeObject(sol); JsonConvert.DeserializeObject(Jsol); // add the solutin. solvedMazes.Add(name, Jsol); }
/// <summary> /// Function Compares Bfs and Dfs algorithm functions. /// </summary> public void CompareSolvers() { IMazeGenerator dfsMaze = new DFSMazeGenerator(); // Print the maze. //Console.WriteLine(dfsMaze.ToString()); StatePool <Position> spDfs = new StatePool <Position>(); //Adapter adDfs = new Adapter(2, 2, 0, 0, 1, 1, "test Dfs", spDfs); Maze maze = dfsMaze.Generate(500, 500); Console.WriteLine(maze.ToString()); Adapter adDfs = new Adapter(maze, spDfs); ISearcher <Position> dfs = new Dfs <Position>(); Solution <Position> solDfs = dfs.Search(adDfs); Console.WriteLine(Adapter.ToJson(solDfs, "test")); // Creates the solution in Json format. /* * StatePool<Position> spBfs = new StatePool<Position>(); * // Adapter adBfs = new Adapter(10, 10, 0, 0, 8, 1, "test Bfs", spBfs); * Maze maze = dfsMaze.Generate(500, 500); * Console.WriteLine(maze.ToString()); * Adapter adBfs = new Adapter(maze, spBfs); * ISearcher<Position> bfs = new Bfs<Position>(); * Solution<Position> solBfs = bfs.search(adBfs); * //Console.WriteLine(adBfs.ToJson(solBfs)); */ }
/// <summary> /// solve the maze problem - singal player. /// </summary> /// <param name="name">maze name</param> /// <param name="algo">0-bfs, 1-dfs</param> /// <returns>get solution of maze problem</returns> public Solution <Position> Solve(string name, int algo) { if (this.mazesSinglePlayerPool.ContainsKey(name)) { if (!this.solutionsSinglePlayerPool.ContainsKey(name)) { ISearcher <Position> searchAlgo; Solution <Position> solution; Maze maze = this.mazesSinglePlayerPool[name]; Adapter <Position> adapter = new MazeToSearchableAdapter <Position>(maze); ISearchable <Position> searchableMaze = new Searchable <Position, Direction>(adapter); switch (algo) { case 0: searchAlgo = new Bfs <Position>(); break; case 1: searchAlgo = new Dfs <Position>(); break; default: //Error at algorithem numeber: 0 - for bfs, 1 - for dfs return(null); } solution = searchAlgo.Search(searchableMaze); this.solutionsSinglePlayerPool.Add(name, solution); } return(this.solutionsSinglePlayerPool[name]); } //name of maze doesn't exist at maze single player pool" return(null); }
/// <summary> /// Compares the solvers. /// </summary> public static void CompareSolvers() { DFSMazeGenerator generator = new DFSMazeGenerator(); Maze maze = generator.Generate(10, 10); //Console.WriteLine(maze); ISearchable <Position> mazeAdapter = new MazeAdapter(maze); ISearcher <Position> bfs = new BestFirstSearch <Position>(); ISearcher <Position> dfs = new Dfs <Position>(); Solution <Position> solution = bfs.Search(mazeAdapter); Console.WriteLine("bfs sol:" + solution.EvaluatedNodes); Console.WriteLine(mazeAdapter.ToString(solution)); solution = dfs.Search(mazeAdapter); Console.WriteLine("dfs sol:" + solution.EvaluatedNodes); Console.WriteLine(mazeAdapter.ToString(solution)); }
/// <summary> /// compares between a bfs and dfs searchers on a maze. /// </summary> public static void CompareSolvers() { IMazeGenerator gen = new DFSMazeGenerator(); // get a random maze size 50X50. Maze maze = gen.Generate(50, 50); /// print the maze. Console.Write(maze); // make the maze searchable. ISearchable <Position> myMaze = new MazeSearchable(maze); // bfs solution. ISearcher <Position> bfs = new Bfs <Position>(); bfs.Search(myMaze); // dfs solution. ISearcher <Position> dfs = new Dfs <Position>(); dfs.Search(myMaze); // write number of nodes evaluated in each search. Console.WriteLine(bfs.GetNumberOfNodesEvaluated()); Console.WriteLine(dfs.GetNumberOfNodesEvaluated()); Console.ReadKey(); }
/// <summary> /// Roy's example. /// </summary> public static void test() { string json = @"{ 'Name': 'mymaze', 'Maze': '0001010001010101110101010000010111111101000001000111010101110001010001011111110100000000011111111111', 'Rows': 10, 'Cols': 10, 'Start': { 'Row': 0, 'Col': 4 }, 'End': { 'Row': 0, 'Col': 0 } }"; Maze maze = Maze.FromJSON(json); Console.Write(maze.ToString()); Adapter <Position> adapter = new MazeToSearchableAdapter <Position>(maze); ISearchable <Position> searchableMaze = new Searchable <Position, Direction>(adapter); ISearcher <Position> bfs = new Bfs <Position>(); ISearcher <Position> dfs = new Dfs <Position>(); Solution <Position> solBfs = bfs.Search(searchableMaze); Solution <Position> solDfs = dfs.Search(searchableMaze); Console.WriteLine("bfs " + solBfs.NodesEvaluated.ToString()); SolutionRepresent <MazeLib.Direction, MazeLib.Position, int> solRepresent = new MazeSolRepreset(solBfs); solRepresent.ConvertSolution(); Console.WriteLine("bfs sol " + solRepresent.ToJSON()); Console.WriteLine("dfs " + solDfs.NodesEvaluated); Console.ReadKey(); }