Esempio n. 1
0
 public bool IsInOpenList(LightCell maze_cell)
 {
     return(open_list.Has(graph[maze_cell.Y, maze_cell.X]));
 }
Esempio n. 2
0
        public void Solve()
        {
            has_solution           = false;
            has_execution_finished = false;

            Debug.Assert(path_cost_last_execution != INFINITY_INT);

            {
                /* If the goal changed update the heuristic. */
                if (piece_of_pseudo_code_being_executed == PieceOfPseudoCode.Null)
                {
                    if (new_goal != null)
                    {
                        Debug.Assert(path_cost.Count > n_searches);
                        InitializeNode(new_goal);
                        if (new_goal.g != INFINITY_INT && new_goal.g + new_goal.h < path_cost[n_searches])
                        {
                            new_goal.h = path_cost[n_searches] - new_goal.g;
                        }
                        delta_h.Add(delta_h[n_searches] + new_goal.h);
                        goal     = new_goal;
                        new_goal = null;
                    }
                    else
                    {
                        delta_h.Add(delta_h[n_searches]);
                    }
                    n_searches++;
                    open_list.Clear();
                }

                /* If some cell became unblocked. */
                if (unblocked_cells != null)
                {
                    Debug.Assert(piece_of_pseudo_code_being_executed == PieceOfPseudoCode.Null ||
                                 piece_of_pseudo_code_being_executed == PieceOfPseudoCode.PIECE_3A_9A ||
                                 piece_of_pseudo_code_being_executed == PieceOfPseudoCode.PIECE_10A_17A);
                    if (piece_of_pseudo_code_being_executed != PieceOfPseudoCode.PIECE_10A_17A)
                    {
                        piece_of_pseudo_code_being_executed = PieceOfPseudoCode.PIECE_3A_9A;

                        for (IEnumerator i = unblocked_cells.GetEnumerator(); i.MoveNext() != false;)
                        //for (Iterator<MazeLightCell> i = unblocked_cells.iterator(); i.hasNext(); )
                        {
                            LightCell maze_cell = (LightCell)i.Current;//i.next();
                            //i.remove();
                            GAAStarNode node = graph[maze_cell.Y, maze_cell.X];
                            InitializeNode(node);
                            int cost = node.GetMazeLightCell().GetCost();
                            Debug.Assert(!node.GetMazeLightCell().IsBlocked());

                            for (int j = 0; j < neighborhood; j++)
                            {
                                int x, y;
                                x = node.GetMazeLightCell().X + Maze.delta_x[j];
                                y = node.GetMazeLightCell().Y + Maze.delta_y[j];
                                if (0 <= x && x < w && 0 <= y && y < h)
                                {
                                    GAAStarNode predecessor = graph[y, x];
                                    InitializeNode(predecessor);

                                    if (predecessor.h > cost + node.h)
                                    {
                                        predecessor.h = cost + node.h;
                                        predecessor.f = predecessor.h;
                                        open_list.Insert(predecessor);
                                    }
                                }
                            }
                            if (step_by_step)
                            {
                                return;
                            }
                        }
                    }

                    piece_of_pseudo_code_being_executed = PieceOfPseudoCode.PIECE_10A_17A;
                    while (open_list.Size() > 0)
                    {
                        GAAStarNode node = (GAAStarNode)open_list.Pop();
                        int         cost = node.GetMazeLightCell().GetCost();

                        for (int i = 0; i < neighborhood; i++)
                        {
                            int x, y;
                            x = node.GetMazeLightCell().X + Maze.delta_x[i];
                            y = node.GetMazeLightCell().Y + Maze.delta_y[i];
                            if (0 <= x && x < w && 0 <= y && y < h)
                            {
                                GAAStarNode predecessor = graph[y, x];
                                if (predecessor == goal)
                                {
                                    continue;
                                }
                                InitializeNode(predecessor);

                                if (predecessor.h > cost + node.h)
                                {
                                    predecessor.h = cost + node.h;
                                    predecessor.f = predecessor.h;
                                    open_list.Insert(predecessor);
                                }
                            }
                        }
                        if (step_by_step)
                        {
                            return;
                        }
                    }
                    unblocked_cells = null;
                }
            }

            /* A*. */
            if (piece_of_pseudo_code_being_executed != PieceOfPseudoCode.PIECE_12_21)
            {
                InitializeNode(start);
                InitializeNode(goal);
                start.g      = start.GetMazeLightCell().GetCost();
                start.parent = null;
                start.f      = start.g + start.h;
                open_list.Insert(start);
                piece_of_pseudo_code_being_executed = PieceOfPseudoCode.PIECE_12_21;
                if (step_by_step)
                {
                    return;
                }
            }

            while (open_list.Size() > 0 && goal.g > ((GAAStarNode)open_list.Peek()).f)
            {
                GAAStarNode node = (GAAStarNode)open_list.Pop();

                for (int i = 0; i < neighborhood; i++)
                {
                    int x, y;
                    x = node.GetMazeLightCell().X + Maze.delta_x[i];
                    y = node.GetMazeLightCell().Y + Maze.delta_y[i];
                    if (0 <= x && x < w && 0 <= y && y < h)
                    {
                        GAAStarNode child = graph[y, x];
                        if (child.GetMazeLightCell().IsBlocked())
                        {
                            continue;
                        }
                        InitializeNode(child);
                        int cost = child.GetMazeLightCell().GetCost();

                        if (node.g != INFINITY_INT && child.g > node.g + cost)
                        {
                            child.parent = node;
                            child.g      = node.g + cost;
                            child.f      = child.g + child.h;
                            open_list.Insert(child);
                        }
                    }
                }
                if (step_by_step)
                {
                    return;
                }
            }

            if (open_list.Size() != 0)
            {
                GAAStarNode node = goal, node_child = null;
                path_cost_last_execution = node.g;
                has_solution             = true;

                if (mark_path)
                {
                    node.GetMazeLightCell().SetNextMazeCell(null);
                    node.GetMazeLightCell().SetPathFlag();
                    node_child = node;
                    node       = node.parent;

                    while (node != null)
                    {
                        node.GetMazeLightCell().SetNextMazeCell(node_child.GetMazeLightCell());
                        node.GetMazeLightCell().SetPathFlag();
                        node_child = node;
                        node       = node.parent;
                    }
                }
            }

            if (has_solution)
            {
                path_cost.Add(path_cost_last_execution);
            }
            else
            {
                path_cost.Add(INFINITY_INT);
            }
            piece_of_pseudo_code_being_executed = PieceOfPseudoCode.Null;
            has_execution_finished = true;
        }
Esempio n. 3
0
 public void InformNewGoal(LightCell new_goal)
 {
     this.new_goal = graph[new_goal.Y, new_goal.X];
 }
Esempio n. 4
0
 public void InformNewStart(LightCell new_start)
 {
     start = graph[new_start.Y, new_start.X];
 }
Esempio n. 5
0
 public int GetMazeLightCellH(LightCell maze_cell)
 {
     return(graph[maze_cell.Y, maze_cell.X].h);
 }
Esempio n. 6
0
 public bool HasBeenVisitedinLastExecution(LightCell maze_cell)
 {
     return(graph[maze_cell.Y, maze_cell.X].search == n_searches);
 }
Esempio n. 7
0
 public AStarNode(LightCell maze_cell, TieBreakingStrategy tie_breaking_strategy)
 {
     closed                     = false;
     this.maze_cell             = maze_cell;
     this.tie_breaking_strategy = tie_breaking_strategy;
 }
Esempio n. 8
0
 public void SetNextMazeCell(LightCell maze_cell)
 {
     next_maze_cell = maze_cell;
 }
Esempio n. 9
0
 public double ManhatanDistance(LightCell goal)
 {
     return(Math.Abs(this.X - goal.X) + Math.Abs(this.Y - goal.Y));
 }
Esempio n. 10
0
 public double RealDistance(LightCell goal)
 {
     return(Math.Sqrt((this.X - goal.X) * (this.X - goal.X) +
                      (this.Y - goal.Y) * (this.Y - goal.Y)));
 }
Esempio n. 11
0
 public int DistanceToGoal(LightCell maze_cell, LightCell goal)
 {
     return(0);
 }