30 #ifndef PLANNER_CSPACE_GRID_ASTAR_H 31 #define PLANNER_CSPACE_GRID_ASTAR_H 34 #define _USE_MATH_DEFINES 39 #include <unordered_map> 42 #include <boost/chrono.hpp> 50 template <
int DIM = 3,
int NONCYCLIC = 2>
57 template <
class T,
int block_w
idth = 0x20>
96 const Vec& p0,
const Vec& p1,
97 const float cost_estim,
const float cost)
100 , cost_estim_(cost_estim)
159 const Vec& s,
const Vec& e,
160 std::list<Vec>& path,
161 std::function<
float(
const Vec&,
Vec&,
const Vec&,
const Vec&)> cb_cost,
162 std::function<
float(
const Vec&,
const Vec&)> cb_cost_estim,
163 std::function<std::vector<Vec>&(
const Vec&,
const Vec&,
const Vec&)> cb_search,
164 std::function<
bool(
const std::list<Vec>&)> cb_progress,
165 const float cost_leave,
166 const float progress_interval,
167 const bool return_best =
false)
170 cb_cost, cb_cost_estim, cb_search, cb_progress,
171 cost_leave, progress_interval, return_best);
177 const Vec& st,
const Vec& en,
178 std::list<Vec>& path,
179 std::function<
float(
const Vec&,
Vec&,
const Vec&,
const Vec&)> cb_cost,
180 std::function<
float(
const Vec&,
const Vec&)> cb_cost_estim,
181 std::function<std::vector<Vec>&(
const Vec&,
const Vec&,
const Vec&)> cb_search,
182 std::function<
bool(
const std::list<Vec>&)> cb_progress,
183 const float cost_leave,
184 const float progress_interval,
185 const bool return_best =
false)
193 for (
int i = NONCYCLIC; i < DIM; i++)
203 open_.push(PriorityVec(cb_cost_estim(s, e), 0, s));
205 auto ts = boost::chrono::high_resolution_clock::now();
208 int cost_estim_min = cb_cost_estim(s, e);
213 if (
open_.size() < 1)
223 std::vector<PriorityVec> centers;
226 if (
open_.size() == 0)
228 PriorityVec center =
open_.top();
230 if (center.v_ == e || center.p_ - center.p_raw_ <= cost_leave)
236 centers.push_back(center);
240 auto tnow = boost::chrono::high_resolution_clock::now();
241 if (boost::chrono::duration<float>(tnow - ts).count() >= progress_interval)
243 std::list<Vec> path_tmp;
246 cb_progress(path_tmp);
251 std::list<GridmapUpdate> updates;
254 #pragma omp for schedule(static) 255 for (
auto it = centers.cbegin(); it < centers.cend(); ++it)
257 const Vec p = it->v_;
258 const float c = it->p_raw_;
259 const float c_estim = it->p_;
263 if (c_estim - c < cost_estim_min)
265 cost_estim_min = c_estim - c;
269 const std::vector<Vec> search_list = cb_search(p, s, e);
272 for (
auto it = search_list.cbegin(); it < search_list.cend(); ++it)
283 const float cost_estim = cb_cost_estim(next, e);
284 if (cost_estim < 0 || cost_estim == FLT_MAX)
287 const float cost = cb_cost(p, next, s, e);
288 if (cost < 0 || cost == FLT_MAX)
291 const float cost_next = c + cost;
292 if (g[next] > cost_next)
296 GridmapUpdate(p, next, cost_next + cost_estim, cost_next));
307 for (
const GridmapUpdate& u : updates)
309 if (g[u.getPos()] > u.getCost())
311 g[u.getPos()] = u.getCost();
312 parents_[u.getPos()] = u.getParentPos();
313 open_.push(u.getPriorityVec());
318 for (
const Vec& p : dont)
353 #endif // PLANNER_CSPACE_GRID_ASTAR_H const Vec & getPos() const
bool searchImpl(Gridmap< float > &g, const Vec &st, const Vec &en, std::list< Vec > &path, std::function< float(const Vec &, Vec &, const Vec &, const Vec &)> cb_cost, std::function< float(const Vec &, const Vec &)> cb_cost_estim, std::function< std::vector< Vec > &(const Vec &, const Vec &, const Vec &)> cb_search, std::function< bool(const std::list< Vec > &)> cb_progress, const float cost_leave, const float progress_interval, const bool return_best=false)
const PriorityVec getPriorityVec() const
void cycleUnsigned(const int res, const ArgList &...rest)
constexpr int getNoncyclic() const
const Vec & getParentPos() const
void setSearchTaskNum(const size_t &search_task_num)
const float getCost() const
PriorityVec(const float &p, const float &p_raw, const Vec &v)
void reset(const CyclicVecInt< DIM, NONCYCLIC > &size)
constexpr int getDim() const
void setQueueSizeLimit(const size_t size)
GridAstar(const Vec size)
bool search(const Vec &s, const Vec &e, std::list< Vec > &path, std::function< float(const Vec &, Vec &, const Vec &, const Vec &)> cb_cost, std::function< float(const Vec &, const Vec &)> cb_cost_estim, std::function< std::vector< Vec > &(const Vec &, const Vec &, const Vec &)> cb_search, std::function< bool(const std::list< Vec > &)> cb_progress, const float cost_leave, const float progress_interval, const bool return_best=false)
std::unordered_map< Vec, Vec, Vec > parents_
const CyclicVecInt< DIM, NONCYCLIC > & size() const
bool isExceeded(const CyclicVecBase< DIM, NONCYCLIC, int > &v) const
reservable_priority_queue< PriorityVec > open_
bool operator<(const PriorityVec &b) const
void reset(const Vec size)
GridmapUpdate(const Vec &p0, const Vec &p1, const float cost_estim, const float cost)
bool findPath(const Vec &s, const Vec &e, std::list< Vec > &path)