Ejemplo n.º 1
0
        private void buttonTestRuntimeGAA_Click(object sender, EventArgs e)
        {
            timer1.Start();
            PresenterThread = new Thread(new ThreadStart(() =>
            {
                SetParameter();
                t0 = DateTime.Now;

                MazeCount        = 10;
                int SuccessCount = 0;

                for (c = 0; c < MazeCount; c++)
                {
                    List <LightCell> blocked_cells = new List <LightCell>(), unblocked_cells = new List <LightCell>();

                    int seed             = DateTime.Now.Millisecond;
                    System.Random random = new System.Random(seed);
                    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 = new GAAStarLazy(maze, true, false, tie_breaking_strategy,
                                                           ManhattanDistanceHeuristic.GetManhattanDistanceHeuristic(), Maze.N_DIRECTIONS_WITHOUT_DIAGONALS);

                    gaa_star.Solve();

                    if (gaa_star.HasSolution())
                    {
                        SuccessCount++;
                    }
                    else
                    {
                        timer1.Stop();
                        PresenterThread.Abort();
                    }
                }
                timer1.Stop();

                /*SetText(("Average count of search is : " + TotalSearchs + "/" + MazeCount +
                 *       " = " + (TotalSearchs / MazeCount)), richTextBox1);*/

                Diff = (DateTime.Now - t0);
                SetText("\n Total Time : " + Diff.ToString() + "\n", richTextBox1);
                SetText("RunTime per each search : " + (Diff.TotalMilliseconds * 1000 / SuccessCount) + " micro second \n", richTextBox1);
            }));
            PresenterThread.Start();
        }
Ejemplo n.º 2
0
        private void toolGAAStar_Click(object sender, EventArgs e)
        {
            try
            {
                if (dLiteMazeCreated)
                {
                    button2_Click(sender, e);
                }

                else if (mazeCreated)
                {
                    maze.CleanPath();
                    gaa_star = new GAAStarLazy(maze, true, must_execute_step_by_step, GetTieBreakingStrategy(),
                                               SelectProperHeuristic(), GetNeighborhood());
                    gaa_star.Solve();

                    for (int i = 0; i < MAZE_W; i++)
                    {
                        for (int j = 0; j < MAZE_H; j++)
                        {
                            maze.cells[i, j].Text = "";

                            if (!maze.cells[i, j].IsPathFlagOn())
                            {
                                if (gaa_star.IsInOpenList(maze.cells[i, j]))
                                {
                                    //سلول ها ی موجود در لیست گره های باز
                                    maze.cells[i, j].BackColor = Color.Violet;
                                }
                                else if (gaa_star.HasBeenVisitedinLastExecution(maze.cells[i, j]))
                                {
                                    //سلول هایی که ملاقات شده اند
                                    maze.cells[i, j].BackColor = Color.LightGreen;
                                }
                            }

                            if (maze.cells[i, j] != maze.GetGoal() && maze.cells[i, j] != maze.GetStart())
                            {
                                if (maze.cells[i, j].IsPathFlagOn())
                                {
                                    maze.cells[i, j].BackColor = Color.DarkRed;
                                }

                                maze.cells[i, j].Text =
                                    (gaa_star.GetMazeCellG(maze.cells[i, j]) != 0x7FFFFFFF ? Convert.ToString(gaa_star.GetMazeCellG(maze.cells[i, j])) : infinit);
                                maze.cells[i, j].Text += "/" + (gaa_star.GetMazeCellH(maze.cells[i, j]) != 0x7FFFFFFF ? Convert.ToString(gaa_star.GetMazeCellH(maze.cells[i, j])) : infinit);
                            }
                        }
                    }
                    UpdateOpenListText("GAA*");
                }
                else if (!mazeCreated)
                {
                    MessageBox.Show("ابتدا دکمه نقشه جدید را کلیک کنید", "خطا",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("ابتدا دکمه نقشه جدید را کلیک کنید", "خطا",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 3
0
        private void buttonGAATest_Click(object sender, EventArgs e)
        {
            timer1.Start();
            PresenterThread = new Thread(new ThreadStart(() =>
            {
                SetParameter();
                t0 = DateTime.Now;

                MazeCount        = 100;
                int TotalSearchs = 0;
                int SuccessCount = 0;

                for (c = 0; c < MazeCount; c++)
                {
                    int OneSearch = 0;

                    List <LightCell> blocked_cells = new List <LightCell>(),
                    unblocked_cells = new List <LightCell>();

                    int seed = DateTime.Now.Millisecond;
                    LightCell maze_cell;
                    System.Random random = new System.Random(seed);

                    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 (!gaa_star_lazy.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 || !gaa_star_lazy.HasSolution())
                            {
                                //LightCell new_goal = new LightCell();

                                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.Count > 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();
        }