A * 算法C++实现

伪代码

openset = PriorityQueue() // pop out node with least totoal cost (heuriscost + trajcost)
openset.push(start, 0)
closedset = {
   }
trajcost[start] = 0

while not openset.empty():
      current = openset.top()
      
      if current = goal:
         break;
      
      openset.pop()
      closedset.put(current)
      
      for next in neighbors(current):
          if (checkcollison(next)):
              delete next
              continue
          if find in closedset:
              delete next
              continue
          next.trajcost = current.trajcost +  calc_trajcost(current, next)
          if not find in openset:
             next.heuricost = calc_heuriscost(next, goal)
             next.totalcost = next.trajcost + next.heuriscost
             parent[next] = current
             openset.push(next)
          if find in openset and openset[next].trajcost > next.trajcost:
             openset[next].trajcost = next.trajcost
             openset[next].totalcost =  openset[next].trajcost + openset[next].heuriscost

reconstruct(goal)
delete start
delete goal

/*************************************************************************
	> File Name: grid_a_star.cpp
	> Author: Yongyu Chen
	> Mail: yongyu.chen@tum.de
 ************************************************************************/

#include <iostream>
// #include <cmath>
#include <limits>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <matplotlibcpp.h>
#include <unordered_map>

#include <boost/heap/binomial_heap.hpp>

using namespace std;
namespace plt = matplotlibcpp;
/**
 * @brief: Grid base A* shortest path planning
 */ 
class Node2d {
   
public:
    int x; // x index of the grid
    int y; // y index of the grid 
    double sum_cost; // cost, euclidean distance 
    double path_cost; // from start node to the current node
    double h_cost; // from 
    Node2d* p_node; // parent node, or replaced by the 

    // constructor 
    Node2d(int x_, int y_, double sum_cost_ = 0, double path_cost_ = 0, double h_cost_ = 0, 
        Node2d* p_node_ = nullptr): x(x_), y(y_),
        sum_cost(sum_cost_), path_cost(path_cost_), h_cost(h_cost_), p_node(p_node_){
   };
};
int calc_xyindex(double position, double min_pos, double reso) {
   
  return std::round((position - min_pos) / reso);
}
int calc_grid_index(Node2d& node, int xwidth, int ywidth) {
   
  return node.y * xwidth + node.x;  
}
double calc_grid_position(int index, double minp, double reso) {
   
  return index * reso + minp;
}

//  
/**
 * @brief:construct the final path after the algorithm is done 
 */ 
std::vector<std::vector<double>> calc_final_path(Node2d* goal, double minx, double miny, 
                                                    double reso) {
   
    std::vector<double> rx;
    std::vector<double> ry;
    Node2d* node = goal;
    while (node->p_node != nullptr) {
    // while the parent node exists
        node = node->p_node;
        rx.push_back(calc_grid_position(node->x, minx, reso));
        ry.push_back(calc_grid_position(node->y, miny, reso));
    }
    return {
   rx, ry};
}
/**
 * @brief: construct the realtime obstacle map
 * 
 */ 
// obstacle map 是一个二维数组包含0和1 
std::vector<std::vector<int> > calc_obstacle_map(
      vector<double>& ox, vector<double>& oy, 
      int& xwidth, int
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
a*算法是一种常用的启发式搜索算法,用于求解路径规划问题。它通过在搜索过程中综合考虑最短路径的代价和预估到目标节点的代价,从而找到最优的路径。 a*算法的基本思想是,在搜索过程中维护一个优先级队列,其中存放着待扩展的节点。算法从起始节点开始,根据节点的代价评估函数(通常为从起始节点到当前节点的已知代价和预估到目标节点的代价之和)计算节点的优先级,并将其插入队列中。然后,从队列中选取优先级最高的节点作为当前节点,并对其进行扩展。 在扩展过程中,算法考虑与当前节点相邻的所有节点,计算它们的代价评估值,并根据这些值更新它们的优先级。如果某个节点已经在队列中存在,并且新的代价评估值更小,那么需要更新队列中该节点的优先级。一直重复以上步骤,直到找到目标节点或者搜索失败(即队列为空)为止。 a*算法的特点是效率较高,因为它通过启发式函数对搜索空间进行剪枝,有利于减少搜索的节点数量。同时,算法还可以通过调整启发式函数的设计来适应不同的问题需求。 在工程下载中,我们可以使用a*算法实现高效的下载管理。例如,可以将下载过程看作是一个路径规划问题,起始节点是我们当前所在的网络位置,目标节点是下载文件所在的服务器位置。算法可以根据网络状况、下载速度等信息来计算节点的代价评估值,并通过搜索找到最优的下载路径。 总而言之,a*算法是一种高效的启发式搜索算法,适用于求解路径规划问题。在工程下载中,可以利用a*算法实现下载管理,提高下载效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值