private void Cell_Click(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Cell cell = sender as Cell; if (listView1.Items[0].Selected) { cell.Image = imageList1.Images[0]; if (cell != maze.GetGoal()) { maze.SetStart(cell); } } else if (listView1.Items[1].Selected) { cell.Image = imageList1.Images[1]; if (cell != maze.GetStart()) { maze.SetGoal(cell); } } else if (listView1.Items[2].Selected) { cell.blocked = true; cell.Image = imageList1.Images[2]; cell.Block(); } else if (listView1.Items[3].Selected) { cell.Image = imageList1.Images[3]; cell.SetCost(GetMaxCost() == 1 ? 1 : 3); } else if (listView1.Items[4].Selected) { cell.Image = imageList1.Images[4]; cell.SetCost(GetMaxCost() == 1 ? 1 : 2); } else if (listView1.Items[5].Selected) { cell.Image = imageList1.Images[5]; cell.SetCost(1); } Application.DoEvents(); } }
//رسم نقشه جدید private void button1_Click(object sender, EventArgs e) { if (dLiteMazeCreated) { MessageBox.Show(" را حذف کنید " + " D*Lite " + "ابتدا نقشه", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { button1.Enabled = false; mazeCreated = true; blocked.Clear(); /*if(!maze.Equals(null)) * for (int i = 0; i < size; i++) * { * for (int j = 0; j < size; j++) * { * this.Controls.Remove(maze.cells[i, j]); * } * }*/ maze = new Maze(DateTime.Now.Millisecond, MAZE_W, MAZE_H, GetProbabilityToBlockACell(), GetMaxCost()); for (int i = 0; i < MAZE_W; i++) { for (int j = 0; j < MAZE_H; j++) { //richTextBox1.Text += // "Cost[" + i + "," + j + "]: " + maze.cells[i, j].GetCost() + "\n"; maze.cells[i, j].BorderStyle = BorderStyle.FixedSingle; maze.cells[i, j].Width = ((this.Width - 220) / MAZE_W) - 1; // 57; maze.cells[i, j].Height = ((this.Height - 90) / MAZE_H) - 1; //31; maze.cells[i, j].Left = i * maze.cells[i, j].Width + 220; // 800; maze.cells[i, j].Top = j * maze.cells[i, j].Height + 80; // 160; maze.cells[i, j].BackColor = Color.White; maze.cells[i, j].Font = new Font("Arial", 8); maze.cells[i, j].TextAlign = ContentAlignment.MiddleCenter; //maze.cells[i, j].SizeMode = PictureBoxSizeMode.StretchImage; this.Controls.Add(maze.cells[i, j]); maze.cells[i, j].MouseClick += new MouseEventHandler(Cell_Click); switch (maze.cells[i, j].GetCost()) { case 1: //maze.cells[i, j].Image = imageList1.Images[6]; break; case 2: maze.cells[i, j].Image = imageList1.Images[4]; break; case 3: maze.cells[i, j].Image = imageList1.Images[3]; break; case 0x7F: maze.cells[i, j].Image = imageList1.Images[2]; maze.cells[i, j].blocked = true; blocked.Add(new Point(i, j)); break; default: break; } if (maze.GetGoal() == maze.cells[i, j]) { maze.cells[i, j].Image = imageList1.Images[1]; maze.cells[i, j].BackColor = Color.Gold; } else if (maze.GetStart() == maze.cells[i, j]) { maze.cells[i, j].Image = imageList1.Images[0]; maze.cells[i, j].BackColor = Color.Blue; } if (maze.cells[i, j].BackColor == Color.Blue) { start = new Point(i, j); } if (maze.cells[i, j].BackColor == Color.Gold) { end = new Point(i, j); } } } } }
/* 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 بازکردنToolStripMenuItem_Click(object sender, EventArgs e) { try { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Text File|*.txt"; ofd.Title = "باز کردن نقشه"; if (ofd.ShowDialog() == DialogResult.OK) { string[] list = System.IO.File.ReadAllLines(ofd.FileName); blocked.Clear(); maze = new Maze(MAZE_W, MAZE_H); for (int i = 0; i < MAZE_W; i++) { for (int j = 0; j < MAZE_H; j++) { maze.cells[i, j].SetCost(int.Parse(list[size * i + j])); maze.cells[i, j].BorderStyle = BorderStyle.FixedSingle; maze.cells[i, j].Width = ((this.Width - 220) / MAZE_W) - 1; // 57; maze.cells[i, j].Height = ((this.Height - 90) / MAZE_H) - 1; //31; maze.cells[i, j].Left = i * maze.cells[i, j].Width + 220; // 800; maze.cells[i, j].Top = j * maze.cells[i, j].Height + 80; // 160; maze.cells[i, j].BackColor = Color.White; maze.cells[i, j].Font = new Font("Arial", 8); maze.cells[i, j].TextAlign = ContentAlignment.MiddleCenter; //maze.cells[i, j].SizeMode = PictureBoxSizeMode.StretchImage; this.Controls.Add(maze.cells[i, j]); maze.cells[i, j].MouseClick += new MouseEventHandler(Cell_Click); switch (maze.cells[i, j].GetCost()) { case 1: //maze.cells[i, j].Image = imageList1.Images[6]; break; case 2: maze.cells[i, j].Image = imageList1.Images[4]; break; case 3: maze.cells[i, j].Image = imageList1.Images[3]; break; case 0x7F: maze.cells[i, j].Image = imageList1.Images[2]; maze.cells[i, j].blocked = true; blocked.Add(new Point(i, j)); break; default: break; } if (maze.GetGoal() == maze.cells[i, j]) { maze.cells[i, j].Image = imageList1.Images[1]; maze.cells[i, j].BackColor = Color.Gold; } else if (maze.GetStart() == maze.cells[i, j]) { maze.cells[i, j].Image = imageList1.Images[0]; maze.cells[i, j].BackColor = Color.Blue; } if (maze.cells[i, j].BackColor == Color.Blue) { start = new Point(i, j); } if (maze.cells[i, j].BackColor == Color.Gold) { end = new Point(i, j); } } } } MessageBox.Show("نقشه باز شد"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); //MessageBox.Show("مطمئن شوید نقشه وجود دارد و دوباره امتحان کنید", // "نقشه باز نشد"); } }
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(); }