10 #ifndef OPEN_CLOSED_LIST_H_
11 #define OPEN_CLOSED_LIST_H_
13 #include "../../utils/floating_point_utils.h"
42 template<
class state_t,
class action_t>
61 BFSNode(
const state_t &node_state,
NodeID parent,
const action_t &action,
double g,
double h,
double node_eval,
91 template<
class state_t,
class action_t>
149 double h,
double node_eval,
NodeID parent);
162 double h,
double node_eval);
204 std::size_t
size()
const;
263 template<
class state_t,
class action_t>
266 : state(node_state), parent_id(parent), gen_action(action), g_cost(g), h_value(h), eval(node_eval),
267 location(loc), in_open(true), reopened(false)
271 template<
class state_t,
class action_t>
276 template<
class state_t,
class action_t>
281 template<
class state_t,
class action_t>
286 template<
class state_t,
class action_t>
290 open_list_heap.clear();
293 template<
class state_t,
class action_t>
297 bool is_stored = node_table.isNodeStored(hash_value,
id);
300 if(node_table.getNode(
id).in_open)
307 template<
class state_t,
class action_t>
309 StateHash hash_value,
double g,
double h,
double node_eval,
NodeID parent)
311 NodeID new_id = node_table.addNewSearchNode(
316 open_list_heap.push_back(new_id);
318 heapifyUp(open_list_heap.size() - 1);
326 template<
class state_t,
class action_t>
329 return open_list_heap.size();
332 template<
class state_t,
class action_t>
335 return size() - openListSize();
338 template<
class state_t,
class action_t>
341 return node_table.size();
344 template<
class state_t,
class action_t>
347 assert(loc < open_list_heap.size());
353 if(!nodeNoWorse(parent_loc, loc)) {
354 swapOpenLocations(loc, parent_loc);
356 heapifyUp(parent_loc);
362 template<
class state_t,
class action_t>
369 template<
class state_t,
class action_t>
372 return nodeNoWorse(getNode(open_list_heap[loc_1]), getNode(open_list_heap[loc_2]));
375 template<
class state_t,
class action_t>
378 if(!heapifyUp(node_table[
id].location))
379 heapifyDown(heapifyUp(node_table[
id].location));
382 template<
class state_t,
class action_t>
385 assert(node_table[
id].in_open ==
false);
386 node_table[id].in_open =
true;
387 node_table[id].reopened =
true;
388 node_table[id].location = open_list_heap.size();
389 open_list_heap.push_back(
id);
391 heapifyUp(node_table[
id].location);
394 template<
class state_t,
class action_t>
397 assert(!open_list_heap.empty());
399 NodeID best_id = open_list_heap[0];
400 node_table[best_id].in_open =
false;
402 open_list_heap[0] = open_list_heap.back();
403 node_table.getNode(open_list_heap[0]).location = 0;
404 open_list_heap.pop_back();
411 template<
class state_t,
class action_t>
414 return node_table[id];
417 template<
class state_t,
class action_t>
420 return node_table[id];
423 template<
class state_t,
class action_t>
425 StateHash hash_value,
double h,
double node_eval)
427 return addNewNodeToOpen(state, action, hash_value, 0.0, h, node_eval, 0);
430 template<
class state_t,
class action_t>
433 return open_list_heap.size() == 0;
436 template<
class state_t,
class action_t>
443 if(left_child_loc >= open_list_heap.size())
445 else if(right_child_loc >= open_list_heap.size() || nodeNoWorse(left_child_loc, right_child_loc))
446 best_child_loc = left_child_loc;
448 best_child_loc = right_child_loc;
450 if(!nodeNoWorse(loc, best_child_loc)) {
451 swapOpenLocations(loc, best_child_loc);
453 heapifyDown(best_child_loc);
460 template<
class state_t,
class action_t>
463 NodeID id = open_list_heap[loc_1];
464 open_list_heap[loc_1] = open_list_heap[loc_2];
465 open_list_heap[loc_2] = id;
467 node_table.getNode(open_list_heap[loc_1]).location = loc_1;
468 node_table.getNode(open_list_heap[loc_2]).location = loc_2;
471 template<
class state_t,
class action_t>
474 for(
unsigned i = 0; i < open_list_heap.size(); i++) {
475 std::cout <<
"ID " << open_list_heap[i] <<
", eval " << getNode(open_list_heap[i]).eval << std::endl;
uint64_t StateHash
The hash value of a state.
Definition: state_hash_function.h:14
std::vector< NodeID > open_list_heap
The heap holding node ids representing the open list.
Definition: open_closed_list.h:260
virtual bool nodeNoWorse(const BFSNode< state_t, action_t > &node_1, const BFSNode< state_t, action_t > &node_2) const
Definition: open_closed_list.h:363
BFSNode(const state_t &node_state, NodeID parent, const action_t &action, double g, double h, double node_eval, BFSOpenLocation loc)
Definition: open_closed_list.h:264
bool fp_greater(double a, double b)
Definition: floating_point_utils.cpp:17
void clear()
Definition: open_closed_list.h:287
bool heapifyDown(BFSOpenLocation loc)
Definition: open_closed_list.h:437
void swapOpenLocations(BFSOpenLocation loc_1, BFSOpenLocation loc_2)
Definition: open_closed_list.h:461
NodeTable< BFSNode< state_t, action_t > > node_table
The table of nodes being stored.
Definition: open_closed_list.h:259
Definition: open_closed_list.h:92
bool in_open
If the node is in the open list or not.
Definition: open_closed_list.h:78
action_t gen_action
A action used to generate this node.
Definition: open_closed_list.h:71
StateLocation
Definition: open_closed_list.h:32
~BFSNode()
Definition: open_closed_list.h:272
virtual void reopenNode(NodeID id)
Definition: open_closed_list.h:383
NodeID parent_id
The id of the parent of this node.
Definition: open_closed_list.h:70
virtual NodeID getBestNodeAndClose()
Definition: open_closed_list.h:395
bool isOpenEmpty() const
Definition: open_closed_list.h:431
unsigned NodeID
The ID of a node is the location of the node in the node table.
Definition: node_table.h:21
virtual ~OpenClosedList()
Definition: open_closed_list.h:282
double g_cost
The g-cost of the node.
Definition: open_closed_list.h:73
virtual NodeID addInitialNodeToOpen(const state_t &state, const action_t &action, StateHash hash_value, double h, double node_eval)
Definition: open_closed_list.h:424
BFSNode< state_t, action_t > & getNode(NodeID id)
Definition: open_closed_list.h:412
virtual void openNodeEvalChanged(NodeID id)
Definition: open_closed_list.h:376
bool heapifyUp(BFSOpenLocation loc)
Definition: open_closed_list.h:345
std::size_t size() const
Definition: open_closed_list.h:339
Definition: node_table.h:45
OpenClosedList()
Definition: open_closed_list.h:277
std::size_t openListSize() const
Definition: open_closed_list.h:327
StateLocation getStateLocation(const state_t &state, StateHash hash_value, NodeID &id)
Definition: open_closed_list.h:294
BFSOpenLocation location
The location of the node in the open list heap. Means nothing if in_open is false.
Definition: open_closed_list.h:77
bool reopened
If the node has been reopened.
Definition: open_closed_list.h:80
Definition: open_closed_list.h:43
double eval
The evaluation of the node.
Definition: open_closed_list.h:75
virtual NodeID addNewNodeToOpen(const state_t &state, const action_t &action, StateHash hash_value, double g, double h, double node_eval, NodeID parent)
Definition: open_closed_list.h:308
void printOpen()
Definition: open_closed_list.h:472
double h_value
The heuristic value of the node.
Definition: open_closed_list.h:74
unsigned BFSOpenLocation
The location of a node in the open list heap.
Definition: open_closed_list.h:18
state_t state
The state corresponding to this node.
Definition: open_closed_list.h:69
std::size_t closedListSize() const
Definition: open_closed_list.h:333