HOG2
HLRTAStar.h
Go to the documentation of this file.
1 
11 #ifndef HLRTASTAR_H
12 #define HLRTASTAR_H
13 
14 #include "LearningAlgorithm.h"
15 #include "FPUtil.h"
16 #include <deque>
17 #include <vector>
18 #include <unordered_map>
19 
20 namespace HLRTA{
21 
22 template <class state>
23 struct learnedData {
25  {
26  validLastMove = false;
27  }
28  state theState; //only for drawing
29  state lastMove;
31  double h1;
32  double h2;
33 };
34 
35 // This class defines the LRTA* algorithm
36 template <class state, class action, class environment>
37 class HLRTAStar : public LearningAlgorithm<state,action,environment> {
38 public:
40  { fAmountLearned = 0.0f; }
41  virtual ~HLRTAStar(void) { }
42 
43  void GetPath(environment *env, const state& from, const state& to, std::vector<state> &thePath);
44  void GetPath(environment *, const state& , const state& , std::vector<action> & ) { assert(false); };
45  virtual const char *GetName() { return "HLRTAStar"; }
46  void SetH1Cost(environment *env, const state &where, const state &to, double val)
47  {
48  fAmountLearned += val-(heur[env->GetStateHash(where)].h1+env->HCost(where, to));
49  heur[env->GetStateHash(where)].h1 = val-env->HCost(where, to);
50  heur[env->GetStateHash(where)].theState = where;
51  }
52  void SetH2Cost(environment *env, const state &where, const state &to, double val)
53  {
54  heur[env->GetStateHash(where)].h2 = val-env->HCost(where, to);
55  heur[env->GetStateHash(where)].theState = where;
56  }
57  void SetLastMove(environment *env, const state &where, const state &to)
58  {
59  heur[env->GetStateHash(where)].lastMove = to;
60  heur[env->GetStateHash(where)].validLastMove = true;
61  }
62  bool GetLastMove(environment *env, const state &where, state &to)
63  {
64  if (heur[env->GetStateHash(where)].validLastMove)
65  to = heur[env->GetStateHash(where)].lastMove;
66 
67  return heur[env->GetStateHash(where)].validLastMove;
68  }
69 
70  double LocalFCost(environment *env, const state &from, const state &neighbor, const state &to)
71  {
72  double myFCost = env->GCost(from,neighbor);
73  state stateLast;
74  if (GetLastMove(env, neighbor, stateLast) && stateLast == from)
75  myFCost += HCost2(env,neighbor,to);
76  else
77  myFCost += HCost1(env,neighbor,to);
78 
79  return myFCost;
80  }
81 
82  double HCost1(environment *env, const state &from, const state &to)
83  {
84  if (heur.find(env->GetStateHash(from)) != heur.end())
85  return heur[env->GetStateHash(from)].h1+env->HCost(from, to);
86  return env->HCost(from, to);
87  }
88 
89  double HCost2(environment *env, const state &from, const state &to)
90  {
91  if (heur.find(env->GetStateHash(from)) != heur.end())
92  return heur[env->GetStateHash(from)].h2+env->HCost(from, to);
93  return env->HCost(from, to);
94  }
95 
96  virtual uint64_t GetNodesExpanded() const { return nodesExpanded; }
97  virtual uint64_t GetNodesTouched() const { return nodesTouched; }
98  virtual void LogFinalStats(StatCollection *s)
99  {
100  s->AddStat("TotalLearning", GetName(),fAmountLearned);
101  }
102 
103  double GetAmountLearned() { return fAmountLearned; }
104  void OpenGLDraw() const {}
105  void OpenGLDraw(const environment *env) const;
106 private:
107  typedef std::unordered_map<uint64_t, learnedData<state>, Hash64 > LearnedHeuristic;
108 
110  state goal;
113 };
114 
116 template <class state, class action, class environment>
117 void HLRTAStar<state, action, environment>::GetPath(environment *env, const state& from, const state& to, std::vector<state> &thePath)
118 {
119  goal = to;
120  thePath.resize(0);
121  if (from==to)
122  return;
123 
124 
125  nodesExpanded = 0;
126  nodesTouched = 0;
127  nodesExpanded++;
128 
129  // Iterate through all edges coming out of *n
130  std::vector<state> neighbors;
131  env->GetSuccessors(from, neighbors);
132 
133  int best = -1;
134  int secondBest = -1;
135 
136  //double bestFCost, secondBestFCost;
137 
138  for (unsigned int x = 0; x < neighbors.size(); x++)
139  {
140  // Generate a child
141  nodesTouched++;
142 
143  if (best == -1){
144  best = x;
145  }
146  else{
147  double myFCost = LocalFCost(env, from, neighbors[x], to);
148  double bestFCost = LocalFCost(env, from, neighbors[best], to);
149 
150  if (fless(myFCost,bestFCost))
151  {
152  secondBest = best;
153  best = x;
154 
155  }
156 
157  else {
158  if (secondBest == -1)
159  {
160  secondBest = x;
161  }
162  else {
163  double secondBestFCost = LocalFCost(env, from, neighbors[secondBest], to);
164  if (fless(myFCost,secondBestFCost))
165  {
166  secondBest = x;
167  }
168  }
169  }
170  }
171  }
172 
173  SetH1Cost(env, from, to, LocalFCost(env, from, neighbors[best], to));
174 
175  if (secondBest == -1)
176  SetH2Cost(env, from, to, 1000);
177  else
178  SetH2Cost(env, from, to, LocalFCost(env, from, neighbors[secondBest], to));
179  SetLastMove(env, from, neighbors[best]);
180 
181 // std::cout<<neighbors[best]<<" "<<from<<std::endl;
182 
183  thePath.push_back(neighbors[best]);
184  thePath.push_back(from);
185  return;
186 }
187 
188 template <class state, class action, class environment>
190 {
191  double learned = 0;
192  for (typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
193  {
194  double thisState = (*it).second.h1;
195  if (learned < thisState)
196  learned = thisState;
197  }
198  for (typename LearnedHeuristic::const_iterator it = heur.begin(); it != heur.end(); it++)
199  {
200  double r = (*it).second.h1;
201  if (r > 0)
202  {
203  e->SetColor(0.5+0.5*r/learned, 0, 0.5, 0.1+0.8*r/learned);
204  e->OpenGLDraw((*it).second.theState);
205  }
206  }
207 }
208 
209 } //HLRTA namespace
210 #endif
HLRTA::HLRTAStar::GetPath
void GetPath(environment *env, const state &from, const state &to, std::vector< state > &thePath)
The core routine of LRTAStar – computes at most one-move path.
Definition: HLRTAStar.h:117
graphMove
Definition: GraphEnvironment.h:34
HLRTA::HLRTAStar::HCost2
double HCost2(environment *env, const state &from, const state &to)
Definition: HLRTAStar.h:89
HLRTA::HLRTAStar::nodesTouched
uint64_t nodesTouched
Definition: HLRTAStar.h:112
HLRTA::HLRTAStar::goal
state goal
Definition: HLRTAStar.h:110
LearningAlgorithm
Definition: LearningAlgorithm.h:15
HLRTA::HLRTAStar
Definition: HLRTAStar.h:37
FPUtil.h
HLRTA::HLRTAStar::GetName
virtual const char * GetName()
Definition: HLRTAStar.h:45
HLRTA
HLRTA*.
Definition: HLRTAStar.h:20
HLRTA::HLRTAStar::LocalFCost
double LocalFCost(environment *env, const state &from, const state &neighbor, const state &to)
Definition: HLRTAStar.h:70
HLRTA::learnedData::theState
state theState
Definition: HLRTAStar.h:28
HLRTA::HLRTAStar::heur
LearnedHeuristic heur
Definition: HLRTAStar.h:109
HLRTA::learnedData
Definition: HLRTAStar.h:23
HLRTA::HLRTAStar::LogFinalStats
virtual void LogFinalStats(StatCollection *s)
Definition: HLRTAStar.h:98
HLRTA::HLRTAStar::GetLastMove
bool GetLastMove(environment *env, const state &where, state &to)
Definition: HLRTAStar.h:62
HLRTA::HLRTAStar::SetLastMove
void SetLastMove(environment *env, const state &where, const state &to)
Definition: HLRTAStar.h:57
Hash64
Definition: SearchEnvironment.h:23
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
HLRTA::HLRTAStar::LearnedHeuristic
std::unordered_map< uint64_t, learnedData< state >, Hash64 > LearnedHeuristic
Definition: HLRTAStar.h:107
HLRTA::HLRTAStar::~HLRTAStar
virtual ~HLRTAStar(void)
Definition: HLRTAStar.h:41
HLRTA::HLRTAStar::nodesExpanded
uint64_t nodesExpanded
Definition: HLRTAStar.h:112
HLRTA::HLRTAStar::GetAmountLearned
double GetAmountLearned()
Definition: HLRTAStar.h:103
HLRTA::HLRTAStar::fAmountLearned
double fAmountLearned
Definition: HLRTAStar.h:111
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
LearningAlgorithm.h
HLRTA::learnedData::h2
double h2
Definition: HLRTAStar.h:32
HLRTA::HLRTAStar::HCost1
double HCost1(environment *env, const state &from, const state &to)
Definition: HLRTAStar.h:82
HLRTA::HLRTAStar::GetNodesTouched
virtual uint64_t GetNodesTouched() const
Definition: HLRTAStar.h:97
StatCollection::AddStat
void AddStat(const char *category, const char *owner, double value)
Add a new stat entry for the given category, owner and value.
Definition: StatCollection.cpp:67
HLRTA::HLRTAStar::GetPath
void GetPath(environment *, const state &, const state &, std::vector< action > &)
Definition: HLRTAStar.h:44
HLRTA::learnedData::learnedData
learnedData()
Definition: HLRTAStar.h:24
HLRTA::HLRTAStar::HLRTAStar
HLRTAStar()
Definition: HLRTAStar.h:39
HLRTA::HLRTAStar::SetH1Cost
void SetH1Cost(environment *env, const state &where, const state &to, double val)
Definition: HLRTAStar.h:46
HLRTA::learnedData::h1
double h1
Definition: HLRTAStar.h:31
HLRTA::HLRTAStar::SetH2Cost
void SetH2Cost(environment *env, const state &where, const state &to, double val)
Definition: HLRTAStar.h:52
HLRTA::HLRTAStar::OpenGLDraw
void OpenGLDraw() const
Definition: HLRTAStar.h:104
HLRTA::learnedData::lastMove
state lastMove
Definition: HLRTAStar.h:29
HLRTA::HLRTAStar::GetNodesExpanded
virtual uint64_t GetNodesExpanded() const
Definition: HLRTAStar.h:96
HLRTA::learnedData::validLastMove
bool validLastMove
Definition: HLRTAStar.h:30