14 #include <unordered_map> 15 #include <unordered_set> 17 #define USE_FIBONACCI_HEAP 18 #include <boost/heap/d_ary_heap.hpp> 19 #include <boost/heap/fibonacci_heap.hpp> 20 #include <boost/program_options.hpp> 78 template <
typename State,
typename Action,
typename Cost,
typename Environment,
79 typename StateHasher = std::hash<State>>
92 std::unordered_map<uint64_t, heapHandle_t, std::hash<uint64_t>> stateToHeap;
93 std::unordered_set<uint64_t, std::hash<uint64_t>> closedSet;
94 std::unordered_map<State, std::tuple<State, Action, Cost, Cost>,
99 openSet.push(Node(startState, Action(),
101 stateToHeap.insert(std::make_pair<>(m_env.
calcIndex(startState), handle));
102 (*handle).handle = handle;
104 std::vector<Neighbor<State, Action, Cost>> neighbors;
105 neighbors.reserve(10);
107 while (!openSet.empty()) {
108 Node current = openSet.top();
109 m_env.onExpandNode(current.state, current.fScore, current.gScore);
110 if (m_env.
isSolution(current.state, current.gScore, cameFrom)) {
113 auto iter = cameFrom.find(m_env.
getGoal());
114 solution.
cost = std::get<3>(iter->second);
116 std::get<3>(iter->second) +
118 while (iter != cameFrom.end()) {
125 solution.
states.push_back(
126 std::make_pair<>(iter->first, std::get<3>(iter->second)));
127 solution.
actions.push_back(std::make_pair<>(
128 std::get<1>(iter->second), std::get<2>(iter->second)));
129 iter = cameFrom.find(std::get<0>(iter->second));
131 solution.
states.push_back(std::make_pair<>(startState, initialCost));
132 std::reverse(solution.
states.begin(), solution.
states.end());
140 stateToHeap.erase(m_env.
calcIndex(current.state));
141 closedSet.insert(m_env.
calcIndex(current.state));
144 m_env.
getNeighbors(current.state, current.action, neighbors);
146 if (closedSet.find(m_env.
calcIndex(neighbor.state)) ==
148 Cost tentative_gScore = current.gScore + neighbor.cost;
149 auto iter = stateToHeap.find(m_env.
calcIndex(neighbor.state));
150 if (iter == stateToHeap.end()) {
153 auto handle = openSet.push(Node(neighbor.state, neighbor.action,
154 fScore, tentative_gScore));
155 (*handle).handle = handle;
158 std::make_pair<>(m_env.
calcIndex(neighbor.state), handle));
159 m_env.onDiscover(neighbor.state, fScore, tentative_gScore);
163 auto handle = iter->second;
167 if (tentative_gScore >= (*handle).gScore) {
172 Cost delta = (*handle).gScore - tentative_gScore;
173 (*handle).gScore = tentative_gScore;
174 (*handle).fScore -= delta;
175 (*handle).state = neighbor.state;
176 openSet.increase(handle);
177 m_env.onDiscover(neighbor.state, (*handle).fScore,
184 cameFrom.erase(neighbor.state);
185 cameFrom.insert(std::make_pair<>(
187 std::make_tuple<>(current.state, neighbor.action, neighbor.cost,
198 Node(
const State& state, Action action, Cost fScore, Cost gScore)
199 : state(state), action(action), fScore(fScore), gScore(gScore) {}
201 bool operator<(
const Node& other)
const {
207 if (fScore != other.fScore) {
208 return fScore > other.fScore;
210 return gScore < other.gScore;
214 friend std::ostream& operator<<(std::ostream& os,
const Node& node) {
215 os <<
"state: " << node.state <<
" fScore: " << node.fScore
216 <<
" gScore: " << node.gScore;
225 #ifdef USE_FIBONACCI_HEAP 226 typename boost::heap::fibonacci_heap<Node>::handle_type handle;
228 typename boost::heap::d_ary_heap<Node, boost::heap::arity<2>,
229 boost::heap::mutable_<true>>::handle_type
234 #ifdef USE_FIBONACCI_HEAP 235 typedef typename boost::heap::fibonacci_heap<Node> openSet_t;
236 typedef typename openSet_t::handle_type heapHandle_t;
238 typedef typename boost::heap::d_ary_heap<Node, boost::heap::arity<2>,
239 boost::heap::mutable_<true>>
241 typedef typename openSet_t::handle_type heapHandle_t;
bool search(const State &startState, PlanResult< State, Action, Cost > &solution, Cost initialCost=0)
Definition: hybrid_astar.hpp:85
std::vector< std::pair< Action, Cost > > actions
actions and their cost
Definition: planresult.hpp:32
Spatiotemporal Hybrid A* Algorithm.
Definition: hybrid_astar.hpp:80
HybridAStar(Environment &environment)
Definition: hybrid_astar.hpp:82
uint64_t calcIndex(const State &s)
Definition: environment.hpp:367
std::vector< std::pair< State, Cost > > states
states and their gScore
Definition: planresult.hpp:30
Represents state transations.
Definition: neighbor.hpp:24
Definition: cl_cbs.hpp:19
Cost cost
actual cost of the result
Definition: planresult.hpp:34
int admissibleHeuristic(const State &s)
Definition: environment.hpp:199
State getGoal()
Definition: environment.hpp:366
Represents the path for an agent.
Definition: planresult.hpp:28
~HybridAStar()
Definition: hybrid_astar.hpp:83
bool isSolution(const State &state, double gscore, std::unordered_map< State, std::tuple< State, Action, double, double >, std::hash< State >> &_camefrom)
Definition: environment.hpp:232
void getNeighbors(const State &s, Action action, std::vector< Neighbor< State, Action, double >> &neighbors)
Definition: environment.hpp:323
Cost fmin
lower bound of the cost (for suboptimal solvers)
Definition: planresult.hpp:36