9 #ifndef OptimisitcSearch_h
10 #define OptimisitcSearch_h
15 #include <unordered_map>
24 template<
typename state>
41 template <
class state>
57 template <
class state,
class action,
class environment>
62 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
63 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & );
72 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
100 uint64_t key =
f.
Peek();
115 { uint64_t key;
return fhat.
Lookup(
env->GetStateHash(val), key); }
160 template <
class state,
class action,
class environment>
163 static char name[32];
164 sprintf(name,
"OptimisticSearch[%1.2f, %1.2f]", bound, weight);
179 template <
class state,
class action,
class environment>
183 if (!InitializeSearch(_env, from, to, thePath))
187 while (!DoSingleSearchStep(thePath))
194 template <
class state,
class action,
class environment>
197 std::vector<state> thePath;
198 if (!InitializeSearch(_env, from, to, thePath))
203 while (!DoSingleSearchStep(thePath))
206 for (
int x = 0; x < thePath.size()-1; x++)
208 path.push_back(_env->GetAction(thePath[x], thePath[x+1]));
223 template <
class state,
class action,
class environment>
227 if (theHeuristic == 0)
231 fhat.Reset(env->GetMaxHash());
232 f.Reset(env->GetMaxHash());
237 if (env->GoalTest(from, to))
245 fhat.AddOpenNode(start, env->GetStateHash(start), 0, weight*theHeuristic->HCost(start, goal));
246 f.AddOpenNode(start, env->GetStateHash(start), 0, theHeuristic->HCost(start, goal));
247 bestSolution = DBL_MAX;
257 template <
class state,
class action,
class environment>
260 fhat.AddOpenNode(newState, env->GetStateHash(newState), 0, weight*theHeuristic->HCost(start, goal));
261 f.AddOpenNode(newState, env->GetStateHash(newState), 0, theHeuristic->HCost(start, goal));
269 template <
class state,
class action,
class environment>
272 fhat.AddOpenNode(newState, env->GetStateHash(newState), cost, weight*theHeuristic->HCost(start, goal));
273 f.AddOpenNode(newState, env->GetStateHash(newState), cost, theHeuristic->HCost(start, goal));
286 template <
class state,
class action,
class environment>
289 if (fhat.OpenSize() == 0)
298 if (bound*(f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h) >= bestSolution)
301 printf(
"Best solution %1.2f\n", bestSolution);
302 printf(
"Best on open %1.2f - bound is %1.2f\n", f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h,
303 (f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h)*bound);
304 ExtractPathToStart(goal, thePath);
306 reverse(thePath.begin(), thePath.end());
314 double oldF = f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h;
317 if (
fless(fhat.Lookat(fhat.Peek()).g+fhat.Lookat(fhat.Peek()).h, bestSolution))
321 nodeOnFHat = fhat.Close();
323 dataLocation d = f.Lookup(env->GetStateHash(fhat.Lookup(nodeOnFHat).data), nodeOnF);
334 f.Lookup(nodeOnF).openedFromF =
true;
336 dataLocation d = fhat.Lookup(env->GetStateHash(f.Lookup(nodeOnF).data), nodeOnFHat);
340 fhat.Close(nodeOnFHat);
341 d = fhat.Lookup(env->GetStateHash(f.Lookup(nodeOnF).data), nodeOnFHat);
346 if (bestSolution != DBL_MAX && !
fequal(oldF, f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h))
349 printf(
"Best solution %1.2f\n", bestSolution);
350 printf(
"Best on open %1.2f - lower bound is %1.2f\n", f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h,
351 (f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h)*bound);
354 if (!fhat.Lookup(nodeOnFHat).reopened)
355 uniqueNodesExpanded++;
360 if (env->GoalTest(fhat.Lookup(nodeOnFHat).data, goal))
363 ExtractPathToStartFromID(nodeOnFHat, thePath);
364 bestSolution = env->GetPathLength(thePath);
366 printf(
"Best solution %1.2f\n", bestSolution);
367 printf(
"Best on open %1.2f - lower bound is %1.2f\n", f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h,
368 (f.Lookat(f.Peek()).g+f.Lookat(f.Peek()).h)*bound);
377 env->GetSuccessors(fhat.Lookup(nodeOnFHat).data, neighbors);
381 for (
int x = 0; x < neighbors.size(); x++)
383 uint64_t childID_fhat, childID_f;
386 d_fhat = fhat.Lookup(env->GetStateHash(neighbors[x]), childID_fhat);
387 d_f = f.Lookup(env->GetStateHash(neighbors[x]), childID_f);
388 double edgeCost = env->GCost(fhat.Lookup(nodeOnFHat).data, neighbors[x]);
400 if (
fless(f.Lookup(nodeOnF).g+edgeCost, f.Lookup(childID_f).g))
403 f.Lookup(childID_f).parentID = nodeOnF;
404 f.Lookup(childID_f).g = f.Lookup(nodeOnF).g+edgeCost;
405 f.Lookup(childID_f).data = neighbors[x];
406 f.KeyChanged(childID_f);
409 fhat.Lookup(childID_fhat).parentID = nodeOnFHat;
410 fhat.Lookup(childID_fhat).g = f.Lookup(nodeOnF).g+edgeCost;
411 fhat.Lookup(childID_fhat).data = neighbors[x];
413 fhat.KeyChanged(childID_fhat);
415 fhat.Reopen(childID_fhat);
423 if (
fless(f.Lookup(nodeOnF).g+edgeCost, f.Lookup(childID_f).g))
427 f.Lookup(childID_f).parentID = nodeOnF;
428 f.Lookup(childID_f).g = f.Lookup(nodeOnF).g+edgeCost;
430 f.Lookup(childID_f).data = neighbors[x];
432 fhat.Lookup(childID_fhat).parentID = nodeOnFHat;
433 fhat.Lookup(childID_fhat).g = fhat.Lookup(nodeOnFHat).g+edgeCost;
434 fhat.Lookup(childID_fhat).data = neighbors[x];
435 fhat.Reopen(childID_fhat);
442 fhat.AddOpenNode(neighbors[x],
443 env->GetStateHash(neighbors[x]),
444 fhat.Lookup(nodeOnFHat).g+edgeCost,
445 weight*theHeuristic->HCost(neighbors[x], goal),
448 f.AddOpenNode(neighbors[x],
449 env->GetStateHash(neighbors[x]),
450 f.Lookup(nodeOnF).g+edgeCost,
451 theHeuristic->HCost(neighbors[x], goal),
465 template <
class state,
class action,
class environment>
468 uint64_t key = fhat.Peek();
469 return fhat.Lookup(key).data;
483 template <
class state,
class action,
class environment>
485 std::vector<state> &thePath)
488 thePath.push_back(fhat.Lookup(
node).data);
490 }
while (fhat.Lookup(
node).parentID !=
node);
491 thePath.push_back(fhat.Lookup(
node).data);
494 template <
class state,
class action,
class environment>
498 fhat.Lookup(env->GetStateHash(s), theID);
499 theID = fhat.Lookup(theID).parentID;
500 return fhat.Lookup(theID).data;
509 template <
class state,
class action,
class environment>
512 printf(
"%u items in closed list\n", (
unsigned int)fhat.ClosedSize());
513 printf(
"%u items in open queue\n", (
unsigned int)fhat.OpenSize());
523 template <
class state,
class action,
class environment>
539 template <
class state,
class action,
class environment>
546 gCost = fhat.Lookat(theID).g;
552 template <
class state,
class action,
class environment>
559 gCost = f.Lookat(theID).g;
565 template <
class state,
class action,
class environment>
572 gCost = fhat.Lookat(theID).g;
578 template <
class state,
class action,
class environment>
585 result = fhat.Lookat(theID);
599 template <
class state,
class action,
class environment>
602 double transparency = 1.0;
603 if (fhat.size() == 0)
606 double bound = DBL_MAX;
609 if (fhat.OpenSize() > 0)
612 const auto &i = f.Lookat(f.Peek());
614 printf(
"Lowest f on open: %f\n", bound);
616 for (
unsigned int x = 0; x < fhat.size(); x++)
618 const auto &data = fhat.Lookat(x);
621 env->SetColor(1.0, 1.0, 0.0, transparency);
622 env->OpenGLDraw(data.data);
626 env->SetColor(0.0, 0.0, 1.0, transparency);
627 env->OpenGLDraw(data.data);
629 else if ((data.where ==
kOpenList) && (data.reopened))
631 env->SetColor(0.0, 0.5, 0.5, transparency);
632 env->OpenGLDraw(data.data);
636 env->SetColor(0.0, 1.0, 0.0, transparency);
637 env->OpenGLDraw(data.data);
639 else if ((data.where ==
kClosedList) && (data.reopened))
641 env->SetColor(0.5, 0.0, 0.5, transparency);
642 env->OpenGLDraw(data.data);
651 if (data.parentID == x)
652 env->SetColor(1.0, 0.5, 0.5, transparency);
654 env->SetColor(1.0, 0.0, 0.0, transparency);
656 env->OpenGLDraw(data.data);
659 env->SetColor(1.0, 0.5, 1.0, 0.5);
660 env->OpenGLDraw(goal);
663 template <
class state,
class action,
class environment>
666 double transparency = 1.0;
667 if (fhat.size() == 0)
670 double bound = DBL_MAX;
673 if (fhat.OpenSize() > 0)
676 const auto &i = f.Lookat(f.Peek());
680 for (
unsigned int x = 0; x < fhat.size(); x++)
682 const auto &data = fhat.Lookat(x);
685 env->SetColor(1.0, 1.0, 0.0, transparency);
686 env->Draw(
d, data.data);
693 if ((data.where ==
kOpenList) && (data.reopened))
695 env->SetColor(0.0, 0.5, 0.5, transparency);
696 env->Draw(
d, data.data);
700 env->SetColor(0.0, 1.0, 0.0, transparency);
701 env->Draw(
d, data.data);
703 else if ((data.where ==
kClosedList) && (data.reopened))
705 env->SetColor(0.5, 0.0, 0.5, transparency);
706 env->Draw(
d, data.data);
715 if (data.parentID == x)
716 env->SetColor(1.0, 0.5, 0.5, transparency);
718 env->SetColor(1.0, 0.0, 0.0, transparency);
720 env->Draw(
d, data.data);
723 for (
unsigned int x = 0; x < f.size(); x++)
725 const auto &data = f.Lookat(x);
726 if (data.openedFromF)
728 env->SetColor(0.0, 0.0, 1.0, transparency);
729 env->Draw(
d, data.data);
733 env->SetColor(1.0, 0.5, 1.0, 0.5);