HOG2
FrontierBFS.h
Go to the documentation of this file.
1 /*
2  * FrontierBFS.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 1/29/11.
6  * Copyright 2011. All rights reserved.
7  *
8  */
9 
10 #ifndef FRONTIERBFS_H
11 #define FRONTIERBFS_H
12 
13 #include <iostream>
14 #include "SearchEnvironment.h"
15 #include <unordered_map>
16 #include "FPUtil.h"
17 
18 typedef std::unordered_map<uint64_t, bool, Hash64> FrontierBFSClosedList;
19 
20 template <class state, class action>
21 class FrontierBFS {
22 public:
24  virtual ~FrontierBFS() {}
25  void GetPath(SearchEnvironment<state, action> *env, state &from, state &to,
26  std::vector<state> &thePath);
27  void GetPath(SearchEnvironment<state, action> *env, state &from, state &to,
28  std::vector<action> &thePath);
29 
30  void InitializeSearch(SearchEnvironment<state, action> *env, state &from);
31  void InitializeSearch(SearchEnvironment<state, action> *env, std::vector<state> &from);
33 
35  {
36  if (mOpen1.size() > 0) return mClosed2;
37  if (mOpen2.size() > 0) return mClosed1;
38  //assert(!"Open and closed are both null");
39  return mClosed1;
40  }
41  uint64_t GetNodesExpanded() { return nodesExpanded; }
42  uint64_t GetNodesTouched() { return nodesTouched; }
43 private:
44 
46  std::deque<state> &currentOpenList,
47  FrontierBFSClosedList &currentClosedList,
48  std::deque<state> &nextOpenList,
49  FrontierBFSClosedList &lastClosedList);
50 
51 
53  int depth;
54 
55  std::deque<state> mOpen1;
56  std::deque<state> mOpen2;
57  FrontierBFSClosedList mClosed1; // store parent id!
58  FrontierBFSClosedList mClosed2; // store parent id!
59 };
60 
61 
62 template <class state, class action>
64  state &from)
65 {
66  nodesExpanded = nodesTouched = 0;
67 
68  mOpen1.clear();
69  mOpen2.clear();
70  mClosed1.clear();
71  mClosed2.clear();
72 
73  mOpen1.push_back(from);
74  depth = 0;
75 }
76 
77 template <class state, class action>
79  std::vector<state> &from)
80 {
81  nodesExpanded = nodesTouched = 0;
82 
83  mOpen1.clear();
84  mOpen2.clear();
85  mClosed1.clear();
86  mClosed2.clear();
87 
88  for (unsigned int x = 0; x < from.size(); x++)
89  mOpen1.push_back(from[x]);
90  depth = 0;
91 }
92 
93 template <class state, class action>
95 {
96  uint64_t n = nodesExpanded;
97  if ((mOpen1.size() != 0) || (mOpen2.size() != 0))
98  {
99  n = nodesExpanded;
100  if (mOpen1.size() == 0)
101  {
102  std::cout << mOpen2.front() << std::endl << mOpen2.back() << std::endl;
103  ExpandLevel(env, mOpen2, mClosed2, mOpen1, mClosed1);
104  mClosed1.clear();
105  }
106  else {
107  std::cout << mOpen1.front() << std::endl << mOpen1.back() << std::endl;
108  ExpandLevel(env, mOpen1, mClosed1, mOpen2, mClosed2);
109  mClosed2.clear();
110  }
111  depth++;
112  }
113  else {
114  return true;
115  }
116  printf("Depth %d complete; nodes expanded %lld (%lld new); %lu in memory\n", depth, nodesExpanded, nodesExpanded - n,
117  mOpen1.size()+mOpen2.size()+mClosed1.size()+mClosed2.size());
118  if ((mOpen1.size() == 0) && (mOpen2.size() == 0))
119  return true;
120  return false;
121 }
122 
123 
124 template <class state, class action>
126  state &from, state &to,
127  std::vector<state> &thePath)
128 {
129  nodesExpanded = nodesTouched = 0;
130 
131  mOpen1.clear();
132  mOpen2.clear();
133  mClosed1.clear();
134  mClosed2.clear();
135 
136  mOpen1.push_back(from);
137 
138  depth = 0;
139  uint64_t n = nodesExpanded;
140  while ((mOpen1.size() != 0) || (mOpen2.size() != 0))
141  {
142  printf("Depth %d; nodes expanded %lld (%lld new); %d in memory\n", depth, nodesExpanded, nodesExpanded - n,
143  mOpen1.size()+mOpen2.size()+mClosed1.size()+mClosed2.size());
144  n = nodesExpanded;
145  if (mOpen1.size() == 0)
146  {
147  ExpandLevel(env, mOpen2, mClosed2, mOpen1, mClosed1);
148  mClosed1.clear();
149  }
150  else {
151  ExpandLevel(env, mOpen1, mClosed1, mOpen2, mClosed2);
152  mClosed2.clear();
153  }
154  depth++;
155  }
156 }
157 
158 template <class state, class action>
160  std::deque<state> &currentOpenList,
161  FrontierBFSClosedList &currentClosedList,
162  std::deque<state> &nextOpenList,
163  FrontierBFSClosedList &lastClosedList)
164 {
165  static std::vector<state> neighbors;
166  neighbors.resize(0);
167  while (currentOpenList.size() > 0)
168  {
169  state s = currentOpenList.front();
170  currentOpenList.pop_front();
171 
172  if (currentClosedList.find(env->GetStateHash(s)) != currentClosedList.end())
173  {
174 // printf("Needed to check against current\n");
175  continue;
176  }
177 // if (lastClosedList.find(env->GetStateHash(s)) != lastClosedList.end())
178 // {
179 // printf("Needed to check against last\n");
180 // continue;
181 // }
182 
183  currentClosedList[env->GetStateHash(s)] = true;
184 
185  nodesExpanded++;
186  env->GetSuccessors(s, neighbors);
187  for (unsigned int x = 0; x < neighbors.size(); x++)
188  {
189  if (currentClosedList.find(env->GetStateHash(neighbors[x])) != currentClosedList.end())
190  continue;
191  if (lastClosedList.find(env->GetStateHash(neighbors[x])) != lastClosedList.end())
192  continue;
193 
194  nextOpenList.push_back(neighbors[x]);
195  }
196  }
197 }
198 
199 template <class state, class action>
201  state &from, state &to,
202  std::vector<action> &thePath)
203 {
204  assert(!"not defined");
205 }
206 
207 
208 #endif
FrontierBFS::FrontierBFS
FrontierBFS()
Definition: FrontierBFS.h:23
FrontierBFSClosedList
std::unordered_map< uint64_t, bool, Hash64 > FrontierBFSClosedList
Definition: FrontierBFS.h:18
FrontierBFS::GetCurrentClosedList
const FrontierBFSClosedList & GetCurrentClosedList()
Definition: FrontierBFS.h:34
FrontierBFS::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: FrontierBFS.h:41
FrontierBFS::GetPath
void GetPath(SearchEnvironment< state, action > *env, state &from, state &to, std::vector< state > &thePath)
Definition: FrontierBFS.h:125
FPUtil.h
FrontierBFS::GetNodesTouched
uint64_t GetNodesTouched()
Definition: FrontierBFS.h:42
FrontierBFS
Definition: FrontierBFS.h:21
FrontierBFS::mOpen2
std::deque< state > mOpen2
Definition: FrontierBFS.h:56
FrontierBFS::nodesExpanded
uint64_t nodesExpanded
Definition: FrontierBFS.h:52
FrontierBFS::~FrontierBFS
virtual ~FrontierBFS()
Definition: FrontierBFS.h:24
SearchEnvironment::GetSuccessors
virtual void GetSuccessors(const state &nodeID, std::vector< state > &neighbors) const =0
FrontierBFS::depth
int depth
Definition: FrontierBFS.h:53
FrontierBFS::InitializeSearch
void InitializeSearch(SearchEnvironment< state, action > *env, state &from)
Definition: FrontierBFS.h:63
FrontierBFS::nodesTouched
uint64_t nodesTouched
Definition: FrontierBFS.h:52
FrontierBFS::mClosed1
FrontierBFSClosedList mClosed1
Definition: FrontierBFS.h:57
FrontierBFS::ExpandLevel
void ExpandLevel(SearchEnvironment< state, action > *env, std::deque< state > &currentOpenList, FrontierBFSClosedList &currentClosedList, std::deque< state > &nextOpenList, FrontierBFSClosedList &lastClosedList)
Definition: FrontierBFS.h:159
SearchEnvironment::GetStateHash
virtual uint64_t GetStateHash(const state &node) const =0
FrontierBFS::mOpen1
std::deque< state > mOpen1
Definition: FrontierBFS.h:55
FrontierBFS::DoOneIteration
bool DoOneIteration(SearchEnvironment< state, action > *env)
Definition: FrontierBFS.h:94
SearchEnvironment
Definition: SearchEnvironment.h:30
FrontierBFS::mClosed2
FrontierBFSClosedList mClosed2
Definition: FrontierBFS.h:58
SearchEnvironment.h