19 #include <unordered_map>
21 template <
class state>
28 template <
class state,
class action,
class environment>
35 void GetPath(environment *env,
const state& from,
const state& to, std::vector<state> &thePath);
36 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & ) { assert(
false); };
37 virtual const char *
GetName() {
return "LRTAStar"; }
38 void SetHCost(environment *env,
const state &where,
const state &to,
double val)
40 heur[env->GetStateHash(where)].theHeuristic = val-env->HCost(where, to);
41 heur[env->GetStateHash(where)].theState = where;
43 double HCost(environment *env,
const state &from,
const state &to)
const
45 auto val =
heur.find(env->GetStateHash(from));
46 if (val !=
heur.end())
48 return val->second.theHeuristic+env->HCost(from, to);
50 return env->HCost(from, to);
76 template <
class state,
class action,
class environment>
86 static const bool benefitRule =
true;
89 double minF = DBL_MAX;
90 double minF_D = DBL_MAX;
103 std::vector<state> neighbors;
104 env->GetSuccessors(from, neighbors);
105 for (
unsigned int x = 0; x < neighbors.size(); x++)
111 if (env->GetOccupancyInfo() && !env->GetOccupancyInfo()->CanMove(from, neighbors[x]))
115 double g = env->GCost(from, neighbors[x]);
118 double h = HCost(env, neighbors[x], to);
136 if (
fless((h-env->HCost(neighbors[x], to))*10
139 minF_D = (h-env->HCost(neighbors[x], to))*10+g+h;
157 double oldH = HCost(env, from, to);
158 double deltaH = newH - oldH;
161 if (benefitRule &&
fless(deltaH, 0.0))
164 deltaH = fabs(deltaH);
168 SetHCost(env, from, to, newH);
173 fAmountLearned += deltaH;
177 thePath.push_back(neighbors[minD]);
179 thePath.push_back(neighbors[minC]);
180 thePath.push_back(from);
184 template <
class state,
class action,
class environment>
188 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
190 double thisState = (*it).second.theHeuristic;
191 if (learned < thisState)
194 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
196 double r = (*it).second.theHeuristic;
199 e->SetColor(0.5+0.5*r/learned, 0, 0, 0.1+0.8*r/learned);
200 e->OpenGLDraw((*it).second.theState);