HOG2
GraphCheck.h
Go to the documentation of this file.
1 /*
2  * GraphCheck.h
3  * hog2
4  *
5  * Created by Zhifu Zhang on 7/7/07.
6  *
7  */
8 
9 #ifndef GRAPHCHECK_H
10 #define GRAPHCHECK_H
11 
12 #include <stdint.h>
13 #include <vector>
14 #include <unordered_map>
15 #include "SearchEnvironment.h"
16 #include "UnitSimulation.h"
17 #include "Graph.h"
18 
19 template <typename T>
20 class SimpleNode {
21 public:
23  {
24  depth = (T)0;
25  me = (T)0;
26  parent = (T)0;
27  }
28  SimpleNode(T m, T p, int d)
29  {
30  depth = d;
31  me = m;
32  parent = p;
33  }
34 
35  T parent;
36  T me;
37  int depth;
38 };
39 
40 
41 // Hash64 struct has been moved to SearchEnvironment.h
42 
43 
44 template <typename state, typename action>
45 class GraphCheck {
46 public:
47  static void NumNodesWithinRadius(SearchEnvironment<state, action> &env, state from, int depth, int &inner_count, int &leaf_count);
48  static void PathCountWithinRadius(SearchEnvironment<state, action> &env, state from, int depth, std::unordered_map<uint64_t, int, Hash64> &counts, std::unordered_map<uint64_t, double, Hash64> &aveCosts );
49 
50 private:
51  static void DFSVisit(SearchEnvironment<state, action> &env, std::vector<SimpleNode<state> > &thePath, int depth, std::unordered_map<uint64_t, int, Hash64> &counts, std::unordered_map<uint64_t, double, Hash64> &aveCosts, double gval);
52 
53 };
54 
55 template <typename state, typename action>
56 void GraphCheck<state, action>::NumNodesWithinRadius(SearchEnvironment<state, action> &env, state from, int depth, int &inner_count, int &leaf_count)
57 {
58  // using BFS
59  inner_count = 0;
60  leaf_count = 0;
61  std::queue<SimpleNode<state> > myqueue;
62  std::unordered_map<uint64_t, SimpleNode<state>, Hash64> closedlist;
63 
64  std::vector<state> neighbors;
65 
66  SimpleNode<state> n0(from, from, 0);
67  myqueue.push(n0);
68 
69  while(! myqueue.empty())
70  {
71  SimpleNode<state> frontN = myqueue.front();
72  uint64_t frontID = env.GetStateHash(frontN.me);
73  myqueue.pop();
74 
75  if (frontN.depth >= depth)
76  {
77  leaf_count++;
78  continue;
79  }
80  else
81  {
82  inner_count++;
83  }
84 
85  env.GetSuccessors(frontN.me, neighbors);
86 
87  for (unsigned int x = 0; x<neighbors.size(); x++)
88  {
89  state neighbor = neighbors[x];
90  uint64_t neighborID = env.GetStateHash(neighbor);
91  if (closedlist.find(neighborID) == closedlist.end())
92  {
93  //count++;
94 
95  SimpleNode<state> newNode(neighborID,frontID, frontN.depth+1);
96  myqueue.push(newNode);
97  }
98  }
99 
100  closedlist[frontID] = frontN;
101  }
102 
103  //return count;
104 }
105 
106 template <typename state, typename action>
107 void GraphCheck<state, action>::PathCountWithinRadius(SearchEnvironment<state, action> &env, state from, int depth, std::unordered_map<uint64_t, int, Hash64> &counts, std::unordered_map<uint64_t, double, Hash64> &aveCosts )
108 {
109  // using recursive version of DFS
110  std::vector<SimpleNode<state> > thePath;
111 
112  SimpleNode<state> n0(from,from,0);
113  counts[env.GetStateHash(from)]++;
114  thePath.push_back(n0);
115 
116  DFSVisit(env, thePath,depth,counts,aveCosts,0);
117 
118  for (std::unordered_map<uint64_t,int, Hash64> ::iterator it = counts.begin(); it != counts.end(); it++)
119  {
120  if (it->second > 0)
121  aveCosts[it->first] /= it->second;
122  }
123 }
124 
125 template <typename state, typename action>
126 void GraphCheck<state, action>::DFSVisit(SearchEnvironment<state, action> &env, std::vector<SimpleNode<state> > &thePath, int depth, std::unordered_map<uint64_t, int, Hash64> &counts, std::unordered_map<uint64_t, double, Hash64> &aveCosts, double gval)
127 {
128  std::vector<state> neighbors;
129 
130  SimpleNode<state> current = thePath.back();
131  if (current.depth >= depth)
132  return;
133 
134  env.GetSuccessors(current.me, neighbors);
135 
136  for (unsigned int x = 0; x<neighbors.size(); x++)
137  {
138  state neighbor = neighbors[x];
139  if (neighbor == current.parent)
140  continue;
141 
142  bool flag = false;
143  std::vector< SimpleNode<state> >::iterator iter;
144 
145  for (iter = thePath.begin(); iter != thePath.end(); iter++)
146  {
147  if (neighbor == iter->me)
148  {
149  flag = true; // this neighbor is in the path
150  break;
151  }
152  }
153 
154  // this check is important!
155  if (flag)
156  continue;
157 
158  uint64_t uniqueID = env.GetStateHash(neighbor);
159 
160  counts[uniqueID]++;
161  aveCosts[uniqueID] += gval + GCost(current.me,neighbor);
162 
163  SimpleNode<state> sn(neighbor, current.me, current.depth+1);
164  thePath.push_back(sn);
165  DFSVisit(env, thePath, depth, counts, aveCosts, aveCosts[uniqueID]); // recursion
166  thePath.pop_back();
167  }
168 }
169 
170 
171 
172 #endif
UnitSimulation.h
Graph.h
graphMove
Definition: GraphEnvironment.h:34
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
SimpleNode
Definition: GraphCheck.h:20
Hash64
Definition: SearchEnvironment.h:23
SimpleNode::parent
T parent
Definition: GraphCheck.h:35
SearchEnvironment::GetSuccessors
virtual void GetSuccessors(const state &nodeID, std::vector< state > &neighbors) const =0
GraphCheck::NumNodesWithinRadius
static void NumNodesWithinRadius(SearchEnvironment< state, action > &env, state from, int depth, int &inner_count, int &leaf_count)
Definition: GraphCheck.h:56
GraphCheck::DFSVisit
static void DFSVisit(SearchEnvironment< state, action > &env, std::vector< SimpleNode< state > > &thePath, int depth, std::unordered_map< uint64_t, int, Hash64 > &counts, std::unordered_map< uint64_t, double, Hash64 > &aveCosts, double gval)
Definition: GraphCheck.h:126
SimpleNode::me
T me
Definition: GraphCheck.h:36
SimpleNode::depth
int depth
Definition: GraphCheck.h:37
GraphCheck
Definition: GraphCheck.h:45
SimpleNode::SimpleNode
SimpleNode()
Definition: GraphCheck.h:22
SearchEnvironment::GetStateHash
virtual uint64_t GetStateHash(const state &node) const =0
SimpleNode::SimpleNode
SimpleNode(T m, T p, int d)
Definition: GraphCheck.h:28
SearchEnvironment
Definition: SearchEnvironment.h:30
GraphCheck::PathCountWithinRadius
static void PathCountWithinRadius(SearchEnvironment< state, action > &env, state from, int depth, std::unordered_map< uint64_t, int, Hash64 > &counts, std::unordered_map< uint64_t, double, Hash64 > &aveCosts)
Definition: GraphCheck.h:107
SearchEnvironment.h