17 #include <unordered_map>
25 template <
class state>
33 template <
class state>
43 template <
class state>
51 template <
class state,
class action,
class environment>
68 void GetPath(environment *env,
const state& from,
const state& to, std::vector<state> &thePath);
69 void GetPath(environment *,
const state& ,
const state& , std::vector<action> & ) { assert(
false); };
71 {
static char name[255];
74 void SetHCost(environment *env,
const state &where,
const state &to,
double val)
76 heur[env->GetStateHash(where)].theHeuristic = val-
BaseHCost(env, where, to);
77 heur[env->GetStateHash(where)].theState = where;
82 return heur[
m_pEnv->GetStateHash(from)].theHeuristic;
85 double HCost(environment *env,
const state &from,
const state &to)
const
87 auto val =
heur.find(env->GetStateHash(from));
88 if (val !=
heur.end())
90 return val->second.theHeuristic+env->HCost(from, to);
97 double BaseHCost(environment *env,
const state &from,
const state &to)
const
100 double HCost(
const state &from,
const state &to)
const
106 for (
typename LearnedHeuristic::const_iterator it =
heur.begin(); it !=
heur.end(); it++)
108 double thisState = (*it).second.theHeuristic;
109 if (learned < thisState)
129 void OpenGLDraw(
const environment *env)
const;
132 typedef std::unordered_map<uint64_t, bool, Hash64 >
ClosedList;
150 template <
class state,
class action,
class environment>
154 if (initialHeuristic)
157 if ((tmp = BaseHCost(env, from, to)) > minInitialHeuristic)
159 initialHeuristic =
false;
160 maxLaterHeuristic = tmp;
163 minInitialHeuristic = tmp;
167 maxLaterHeuristic =
std::max(BaseHCost(env, from, to), maxLaterHeuristic);
178 astar.SetHeuristic(
this);
179 astar.InitializeSearch(env, from, to, thePath);
181 for (
int x = 0; x < nodeExpansionLimit; x++)
183 if (astar.CheckNextNode() == to)
185 astar.DoSingleSearchStep(thePath);
193 nodesExpanded = astar.GetNodesExpanded();
194 nodesTouched = astar.GetNodesTouched();
197 if (thePath.size() != 0)
205 double bestLearning = -1;
208 unsigned int openSize = astar.GetNumOpenItems();
210 for (
unsigned int x = 0; x <
openSize; x++)
212 const auto data = astar.GetOpenItem(x);
213 double currLearning = HCostLearned(data.data);
217 (currLearning == 0 && bestLearning != 0) ||
218 (currLearning == 0 && bestLearning == 0 && (
fless(data.g+data.h, bestF))) ||
219 (currLearning != 0 && bestLearning != 0 && (
fless(data.g+data.h, bestF))))
221 bestLearning = currLearning;
222 bestF = data.g+data.h;
227 if ((bestF == -1) || (
fless(data.g+data.h, bestF)))
229 bestF = data.g+data.h;
233 if (randomizeMoves &&
fequal(data.g+data.h, bestF))
236 if (0 == random()%randCount)
243 if (
verbose) std::cout <<
"Preparing border state: " << data.data <<
" h: " << data.h << std::endl;
246 std::vector<state> succ;
252 state s = q.top().theState;
253 if (
verbose) std::cout <<
"Starting with " << s <<
" h: " << q.top().heuristic <<
"/" << HCost(s, to) << std::endl;
256 env->GetSuccessors(s, succ);
257 double hCost = HCost(s, to);
258 for (
unsigned int x = 0; x < succ.size(); x++)
262 if (!astar.GetClosedListGCost(succ[x], succHCost))
264 if (
verbose) std::cout << succ[x] <<
" not in closed\n";
267 double edgeCost = env->GCost(s, succ[x]);
268 if (
verbose) std::cout << s <<
" to " << succ[x] <<
" " << edgeCost <<
" ";
269 succHCost = HCost(env, succ[x], to);
270 if (c[env->GetStateHash(succ[x])])
272 if (
verbose) std::cout << succ[x] <<
" updated before ";
273 if (
fless(hCost + edgeCost, succHCost))
275 if (
verbose) std::cout <<
"lowering cost to " << hCost + edgeCost <<
" from " << succHCost << std::endl;
276 fAmountLearned = fAmountLearned - (succHCost - (hCost+edgeCost));
277 if (
verbose) std::cout <<
" learning now " << fAmountLearned;
278 SetHCost(env, succ[x], to, hCost + edgeCost);
281 if (
verbose) std::cout << std::endl;
286 if (
verbose) std::cout << succ[x] <<
" NOT updated before ";
289 if (
verbose) std::cout <<
"setting cost to " << hCost + edgeCost <<
" over " << succHCost;
290 fAmountLearned += (edgeCost + hCost) - succHCost;
291 if (
verbose) std::cout <<
" learning now " << fAmountLearned;
292 SetHCost(env, succ[x], to, hCost + edgeCost);
294 c[env->GetStateHash(succ[x])] =
true;
296 if (
verbose) std::cout << std::endl;
306 astar.ExtractPathToStart(first, thePath);
307 if (
verbose) std::cout <<
"LSS-LRTA* heading towards " << thePath[0] <<
"with h-value " << HCost(env, thePath[0], to) << std::endl;
312 template <
class state,
class action,
class environment>
318 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
320 double thisState = (*it).second.theHeuristic;
321 if (learned < thisState)
324 for (
typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
326 double r = (*it).second.theHeuristic;
329 e->SetColor(0.5+0.5*r/learned, 0, 0, 0.1+0.8*r/learned);
330 e->OpenGLDraw((*it).second.theState);