9 #ifndef DynamicPotentialSearch_h
10 #define DynamicPotentialSearch_h
15 #include <unordered_map>
23 #include <unordered_map>
39 template<
typename state>
43 DPSData(
const state &theData,
double gCost,
double hCost,
const state &par)
55 template <
class state,
class action,
class environment>
60 void GetPath(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
61 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & );
65 typename std::unordered_map<uint64_t, DPSData<state>>::const_iterator
iter;
68 bool InitializeSearch(environment *
env,
const state& from,
const state& to, std::vector<state> &thePath);
76 thePath.push_back(
node);
92 bool GetNext(
double &g,
double &h);
149 template <
class state,
class action,
class environment>
152 static char name[32];
153 sprintf(name,
"DynamicPotentialSearch[%1.2f, %1.2f]", bound, weight);
168 template <
class state,
class action,
class environment>
172 if (!InitializeSearch(_env, from, to, thePath))
176 while (!DoSingleSearchStep(thePath))
180 template <
class state,
class action,
class environment>
183 std::vector<state> thePath;
184 if (!InitializeSearch(_env, from, to, thePath))
189 while (!DoSingleSearchStep(thePath))
191 for (
int x = 0; x < thePath.size()-1; x++)
193 path.push_back(_env->GetAction(thePath[x], thePath[x+1]));
208 template <
class state,
class action,
class environment>
211 bestSolution = DBL_MAX;
213 if (theHeuristic == 0)
222 if (env->GoalTest(from, to))
226 openClosed[env->GetStateHash(start)] = {start, 0, theHeuristic->HCost(start, goal), start};
265 template <
class state,
class action,
class environment>
269 double fmin = DBL_MAX;
271 for (
const auto &item : openClosed)
273 auto &i = item.second;
274 if (i.open ==
true &&
fless(i.g+i.h, fmin))
281 for (
auto &item : openClosed)
283 auto &i = item.second;
289 pr = (bound * fmin - i.g)/i.
h;
306 if (env->GoalTest(next->
data, goal))
308 ExtractPathToStart(next->
data, thePath);
312 env->GetSuccessors(next->
data, neighbors);
315 for (
int x = 0; x < neighbors.size(); x++)
317 uint64_t hash = env->GetStateHash(neighbors[x]);
318 double edgeCost = env->GCost(next->
data, neighbors[x]);
321 auto item = openClosed.find(hash);
323 if (item == openClosed.end())
325 openClosed[hash] = {neighbors[x], next->
g+edgeCost, theHeuristic->HCost(neighbors[x], goal), next->
data};
328 auto &i = item->second;
329 if (
fless(next->
g+edgeCost+i.h, i.g+i.h))
331 i.parent = next->
data;
332 i.g = next->
g+edgeCost;
343 template <
class state,
class action,
class environment>
346 double fmin = DBL_MAX;
348 for (
const auto &item : openClosed)
350 auto &i = item.second;
351 if (i.open ==
true &&
fless(i.g+i.h, fmin))
358 template <
class state,
class action,
class environment>
361 iter = openClosed.begin();
362 while ((iter != openClosed.end()) && iter->second.open ==
false)
368 template <
class state,
class action,
class environment>
371 if (iter == openClosed.end())
377 while ((iter != openClosed.end()) && iter->second.open ==
false)
512 template <
class state,
class action,
class environment>
518 template <
class state,
class action,
class environment>
521 double transparency = 1.0;
523 for (
const auto &item : openClosed)
525 const auto &i = item.second;
526 if (i.open && i.reopened)
528 env->SetColor(0.0, 0.5, 0.5, transparency);
529 env->Draw(
d, i.data);
533 env->SetColor(0.0, 1.0, 0.0, transparency);
534 env->Draw(
d, i.data);
536 else if (!i.open && i.reopened)
538 env->SetColor(0.5, 0.0, 0.5, transparency);
539 env->Draw(
d, i.data);
543 env->SetColor(1.0, 0.0, 0.0, transparency);
544 env->Draw(
d, i.data);
547 env->SetColor(1.0, 0.5, 1.0, 0.5);