18 #include <unordered_map>
20 template <
class state,
class action,
class environment>
25 void DoBFS(environment *env, state from);
26 void GetPath(environment *env, state from, state to,
27 std::vector<state> &thePath);
44 template <
class state,
class action,
class environment>
49 std::deque<state> mOpen;
50 std::deque<int> depth;
51 std::unordered_map<state, bool> mClosed;
53 std::vector<state> successors;
54 nodesExpanded = nodesTouched = 0;
61 mOpen.push_back(from);
64 uint64_t lastNodes = 0, lastIter = 0;
65 while (mOpen.size() > 0)
67 assert(mOpen.size() == depth.size());
68 state s = mOpen.front();
70 if (depth.front() != currDepth)
73 lastIter = nodesExpanded-lastNodes;
74 lastNodes = nodesExpanded;
76 currDepth = depth.front();
79 if (mClosed.find(s) != mClosed.end())
90 if (nodesExpanded > nodeLimit)
92 printf(
"Node limit hit. Aborting search\n");
95 env->GetSuccessors(s, successors);
96 for (
unsigned int x = 0; x < successors.size(); x++)
98 if (mClosed.find(successors[x]) == mClosed.end())
100 mOpen.push_back(successors[x]);
101 depth.push_back(currDepth+1);
113 template <
class state,
class action,
class environment>
115 state from, state to,
116 std::vector<state> &thePath)
119 std::deque<std::pair<state, uint16_t>> mOpen;
121 std::unordered_map<state, state> mClosed;
124 bool goalFound =
false;
126 nodesExpanded = nodesTouched = 0;
133 mOpen.push_back({from, 0});
134 mClosed[from] = from;
137 uint64_t lastNodes = 0, lastIter = 0;
140 while (mOpen.size() > 0)
142 s = mOpen.front().first;
143 if (mOpen.front().second != currDepth)
146 printf(
"%ld tot %d inc %lld b %.2f [%d]\n", currDepth, nodesExpanded, nodesExpanded-lastNodes, (
double)(nodesExpanded-lastNodes)/lastIter, numGoals);
147 lastIter = nodesExpanded-lastNodes;
148 lastNodes = nodesExpanded;
150 currDepth = mOpen.front().second;
153 if (env->GoalTest(s, to))
164 if (nodesExpanded > nodeLimit)
166 printf(
"Node limit hit. Aborting search\n");
171 env->GetSuccessors(s, thePath);
172 for (
unsigned int x = 0; x < thePath.size(); x++)
174 if (mClosed.find(thePath[x]) == mClosed.end())
176 mOpen.push_back({thePath[x], currDepth+1});
180 mClosed[thePath[x]] = s;
195 thePath.push_back(s);
198 }
while (!(s == parent));
200 std::reverse(thePath.begin(), thePath.end());