19 #include <unordered_map>
31 template <
class state,
class action,
class environment,
class dataStructure = NBSQueue<state, 1>,
class priorityQueue = BDOpenClosed<state, NBSCompareOpenReady<state, BDOpenClosedData<state>>, NBSCompareOpenWaiting<state, BDOpenClosedData<state>>>>
39 void GetPath(environment *
env,
const state& from,
const state& to,
49 virtual const char *
GetName() {
return "NBS"; }
63 auto l =
queue.forwardQueue.Lookup(
env->GetStateHash(s), childID);
69 return queue.backwardQueue.Lookup(
env->GetStateHash(s), childID);
74 auto l =
queue.forwardQueue.Lookup(
env->GetStateHash(s), childID);
76 return queue.forwardQueue.Lookat(childID).g;
82 auto l =
queue.backwardQueue.Lookup(
env->GetStateHash(s), childID);
84 return queue.backwardQueue.Lookat(childID).g;
92 uint64_t necessary = 0;
93 for (
const auto &i :
counts)
121 queue.backwardQueue.Lookup(
env->GetStateHash(
node), theID);
127 thePath.push_back(
queue.backwardQueue.Lookup(
node).data);
130 thePath.push_back(
queue.backwardQueue.Lookup(
node).data);
136 queue.forwardQueue.Lookup(
env->GetStateHash(
node), theID);
143 thePath.push_back(
queue.forwardQueue.Lookup(
node).data);
146 thePath.push_back(
queue.forwardQueue.Lookup(
node).data);
152 void Expand(uint64_t nextID,
153 priorityQueue ¤t,
154 priorityQueue &opposite,
183 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
187 if (InitializeSearch(env, from, to, forward, backward, thePath) ==
false)
190 while (!ExpandAPair(thePath))
194 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
197 std::vector<state> &thePath)
200 forwardHeuristic = forward;
201 backwardHeuristic = backward;
202 currentSolutionEstimate = 0;
203 currentCost = DBL_MAX;
204 queue.Reset(env->GetMaxHash());
214 queue.forwardQueue.AddOpenNode(start, env->GetStateHash(start), 0, forwardHeuristic->HCost(start, goal));
215 queue.backwardQueue.AddOpenNode(goal, env->GetStateHash(goal), 0, backwardHeuristic->HCost(goal, start));
220 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
223 uint64_t nForward, nBackward;
224 bool result = queue.GetNextPair(nForward, nBackward);
228 if (currentCost == DBL_MAX)
233 ExtractFromMiddle(thePath);
236 else if (queue.forwardQueue.Lookup(nForward).data == queue.backwardQueue.Lookup(nBackward).data)
240 ExtractFromMiddle(thePath);
243 else if (!
fless(queue.GetLowerBound(), currentCost))
245 ExtractFromMiddle(thePath);
249 counts[queue.GetLowerBound()]+=2;
250 Expand(nForward, queue.forwardQueue, queue.backwardQueue, forwardHeuristic, goal);
251 Expand(nBackward, queue.backwardQueue, queue.forwardQueue, backwardHeuristic, start);
255 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
258 std::vector<state> pFor, pBack;
260 ExtractPathToGoal(middleNode, pBack);
262 ExtractPathToStart(middleNode, pFor);
263 reverse(pFor.begin(), pFor.end());
265 thePath.insert( thePath.end(), pBack.begin()+1, pBack.end() );
269 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
272 return ExpandAPair(thePath);
276 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
278 priorityQueue ¤t,
279 priorityQueue &opposite,
285 uint64_t tmp = current.Close();
286 assert(tmp == nextID);
291 if (
fgreatereq(current.Lookup(nextID).g + current.Lookup(nextID).h, currentCost))
295 env->GetSuccessors(current.Lookup(nextID).data, neighbors);
296 for (
auto &succ : neighbors)
301 auto loc = current.Lookup(env->GetStateHash(succ), childID);
304 double edgeCost = env->GCost(current.Lookup(nextID).data, succ);
305 if (
fgreatereq(current.Lookup(nextID).g+edgeCost, currentCost))
313 if (
fless(current.Lookup(nextID).g + edgeCost, current.Lookup(childID).g))
316 double oldGCost = current.Lookup(childID).g;
317 current.Lookup(childID).parentID = nextID;
318 current.Lookup(childID).g = current.Lookup(nextID).g + edgeCost;
319 current.Reopen(childID);
323 auto loc = opposite.Lookup(env->GetStateHash(succ), reverseLoc);
326 if (
fless(current.Lookup(nextID).g + edgeCost + opposite.Lookup(reverseLoc).g, currentCost))
328 if (currentCost == DBL_MAX)
339 currentCost = current.Lookup(nextID).g + edgeCost + opposite.Lookup(reverseLoc).g;
350 if (
fless(current.Lookup(nextID).g+edgeCost, current.Lookup(childID).g))
352 current.Lookup(childID).parentID = nextID;
353 current.Lookup(childID).g = current.Lookup(nextID).g+edgeCost;
354 current.KeyChanged(childID);
358 auto loc = opposite.Lookup(env->GetStateHash(succ), reverseLoc);
361 if (
fless(current.Lookup(nextID).g+edgeCost + opposite.Lookup(reverseLoc).g, currentCost))
374 currentCost = current.Lookup(nextID).g+edgeCost + opposite.Lookup(reverseLoc).g;
384 current.Remove(childID);
386 #endif // !ADMISSIBLE
393 auto locReverse = opposite.Lookup(env->GetStateHash(succ), reverseLoc);
412 double newNodeF = current.Lookup(nextID).g + edgeCost + heuristic->
HCost(succ, target);
413 if (
fless(newNodeF, currentCost))
415 if (
fless(newNodeF, queue.GetLowerBound()))
416 current.AddOpenNode(succ,
417 env->GetStateHash(succ),
418 current.Lookup(nextID).g + edgeCost,
419 heuristic->
HCost(succ, target),
422 current.AddOpenNode(succ,
423 env->GetStateHash(succ),
424 current.Lookup(nextID).g + edgeCost,
425 heuristic->
HCost(succ, target),
431 if (
fless(current.Lookup(nextID).g + edgeCost + opposite.Lookup(reverseLoc).g, currentCost))
433 if (currentCost == DBL_MAX)
470 currentCost = current.Lookup(nextID).g + edgeCost + opposite.Lookup(reverseLoc).g;
492 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
495 uint64_t doubles = 0;
496 for (
unsigned int x = 0; x < queue.forwardQueue.size(); x++)
499 const auto &data = queue.forwardQueue.Lookat(x);
502 auto loc = queue.backwardQueue.Lookup(env->GetStateHash(data.data), key);
511 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
514 OpenGLDraw(queue.forwardQueue);
515 OpenGLDraw(queue.backwardQueue);
518 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
521 double transparency = 0.9;
526 if (q.OpenReadySize() > 0)
530 for (
unsigned int x = 0; x < q.size(); x++)
532 const auto &data = q.Lookat(x);
535 env->SetColor(1.0, 1.0, 0.0, transparency);
536 env->OpenGLDraw(data.data);
540 env->SetColor(0.0, 0.5, 0.5, transparency);
541 env->OpenGLDraw(data.data);
545 env->SetColor(0.0, 1.0, 0.0, transparency);
546 env->OpenGLDraw(data.data);
548 else if (data.where ==
kClosed)
550 if (&q == &queue.backwardQueue)
551 env->SetColor(1.0, 0.0, 1.0, transparency);
553 env->SetColor(1.0, 0.0, 0.0, transparency);
554 env->OpenGLDraw(data.data);
559 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
562 Draw(
d, queue.forwardQueue);
563 Draw(
d, queue.backwardQueue);
566 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
569 double transparency = 0.9;
574 if (q.OpenReadySize() > 0)
578 for (
unsigned int x = 0; x < q.size(); x++)
580 const auto &data = q.Lookat(x);
583 env->SetColor(1.0, 1.0, 0.0, transparency);
584 env->Draw(
d, data.data);
588 env->SetColor(1.0, 0.50, 0.25, transparency);
589 env->Draw(
d, data.data);
593 env->SetColor(1.0, 0.75, 0.25, transparency);
594 env->Draw(
d, data.data);
596 else if (data.where ==
kClosed)
598 if (&q == &queue.backwardQueue)
599 env->SetColor(0.25, 0.5, 1.0, transparency);
601 env->SetColor(1.0, 0.0, 0.0, transparency);
602 env->Draw(
d, data.data);
607 template <
class state,
class action,
class environment,
class dataStructure,
class priorityQueue>
610 double val = queue.GetLowerBound();
612 assert(!
"Implementaion incomplete");