app/astar.cpp
// Gives vector of x and y coordinates of optimal path given the full map.
Gives vector of x and y coordinates of optimal path given the full map.
Gives vector of x and y coordinates of optimal path given the full map .
std::vector<std::pair<int, int>> astar::astar_path(
                                std::vector<std::vector<int>> map) {
    int neighbours = 8;  
    std::vector<std::pair<int, int>> path;
    std::priority_queue<nodes, std::vector<nodes>,
                        std::greater<nodes>> open_list;
    std::priority_queue<nodes, std::vector<nodes>,
                          std::greater<nodes>> temp_list;
    std::vector<std::vector<int> > open(800, std::vector<int>(800, 0));
    std::vector<std::vector<int> > visited(800, std::vector<int>(800, 0));
    std::vector<std::vector<int> > parentDir(800, std::vector<int>(800, 0));
    std::vector<int> moveX { 1, 1, 0, -1, -1, -1, 0, 1 };
    std::vector<int> moveY { 0, 1, 1, 1, 0, -1, -1, -1 };
    nodes start(x_start_, y_start_, 0, 0);
    start.f_cost_ = start.compute_f(x_goal_, y_goal_);
    open_list.push(start);
    open[start.x_][start.y_] = start.f_cost_;
    while (!open_list.empty()) {
      nodes current = open_list.top();
      open_list.pop();
      open[current.x_][current.y_] = 0;
      visited[current.x_][current.y_] = 1;
      if (current.x_ == x_goal_ && current.y_ == y_goal_) {
        int p = current.x_, q = current.y_;
        while (!(p == x_start_ && q == y_start_)) {
          int j = parentDir[p][q];
          path.push_back(std::make_pair(p, q));
          p += moveX[j];
          q += moveY[j];
        }
        return path;
      }
      int dir = 0;
      while (dir < 8) {
          int dx = current.x_ + moveX[dir], dy = current.y_ + moveY[dir];
        if (!(dx < 0 || dx > 799 || dy < 0 || dy > 799 ||
              map[dx][dy] == 0 || visited[dx][dy] == 1)) {
            nodes child(dx, dy, current.g_cost_, current.f_cost_);
            child.g_cost_ = child.compute_g(dir);
            child.f_cost_ = child.compute_f(x_goal_, y_goal_);
          if (open[dx][dy] == 0) {
              open[dx][dy] = child.f_cost_;
              open_list.push(child);
              parentDir[dx][dy] = (dir + neighbours / 2) % neighbours;
            } else if (open[dx][dy] > child.f_cost_) {
              open[dx][dy] = child.f_cost_;
              parentDir[dx][dy] = (dir + 8 / 2) % 8;
            while (!(open_list.top().x_ == dx && open_list.top().y_ == dy)) {
              temp_list.push(open_list.top());
              open_list.pop();
            }
            open_list.pop();
            while (!temp_list.empty()) {
              open_list.push(temp_list.top());
              temp_list.pop();
            }
            open_list.push(child);
          }
        }
        ++dir;
      }
    }
    std::cout << "Path not found."
              << std::endl;
    path.push_back(std::make_pair(-1, -1));
    return path;
}
std :: vector < std :: pair < int , int > > astar :: astar_path ( std :: vector < std :: vector < int > > map ) { int neighbours = 8 ; std :: vector < std :: pair < int , int > > path ; std :: priority_queue < nodes , std :: vector < nodes > , std :: greater < nodes > > open_list ; std :: priority_queue < nodes , std :: vector < nodes > , std :: greater < nodes > > temp_list ; std :: vector < std :: vector < int > > open ( 800 , std :: vector < int > ( 800 , 0 ) ) ; std :: vector < std :: vector < int > > visited ( 800 , std :: vector < int > ( 800 , 0 ) ) ; std :: vector < std :: vector < int > > parentDir ( 800 , std :: vector < int > ( 800 , 0 ) ) ; std :: vector < int > moveX { 1 , 1 , 0 , -1 , -1 , -1 , 0 , 1 } ; std :: vector < int > moveY { 0 , 1 , 1 , 1 , 0 , -1 , -1 , -1 } ; nodes start ( x_start_ , y_start_ , 0 , 0 ) ; start . f_cost_ = start . compute_f ( x_goal_ , y_goal_ ) ; open_list . push ( start ) ; open [ start . x_ ] [ start . y_ ] = start . f_cost_ ; while ( ! open_list . empty ( ) ) { nodes current = open_list . top ( ) ; open_list . pop ( ) ; open [ current . x_ ] [ current . y_ ] = 0 ; visited [ current . x_ ] [ current . y_ ] = 1 ; if ( current . x_ == x_goal_ && current . y_ == y_goal_ ) { int p = current . x_ , q = current . y_ ; while ( ! ( p == x_start_ && q == y_start_ ) ) { int j = parentDir [ p ] [ q ] ; path . push_back ( std :: make_pair ( p , q ) ) ; p += moveX [ j ] ; q += moveY [ j ] ; } return path ; } int dir = 0 ; while ( dir < 8 ) { int dx = current . x_ + moveX [ dir ] , dy = current . y_ + moveY [ dir ] ; if ( ! ( dx < 0 || dx > 799 || dy < 0 || dy > 799 || map [ dx ] [ dy ] == 0 || visited [ dx ] [ dy ] == 1 ) ) { nodes child ( dx , dy , current . g_cost_ , current . f_cost_ ) ; child . g_cost_ = child . compute_g ( dir ) ; child . f_cost_ = child . compute_f ( x_goal_ , y_goal_ ) ; if ( open [ dx ] [ dy ] == 0 ) { open [ dx ] [ dy ] = child . f_cost_ ; open_list . push ( child ) ; parentDir [ dx ] [ dy ] = ( dir + neighbours / 2 ) % neighbours ; } else if ( open [ dx ] [ dy ] > child . f_cost_ ) { open [ dx ] [ dy ] = child . f_cost_ ; parentDir [ dx ] [ dy ] = ( dir + 8 / 2 ) % 8 ; while ( ! ( open_list . top ( ) . x_ == dx && open_list . top ( ) . y_ == dy ) ) { temp_list . push ( open_list . top ( ) ) ; open_list . pop ( ) ; } open_list . pop ( ) ; while ( ! temp_list . empty ( ) ) { open_list . push ( temp_list . top ( ) ) ; temp_list . pop ( ) ; } open_list . push ( child ) ; } } ++ dir ; } } std :: cout << " " << std :: endl ; path . push_back ( std :: make_pair ( -1 , -1 ) ) ; return path ; }
Gives vector of x and y coordinates of optimal path given the full map.
Gives vector of x and y coordinates of optimal path given the full map .
// max no of possible neighbors for any node // Create a priority list of open nodes // Create a priority list to store temporary nodes // Create 2D vectors to store open and visited nodes. // 8 possible moves in x and y direction /**This loop takes the first entry of the open list as the current node
     * since it has the lowest total cost function.*/ /** Trace the path and store it of the goal is found.*/ /** Expand all the nodes for current node, calculate their 
       * costs and store them in their variables.*/ /** For all the nodes lying outside the map, blocked
           * or is in the visisted list then simply ignor them.*/ /* For new nodes, calculate their costs and store them.*/ /* If the node exists in open list but has more
            * promising total cost then it is replaced by
            * the previous node and it's parent direction is updated.*/