10 #ifndef HEURISTICLEARNINGMEASURE_H
11 #define HEURISTICLEARNINGMEASURE_H
14 #include <unordered_map>
19 template <
class state>
28 template <
class state>
40 {
return (
size_t)(x*100); }
51 template <
class state,
class action,
class environment>
61 " over " <<
learnData.size() <<
" states." << std::endl;
69 typedef std::unordered_map<double, sVal, HashDouble> HistogramData;
70 HistogramData histogram;
71 for (
typename EnvironmentData::const_iterator it =
learnData.begin(); it !=
learnData.end(); it++)
73 double diff = (*it).second.learnedHeuristic - (*it).second.initialHeuristic;
74 histogram[diff].increase();
75 histogram[diff].diff = diff;
77 for (
typename HistogramData::const_iterator it = histogram.begin(); it != histogram.end(); it++)
79 printf(
"Histogram:\t%f\t%d\n", (*it).second.diff, (*it).second.count);
85 double maxLearning = 0;
86 for (
typename EnvironmentData::const_iterator it =
learnData.begin(); it !=
learnData.end(); it++)
88 double cnt = (*it).second.learnedHeuristic - (*it).second.initialHeuristic;
89 if (cnt > maxLearning)
93 for (
typename EnvironmentData::const_iterator it =
learnData.begin(); it !=
learnData.end(); it++)
95 double r = (*it).second.learnedHeuristic - (*it).second.initialHeuristic;
98 env->SetColor(0.5+0.5*r/maxLearning, 0, 0, 0.1+0.8*r/maxLearning);
99 env->OpenGLDraw((*it).second.theState);
111 for (
typename EnvironmentData::const_iterator it =
learnData.begin(); it !=
learnData.end(); it++)
116 sum += (*it).second.learnedHeuristic - (*it).second.initialHeuristic;
123 std::vector<state> thePath;
136 std::vector<state> thePath;
140 for (
unsigned int x = 0; x < 20; x++)
148 std::vector<state> thePath;
152 for (
unsigned int x = 0; x < 20; x++)
166 std::vector<state> tempQueue;
167 std::vector<state> succ;
168 tempQueue.push_back(start);
175 while (tempQueue.size() > 0)
177 state s = tempQueue.back();
178 tempQueue.pop_back();
195 learnData[env->GetStateHash(s)].theState = s;
196 learnData[env->GetStateHash(s)].learnedHeuristic = hCost;
197 learnData[env->GetStateHash(s)].initialHeuristic = env->HCost(s, goal);
199 if (
fequal(hCost+gCost, optimalCost))
201 env->GetSuccessors(s, succ);
202 for (
unsigned int y = 0; y < succ.size(); y++)
203 tempQueue.push_back(succ[y]);
221 for (
typename EnvironmentData::const_iterator it =
learnData.begin(); it !=
learnData.end(); it++)
223 q.push((*it).second);
226 std::vector<state> succ;
233 state s = q.top().theState;
236 env->GetSuccessors(s, succ);
237 for (
unsigned int x = 0; x < succ.size(); x++)
239 double edgeCost = env->GCost(s, succ[x]);
242 env->HCost(succ[x], goal)))
245 if (
learnData[env->GetStateHash(succ[x])].learnedHeuristic == -1)
247 learnData[env->GetStateHash(succ[x])].initialHeuristic = env->HCost(succ[x], goal);
248 learnData[env->GetStateHash(succ[x])].learnedHeuristic = env->HCost(succ[x], goal);
249 learnData[env->GetStateHash(succ[x])].theState = succ[x];
252 if (
learnData[env->GetStateHash(s)].learnedHeuristic-edgeCost-
learnData[env->GetStateHash(succ[x])].learnedHeuristic > 0)
254 learning +=
learnData[env->GetStateHash(s)].learnedHeuristic-edgeCost-
learnData[env->GetStateHash(succ[x])].learnedHeuristic;
255 learnData[env->GetStateHash(succ[x])].learnedHeuristic =
learnData[env->GetStateHash(s)].learnedHeuristic-edgeCost;
256 q.push(
learnData[env->GetStateHash(succ[x])]);
301 std::cout <<
"Learned on " <<
learnData.size() <<
" states." << std::endl;