10 #ifndef UNITCOSTBIDIRECTIONALBFS
11 #define UNITCOSTBIDIRECTIONALBFS
15 #include <unordered_map>
18 template <
class state,
class action>
24 std::vector<state> &thePath);
26 std::vector<action> &thePath);
34 std::vector<state> &thePath);
36 std::vector<state> &nodesToExpand,
37 std::vector<state> &expansionLocation,
54 template <
class state,
class action>
57 std::vector<state> &thePath)
59 nodesExpanded = nodesTouched = 0;
61 startNodeTable.clear();
62 goalNodeTable.clear();
68 startOpenA.push_back(from);
69 goalOpenA.push_back(to);
81 if (ExpandLayer(env, startOpenA, startOpenB, startNodeTable, goalNodeTable))
86 if (startOpenB.size() == 0)
break;
90 if (ExpandLayer(env, goalOpenA, goalOpenB, goalNodeTable, startNodeTable))
95 if (goalOpenB.size() == 0)
break;
99 if (ExpandLayer(env, startOpenB, startOpenA, startNodeTable, goalNodeTable))
104 if (startOpenA.size() == 0)
break;
108 if (ExpandLayer(env, goalOpenB, goalOpenA, goalNodeTable, startNodeTable))
113 if (goalOpenA.size() == 0)
break;
116 ExtractPath(env, thePath);
119 template <
class state,
class action>
121 state from, state to,
122 std::vector<action> &thePath)
124 assert(!
"not defined");
127 template <
class state,
class action>
129 std::vector<state> &thePath)
132 std::vector<state> forwardPart;
133 std::vector<state> backwardPart;
135 forwardPart.push_back(middle);
136 backwardPart.push_back(middle);
139 if (startNodeTable.find(env->
GetStateHash(forwardPart.back())) != startNodeTable.end())
141 state s = startNodeTable[env->
GetStateHash(forwardPart.back())];
142 if (forwardPart.back() == s)
147 forwardPart.push_back(s);
157 if (goalNodeTable.find(env->
GetStateHash(backwardPart.back())) != goalNodeTable.end())
159 state s = goalNodeTable[env->
GetStateHash(backwardPart.back())];
160 if (backwardPart.back() == s)
165 backwardPart.push_back(s);
174 while (forwardPart.size() > 0)
176 thePath.push_back(forwardPart.back());
177 forwardPart.pop_back();
179 for (
unsigned int x = 1; x < backwardPart.size(); x++)
181 thePath.push_back(backwardPart[x]);
188 template <
class state,
class action>
190 std::vector<state> &nodesToExpand,
191 std::vector<state> &expansionLocation,
195 while (nodesToExpand.size() > 0)
197 state s = nodesToExpand.back();
198 nodesToExpand.pop_back();
204 if (completionHash.find(env->
GetStateHash(s)) != completionHash.end())
207 assert(startNodeTable.find(env->
GetStateHash(s)) != startNodeTable.end());
208 assert(goalNodeTable.find(env->
GetStateHash(s)) != goalNodeTable.end());
213 std::vector<state> succ;
216 for (
unsigned int x = 0; x < succ.size(); x++)
220 if (completionHash.find(env->
GetStateHash(succ[x])) != completionHash.end())
225 assert(startNodeTable.find(env->
GetStateHash(middle)) != startNodeTable.end());
226 assert(goalNodeTable.find(env->
GetStateHash(middle)) != goalNodeTable.end());
229 if (duplicateHash.find(env->
GetStateHash(succ[x])) != duplicateHash.end())
238 expansionLocation.push_back(succ[x]);