18 #include <unordered_map>
22 template <
class state>
36 template <
class state,
class action,
class environment>
43 void GetPath(environment *env,
const state& from,
const state& to, std::vector<state> &thePath);
44 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & ) { assert(
false); };
45 virtual const char *
GetName() {
return "HLRTAStar"; }
46 void SetH1Cost(environment *env,
const state &where,
const state &to,
double val)
49 heur[env->GetStateHash(where)].h1 = val-env->HCost(where, to);
50 heur[env->GetStateHash(where)].theState = where;
52 void SetH2Cost(environment *env,
const state &where,
const state &to,
double val)
54 heur[env->GetStateHash(where)].h2 = val-env->HCost(where, to);
55 heur[env->GetStateHash(where)].theState = where;
57 void SetLastMove(environment *env,
const state &where,
const state &to)
59 heur[env->GetStateHash(where)].lastMove = to;
60 heur[env->GetStateHash(where)].validLastMove =
true;
62 bool GetLastMove(environment *env,
const state &where, state &to)
64 if (
heur[env->GetStateHash(where)].validLastMove)
65 to =
heur[env->GetStateHash(where)].lastMove;
67 return heur[env->GetStateHash(where)].validLastMove;
72 double myFCost = env->GCost(from,
neighbor);
82 double HCost1(environment *env,
const state &from,
const state &to)
84 if (
heur.find(env->GetStateHash(from)) !=
heur.end())
85 return heur[env->GetStateHash(from)].h1+env->HCost(from, to);
86 return env->HCost(from, to);
89 double HCost2(environment *env,
const state &from,
const state &to)
91 if (
heur.find(env->GetStateHash(from)) !=
heur.end())
92 return heur[env->GetStateHash(from)].h2+env->HCost(from, to);
93 return env->HCost(from, to);
105 void OpenGLDraw(
const environment *env)
const;
116 template <
class state,
class action,
class environment>
130 std::vector<state> neighbors;
131 env->GetSuccessors(from, neighbors);
138 for (
unsigned int x = 0; x < neighbors.size(); x++)
147 double myFCost = LocalFCost(env, from, neighbors[x], to);
148 double bestFCost = LocalFCost(env, from, neighbors[best], to);
150 if (
fless(myFCost,bestFCost))
158 if (secondBest == -1)
163 double secondBestFCost = LocalFCost(env, from, neighbors[secondBest], to);
164 if (
fless(myFCost,secondBestFCost))
173 SetH1Cost(env, from, to, LocalFCost(env, from, neighbors[best], to));
175 if (secondBest == -1)
176 SetH2Cost(env, from, to, 1000);
178 SetH2Cost(env, from, to, LocalFCost(env, from, neighbors[secondBest], to));
179 SetLastMove(env, from, neighbors[best]);
183 thePath.push_back(neighbors[best]);
184 thePath.push_back(from);
188 template <
class state,
class action,
class environment>
192 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
194 double thisState = (*it).second.h1;
195 if (learned < thisState)
198 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
200 double r = (*it).second.h1;
203 e->SetColor(0.5+0.5*r/learned, 0, 0.5, 0.1+0.8*r/learned);
204 e->OpenGLDraw((*it).second.theState);