static void Main(string[] args) { string path = Console.ReadLine(); Maze maze; try { maze = new Maze(path); } catch (Exception e) { Console.WriteLine(e.Message); maze = new Maze(@"C:\Users\Economist\documents\visual studio 2015\Projects\PathFinder\PathFinder\tests\maze1.txt"); } maze.Print(maze.GetMaze()); maze.FindBestWay(); Console.WriteLine(maze.GetWay()); Console.ReadKey(); }
/* Public: */ public AlgorithmsTest() { DateTime t0 = DateTime.Now; int MazeCount = 5; int TotalSearchs = 0; int SuccessCount = 0; for (int c = 0; c < MazeCount; c++) { int OneSearch = 0; HashSet blocked_cells = new HashSet(), unblocked_cells = new HashSet(); long seed = DateTime.Now.Ticks; //seed = 27261468842294L; LightCell maze_cell; java.util.Random random = new java.util.Random(seed); System.Console.WriteLine("Seed: " + seed); Maze maze = new Maze(seed, MAZE_W, MAZE_H, PROBABILITY_TO_BLOCK_A_CELL, MAZE_CELL_MAX_COST); //GAAStarLazy gaa_star_lazy = new GAAStarLazy(maze , true , false , tie_breaking_strategy , // ManhattanDistanceHeuristic.GetManhattanDistanceHeuristic() , Maze.N_DIRECTIONS_WITHOUT_DIAGONALS); for (maze_cell = maze.GetStart(); maze_cell != maze.GetGoal();) { AStar a_star = new AStar(maze, true, false, tie_breaking_strategy, ManhattanDistanceHeuristic.GetManhattanDistanceHeuristic(), Maze.N_DIRECTIONS_WITHOUT_DIAGONALS); a_star.Solve(); OneSearch++; //System.Console.WriteLine("A*:\n" + maze); //System.Console.WriteLine("A*:\n"); //maze.CleanPath(); //gaa_star_lazy.Solve(); //System.Console.WriteLine("GAA*:\n"); //System.Console.WriteLine("GAA*:\n" + maze); if (!a_star.HasSolution()) { System.Console.WriteLine("No solution."); //System.err.println("Fail: Some algorithms found the solution."); //System.err.println("A*: " + a_star.HasSolution()); //System.exit(1); Application.Restart(); } else { SuccessCount++; //System.Console.WriteLine(OneSearch + " The solution has the following cost: " + a_star.GetPathCost()); } for (int distance = 0; maze_cell != maze.GetGoal(); distance += 1, maze_cell = maze_cell.GetNextMazeCell()) { if (distance >= DISTANCE_BEFORE_CHANGE || !a_star.HasSolution()) { LightCell new_goal; maze.CleanPath(); maze.SetStart(maze_cell); //gaa_star_lazy.InformNewStart(maze_cell); blocked_cells.clear(); unblocked_cells.clear(); /* Block some cells. */ for (int i = 0; i < N_CHANGED_CELLS; i++) { LightCell blocked_maze_cell; int x, y; x = random.nextInt(maze.GetW()); y = random.nextInt(maze.GetH()); blocked_maze_cell = maze.GetMazeCell(x, y); if (blocked_maze_cell != maze.GetStart() && blocked_maze_cell != maze.GetGoal() && !blocked_maze_cell.IsBlocked() && !blocked_cells.contains(blocked_maze_cell)) { blocked_maze_cell.Block(); blocked_cells.add(blocked_maze_cell); } } /* Unblock or change the cost of some cells. */ for (int i = 0; i < N_CHANGED_CELLS; i++) { LightCell unblocked_maze_cell; int x, y; x = random.nextInt(maze.GetW()); y = random.nextInt(maze.GetH()); unblocked_maze_cell = maze.GetMazeCell(x, y); if (!blocked_cells.contains(unblocked_maze_cell) && !unblocked_cells.contains(unblocked_maze_cell)) { int new_cost = random.nextInt(MAZE_CELL_MAX_COST) + 1; if (unblocked_maze_cell.IsBlocked() || unblocked_maze_cell.GetCost() > new_cost) { unblocked_cells.add(unblocked_maze_cell); } unblocked_maze_cell.SetCost(new_cost); } } /* Change the goal. */ do { int x, y; x = random.nextInt(maze.GetW()); y = random.nextInt(maze.GetH()); new_goal = maze.GetMazeCell(x, y); } while (blocked_cells.contains(new_goal) || unblocked_cells.contains(new_goal) || new_goal == maze.GetGoal() || new_goal == maze.GetStart()); if (new_goal.IsBlocked()) { unblocked_cells.add(new_goal); maze.SetGoal(new_goal); } else { int old_cost = maze.GetGoal().GetCost(); maze.SetGoal(new_goal); if (old_cost > maze.GetGoal().GetCost()) { unblocked_cells.add(maze.GetGoal()); } } //gaa_star_lazy.InformNewGoal(new_goal); //if(unblocked_cells.size() > 0) gaa_star_lazy.InformUnblockedCells(unblocked_cells); break; } } } TotalSearchs += OneSearch; System.Console.WriteLine(c + " Goal is : [" + maze.GetGoal().X + ", " + maze.GetGoal().Y + "] Current is : [" + maze_cell.X + ", " + maze_cell.Y + "]" ); }//End of produce 100 maze TimeSpan diff = (DateTime.Now - t0); System.Console.WriteLine("Total RunTime : " + diff.ToString()); System.Console.WriteLine("RunTime per search : " + (diff.TotalMilliseconds * 1000 / TotalSearchs) + " micro second"); System.Console.WriteLine("Average count of search is : " + (TotalSearchs / MazeCount)); }
private void buttonStartTest_Click(object sender, EventArgs e) { timer1.Start(); PresenterThread = new Thread(new ThreadStart(() => { SetParameter(); t0 = DateTime.Now; MazeCount = 10; int TotalSearchs = 0; int SuccessCount = 0; for (c = 0; c < MazeCount; c++) { int OneSearch = 0; //Hashtable blocked_cells = new Hashtable() , unblocked_cells = new Hashtable(); //HashSet blocked_cells = new HashSet(), unblocked_cells = new HashSet(); List <LightCell> blocked_cells = new List <LightCell>(), unblocked_cells = new List <LightCell>(); //long seed = DateTime.Now.Ticks;//.Millisecond; int seed = DateTime.Now.Millisecond; LightCell maze_cell; //java.util.Random random = new java.util.Random(seed); System.Random random = new System.Random(seed); //richTextBox1.Text += ("Seed: " + seed) + "\n"; //richTextBox1.Refresh(); SetText(("Seed: " + seed) + "\n", richTextBox1); Maze maze = new Maze(seed, MAZE_W, MAZE_H, PROBABILITY_TO_BLOCK_A_CELL, MAZE_CELL_MAX_COST); //GAAStarLazy gaa_star_lazy = new GAAStarLazy(maze , true , false , tie_breaking_strategy , // ManhattanDistanceHeuristic.GetManhattanDistanceHeuristic() , Maze.N_DIRECTIONS_WITHOUT_DIAGONALS); for (maze_cell = maze.GetStart(); maze_cell != maze.GetGoal();) { AStar a_star = new AStar(maze, true, false, tie_breaking_strategy, ManhattanDistanceHeuristic.GetManhattanDistanceHeuristic(), Maze.N_DIRECTIONS_WITHOUT_DIAGONALS); a_star.Solve(); OneSearch++; //System.out.println("A*:\n" + maze); //System.out.println("A*:\n"); //maze.CleanPath(); //gaa_star_lazy.Solve(); //System.out.println("GAA*:\n"); //System.out.println("GAA*:\n" + maze); if (!a_star.HasSolution()) { //richTextBox1.Text += ("No solution.") + "\n"; SetText(("No solution.") + "\n", richTextBox1); //System.err.println("Fail: Some algorithms found the solution."); //System.err.println("A*: " + a_star.HasSolution()); //throw new Exception("Error");\ timer1.Stop(); PresenterThread.Abort(); //break; } else { SuccessCount++; //System.out.println(OneSearch + " The solution has the following cost: " + a_star.GetPathCost()); } for (int distance = 0; maze_cell != maze.GetGoal(); distance += 1, maze_cell = maze_cell.GetNextMazeCell()) { if (distance >= DISTANCE_BEFORE_CHANGE || !a_star.HasSolution()) { LightCell new_goal; maze.CleanPath(); maze.SetStart(maze_cell); //gaa_star_lazy.InformNewStart(maze_cell); blocked_cells.Clear(); //blocked_cells.clear(); unblocked_cells.Clear(); //unblocked_cells.clear(); if (checkBoxBlockSomeCell.Checked) { /* Block some cells. */ for (int i = 0; i < N_CHANGED_CELLS; i++) { LightCell blocked_maze_cell; int x, y; x = random.Next(maze.GetW()); //x = random.nextInt(maze.GetW()); y = random.Next(maze.GetH()); // y = random.nextInt(maze.GetH()); blocked_maze_cell = maze.GetMazeCell(x, y); if (blocked_maze_cell != maze.GetStart() && blocked_maze_cell != maze.GetGoal() && !blocked_maze_cell.IsBlocked() && !blocked_cells.Contains(blocked_maze_cell)) { blocked_maze_cell.Block(); blocked_cells.Add(blocked_maze_cell); } } } if (checkBoxUnBlock.Checked) { /* Unblock or change the cost of some cells. */ for (int i = 0; i < N_CHANGED_CELLS; i++) { LightCell unblocked_maze_cell; int x, y; x = random.Next(maze.GetW()); //x = random.nextInt(maze.GetW()); y = random.Next(maze.GetH()); // y = random.nextInt(maze.GetH()); unblocked_maze_cell = maze.GetMazeCell(x, y); if (!blocked_cells.Contains(unblocked_maze_cell) && !unblocked_cells.Contains(unblocked_maze_cell)) { int new_cost = random.Next(MAZE_CELL_MAX_COST) + 1; //int new_cost = random.nextInt(MAZE_CELL_MAX_COST) + 1; if (unblocked_maze_cell.IsBlocked() || unblocked_maze_cell.GetCost() > new_cost) { unblocked_cells.Add(unblocked_maze_cell); } unblocked_maze_cell.SetCost(new_cost); } } } if (checkBoxMovingTarget.Checked) { /* Change the goal. */ do { int x, y; x = random.Next(maze.GetW()); //x = random.nextInt(maze.GetW()); y = random.Next(maze.GetH()); // y = random.nextInt(maze.GetH()); new_goal = maze.GetMazeCell(x, y); } while (blocked_cells.Contains(new_goal) || unblocked_cells.Contains(new_goal) || new_goal == maze.GetGoal() || new_goal == maze.GetStart()); if (new_goal.IsBlocked()) { unblocked_cells.Add(new_goal); maze.SetGoal(new_goal); } else { int old_cost = maze.GetGoal().GetCost(); maze.SetGoal(new_goal); if (old_cost > maze.GetGoal().GetCost()) { unblocked_cells.Add(maze.GetGoal()); } } } //gaa_star_lazy.InformNewGoal(new_goal); //if(unblocked_cells.size() > 0) gaa_star_lazy.InformUnblockedCells(unblocked_cells); break; } } } TotalSearchs += OneSearch; /*richTextBox1.Text += c + * " Goal is : [" + maze.GetGoal().X + ", " + maze.GetGoal().Y + * "] Current is : [" + maze_cell.X + ", " + maze_cell.Y + "] \n"; * richTextBox1.Refresh();*/ SetText("Maze " + (c + 1) + " Goal is : [" + maze.GetGoal().X + ", " + maze.GetGoal().Y + "] Current is : [" + maze_cell.X + ", " + maze_cell.Y + "] \n", richTextBox1); }//End of produce 100 maze //richTextBox1.Text += ("Average count of search is : " + (TotalSearchs / MazeCount)); //richTextBox1.Refresh(); SetText(("Average count of search is : " + TotalSearchs + "/" + MazeCount + " = " + (TotalSearchs / MazeCount)), richTextBox1); Diff = (DateTime.Now - t0); SetText("\n" + Diff.ToString() + "\n", richTextBox1); SetText("RunTime per each search : " + (Diff.TotalMilliseconds * 1000 / TotalSearchs) + " micro second \n", richTextBox1); timer1.Stop(); })); PresenterThread.Start(); //richTextBox1.Text = (DateTime.Now - t0).ToString(); //PresenterThread.Abort(); }