94a_star_search(GraphTp& g,
typename GraphTp::vertex_type& start,
typename GraphTp::vertex_type& end,
const H& h){
98 std::multimap<uint_t, uint_t> came_from;
103 came_from.insert(start.id, start.id);
107 typedef typename H::cost_type cost_t;
108 typedef typename GraphTp::vertex_type vertex_t;
109 typedef typename GraphTp::adjacency_iterator adjacency_iterator;
110 typedef typename GraphTp::vertex_type node_t;
113 std::set<node_t,astar_impl::id_astar_node_compare> explored;
114 searchable_priority_queue open;
118 start.data.g_cost = 0.0;
123 start.data.f_cost = h(start, end);
126 while(!open.empty()){
129 const node_t cv = open.top();
142 std::pair<adjacency_iterator,adjacency_iterator> neighbors = g.get_vertex_neighbors(cv);
143 auto itr = neighbors.first;
146 for(; itr != neighbors.second; itr++){
148 node_t& nv = g.get_vertex(itr);
150 if(open.contains(nv)){
157 if(!nv.data.can_move()){
167 auto itr = std::find_if(explored.begin(), explored.end(),
168 [=](
const node_t& n){return (n.id == nid);});
171 if(itr != explored.end()){
177 cost_t tg_cost = cv.data.g_cost + h(cv, nv);
179 if (tg_cost >= nv.data.g_cost) {
187 nv.data.g_cost = tg_cost;
190 nv.data.f_cost = nv.data.g_cost + h(nv, end);
206 return std::vector<IdTp>();
209 std::vector<IdTp> path;
210 path.push_back(start);
212 auto next_itr = map.find(start);
214 if(next_itr == map.end()){
217 throw std::logic_error(
"Key: "+std::to_string(start)+
" does not exist");
220 IdTp next = next_itr->second;
221 path.push_back(next);
223 while(next_itr!=map.end()){
225 next_itr = map.find(next);
226 if(next_itr != map.end()){
227 next = next_itr->second;
228 path.push_back(next);
233 std::vector<IdTp> the_path;
234 the_path.reserve(path.size());
235 auto itrb = path.rbegin();
236 auto itre = path.rend();
239 the_path.push_back(*itrb++);
MapType::iterator add_or_update_map(MapType &map, const KeyArgType &k, const ValueArgType &v)
See Scott Meyers Effective STL Item 24 for this function. If k isn't in the map efficiently add pair ...
Definition a_star_search.h:30
std::multimap< uint_t, uint_t > a_star_search(GraphTp &g, typename GraphTp::vertex_type &start, typename GraphTp::vertex_type &end, const H &h)
Simple implementation of A* algorithm at the moment the algorithm is only usable with a boost_unidire...
Definition a_star_search.h:94