Assignment Search Framework
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
open_closed_list.h
Go to the documentation of this file.
1 /*
2  * open_closed_list.h
3  *
4  * LICENSE HERE
5  *
6  * Created on: 2016-09-12
7  * Author: Rick Valenzano
8  */
9 
10 #ifndef OPEN_CLOSED_LIST_H_
11 #define OPEN_CLOSED_LIST_H_
12 
13 #include "../../utils/floating_point_utils.h"
14 #include "node_table.h"
15 #include <stdio.h>
16 #include <iostream>
17 
18 typedef unsigned BFSOpenLocation;
19 
32 enum class StateLocation
33 {
34  open, closed, none
35 };
36 
42 template<class state_t, class action_t>
43 class BFSNode
44 {
45 public:
61  BFSNode(const state_t &node_state, NodeID parent, const action_t &action, double g, double h, double node_eval,
62  BFSOpenLocation loc);
63 
67  ~BFSNode();
68 
69  state_t state;
71  action_t gen_action;
72 
73  double g_cost;
74  double h_value;
75  double eval;
76 
78  bool in_open;
79 
80  bool reopened;
81 };
82 
91 template<class state_t, class action_t>
93 {
94 public:
99 
103  virtual ~OpenClosedList();
104 
108  void clear();
109 
118  StateLocation getStateLocation(const state_t &state, StateHash hash_value, NodeID &id);
119 
127 
134  const BFSNode<state_t, action_t> &getNode(NodeID id) const;
135 
148  virtual NodeID addNewNodeToOpen(const state_t &state, const action_t &action, StateHash hash_value, double g,
149  double h, double node_eval, NodeID parent);
150 
161  virtual NodeID addInitialNodeToOpen(const state_t &state, const action_t &action, StateHash hash_value,
162  double h, double node_eval);
163 
169  virtual NodeID getBestNodeAndClose();
170 
176  virtual void openNodeEvalChanged(NodeID id);
177 
183  virtual void reopenNode(NodeID id);
184 
190  std::size_t openListSize() const;
191 
197  std::size_t closedListSize() const;
198 
204  std::size_t size() const;
205 
211  bool isOpenEmpty() const;
212 
213 protected:
221  virtual bool nodeNoWorse(const BFSNode<state_t, action_t> &node_1,
222  const BFSNode<state_t, action_t> &node_2) const;
223 
231  virtual bool nodeNoWorse(BFSOpenLocation loc_1, BFSOpenLocation loc_2) const;
232 
239  bool heapifyUp(BFSOpenLocation loc);
240 
247  bool heapifyDown(BFSOpenLocation loc);
248 
256 
257  void printOpen();
258 
260  std::vector<NodeID> open_list_heap;
261 };
262 
263 template<class state_t, class action_t>
264 BFSNode<state_t, action_t>::BFSNode(const state_t& node_state, NodeID parent, const action_t& action, double g,
265  double h, double node_eval, BFSOpenLocation loc)
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)
268 {
269 }
270 
271 template<class state_t, class action_t>
273 {
274 }
275 
276 template<class state_t, class action_t>
278 {
279 }
280 
281 template<class state_t, class action_t>
283 {
284 }
285 
286 template<class state_t, class action_t>
288 {
289  node_table.clear();
290  open_list_heap.clear();
291 }
292 
293 template<class state_t, class action_t>
295  NodeID& id)
296 {
297  bool is_stored = node_table.isNodeStored(hash_value, id);
298 
299  if(is_stored) {
300  if(node_table.getNode(id).in_open)
301  return StateLocation::open;
302  return StateLocation::closed;
303  }
304  return StateLocation::none;
305 }
306 
307 template<class state_t, class action_t>
308 NodeID OpenClosedList<state_t, action_t>::addNewNodeToOpen(const state_t& state, const action_t &action,
309  StateHash hash_value, double g, double h, double node_eval, NodeID parent)
310 {
311  NodeID new_id = node_table.addNewSearchNode(
312  BFSNode<state_t, action_t>(state, parent, action, g, h, node_eval, open_list_heap.size()), hash_value);
313 
314  //std::cout << "New ID " << new_id << std::endl;
315  //std::cout << "Node Table Size " << node_table.size() << std::endl;
316  open_list_heap.push_back(new_id);
317 
318  heapifyUp(open_list_heap.size() - 1);
319 
320  //printOpen();
321  //std::cout << std::endl;
322 
323  return new_id;
324 }
325 
326 template<class state_t, class action_t>
328 {
329  return open_list_heap.size();
330 }
331 
332 template<class state_t, class action_t>
334 {
335  return size() - openListSize();
336 }
337 
338 template<class state_t, class action_t>
339 inline std::size_t OpenClosedList<state_t, action_t>::size() const
340 {
341  return node_table.size();
342 }
343 
344 template<class state_t, class action_t>
346 {
347  assert(loc < open_list_heap.size());
348  if(loc == 0)
349  return false;
350 
351  BFSOpenLocation parent_loc = (loc - 1) / 2;
352 
353  if(!nodeNoWorse(parent_loc, loc)) {
354  swapOpenLocations(loc, parent_loc);
355 
356  heapifyUp(parent_loc);
357  return true;
358  }
359  return false;
360 }
361 
362 template<class state_t, class action_t>
364  const BFSNode<state_t, action_t>& node_2) const
365 {
366  return !fp_greater(node_1.eval, node_2.eval);
367 }
368 
369 template<class state_t, class action_t>
371 {
372  return nodeNoWorse(getNode(open_list_heap[loc_1]), getNode(open_list_heap[loc_2]));
373 }
374 
375 template<class state_t, class action_t>
377 {
378  if(!heapifyUp(node_table[id].location))
379  heapifyDown(heapifyUp(node_table[id].location));
380 }
381 
382 template<class state_t, class action_t>
384 {
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);
390 
391  heapifyUp(node_table[id].location);
392 }
393 
394 template<class state_t, class action_t>
396 {
397  assert(!open_list_heap.empty());
398 
399  NodeID best_id = open_list_heap[0];
400  node_table[best_id].in_open = false;
401 
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();
405 
406  heapifyDown(0);
407 
408  return best_id;
409 }
410 
411 template<class state_t, class action_t>
413 {
414  return node_table[id];
415 }
416 
417 template<class state_t, class action_t>
419 {
420  return node_table[id];
421 }
422 
423 template<class state_t, class action_t>
424 inline NodeID OpenClosedList<state_t, action_t>::addInitialNodeToOpen(const state_t& state, const action_t& action,
425  StateHash hash_value, double h, double node_eval)
426 {
427  return addNewNodeToOpen(state, action, hash_value, 0.0, h, node_eval, 0);
428 }
429 
430 template<class state_t, class action_t>
432 {
433  return open_list_heap.size() == 0;
434 }
435 
436 template<class state_t, class action_t>
438 {
439  BFSOpenLocation left_child_loc = loc * 2 + 1;
440  BFSOpenLocation right_child_loc = loc * 2 + 2;
441  BFSOpenLocation best_child_loc;
442 
443  if(left_child_loc >= open_list_heap.size())
444  return false;
445  else if(right_child_loc >= open_list_heap.size() || nodeNoWorse(left_child_loc, right_child_loc))
446  best_child_loc = left_child_loc;
447  else
448  best_child_loc = right_child_loc;
449 
450  if(!nodeNoWorse(loc, best_child_loc)) {
451  swapOpenLocations(loc, best_child_loc);
452 
453  heapifyDown(best_child_loc);
454  return true;
455  }
456 
457  return false;
458 }
459 
460 template<class state_t, class action_t>
462 {
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;
466 
467  node_table.getNode(open_list_heap[loc_1]).location = loc_1;
468  node_table.getNode(open_list_heap[loc_2]).location = loc_2;
469 }
470 
471 template<class state_t, class action_t>
473 {
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;
476  }
477 }
478 
479 #endif /* OPEN_CLOSED_LIST_H_ */
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