9 #ifndef hog2_glut_BidirectionalDijkstra_h
10 #define hog2_glut_BidirectionalDijkstra_h
15 template <
class state>
24 template <
class state,
class action,
class environment,
class priorityQueue = AStarOpenClosed<state,BDCompare<state>> >
29 void GetPath(environment *
env,
const state& from,
const state& to,std::vector<state> &thePath);
30 bool InitializeSearch(environment *
env,
const state& from,
const state& to,std::vector<state> &thePath);
32 void ExtractPath(uint64_t node1,uint64_t node2,std::vector<state> &thePath);
47 virtual const char *
GetName() {
return "Bidirectional Dijkstra"; }
82 void OpenGLDraw(
const priorityQueue &queue)
const;
84 void Expand(priorityQueue ¤t,priorityQueue &opposite,
const state &target);
107 template <
class state,
class action,
class environment,
class priorityQueue>
111 if(InitializeSearch(env,from,to,thePath)==
false)
114 while(!DoSingleSearchStep(thePath))
118 template<
class state,
class action,
class environment,
class priorityQueue>
123 forwardQueue.Reset();
124 backwardQueue.Reset();
135 forwardQueue.AddOpenNode(start,env->GetStateHash(start),0,0);
136 backwardQueue.AddOpenNode(goal,env->GetStateHash(goal),0,0);
141 template<
class state,
class action,
class environment,
class priorityQueue>
144 if(forwardQueue.OpenSize()==0 && backwardQueue.OpenSize()==0)
149 if(forwardQueue.OpenSize()==0)
151 Expand(backwardQueue,forwardQueue,start);
153 else if(backwardQueue.OpenSize()==0)
155 Expand(forwardQueue,backwardQueue,goal);
158 uint64_t forward = forwardQueue.Peek();
159 uint64_t backward = backwardQueue.Peek();
164 double p1=nextForward.
g;
165 double p2=nextBackward.
g;
171 forwardDirection =
true;
173 forwardDirection =
false;
180 forwardDirection =
true;
185 forwardDirection =
false;
189 if(forwardQueue.size() <= backwardQueue.size())
191 forwardDirection =
true;
195 forwardDirection =
false;
201 Expand(forwardQueue,backwardQueue,goal);
205 Expand(backwardQueue, forwardQueue, start);
212 if(
fless(currentCost,DBL_MAX))
214 uint64_t fminID = forwardQueue.Peek();
215 uint64_t bminID = backwardQueue.Peek();
220 if (!
fless((fmin.
g+bmin.
g+epsilon), currentCost))
222 optimalPathCost = currentCost;
223 ExtractPath(middleNode,middleNode2,thePath);
233 template<
class state,
class action,
class environment,
class priorityQueue>
236 while(forwardQueue.Lookup(node1).parentID != node1)
238 thePath.insert(thePath.begin(), forwardQueue.Lookup(node1).data);
239 node1 = forwardQueue.Lookup(node1).parentID;
242 thePath.insert(thePath.begin(), forwardQueue.Lookup(node1).data);
244 while(backwardQueue.Lookup(node2).parentID != node2)
246 thePath.push_back(backwardQueue.Lookup(node2).data);
247 node2 = backwardQueue.Lookup(node2).parentID;
250 thePath.push_back(backwardQueue.Lookup(node2).data);
253 template<
class state,
class action,
class environment,
class priorityQueue>
256 uint64_t nextID=current.Close();
337 env->GetSuccessors(current.Lookup(nextID).data,neighbors);
339 for(
auto &succ : neighbors)
345 auto loc = current.Lookup(env->GetStateHash(succ), childID);
353 double edgeCost=env->GCost(current.Lookup(nextID).data,succ);
355 if(
fless(current.Lookup(nextID).g+edgeCost,current.Lookup(childID).g))
357 current.Lookup(childID).parentID=nextID;
358 current.Lookup(childID).g=current.Lookup(nextID).g+edgeCost;
359 current.KeyChanged(childID);
366 auto loc=opposite.Lookup(env->GetStateHash(succ),reverseLoc);
370 if(
fless(current.Lookup(nextID).g+edgeCost+opposite.Lookup(reverseLoc).g,currentCost))
375 currentCost = current.Lookup(nextID).g+edgeCost + opposite.Lookup(reverseLoc).g;
380 middleNode2 = reverseLoc;
384 middleNode = reverseLoc;
385 middleNode2 = nextID;
396 double edgeCost=env->GCost(current.Lookup(nextID).data,succ);
398 current.AddOpenNode(succ,env->GetStateHash(succ),current.Lookup(nextID).g+edgeCost,0,nextID);
403 auto loc = opposite.Lookup(env->GetStateHash(succ), reverseLoc);
407 if (
fless(current.Lookup(nextID).g+edgeCost + opposite.Lookup(reverseLoc).g, currentCost))
414 currentCost = current.Lookup(nextID).g+edgeCost + opposite.Lookup(reverseLoc).g;
419 middleNode2 = reverseLoc;
423 middleNode = reverseLoc;
424 middleNode2 = nextID;
434 template<
class state,
class action,
class environment,
class priorityQueue>
437 OpenGLDraw(forwardQueue);
438 OpenGLDraw(backwardQueue);
441 template<
class state,
class action,
class environment,
class priorityQueue>
444 double transparency=0.9;
449 if (queue.OpenSize() > 0)
453 for (
unsigned int x = 0; x < queue.size(); x++)
458 env->SetColor(1.0, 1.0, 0.0, transparency);
459 env->OpenGLDraw(data.
data);
463 env->SetColor(0.0, 0.5, 0.5, transparency);
464 env->OpenGLDraw(data.
data);
468 env->SetColor(0.0, 1.0, 0.0, transparency);
469 env->OpenGLDraw(data.
data);
473 env->SetColor(0.5, 0.0, 0.5, transparency);
474 env->OpenGLDraw(data.
data);
478 env->SetColor(1.0, 0.0, 0.0, transparency);
479 env->OpenGLDraw(data.
data);
485 template<
class state,
class action,
class environment,
class priorityQueue>
490 nodesExpanded_NN_PathCost = nodesExpanded_NF_PathCost = nodesExpanded_FN_PathCost = nodesExpanded_FF_PathCost = nodesExpanded_RN_PathCost = nodesExpanded_RF_PathCost = 0;
492 nodesExpanded_NN_PathLength = nodesExpanded_NF_PathLength = nodesExpanded_FN_PathLength = nodesExpanded_FF_PathLength = nodesExpanded_RN_PathLength = nodesExpanded_RF_PathLength = 0;
494 this->optimalPathCost = optimalPathCost;
495 this->optimalPathLength = optimalPathLength;
497 std::vector<state> thePath;
499 GetPath(env, from, to, thePath);
501 countRegions =
false;