HOG2
MeroB.h
Go to the documentation of this file.
1 /*
2  * MeroB.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant, modified by Zhifu Zhang
6  *
7  */
8 
9 #ifndef MEROB_H
10 #define MEROB_H
11 
12 #include <math.h>
13 #include "GraphAlgorithm.h"
14 #include "SearchEnvironment.h"
15 #include "GraphEnvironment.h"
16 #include <unordered_map>
17 #include "FPUtil.h"
18 #include "OpenClosedList.h"
19 
20 #ifndef UINT32_MAX
21 #define UINT32_MAX 4294967295U
22 #endif
23 
24 //const double pi = 3.141592654;
25 
26 #define MB_A 1
27 #define MB_B 2
28 #define MB_BP 3
29 
30 //typedef std::unordered_map<uint64_t, double> NodeHashTable;
31 
32 namespace MeroBUtil
33 {
34  class SearchNode {
35  public:
36  SearchNode(double _fCost=0, double _gCost=0, graphState curr=0, graphState prev=0)
37  :fCost(_fCost), gCost(_gCost), currNode(curr), prevNode(prev) {}
38 
40  :fCost(0), gCost(0), currNode(curr), prevNode(0) {}
41 
42  void copy(double f, double g, graphState curr, graphState prev)
43  {
44  fCost = f;
45  gCost = g;
46  currNode = curr;
47  prevNode = prev;
48  }
49 
50  double fCost;
51  double gCost;
54  };
55 
56  struct SearchNodeEqual {
57  bool operator()(const SearchNode &i1, const SearchNode &i2) const
58  { return (i1.currNode == i2.currNode); }
59  };
60 
61  struct SearchNodeCompare { // true means i2 is preferable over i1
62  // prefering larger g, i.e. smaller h is also in favor of goal nodes
63  bool operator()(const SearchNode &i1, const SearchNode &i2) const
64  {
65  if (fequal(i1.fCost, i2.fCost))
66  {
67  return (fless(i1.gCost, i2.gCost));
68  }
69  return (fgreater(i1.fCost, i2.fCost));
70  }
71  };
72 
73  struct GGreater {
74  bool operator()(const SearchNode &i1, const SearchNode &i2) const
75  {
76  if (fequal(i1.gCost,i2.gCost))
77  return fgreater(i1.fCost,i2.fCost);
78 
79  return fgreater(i1.gCost,i2.gCost);
80  }
81  };
82 
83  struct SearchNodeHash {
84  size_t operator()(const SearchNode &x) const
85  { return (size_t)(x.currNode); }
86  };
87 
88  struct graphGenerator {
89  static void SetLoc(node *n, double x, double y, double z)
90  {
94 // std::cout << *n << std::endl;
95 // printf("(%f, %f, %f)\n", x, y, z);
96  }
97 
98  static void GetLoc(node *n, double &x, double &y, double &z)
99  {
103  }
104 
105  // Note: this is from Martelli paper
106  static Graph* genFig1(unsigned int N)
107  {
108  // h(0) = h(1) = 0;
109  // h(i) = 2^(i-1) + 2i - 3, for 1<i<=N
110  // c(i,j) = 2^(i-2) + i - 2^(j-1) - j, for 1<=j<i<=N
111  // c(1,0) = 2^(N-1) + N - 2
112  assert(N >= 3);
113 
114  Graph *g = new Graph();
115 
116  // add nodes
117 
118  for (unsigned int nodeID = 0; nodeID <= N; nodeID++)
119  {
120  node *n = new node("");
121  if (nodeID == 0 || nodeID == 1)
122  {
124  }
125 // EX2
126 // else if (nodeID == N)
127 // {
128 // n->SetLabelF(GraphSearchConstants::kHCost, 0);
129 // }
130  else {
131  double h = pow(2,nodeID-1) + 2*nodeID - 3;
133  }
134  g->AddNode(n); // the real nodeID is assigned here
135 
136  if (nodeID == 0)
137  {
138  SetLoc(n,-1,-1,0);
139  }
140  else // nodes 1 - N will be drawn along 3/4 circle
141  {
142  double alpha = ((double)N - nodeID) * 1.5*PI /(N - 1.0);
143  double beta = alpha - 0.5*PI;
144  SetLoc(n,cos(beta),sin(beta),0);
145  }
146  }
147 
148  // add edges
149  double c = pow(2,N-1) + N - 2;
150  edge *e = new edge(1,0,c);
151  g->AddEdge(e);
152  for (unsigned int j = 1; j < N; j++)
153  {
154  for (unsigned int i = j+1; i <= N; i++)
155  {
156  c = pow(2,i-2) + i - pow(2,j-1) - j;
157  e = new edge(i,j,c);
158  g->AddEdge(e);
159  }
160  }
161 
162  return g;
163  }
164 
165  static Graph* genFig2(unsigned int N)
166  {
167  // h(0) = h(2N-1) = 0
168  // h(i) = 2(N-1)^2 - N - i + 2, for i = 1,...,N-1
169  // h(N+j) = 2(N-1)(N-2-j) + 1, for j = 0,...,N-2
170  // c(0,i) = 1, for i = 1,...,N-1
171  // c(i,N) = 2(N-1)(i-1) + 1, for i = 1,...,N-1
172  // c(i,i+1) = 2N - 2, for i = N,...,2N-1 (should it be 2N-2 ?)
173  assert(N >= 2);
174 
175  Graph* g = new Graph();
176 
177  // add nodes
178  node* n = new node("");
180  SetLoc(n, 0, -0.9, 0);
181  g->AddNode(n);
182 
183  for (unsigned int i=1;i<=N-1;i++)
184  {
185  n = new node("");
186  double h = 2*(N-1)*(N-1) - N - i + 2;
188  g->AddNode(n);
189  SetLoc(n, -1+(double)(i-1)*2.0/((double)N-2.0), 0.0, 0);
190  }
191 
192  for (unsigned int j=0; j<=N-2; j++)
193  {
194  n = new node("");
195  double h = 0;//2*(N-1)*(N-2-j) + 1;
196  //double h = 2*(N-1)*(N-2-j) + 1;
198  g->AddNode(n);
199  SetLoc(n, -(double)j/((double)N-1.0), 0.9+((j%2)?0.1:0.0), 0);
200  }
201 
202  n = new node(""); // the last node (2N-1)
204  SetLoc(n, -1, 1, 0);
205  g->AddNode(n);
206 
207  // add edges
208 
209  // type 1
210  for (unsigned int i=1;i<=N-1;i++)
211  {
212  edge* e = new edge(0,i,1);
213  g->AddEdge(e);
214  }
215 
216  // type 2
217  for (unsigned int i=1; i<=N-1; i++)
218  {
219  double c = 2*(N-1)*(i-1) + 1;
220  edge* e = new edge(i,N,c);
221  g->AddEdge(e);
222  }
223 
224  // type 3
225  for (unsigned int i=N; i<=2*N-2; i++)
226  {
227  double c = 2*N - 2;
228  edge* e = new edge(i,i+1,c);
229  g->AddEdge(e);
230  }
231 
232  return g;
233  }
234  };
235 
238 
239  typedef std::unordered_map<graphState, MeroBUtil::SearchNode > NodeLookupTable;
240 
243 }
244 
245 class MeroB : public GraphAlgorithm {
246 public:
247  MeroB() { verID = MB_A; heuristic = 0; }
248  MeroB(unsigned int v) { verID = v; }
249  void SetVersion(unsigned int v) {verID = v;}
250  virtual ~MeroB() {}
251  void GetPath(GraphEnvironment *env, Graph* _g, graphState from, graphState to, std::vector<graphState> &thePath);
253  virtual double GetSolutionCost() { return 0; }
254  virtual const char* GetName() { return "MeroB"; }
255  virtual int GetSolutionEdges() { return 0; }
256 
257 
258  uint64_t GetNodesExpanded() { return nodesExpanded; }
259  uint64_t GetNodesTouched() { return nodesTouched; }
260  uint64_t GetNodesReopened() { return -1; }
261 
262  bool InitializeSearch(GraphEnvironment *env, Graph* g, graphState from, graphState to, std::vector<graphState> &thePath);
263  bool DoSingleSearchStep(std::vector<graphState> &thePath);
264  bool DoSingleStepA(std::vector<graphState> &thePath);
265  bool DoSingleStepB(std::vector<graphState> &thePath);
266  bool DoSingleStepBP(std::vector<graphState> &thePath);
267  void ExtractPathToStart(graphState goalNode, std::vector<graphState> &thePath);
268  void OpenGLDraw() const;
269  //void OpenGLDraw() const;
270  void DrawText(double x, double y, double z, float r, float g, float b, char* str);
271  void DrawEdge(unsigned int from, unsigned int to, double weight);
272 
273 private:
275  unsigned int verID;
276  double F;
278  std::vector<graphState> neighbors;
283  MeroBUtil::GQueue FCache; // storing nodes with f < F, this is temporary cache
284 
285  Graph *g; // for OpenGL drawing only
286 };
287 
288 #endif
Graph::AddNode
int AddNode(node *)
Definition: Graph.cpp:136
MeroB::env
GraphEnvironment * env
Definition: MeroB.h:280
MeroBUtil::SearchNode::copy
void copy(double f, double g, graphState curr, graphState prev)
Definition: MeroB.h:42
node::SetLabelF
void SetLabelF(unsigned int index, double val) const
Definition: Graph.cpp:687
MeroBUtil::SearchNode::SearchNode
SearchNode(double _fCost=0, double _gCost=0, graphState curr=0, graphState prev=0)
Definition: MeroB.h:36
MeroBUtil::GGreater
Definition: MeroB.h:73
graphState
unsigned long graphState
Definition: GraphEnvironment.h:32
MeroB::FCache
MeroBUtil::GQueue FCache
Definition: MeroB.h:283
MeroBUtil::GGreater::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: MeroB.h:74
MeroBUtil::SearchNodeCompare
Definition: MeroB.h:61
MeroB::MeroB
MeroB()
Definition: MeroB.h:247
MeroB::GetName
virtual const char * GetName()
Definition: MeroB.h:254
Heuristic< graphState >
MeroBUtil::SearchNode::SearchNode
SearchNode(graphState curr)
Definition: MeroB.h:39
MeroB::SetHeuristic
void SetHeuristic(Heuristic< graphState > *heur)
Definition: MeroB.h:252
MeroB::goal
graphState goal
Definition: MeroB.h:279
Graph::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:170
MeroB::DoSingleStepA
bool DoSingleStepA(std::vector< graphState > &thePath)
Definition: MeroB.cpp:70
MeroB::~MeroB
virtual ~MeroB()
Definition: MeroB.h:250
FPUtil.h
MeroBUtil::graphGenerator::SetLoc
static void SetLoc(node *n, double x, double y, double z)
Definition: MeroB.h:89
OpenClosedList.h
MeroB::neighbors
std::vector< graphState > neighbors
Definition: MeroB.h:278
MeroB::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: MeroB.h:258
GraphSearchConstants::kXCoordinate
@ kXCoordinate
Definition: GraphEnvironment.h:51
MeroBUtil::SearchNode::currNode
graphState currNode
Definition: MeroB.h:52
MeroB::GetNodesReopened
uint64_t GetNodesReopened()
Definition: MeroB.h:260
Graph
A generic Graph class.
Definition: Graph.h:66
MeroB::InitializeSearch
bool InitializeSearch(GraphEnvironment *env, Graph *g, graphState from, graphState to, std::vector< graphState > &thePath)
Definition: MeroB.cpp:27
MeroB::DrawText
void DrawText(double x, double y, double z, float r, float g, float b, char *str)
Definition: MeroB.cpp:673
GraphSearchConstants::kHCost
@ kHCost
Definition: GraphEnvironment.h:50
MeroB::nodesTouched
uint64_t nodesTouched
Definition: MeroB.h:277
MeroBUtil::NodeLookupTable
std::unordered_map< graphState, MeroBUtil::SearchNode > NodeLookupTable
Definition: MeroB.h:239
MeroB::DoSingleStepB
bool DoSingleStepB(std::vector< graphState > &thePath)
Definition: MeroB.cpp:180
PI
static const double PI
Definition: GLUtil.h:66
MeroB::GetSolutionCost
virtual double GetSolutionCost()
Definition: MeroB.h:253
MeroB::SetVersion
void SetVersion(unsigned int v)
Definition: MeroB.h:249
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
MeroBUtil::PQueue
OpenClosedList< MeroBUtil::SearchNode, MeroBUtil::SearchNodeHash, MeroBUtil::SearchNodeEqual, MeroBUtil::SearchNodeCompare > PQueue
Definition: MeroB.h:237
MeroB::verID
unsigned int verID
Definition: MeroB.h:275
MeroB::DoSingleStepBP
bool DoSingleStepBP(std::vector< graphState > &thePath)
Definition: MeroB.cpp:329
MeroB::OpenGLDraw
void OpenGLDraw() const
Definition: MeroB.cpp:574
MeroB::DoSingleSearchStep
bool DoSingleSearchStep(std::vector< graphState > &thePath)
Definition: MeroB.cpp:57
OpenClosedList
A simple Heap class.
Definition: OpenClosedList.h:27
MeroBUtil::SearchNode::fCost
double fCost
Definition: MeroB.h:50
MeroBUtil::SearchNode::prevNode
graphState prevNode
Definition: MeroB.h:53
MeroBUtil::SearchNodeEqual
Definition: MeroB.h:56
MeroB::DrawEdge
void DrawEdge(unsigned int from, unsigned int to, double weight)
Definition: MeroB.cpp:694
MeroBUtil::SearchNode
Definition: MeroB.h:34
MeroB::start
graphState start
Definition: MeroB.h:279
MeroBUtil::graphGenerator::GetLoc
static void GetLoc(node *n, double &x, double &y, double &z)
Definition: MeroB.h:98
GraphSearchConstants::kYCoordinate
@ kYCoordinate
Definition: GraphEnvironment.h:52
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
MeroB::g
Graph * g
Definition: MeroB.h:285
MeroBUtil::SearchNodeHash
Definition: MeroB.h:83
MeroB::GetPath
void GetPath(GraphEnvironment *env, Graph *_g, graphState from, graphState to, std::vector< graphState > &thePath)
Definition: MeroB.cpp:15
MeroB::closedList
MeroBUtil::NodeLookupTable closedList
Definition: MeroB.h:282
MeroB::MeroB
MeroB(unsigned int v)
Definition: MeroB.h:248
MeroB::openQueue
MeroBUtil::PQueue openQueue
Definition: MeroB.h:281
GraphAlgorithm.h
MeroB
Definition: MeroB.h:245
node::GetLabelF
double GetLabelF(unsigned int index) const
Definition: Graph.h:215
MeroBUtil::graphGenerator
Definition: MeroB.h:88
MeroB::ExtractPathToStart
void ExtractPathToStart(graphState goalNode, std::vector< graphState > &thePath)
Definition: MeroB.cpp:550
MeroBUtil::GQueue
OpenClosedList< MeroBUtil::SearchNode, MeroBUtil::SearchNodeHash, MeroBUtil::SearchNodeEqual, MeroBUtil::GGreater > GQueue
Definition: MeroB.h:242
MeroBUtil::graphGenerator::genFig2
static Graph * genFig2(unsigned int N)
Definition: MeroB.h:165
MeroBUtil::SearchNode::gCost
double gCost
Definition: MeroB.h:51
MeroB::GetSolutionEdges
virtual int GetSolutionEdges()
Definition: MeroB.h:255
MeroBUtil::SearchNodeHash::operator()
size_t operator()(const SearchNode &x) const
Definition: MeroB.h:84
MeroBUtil::SearchNodeCompare::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: MeroB.h:63
GraphAlgorithm
Definition: GraphAlgorithm.h:17
MeroBUtil
Definition: MeroB.h:32
MeroB::F
double F
Definition: MeroB.h:276
MeroB::GetNodesTouched
uint64_t GetNodesTouched()
Definition: MeroB.h:259
GraphSearchConstants::kZCoordinate
@ kZCoordinate
Definition: GraphEnvironment.h:53
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
MeroB::heuristic
Heuristic< graphState > * heuristic
Definition: MeroB.h:274
MB_A
#define MB_A
Definition: MeroB.h:26
GraphEnvironment
Definition: GraphEnvironment.h:291
MeroB::nodesExpanded
uint64_t nodesExpanded
Definition: MeroB.h:277
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h
MeroBUtil::SearchNodeEqual::operator()
bool operator()(const SearchNode &i1, const SearchNode &i2) const
Definition: MeroB.h:57
GraphEnvironment.h
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129
MeroBUtil::graphGenerator::genFig1
static Graph * genFig1(unsigned int N)
Definition: MeroB.h:106