HOG2
HeuristicError.h
Go to the documentation of this file.
1 //
2 // HeuristicError.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 1/24/18.
6 // Copyright © 2018 University of Denver. All rights reserved.
7 //
8 
9 #ifndef HeuristicError_h
10 #define HeuristicError_h
11 
12 #include "Heuristic.h"
13 
14 // This function counts the number of states that match the functional criteria on the heuristic when
15 // regressing the given distance and then doing a forward search the forward distance.
16 // For instance, HR2 could be tested by regressing 3, forward 2, and testing for 0 heuristics.
17 // Returns the percentage of states at the regression distance that have a child that meet the criteria
18 template <class environment, class state, typename func>
19 float MeasureHeuristicErrors(environment *e, state s, Heuristic<state> *h, int regressionDistance, int forwardDistance, func f)
20 {
21 // std::cout << "Processing " << s << "\n";
22  // Store states and their depth
23  std::unordered_map<state, int> table;
24  std::vector<state> succ;
25  table[s] = 0;
26  for (int x = 0; x < regressionDistance; x++)
27  {
28  for (auto iter = table.begin(); iter != table.end(); iter++)
29  {
30  // Expands states at current depth
31  if (iter->second == x)
32  {
33  e->GetSuccessors(iter->first, succ);
34  for (auto &itm : succ)
35  {
36  if (table.find(itm) == table.end())
37  {
38 // std::cout << "Adding (" << itm << ") at depth " << x+1 << "\n";
39  table[itm] = x+1;
40  }
41  }
42  }
43  }
44  }
45 
46  std::function<int (const state&, int)> DFS;
47  DFS = [h,&s,&f,e,&DFS,&table](const state &i, int depth)->int {
48  if (depth == 0)
49  return f(h->HCost(i, s))?1:0;
50  std::vector<state> neighbors;
51  e->GetSuccessors(i, neighbors);
52  int count = 0;
53  for (const state &succ : neighbors)
54  {
55 // if (table.find(succ) != table.end())
56 // continue;
57  count += DFS(succ, depth-1);
58  }
59  return count;
60  };
61 
62  float total = 0;
63  float pass = 0;
64  float children = 0;
65  for (auto iter = table.begin(); iter != table.end(); iter++)
66  {
67  // Expands states at current depth
68  if (iter->second == regressionDistance)
69  {
70  total++;
71  int res = DFS(iter->first, forwardDistance);
72  if (res > 0)
73  pass++;
74  children += res;
75  }
76  }
77  printf("%d of %d match condition. Avg %1.2f per state\n", (int)pass, (int)total, children/total);
78  return pass/total;
79 }
80 
81 #endif /* HeuristicError_h */
Heuristic
Definition: Heuristic.h:30
Heuristic.h
DFS
Definition: DFS.h:19
Heuristic::HCost
virtual double HCost(const state &a, const state &b) const
Definition: Heuristic.h:73
MeasureHeuristicErrors
float MeasureHeuristicErrors(environment *e, state s, Heuristic< state > *h, int regressionDistance, int forwardDistance, func f)
Definition: HeuristicError.h:19