HOG2
Heuristic.h
Go to the documentation of this file.
1 //
2 // Heuristic.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 8/19/15.
6 // Copyright (c) 2015 University of Denver. All rights reserved.
7 //
8 
9 #ifndef hog2_glut_Heuristic_h
10 #define hog2_glut_Heuristic_h
11 
12 #include <vector>
13 #include <cstdint>
14 #include <cstddef>
15 
20 };
21 
23 {
25  unsigned int whichNode;
26  unsigned int numChildren;
27 };
28 
29 template <class state>
30 class Heuristic {
31 public:
32  Heuristic() { for (int x = 0; x < 256; x++) histogram[x] = 0; }
33  virtual ~Heuristic() {}
34  virtual double HCost(const state &a, const state &b) const;
35  std::vector<HeuristicTreeNode> lookups;
36  std::vector<Heuristic*> heuristics;
37  mutable uint64_t histogram[256];
38 private:
39  double HCost(const state &s1, const state &s2, int treeNode) const;
40 };
41 
42 template <class state>
43 class ZeroHeuristic : public Heuristic<state> {
44 public:
45  double HCost(const state &a, const state &b) const { return 0; }
46 };
47 
48 template <class state>
49 class WeightedHeuristic : public Heuristic<state> {
50 public:
52  double HCost(const state &a, const state &b) const { return weight*h->HCost(a, b); }
53  void SetWeight(double w) { weight = w; }
54 //private:
56  double weight;
57 };
58 
59 template <class state>
60 class OffsetHeuristic : public Heuristic<state> {
61 public:
62  OffsetHeuristic(Heuristic<state> *h, double off) :h(h), offset(off){}
63  double HCost(const state &a, const state &b) const { return std::max(h->HCost(a, b)-offset, 0.0); }
64  void SetOffset(double w) { offset = w; }
65  //private:
67  double offset;
68 };
69 
70 
71 
72 template <class state>
73 double Heuristic<state>::HCost(const state &s1, const state &s2) const
74 {
75  return HCost(s1, s2, 0);
76 }
77 
78 template <class state>
79 double Heuristic<state>::HCost(const state &s1, const state &s2, int treeNode) const
80 {
81  double hval = 0;
82  switch (lookups[treeNode].nodeType)
83  {
84  case kMaxNode:
85  {
86  for (size_t x = 0; x < lookups[treeNode].numChildren; x++)
87  {
88  hval = std::max(hval, HCost(s1, s2, lookups[treeNode].whichNode+x));
89  }
90  } break;
91  case kAddNode:
92  {
93  for (size_t x = 0; x < lookups[treeNode].numChildren; x++)
94  {
95  hval += HCost(s1, s2, lookups[treeNode].whichNode+x);
96  }
97  } break;
98  case kLeafNode:
99  {
100  hval = heuristics[lookups[treeNode].whichNode]->HCost(s1, s2);
101  } break;
102  }
103 // histogram[int(hval)]++;
104  return hval;
105 }
106 
107 #endif
WeightedHeuristic
Definition: Heuristic.h:49
ZeroHeuristic::HCost
double HCost(const state &a, const state &b) const
Definition: Heuristic.h:45
HeuristicTreeNode::whichNode
unsigned int whichNode
Definition: Heuristic.h:25
HeuristicTreeNode::numChildren
unsigned int numChildren
Definition: Heuristic.h:26
ZeroHeuristic
Definition: Heuristic.h:43
Heuristic::heuristics
std::vector< Heuristic * > heuristics
Definition: Heuristic.h:36
Heuristic
Definition: Heuristic.h:30
HeuristicTreeNode
Definition: Heuristic.h:22
HeuristicTreeNode::nodeType
HeuristicTreeNodeType nodeType
Definition: Heuristic.h:24
OffsetHeuristic::HCost
double HCost(const state &a, const state &b) const
Definition: Heuristic.h:63
kAddNode
@ kAddNode
Definition: Heuristic.h:18
WeightedHeuristic::HCost
double HCost(const state &a, const state &b) const
Definition: Heuristic.h:52
HeuristicTreeNodeType
HeuristicTreeNodeType
Definition: Heuristic.h:16
OffsetHeuristic::h
Heuristic< state > * h
Definition: Heuristic.h:66
Heuristic::Heuristic
Heuristic()
Definition: Heuristic.h:32
OffsetHeuristic
Definition: Heuristic.h:60
OffsetHeuristic::SetOffset
void SetOffset(double w)
Definition: Heuristic.h:64
WeightedHeuristic::WeightedHeuristic
WeightedHeuristic(Heuristic< state > *h, double weight)
Definition: Heuristic.h:51
WeightedHeuristic::weight
double weight
Definition: Heuristic.h:56
kLeafNode
@ kLeafNode
Definition: Heuristic.h:19
Heuristic::HCost
virtual double HCost(const state &a, const state &b) const
Definition: Heuristic.h:73
WeightedHeuristic::SetWeight
void SetWeight(double w)
Definition: Heuristic.h:53
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
Heuristic::~Heuristic
virtual ~Heuristic()
Definition: Heuristic.h:33
OffsetHeuristic::OffsetHeuristic
OffsetHeuristic(Heuristic< state > *h, double off)
Definition: Heuristic.h:62
OffsetHeuristic::offset
double offset
Definition: Heuristic.h:67
WeightedHeuristic::h
Heuristic< state > * h
Definition: Heuristic.h:55
Heuristic::histogram
uint64_t histogram[256]
Definition: Heuristic.h:37
kMaxNode
@ kMaxNode
Definition: Heuristic.h:17
Heuristic::lookups
std::vector< HeuristicTreeNode > lookups
Definition: Heuristic.h:35