9 #ifndef hog2_glut_daLRTAStar_h
10 #define hog2_glut_daLRTAStar_h
16 #include <unordered_map>
27 template <
class state>
35 template <
class state>
45 template <
class state>
54 template <
class state,
class action,
class environment>
61 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> & ) { assert(
false); };
66 static char name[255];
69 void SetHCost(environment *env,
const state &where,
const state &to,
double val)
71 heur[env->GetStateHash(where)].theHeuristic = val-env->HCost(where, to);
73 heur[env->GetStateHash(where)].theState = where;
76 double HCost(environment *env,
const state &from,
const state &to)
const
78 auto val =
heur.find(env->GetStateHash(from));
79 if (val !=
heur.end())
81 return val->second.theHeuristic+env->HCost(from, to);
83 return env->HCost(from, to);
92 if (
heur.find(env->GetStateHash(from)) !=
heur.end())
93 return heur[env->GetStateHash(from)].theHeuristic;
96 double HCost(
const state &from,
const state &to)
const
109 void OpenGLDraw(
const environment *env)
const;
113 typedef std::unordered_map<uint64_t, bool, Hash64 >
ClosedList;
116 bool ExpandLSS(environment *env,
const state &from,
const state &to, std::vector<state> &thePath);
117 void BuildLSSQ(environment *env,
pQueue &q, state &best,
const state &target);
134 template <
class state,
class action,
class environment>
144 if (thePath.size() != 0)
150 gotoGoal = ExpandLSS(env, from, to, thePath);
152 BuildLSSQ(env, q1, first, to);
153 LearnHeuristic(env, q1, to);
158 astar.ExtractPathToStart(first, thePath);
163 template <
class state,
class action,
class environment>
166 bool expandedGoal =
false;
167 astar.InitializeSearch(env, from, to, thePath);
168 astar.SetHeuristic(
this);
170 for (
int x = 0; x < nodeExpansionLimit; x++)
172 if (astar.CheckNextNode() == to)
177 astar.DoSingleSearchStep(thePath);
179 nodesExpanded = astar.GetNodesExpanded();
180 nodesTouched = astar.GetNodesTouched();
184 template <
class state,
class action,
class environment>
190 unsigned int openSize = astar.GetNumOpenItems();
191 for (
unsigned int x = 0; x <
openSize; x++)
193 const auto data = astar.GetOpenItem(x);
194 if ((bestF == -1) || (
fless(HCostLearned(env,data.data)*10000+(data.g+data.h), bestF)))
196 bestF = HCostLearned(env,data.data)*10000+(data.g+data.h);
199 double h = HCost(env, data.data, target);
201 if (
verbose) std::cout <<
"Preparing border state: " << data.data <<
" h: " << h << std::endl;
205 template <
class state,
class action,
class environment>
208 std::vector<state> succ;
214 state s = q.top().theState;
215 if (
verbose) std::cout <<
"Starting with " << s <<
" h: " << q.top().heuristic <<
"/" << HCost(env, s, to) << std::endl;
218 env->GetSuccessors(s, succ);
219 double hCost = HCost(env, s, to);
220 for (
unsigned int x = 0; x < succ.size(); x++)
224 if (!astar.GetClosedListGCost(succ[x], succHCost))
226 if (
verbose) std::cout << succ[x] <<
" not in closed\n";
230 double edgeCost = env->GCost(s, succ[x]);
231 if (
verbose) std::cout << s <<
" to " << succ[x] <<
" " << edgeCost <<
" ";
232 succHCost = HCost(env, succ[x], to);
233 if (c[env->GetStateHash(succ[x])])
235 if (
verbose) std::cout << succ[x] <<
" updated before ";
236 if (
fless(hCost + edgeCost, succHCost))
238 if (
verbose) std::cout <<
"lowering cost to " << hCost + edgeCost <<
" from " << succHCost << std::endl;
239 fAmountLearned = fAmountLearned - (succHCost - (hCost+edgeCost));
240 if (
verbose) std::cout <<
" learning now " << fAmountLearned;
241 SetHCost(env, succ[x], to, hCost + edgeCost);
244 if (
verbose) std::cout << std::endl;
249 if (
verbose) std::cout << succ[x] <<
" NOT updated before ";
252 if (
verbose) std::cout <<
"setting cost to " << hCost + edgeCost <<
" over " << succHCost;
253 fAmountLearned += (edgeCost + hCost) - succHCost;
254 if (
verbose) std::cout <<
" learning now " << fAmountLearned;
255 SetHCost(env, succ[x], to, hCost + edgeCost);
257 c[env->GetStateHash(succ[x])] =
true;
259 if (
verbose) std::cout << std::endl;
266 template <
class state,
class action,
class environment>
271 unsigned int openSize = astar.GetNumOpenItems();
272 for (
unsigned int x = 0; x <
openSize; x++)
274 const auto data = astar.GetOpenItem(x);
275 if (data.data == first)
276 e->SetColor(0, 0.5, 0.5);
278 e->SetColor(0, 1, 0);
279 e->OpenGLDraw(data.data);
283 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
285 double thisState = (*it).second.theHeuristic;
286 if (learned < thisState)
292 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
294 double r = (*it).second.theHeuristic;
297 sprintf(str,
"%3.2f", r);
300 e->SetColor(0.5+0.5*r/learned, 0, 0.0, 0.5+0.5*r/learned);
302 e->OpenGLDraw((*it).second.theState);