/// <summary> /// Compares BFS and DFS solvers. /// </summary> public static void CompareSolvers(Maze maze) { var searchableMaze = new MazeAdapter(maze); int dfsNodesEvaluated, bfsNodesEvaluated; Solution <Position> dfsSolution, bfsSolution; Searcher <Position> searcher = new BFS <Position>(new CostComperator <Position>()); bfsSolution = searcher.Search(searchableMaze); bfsNodesEvaluated = searcher.GetNumberOfNodesEvaluated(); searcher = new DFS <Position>(); dfsSolution = searcher.Search(searchableMaze); dfsNodesEvaluated = searcher.GetNumberOfNodesEvaluated(); bool isBfsBetter = dfsNodesEvaluated > bfsNodesEvaluated; Console.WriteLine("Better solution was found by {0} ({1} vs. {2} nodes evaluated)", isBfsBetter ? "BFS" : "DFS", isBfsBetter ? bfsNodesEvaluated : dfsNodesEvaluated, isBfsBetter ? dfsNodesEvaluated : bfsNodesEvaluated); var drawer = new MazeDrawer(); drawer.DrawMaze(searchableMaze, isBfsBetter ? bfsSolution:dfsSolution); }
/// <summary> /// Solves the maze. /// </summary> /// <param name="name">The name.</param> /// <param name="algorithm">The algorithm.</param> /// <returns></returns> public /*Solution<Position>*/ List <string> SolveMaze(string name, int algorithm) { SingleGame game = cash.GetSingleGame(name); if (game != null) { //Solution<Position> solution = cash.GetSolution(name); /*Solution<Position> solution = game.Solution; * if (!(solution == null)) * { * return solution; * }*/ //Maze maze = cash.GetMaze(name); Maze maze = game.Maze; ISearcher <Position> searcher = null; if (algorithm == 0) { searcher = new BFS <Position>(); } else if (algorithm == 1) { searcher = new DFS <Position>(); } MazeAdapter <Position> mazeAdapter = new MazeAdapter <Position>(maze); Solution <Position> solution = searcher.Search(mazeAdapter); //cash.AddSolution(name, solution); //game.Solution = solution; return(this.GetSolutionString(solution.List)); //solution; } return(null); }
/// <summary> /// Solves the specified maze. /// </summary> /// <param name="name">The name.</param> /// <param name="algo">The search algorithm.</param> /// <returns></returns> /// <exception cref="System.ArgumentException">no such maze , re-enter a command</exception> public Solution <State <Position> > Solve(string name, Searcher <Position> algo) { if (!dataBase.IsMazeExists(name)) { throw new ArgumentException("no such maze , re-enter a command"); } Maze maze = dataBase.GetMaze(name); MazeAdapter mazeA = new MazeAdapter(maze); if (algo is Dfs <Position> ) { if (!dataBase.IsDfsSolutionExists(name)) { dataBase.AddDfsSolution(name, algo.Search(mazeA)); State <Position> .StatePool.ResetPool(); } return(dataBase.getDfsSolution(name)); } else { if (!dataBase.IsBfsSolutionExists(name)) { dataBase.AddBfsSolution(name, algo.Search(mazeA)); State <Position> .StatePool.ResetPool(); } return(dataBase.getBfsSolution(name)); } }
/// <summary> /// Solves the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns></returns> public MazeSolution SolveMaze(SolveRequest request) { // if there is a maze with this name if (!mazes.ContainsKey(request.MazeName)) { throw new InvalidOperationException(string.Format("There is no maze with the name \"{0}\"", request.MazeName)); } // if solution exists in the cache if (solutionCache.ContainsKey(request.MazeName)) { return(solutionCache[request.MazeName]); } // initialize the search components MazeAdapter adapter = new MazeAdapter(mazes[request.MazeName]); ISearcher <Position> algorithm = GetAlgorithm(request.Algorithm); // search for the solution Solution <Position> sol = algorithm.Search(adapter); // create the mazeSolution object. MazeSolution path = new MazeSolution(algorithm.GetNumberOfNodesEvaluated(), request.MazeName, sol); // save the solution in the cache solutionCache[request.MazeName] = path; //return the solution return(path); }
public string SolveMaze(string mazeName, string algorithm) { if (!NameExistsInDictionary(SinglePlayerMazes, mazeName)) { throw new Exception($"there is no maze with this name {mazeName}"); } if (MazeExistsInSolutions(mazeName)) { SolutionAdapter solutionAdapter1 = new SolutionAdapter(MazeSolutions[mazeName], mazeName); return(solutionAdapter1.ToJson()); } MazeAdapter mazeAdapter = new MazeAdapter(SinglePlayerMazes[mazeName]); ISearcher <Position> searchAlgorithm = GetAlgorithmAccordingToIndicator(algorithm); Solution <Position> solution = searchAlgorithm.Search(mazeAdapter); SolutionAdapter solutionAdapter = new SolutionAdapter(solution, mazeName); MazeSolutions[mazeName] = solution; JObject jobject = new JObject(); jobject["Name"] = mazeName; jobject["Path"] = solutionAdapter.ToJson(); jobject["NodesEvaluated"] = solutionAdapter.Solution.NodesEvaluated;//check if it is connected to the solutiion property return(jobject.ToString()); }
/// <summary> /// Solves the specified maze. /// </summary> /// <param name="name">The name.</param> /// <param name="algorithm">The algorithm.</param> /// <returns> /// solves the maze and returns the solution /// </returns> /// <exception cref="System.Exception">Failed at Solve function in model class</exception> public Solution <Position> Solve(string name, Algorithm algorithm) { Solution <MazeLib.Position> sol; MazeAdapter mazeAdapter = new MazeAdapter(GenerateMazeModel.mazes[name]); // if the maze is exists switch (algorithm) { case Algorithm.BFS: { WeightForEdges <MazeLib.Position> we = new WeightsForShortestWay <MazeLib.Position>(); ISearcher <Position> bfs = new BFS <MazeLib.Position>(we); // solve the maze with bfs. sol = bfs.Search(mazeAdapter); } break; case Algorithm.DFS: { ISearcher <Position> dfs = new DFS <Position>(); // solves the maze with dfs. sol = dfs.Search(mazeAdapter); } break; default: return(null); } return(sol); }
/// <summary> /// Gets the BFS solution. /// </summary> /// <param name="name">The name.</param> /// <returns></returns> /// string IModel.GetBFSSolution(string name) { bfsMutex.WaitOne(); ISearchable <Position> mazeObjectAdapter = new MazeAdapter(mazes[name]); if (solutionsBFS.ContainsKey(name)) { return(MazeAdapter.ToString(solutionsBFS[name])); } Solution <Position> solution = BFS.Search(mazeObjectAdapter); State <Position> .StatePool.Clear(); bfsMutex.ReleaseMutex(); return(MazeAdapter.ToString(solution)); //return solution; }
static void Main(string[] args) { DFSMazeGenerator mazeGen = new DFSMazeGenerator(); Maze maze = mazeGen.Generate(50, 50); Console.WriteLine(maze.ToString()); MazeAdapter mazeAdapter = new MazeAdapter(maze); SearchAlgorithmFactory <Position> factory = new SearchAlgorithmFactory <Position>(); ISearcher <Position> bfsSearcher = factory.GetSearchAlgorithm("bfs"); ISearcher <Position> dfsSearcher = factory.GetSearchAlgorithm("dfs"); Solution <Position> solution = bfsSearcher.Search(mazeAdapter); Solution <Position> solution1 = dfsSearcher.Search(mazeAdapter); Console.WriteLine("the bfs solved the maze int {0}", bfsSearcher.GetNumberOfNodesEvaluated()); Console.WriteLine("the dfs solved the maze int {0}", dfsSearcher.GetNumberOfNodesEvaluated()); Console.ReadKey(); }
public JObject SolveMaze(string name) { Maze mazeFromModel = GetMaze(name); //create new adapter Maze maze = GetMaze(name); adapter = new MazeAdapter <Position>(maze); ser = new BestFirstSearch <Position>(); sol = ser.search(adapter); Console.WriteLine(sol.Path.Count()); Console.WriteLine(sol.Path.Count()); solAdapter = new SolutionAdapter(sol); JObject mazeSolutionObj = new JObject(); mazeSolutionObj["Name"] = name; mazeSolutionObj["Solution"] = solAdapter.ToString(); return(mazeSolutionObj); }
/// <summary> /// solve a maze /// </summary> /// <param name="name">the name of the maze</param> /// <param name="algo">the algorithm to solve the maze with</param> /// <returns>the solution to the maze</returns> public MazeSolution Solve(string name, int algo) { // solve the maze and return the solution Maze maze = singlePlayerMazes[name]; MazeAdapter mazeAdapter = new MazeAdapter(maze); MazeSolution ms; // solve with the correct algorithm if (algo == 0) { ms = new MazeSolution(new BFS <Position>().Search(mazeAdapter), maze.Name); } else { ms = new MazeSolution(new DFS <Position>().Search(mazeAdapter), maze.Name); } // return the solution return(ms); }
public static void CompareSolvers() { DFSMazeGenerator mazeGenerator = new DFSMazeGenerator(); //Maze m = mazeGenerator.Generate(10, 10); string json = @"{ 'Name': 'mymaze', 'Maze': '0001010001010101110101010000010111111101000001000111010101110001010001011111110100000000011111111111', 'Rows': 10, 'Cols': 10, 'Start': { 'Row': 0, 'Col': 4 }, 'End': { 'Row': 0, 'Col': 0 } }"; Maze m = Maze.FromJSON(json); //Console.Write(maze.ToString()); MazeAdapter <Position> maze = new MazeAdapter <Position>(m); maze.printMaze(); Console.WriteLine("------------DFS------------"); DFS <Position> dfs = new DFS <Position>(); Solution <Position> solDFS = dfs.Search(maze); Console.WriteLine("DFS maze solution: " + maze.ToSolution(solDFS)); Console.WriteLine("DFS number of nodes evaluated: " + dfs.GetNumberOfNodesEvaluated()); Console.WriteLine("------------BFS------------"); BFS <Position> bfs = new BFS <Position>(); Solution <Position> solBFS = bfs.Search(maze, new CostComparator <Position>()); Console.WriteLine("BFS maze solution: " + maze.ToSolution(solBFS)); Console.WriteLine("BFS number of nodes evaluated: " + bfs.GetNumberOfNodesEvaluated()); Console.WriteLine("Press enter to close..."); Console.ReadKey(); return; }
/// <summary> /// Mains the specified arguments. /// </summary> /// <param name="args">The arguments.</param> static void Main(string[] args) { DFSMazeGenerator mazeGen = new DFSMazeGenerator(); Maze maze = mazeGen.Generate(50, 50); Console.WriteLine(maze.ToString()); MazeAdapter mazeAdapter = new MazeAdapter(maze); SearchAlgorithmFactory <Position> factory = new SearchAlgorithmFactory <Position>(); ISearcher <Position> bfsSearcher = factory.GetSearchAlgorithm("bfs"); ISearcher <Position> dfsSearcher = factory.GetSearchAlgorithm("dfs"); Solution <Position> solution1 = bfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd1 = new SolutionAdapter(solution1, "bla"); Solution <Position> solution2 = dfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd2 = new SolutionAdapter(solution2, "bla"); string a = solutionAd1.ToJson(); Console.WriteLine("the bfs solved the maze int {0}", solution1.NodesEvaluated); Console.WriteLine("the dfs solved the maze int {0}", solution2.NodesEvaluated); Console.ReadKey(); }
/// <summary> /// Compares the solvers. /// </summary> /// <param name="row">The row.</param> /// <param name="col">The col.</param> public static void CompareSolvers(int row, int col) { DFSMazeGenerator generator = new DFSMazeGenerator(); Maze maze = generator.Generate(row, col); Console.WriteLine(maze.ToString()); ISearchable <Position> searchable = new MazeAdapter(maze); ISearcher <Position> searcher = new BFSSearcher <Position>(); Solution <Position> solution = searcher.Search(searchable); Console.WriteLine("BFS solved the maze with {0} evaluated nodes", searcher.GetNumberOfNodesEvaluated()); searcher = new DFSSearcher <Position>(); solution = searcher.Search(searchable); Console.WriteLine("DFS solved the maze with {0} evaluated nodes", searcher.GetNumberOfNodesEvaluated()); Console.WriteLine(""); }
/// <summary> /// solve a maze /// </summary> /// <param name="name">the name of the maze</param> /// <param name="searcher">the algorithem to solve with</param> /// <returns>the solution to the maze</returns> public MazeSolution SolveMaze(string name, SearchAlgo searcher) { // checks if the maze exist if (!singlePlayerMazes.ContainsKey(name)) { return(null); } // checks if the solution exist if (singlePlayerSolved.ContainsKey(name)) { return(singlePlayerSolved[name]); } // solve the maze and return the solution Maze maze = singlePlayerMazes[name]; MazeAdapter mazeAdapter = new MazeAdapter(maze); MazeSolution ms = new MazeSolution(searcher(mazeAdapter), maze.Name); // save the current solution singlePlayerSolved.Add(name, ms); return(ms); }
/// <summary> /// Solves the maze. /// </summary> /// <param name="mazeName">Name of the maze.</param> /// <param name="algorithm">The algorithm.</param> /// <param name="player">The player.</param> /// <returns></returns> public string SolveMaze(string mazeName, string algorithm, Player player) { if (!NameExistsInDictionary(SinglePlayerMazes, mazeName)) { return($"Error: there is no maze with this name {mazeName}"); } if (MazeExistsInSolutions(mazeName)) { SolutionAdapter solutionAdapter1 = new SolutionAdapter(MazeSolutions[mazeName], mazeName); return(solutionAdapter1.ToJson()); } MazeAdapter mazeAdapter = new MazeAdapter(SinglePlayerMazes[mazeName]); ISearcher <Position> searchAlgorithm = GetAlgorithmAccordingToIndicator(algorithm); Solution <Position> solution = searchAlgorithm.Search(mazeAdapter); SolutionAdapter solutionAdapter = new SolutionAdapter(solution, mazeName); MazeSolutions[mazeName] = solution; return(solutionAdapter.ToJson()); }
static void Main(string[] args) { 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); DFSMazeGenerator mazeGen = new DFSMazeGenerator(); // Maze maze = mazeGen.Generate(10,10); Console.WriteLine(maze.ToString()); MazeAdapter mazeAdapter = new MazeAdapter(maze); SearchAlgorithmFactory <Position> factory = new SearchAlgorithmFactory <Position>(); ISearcher <Position> bfsSearcher = factory.GetSearchAlgorithm("bfs"); ISearcher <Position> dfsSearcher = factory.GetSearchAlgorithm("dfs"); Solution <Position> solution1 = bfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd1 = new SolutionAdapter(solution1, "bla"); Solution <Position> solution2 = dfsSearcher.Search(mazeAdapter); SolutionAdapter solutionAd2 = new SolutionAdapter(solution2, "bla"); string a = solutionAd1.ToJson(); Console.WriteLine("the bfs solved the maze int {0}", solution1.NodesEvaluated); Console.WriteLine("the dfs solved the maze int {0}", solution2.NodesEvaluated); Console.ReadKey(); }
public string Execute(string[] args, TcpClient client = null) { string name = args[0]; string algorithm = args[1]; //get the maze from the model Maze mazeFromModel = this.model.GetMaze(name); if (mazeFromModel == null) { //return there is no maze } //create new adapter adapter = new MazeAdapter <Position>(mazeFromModel); //if 0 then bfs, if 1 dfs, otherwise print error if (algorithm.Equals("0")) { ser = new BestFirstSearch <Position>(); } else if (algorithm.Equals("1")) { ser = new DFS <Position>(); } else { //return algorithm input invalid } sol = ser.search(adapter); Console.WriteLine(sol.Path.Count()); Console.WriteLine(sol.Path.Count()); solAdapter = new SolutionAdapter(sol); //add the solved maze to the solved mazes dictionary in the model this.model.AddSolvedMaze(name, solAdapter.ToString()); solJson = new SolutionJson(name, solAdapter.ToString(), ser.getNumberOfNodesEvaluated()); return(solJson.solveToJSON()); }
/// <summary> /// Solves the game. /// </summary> /// <param name="s">The s.</param> /// <param name="algo">The algo.</param> /// <returns></returns> public String SolveGame(String s, String algo) { if (!GameDictionary.ContainsKey(s)) { return("not such maze"); } Maze maze = GameDictionary[s]; ISearchable <Position> searchable = new MazeAdapter(maze); ISearcher <Position> searcher; if (algo.Equals("BFS")) { searcher = new BFSSearcher <Position>(); } else { searcher = new DFSSearcher <Position>(); } Solution <Position> solution = searcher.Search(searchable); return(ConvertToString(solution)); }
/// <summary> /// Solves the maze. /// </summary> /// <param name="name">The name.</param> /// <param name="algorithm">The algorithm.</param> /// <returns> /// the maze solution /// </returns> public MazeSolution SolveMaze(string name, Algorithm algorithm) { MazeSolution solution; // try searching the solution in the cache if (_solutions.TryGetValue(name, out solution)) { return(solution); } Maze maze; if (_mazes.TryGetValue(name, out maze)) { ISearchable <Position> adapter = new MazeAdapter(maze); ISolution <Position> sol = _algorithms[(int)algorithm].Search(adapter); solution = new MazeSolution(name, sol, _algorithms[(int)algorithm].GetNumberOfNodesEvaluated()); _solutions.Add(name, solution); return(solution); } Console.WriteLine("the maze: " + name + "does not exist"); //TODO: handle a case when the maze does not exist return(null); }