/// <summary> /// Generates a thread that solves the maze 'mazeName' /// </summary> /// <param name="mazeName">maze name</param> /// <param name="algorithm">algorithm to solve with</param> public void solveMaze(string mazeName, string algorithm) { ISearchingAlgorithm searching = null; if ("BFS" == algorithm) { searching = new BFS(); } else if ("DFS" == algorithm) { searching = new DFS(); } else { m_controller.errOutput("wrong argument inserted for <algorithm>, Algorithm options are BFS/DFS\n"); return; } m_controller.Output("Solving maze...\n"); Thread thread = new Thread(() => solve(searching, mazeName)); m_threads.Add(thread); thread.Start(); }
/// <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"); } }
/// <summary> /// Solve Maze - Solves the given maze with a given /// solving algorithm - BFS/DFS /// </summary> /// <param name="mazeName">Maze name</param> /// <param name="solvingAlgorithm">Solving algorithm - BFS/DFS</param> public void solveMaze(string mazeName, string solvingAlgorithm) { ISearchingAlgorithm searchingAlgorithm = null; if (solvingAlgorithm == "DFS") { searchingAlgorithm = new DFS(); } else if (solvingAlgorithm == "BFS") { searchingAlgorithm = new BFS(); } else { return; } var resetEvent = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback((state) => { solve(searchingAlgorithm, mazeName); resetEvent.Set(); })); resetEvent.WaitOne(); }
/// <summary> /// Solve the maze 'mazeName' with BFS/DFS algorithm and saves the solution in the dictionary /// </summary> /// <param name="searching">Isearching algorithm to solve the maze with</param> /// <param name="mazeName">maze naze</param> private void solve(ISearchingAlgorithm searching, string mazeName) { ISearchable maze; try { m_mutexDictionary[mazeName].WaitOne(); maze = new SearchableMaze3D(m_mazesDictionary[mazeName]); } catch (Exception) { m_controller.errOutput("The maze named: " + mazeName + "does not exist!\n"); return; } Solution solution = searching.Solve(maze); m_solutionsDictionary[mazeName] = solution; m_mutexDictionary[mazeName].ReleaseMutex(); m_controller.Output("Solution for " + mazeName + " is ready!\n"); }
/// <summary> /// Prints the test results for either BFS or DFS searching algorithms (defined by args) /// </summary> /// <param name="algo"></param> /// <param name="searching"></param> /// <param name="maze"></param> private static void print(string algo, ISearchingAlgorithm searching, ISearchable maze) { Solution solution = searching.Solve(maze); Console.WriteLine("Problem to solve using {0} Algorithm :", algo); Console.WriteLine("**************************************"); maze.print(); if (solution.isSolutionExists()) { Console.WriteLine("Solution Using " + algo + " algorithm :"); Console.WriteLine("**************************************"); Console.WriteLine("(x,y,z)"); solution.printSolution(); Console.WriteLine("\nNodes generated: " + searching.getNumberOfNodesEvaluated()); Console.WriteLine("\nSolving time (miliseconds): " + searching.GetSolvingTimeMiliseconds()); //Console.WriteLine("\nSolution length (number of nodes) : " + solution.getSolutionSteps()); } else { Console.WriteLine("No Solution found!"); } }
protected AbstractSearchingTest(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; _searchingAlgorithm = new T(); }