11 #ifndef DelayedHeuristicAStar_H
12 #define DelayedHeuristicAStar_H
18 template <
class state,
class Environment>
45 for (
int x = 0; x <
states.size(); x++)
62 template <
class state,
class action,
class environment,
class batchHeuristic = HeuristicLookupBuffer<state, environment>,
class openList = AStarOpenClosed<state, AStarCompareWithF<state>, AStarOpenClosedDataWithF<state>> >
70 phi = [](
double h,
double g){
return g+h; };
73 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
74 void GetPath(environment *,
const state&,
const state&, std::vector<action> & );
79 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
101 bool GetHCost(
const state &val,
double &hCost)
const;
129 void FullBPMX(uint64_t nodeID,
int distance);
137 void SetPhi(std::function<
double(
double,
double)> p)
141 double Phi(
double h,
double g)
148 phi = [=](
double h,
double g){
return g+
weight*h; };
164 std::function<double(
double,
double)>
phi;
193 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
196 static char name[32];
197 sprintf(name,
"DelayedHeuristicAStar[]");
212 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
215 if (!InitializeSearch(_env, from, to, thePath))
219 while (!DoSingleSearchStep(thePath))
226 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
229 std::vector<state> thePath;
230 if (!InitializeSearch(_env, from, to, thePath))
235 while (!DoSingleSearchStep(thePath))
238 for (
size_t x = 0; x < thePath.size()-1; x++)
240 path.push_back(_env->GetAction(thePath[x], thePath[x+1]));
255 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
258 if (theHeuristic == 0)
262 openClosedList.Reset(env->GetMaxHash());
267 currentCostLimit = 0;
268 delayedStates.resize(0);
269 batch.Reset(_env, to, batchLookupSize);
271 if (env->GoalTest(from, to) && (stopAfterGoal))
279 openClosedList.AddOpenNode(start, env->GetStateHash(start), phi(h, 0), 0, h);
294 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
297 if (openClosedList.OpenSize() == 0)
299 HandleBatchedStates();
301 if (openClosedList.OpenSize() == 0)
307 if (
fgreater(openClosedList.Lookup(openClosedList.Peek()).f, currentCostLimit))
308 HandleBatchedStates();
310 uint64_t nodeid = openClosedList.Close();
315 if (!openClosedList.Lookup(nodeid).reopened)
316 uniqueNodesExpanded++;
319 if ((stopAfterGoal) && (env->GoalTest(openClosedList.Lookup(nodeid).data, goal)))
321 ExtractPathToStartFromID(nodeid, thePath);
323 reverse(thePath.begin(), thePath.end());
324 goalFCost = openClosedList.Lookup(nodeid).f;
330 neighborID.resize(0);
331 neighborLoc.resize(0);
336 env->GetSuccessors(openClosedList.Lookup(nodeid).data, neighbors);
337 double bestH = openClosedList.Lookup(nodeid).h;
338 double lowHC = DBL_MAX;
340 for (
unsigned int x = 0; x < neighbors.size(); x++)
343 neighborLoc.push_back(openClosedList.Lookup(env->GetStateHash(neighbors[x]), theID));
344 neighborID.push_back(theID);
345 edgeCosts.push_back(env->GCost(openClosedList.Lookup(nodeid).data, neighbors[x]));
349 for (
size_t x = 0; x < neighbors.size(); x++)
354 theConstraint->ShouldNotGenerate(start, openClosedList.Lookup(nodeid).data, neighbors[x],
355 openClosedList.Lookup(nodeid).g+edgeCosts[x], goal))
358 switch (neighborLoc[x])
363 if (
fless(openClosedList.Lookup(nodeid).g+edgeCosts[x], openClosedList.Lookup(neighborID[x]).g))
365 auto &i = openClosedList.Lookup(neighborID[x]);
367 i.g = openClosedList.Lookup(nodeid).g+edgeCosts[x];
369 openClosedList.Reopen(neighborID[x]);
373 i.data = neighbors[x];
379 if (
fless(openClosedList.Lookup(nodeid).g+edgeCosts[x], openClosedList.Lookup(neighborID[x]).g))
381 auto &i = openClosedList.Lookup(neighborID[x]);
383 i.g = openClosedList.Lookup(nodeid).g+edgeCosts[x];
388 i.data = neighbors[x];
389 openClosedList.KeyChanged(neighborID[x]);
399 delayedStates.push_back({neighbors[x],
400 env->GetStateHash(neighbors[x]),
401 openClosedList.Lookup(nodeid).g+edgeCosts[x],
403 batch.Add(neighbors[x]);
404 if (batch.HitNodeLimit())
405 HandleBatchedStates();
420 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
423 if (delayedStates.size() == 0)
425 auto vec = batch.Evaluate();
426 for (
int x = 0; x < delayedStates.size(); x++)
428 double h = vec.at(x);
429 openClosedList.AddOpenNode(delayedStates[x].s,
430 delayedStates[x].hash,
431 phi(h, delayedStates[x].g),
434 delayedStates[x].parent);
436 delayedStates.resize(0);
437 currentCostLimit = openClosedList.Lookup(openClosedList.Peek()).f;
449 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
452 uint64_t key = openClosedList.Peek();
453 return openClosedList.Lookup(key).data;
465 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
472 std::vector<state> succ;
473 env->GetSuccessors(openClosedList.Lookup(nodeID).data, succ);
474 double parentH = openClosedList.Lookup(nodeID).h;
477 for (
unsigned int x = 0; x < succ.size(); x++)
480 dataLocation loc = openClosedList.Lookup(env->GetStateHash(succ[x]), theID);
481 double edgeCost = env->GCost(openClosedList.Lookup(nodeID).data, succ[x]);
482 double newHCost = parentH-edgeCost;
488 if (
fgreater(newHCost, openClosedList.Lookup(theID).h))
490 openClosedList.Lookup(theID).h = newHCost;
491 FullBPMX(theID, distance-1);
496 if (
fgreater(newHCost, openClosedList.Lookup(theID).h))
498 openClosedList.Lookup(theID).h = newHCost;
499 openClosedList.KeyChanged(theID);
516 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
518 std::vector<state> &thePath)
521 thePath.push_back(openClosedList.Lookup(
node).data);
522 node = openClosedList.Lookup(
node).parentID;
523 }
while (openClosedList.Lookup(
node).parentID !=
node);
524 thePath.push_back(openClosedList.Lookup(
node).data);
527 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
531 openClosedList.Lookup(env->GetStateHash(s), theID);
532 theID = openClosedList.Lookup(theID).parentID;
533 return openClosedList.Lookup(theID).data;
536 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
540 for (
unsigned int x = 0; x < openClosedList.size(); x++)
542 const auto &data = openClosedList.Lookat(x);
543 if (
fless(data.g + data.h, goalFCost))
556 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
559 printf(
"%u items in closed list\n", (
unsigned int)openClosedList.ClosedSize());
560 printf(
"%u items in open queue\n", (
unsigned int)openClosedList.OpenSize());
570 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
573 return openClosedList.size();
586 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
590 dataLocation loc = openClosedList.Lookup(env->GetStateHash(val), theID);
593 gCost = openClosedList.Lookat(theID).g;
599 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
603 dataLocation loc = openClosedList.Lookup(env->GetStateHash(val), theID);
606 gCost = openClosedList.Lookat(theID).g;
612 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
616 dataLocation loc = openClosedList.Lookup(env->GetStateHash(val), theID);
619 hCost = openClosedList.Lookat(theID).h;
625 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
632 result = openClosedList.Lookat(theID);
646 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
649 double transparency = 1.0;
650 if (openClosedList.size() == 0)
654 if (openClosedList.OpenSize() > 0)
656 top = openClosedList.Peek();
667 for (
unsigned int x = 0; x < openClosedList.size(); x++)
669 const auto &data = openClosedList.Lookat(x);
672 env->SetColor(1.0, 1.0, 0.0, transparency);
673 env->OpenGLDraw(data.data);
675 if ((data.where ==
kOpenList) && (data.reopened))
677 env->SetColor(0.0, 0.5, 0.5, transparency);
678 env->OpenGLDraw(data.data);
682 env->SetColor(0.0, 1.0, 0.0, transparency);
683 env->OpenGLDraw(data.data);
685 else if ((data.where ==
kClosedList) && (data.reopened))
687 env->SetColor(0.5, 0.0, 0.5, transparency);
688 env->OpenGLDraw(data.data);
697 if (data.parentID == x)
698 env->SetColor(1.0, 0.5, 0.5, transparency);
700 env->SetColor(1.0, 0.0, 0.0, transparency);
702 env->OpenGLDraw(data.data);
705 env->SetColor(1.0, 0.5, 1.0, 0.5);
706 env->OpenGLDraw(goal);
715 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
718 double transparency = 1.0;
719 if (openClosedList.size() == 0)
723 if (openClosedList.OpenSize() > 0)
725 top = openClosedList.Peek();
727 for (
unsigned int x = 0; x < openClosedList.size(); x++)
729 const auto &data = openClosedList.Lookat(x);
732 env->SetColor(1.0, 1.0, 0.0, transparency);
733 env->Draw(disp, data.
data);
735 else if ((data.where ==
kOpenList) && (data.reopened))
737 env->SetColor(0.0, 0.5, 0.5, transparency);
738 env->Draw(disp, data.
data);
742 env->SetColor(0.0, 1.0, 0.0, transparency);
743 env->Draw(disp, data.
data);
745 else if ((data.where ==
kClosedList) && (data.reopened))
747 env->SetColor(0.5, 0.0, 0.5, transparency);
748 env->Draw(disp, data.
data);
757 if (data.parentID == x)
758 env->SetColor(1.0, 0.5, 0.5, transparency);
760 env->SetColor(1.0, 0.0, 0.0, transparency);
762 env->Draw(disp, data.
data);
765 env->SetColor(1.0, 0.5, 1.0, 0.5);
766 env->Draw(disp, goal);
767 for (
unsigned int x = 0; x < delayedStates.size(); x++)
770 env->Draw(disp, delayedStates[x].s);
774 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
778 double transparency = 1.0;
779 if (openClosedList.size() == 0)
783 if (openClosedList.OpenSize() > 0)
785 top = openClosedList.Peek();
787 for (
unsigned int x = 0; x < openClosedList.size(); x++)
789 const auto &data = openClosedList.Lookat(x);
793 env->SetColor(1.0, 1.0, 0.0, transparency);
794 s+=env->SVGDraw(data.data);
796 else if ((data.where ==
kOpenList) && (data.reopened))
798 env->SetColor(0.0, 0.5, 0.5, transparency);
799 s+=env->SVGDraw(data.data);
803 env->SetColor(0.0, 1.0, 0.0, transparency);
804 s+=env->SVGDraw(data.data);
806 else if ((data.where ==
kClosedList) && (data.reopened))
808 env->SetColor(0.5, 0.0, 0.5, transparency);
809 s+=env->SVGDraw(data.data);
813 env->SetColor(1.0, 0.0, 0.0, transparency);
814 s+=env->SVGDraw(data.data);
820 template <
class state,
class action,
class environment,
class batchHeuristic,
class openList>
825 if (openClosedList.size() == 0)
829 if (openClosedList.OpenSize() > 0)
831 top = openClosedList.Peek();
833 for (
unsigned int x = 0; x < openClosedList.size(); x++)
835 const auto &data = openClosedList.Lookat(x);
862 env->SetColor(0.0, 0.0, 0.0);
864 sprintf(
d,
"%1.1f", data.g+data.h);
865 s+=env->SVGLabelState(data.data,
d, 0.35, -0.6, -1.1);
866 sprintf(
d,
"g:%1.1f", data.g);
867 s+=env->SVGLabelState(data.data,
d, 0.25, -0.6, -0.75);
868 sprintf(
d,
"h:%1.1f", data.h);
869 s+=env->SVGLabelState(data.data,
d, 0.25, -0.6, -0.48);