HOG2
CFOptimalRefinement.h
Go to the documentation of this file.
1 /*
2  * CFOptimalRefinement.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 5/22/07.
6  * Copyright 2007 __MyCompanyName__. All rights reserved.
7  *
8  */
9 
10 #ifndef CFOPTIMALREFINEMENT_H
11 #define CFOPTIMALREFINEMENT_H
12 
13 #include "SearchAlgorithm.h"
14 #include "OpenClosedList.h"
15 #include "FPUtil.h"
16 #include "Graph.h"
17 #include "GraphAbstraction.h"
18 
19 #include <unordered_map>
20 
22 
24  enum {
25  kAbstractionLevel = 0, // this is a LONG label
26  kCorrespondingNode = 1, // this is a LONG label
27  kGCost = 2, // this is a DOUBLE label
28  kHCost = 3, // this is a DOUBLE label
29  kOptimalFlag = 4, // this is a LONG label
30  kInOpenList = 5 // this is a LONG label
31  };
32 
33  struct GNode {
34  GNode(node *nn) :n(nn) {}
35  GNode() :n(0) {}
36  node *n;
37  };
38 
39  struct NodeEqual {
40  bool operator()(const GNode &i1, const GNode &i2) const
41  { return (i1.n->getUniqueID() == i2.n->getUniqueID()); }
42  };
43 
44  struct NodeCompare {
45  // return true if we prefer i2 over i1
46  bool operator()(const GNode &i1, const GNode &i2) const
47  {
48  // if f-cost is tied
49  if (fequal(i1.n->GetLabelF(kGCost)+i1.n->GetLabelF(kHCost),
50  i2.n->GetLabelF(kGCost)+i2.n->GetLabelF(kHCost)))
51  {
52  // if both in/not in open list
53  if (i1.n->GetLabelL(kInOpenList) == i2.n->GetLabelL(kInOpenList))
54  {
55  if ((i1.n->GetLabelL(kInOpenList) == 0) && (i1.n->GetLabelL(kAbstractionLevel) == 0))
56  return true;
57  if ((i2.n->GetLabelL(kInOpenList) == 0) && (i2.n->GetLabelL(kAbstractionLevel) == 0))
58  return false;
59 
60  return fgreater(std::min(i1.n->GetLabelF(kGCost), i1.n->GetLabelF(kHCost)),
62 
63  // return true if node1 has lower g-cost
64  //return (fless(i1.n->GetLabelF(kGCost), i2.n->GetLabelF(kGCost)));
65  }
66  // return true if node1 isn't in open list
67  return (i2.n->GetLabelL(kInOpenList));
68  }
69  // return true if node1 has higher f-cost
70  return (fgreater(i1.n->GetLabelF(kGCost)+i1.n->GetLabelF(kHCost),
71  i2.n->GetLabelF(kGCost)+i2.n->GetLabelF(kHCost)));
72  }
73  };
74 
75  struct NodeHash {
76  size_t operator()(const GNode &x) const
77  { return (size_t)(x.n->getUniqueID()); }
78  };
79 }
80 
83 
84 typedef std::unordered_map<uint32_t, CFOptimalRefinementConstants::GNode> NodeLookupTable;
85 
86 // variables starting with "a" are in the abstraction
87 // variables starting with "g" are in the defined search graph
89 public:
91  virtual ~CFOptimalRefinement();
92  virtual const char *GetName();
93  virtual path *GetPath(GraphAbstraction *aMap, node *from, node *to, reservationProvider *rp = 0);
95  bool InitializeSearch(GraphAbstraction *aMap, node *from, node *to);
96  void OpenGLDraw() const;
97 private:
98  node *FindTopLevelNode(node *one, node *two, GraphAbstraction *aMap);
99  void SetInitialValues(node *gNewNode, node *aRealNode, node *gParent);
100  void UpdateNode(node *gNode);
101  void UpdateH(node *gNode);
102  void UpdateG(node *gNode);
103  void UpdateOptH(node *gNode);
104  void MakeNeighborsOpen(node *gNode);
105  void RefineNode(node *gNode);
106  node *GetRealNode(node *gNode) const;
107  bool ShouldAddEdge(node *aLowerNode, node *aHigherNode);
108 
114 };
115 
116 
117 #endif
GraphAbstraction
A generic class for basic operations on Graph abstractions.
Definition: GraphAbstraction.h:63
CFOptimalRefinement::q
PQueue q
Definition: CFOptimalRefinement.h:109
Graph.h
CFOptimalRefinementConstants::kAbstractionLevel
@ kAbstractionLevel
Definition: CFOptimalRefinement.h:25
CFOptimalRefinementConstants
Definition: CFOptimalRefinement.h:21
CFOptimalRefinement::RefineNode
void RefineNode(node *gNode)
Definition: CFOptimalRefinement.cpp:245
min
double min(double a, double b)
Definition: FPUtil.h:35
CFOptimalRefinementConstants::NodeHash::operator()
size_t operator()(const GNode &x) const
Definition: CFOptimalRefinement.h:76
NodeLookupTable
std::unordered_map< uint32_t, CFOptimalRefinementConstants::GNode > NodeLookupTable
Definition: CFOptimalRefinement.h:84
CFOptimalRefinement::UpdateH
void UpdateH(node *gNode)
Definition: CFOptimalRefinement.cpp:153
CFOptimalRefinement::GetPath
virtual path * GetPath(GraphAbstraction *aMap, node *from, node *to, reservationProvider *rp=0)
Definition: CFOptimalRefinement.cpp:30
SearchAlgorithm.h
CFOptimalRefinement::aGoal
node * aGoal
Definition: CFOptimalRefinement.h:110
CFOptimalRefinement::UpdateNode
void UpdateNode(node *gNode)
Definition: CFOptimalRefinement.cpp:141
CFOptimalRefinement::gStart
node * gStart
Definition: CFOptimalRefinement.h:111
FPUtil.h
PQueue
OpenClosedList< CFOptimalRefinementConstants::GNode, CFOptimalRefinementConstants::NodeHash, CFOptimalRefinementConstants::NodeEqual, CFOptimalRefinementConstants::NodeCompare > PQueue
Definition: CFOptimalRefinement.h:82
OpenClosedList.h
Graph
A generic Graph class.
Definition: Graph.h:66
CFOptimalRefinementConstants::kHCost
@ kHCost
Definition: CFOptimalRefinement.h:28
CFOptimalRefinement::InitializeSearch
bool InitializeSearch(GraphAbstraction *aMap, node *from, node *to)
Definition: CFOptimalRefinement.cpp:69
CFOptimalRefinement::MakeNeighborsOpen
void MakeNeighborsOpen(node *gNode)
Definition: CFOptimalRefinement.cpp:231
CFOptimalRefinementConstants::NodeEqual
Definition: CFOptimalRefinement.h:39
CFOptimalRefinement::DoOneSearchStep
path * DoOneSearchStep()
Definition: CFOptimalRefinement.cpp:43
CFOptimalRefinement::SetInitialValues
void SetInitialValues(node *gNewNode, node *aRealNode, node *gParent)
Definition: CFOptimalRefinement.cpp:113
CFOptimalRefinement::GetName
virtual const char * GetName()
Definition: CFOptimalRefinement.cpp:25
node::getUniqueID
int getUniqueID() const
Definition: Graph.h:177
CFOptimalRefinementConstants::NodeCompare
Definition: CFOptimalRefinement.h:44
OpenClosedList
A simple Heap class.
Definition: OpenClosedList.h:27
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
CFOptimalRefinementConstants::kGCost
@ kGCost
Definition: CFOptimalRefinement.h:27
CFOptimalRefinementConstants::kCorrespondingNode
@ kCorrespondingNode
Definition: CFOptimalRefinement.h:26
CFOptimalRefinementConstants::GNode
Definition: CFOptimalRefinement.h:33
CFOptimalRefinementConstants::kInOpenList
@ kInOpenList
Definition: CFOptimalRefinement.h:30
CFOptimalRefinement::UpdateOptH
void UpdateOptH(node *gNode)
Definition: CFOptimalRefinement.cpp:199
CFOptimalRefinement::UpdateG
void UpdateG(node *gNode)
Definition: CFOptimalRefinement.cpp:176
CFOptimalRefinement::ShouldAddEdge
bool ShouldAddEdge(node *aLowerNode, node *aHigherNode)
Definition: CFOptimalRefinement.cpp:326
CFOptimalRefinement::gGoal
node * gGoal
Definition: CFOptimalRefinement.h:111
CFOptimalRefinementConstants::GNode::GNode
GNode(node *nn)
Definition: CFOptimalRefinement.h:34
node::GetLabelF
double GetLabelF(unsigned int index) const
Definition: Graph.h:215
CFOptimalRefinementConstants::NodeEqual::operator()
bool operator()(const GNode &i1, const GNode &i2) const
Definition: CFOptimalRefinement.h:40
reservationProvider
Definition: ReservationProvider.h:33
CFOptimalRefinementConstants::NodeHash
Definition: CFOptimalRefinement.h:75
SearchAlgorithm
A generic algorithm which can be used for pathfinding.
Definition: SearchAlgorithm.h:25
CFOptimalRefinement::~CFOptimalRefinement
virtual ~CFOptimalRefinement()
Definition: CFOptimalRefinement.cpp:21
CFOptimalRefinement::g
Graph * g
Definition: CFOptimalRefinement.h:113
CFOptimalRefinementConstants::GNode::GNode
GNode()
Definition: CFOptimalRefinement.h:35
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
CFOptimalRefinement
Definition: CFOptimalRefinement.h:88
CFOptimalRefinement::absGraph
GraphAbstraction * absGraph
Definition: CFOptimalRefinement.h:112
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
CFOptimalRefinement::FindTopLevelNode
node * FindTopLevelNode(node *one, node *two, GraphAbstraction *aMap)
Definition: CFOptimalRefinement.cpp:90
CFOptimalRefinement::CFOptimalRefinement
CFOptimalRefinement()
Definition: CFOptimalRefinement.cpp:15
CFOptimalRefinementConstants::NodeCompare::operator()
bool operator()(const GNode &i1, const GNode &i2) const
Definition: CFOptimalRefinement.h:46
CFOptimalRefinement::OpenGLDraw
void OpenGLDraw() const
Definition: CFOptimalRefinement.cpp:338
CFOptimalRefinement::GetRealNode
node * GetRealNode(node *gNode) const
Definition: CFOptimalRefinement.cpp:321
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
GraphAbstraction.h
CFOptimalRefinement::aStart
node * aStart
Definition: CFOptimalRefinement.h:110
CFOptimalRefinementConstants::kOptimalFlag
@ kOptimalFlag
Definition: CFOptimalRefinement.h:29
CFOptimalRefinementConstants::GNode::n
node * n
Definition: CFOptimalRefinement.h:36