HOG2
LearningUnit.h
Go to the documentation of this file.
1 //
2 // LearningUnit.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 2/23/12.
6 // Copyright (c) 2012 University of Denver. All rights reserved.
7 //
8 
9 #ifndef hog2_glut_LearningUnit_h
10 #define hog2_glut_LearningUnit_h
11 
12 #include "Unit.h"
13 #include <deque>
14 #include "FPUtil.h"
15 #include "LearningAlgorithm.h"
16 
17 template <class state, class action, class environment>
18 class LearningUnit : public Unit<state, action, environment> {
19 
20 public:
22  {
23  currentLoc = s;
24  goalLoc = target;
25  //goal = target;
26  algorithm = alg;
27  totalLearned = 0;
28  StartNewTrial(0);
29  }
30  virtual ~LearningUnit() { delete algorithm; }
31  virtual const char *GetName() { return algorithm->GetName(); }
32  bool Done() { return (currentLoc==goalLoc) && fequal(algorithm->GetAmountLearned(), totalLearned); }
34  if (s)
35  {
36  s->AddStat("nodesExpanded", GetName(), nodesExpanded);
37  s->AddStat("nodesTouched", GetName(), nodesTouched);
38  }
39  nodesExpanded = 0;
40  nodesTouched = 0;
41  // printf("New trial: %f learned this trial; %f total\n",
42  // algorithm->GetAmountLearned()-totalLearned, algorithm->GetAmountLearned());
43  totalLearned = algorithm->GetAmountLearned();
44  }
45 
46  virtual bool MakeMove(environment *, OccupancyInterface<state,action> *, SimulationInfo<state,action,environment> *, action& a);
47  // virtual void printRoundStats(FILE *f) { fprintf(f,"%8.2f",amountLearned); }
48 
49  virtual void OpenGLDraw(const environment *e, const SimulationInfo<state,action,environment> *) const;
50  virtual void UpdateLocation(environment *, state &s, bool success, SimulationInfo<state,action,environment> *)
51  {
52  if (success)
53  {
54  //std::cout << "Updating location from " << currentLoc << " to " << s << std::endl;
55  currentLoc = s;
56  }
57  else {
58  //std::cout << "Move failed, not updating location" << std::endl;
59  }
60  }
61  void GetGoal(state &s) { s = goalLoc; }
62  void GetLocation(state &s) { s = currentLoc; }
63  virtual void LogFinalStats(StatCollection *s)
64  {
65  s->AddStat("nodesExpanded", GetName(), nodesExpanded);
66  s->AddStat("nodesTouched", GetName(), nodesTouched);
67  algorithm->LogFinalStats(s);
68  }
69 protected:
71  LearningAlgorithm<state, action, environment> *algorithm; // pointer to the LRTA* algorithm
72  double totalLearned; // Actual amount learned
73  std::vector<state> path;
75  //Unit<state, action, environment> *goal;
76 };
77 
78 template <class state, class action, class environment>
80 {
81  if (currentLoc == goalLoc)
82  return false;
83  // if (GetUnitGroup() == 0)
84  // return false;
85  if (path.size() <= 1)
86  {
87  algorithm->GetPath(e, currentLoc, goalLoc, path);
88  nodesExpanded += algorithm->GetNodesExpanded();
89  nodesTouched += algorithm->GetNodesTouched();
90  if (path.size() <= 1)
91  return false;
92  //std::reverse(path.begin(), path.end());
93  }
94  a = e->GetAction(path[path.size()-1], path[path.size()-2]);
95  path.pop_back();
96  return true;
97 }
98 
99 template <class state, class action, class environment>
101 {
103  si->GetPublicUnitInfo(si->GetCurrentUnit(), i);
104  e->SetColor(0.5, 0.25, 0.0, 1.0);
105  if (fgreater(si->GetSimulationTime(), i.nextTime))
106  e->OpenGLDraw(i.currentState);
107  else
108  e->OpenGLDraw(i.lastState, i.currentState,
109  (si->GetSimulationTime()-i.lastTime)/(i.nextTime-i.lastTime));
110  //e->OpenGLDraw(currentLoc);
111 
112  e->SetColor(0.0, 1.0, 0.0, 1.0);
113  e->OpenGLDraw(goalLoc);
114  e->SetColor(1.0, 1.0, 0.0, 1.0);
115  e->OpenGLDraw(currentLoc);
116 
117  algorithm->OpenGLDraw(e);
118 
119 
120  // e->SetColor(0.0, 0.0, 0.5, 0.25);
121  // for (typename LSStateStorage::const_iterator it = hashTable.begin(); it != hashTable.end(); it++)
122  // {
123  // if ((*it).second.theState == currentLoc)
124  // {
125  // }
126  // else {
127  // e->OpenGLDraw((*it).second.theState);
128  // }
129  // }
130 }
131 
132 #endif
LearningUnit::path
std::vector< state > path
Definition: LearningUnit.h:73
LearningUnit::MakeMove
virtual bool MakeMove(environment *, OccupancyInterface< state, action > *, SimulationInfo< state, action, environment > *, action &a)
Definition: LearningUnit.h:79
SimulationInfo
Definition: SimulationInfo.h:13
LearningUnit::GetName
virtual const char * GetName()
Definition: LearningUnit.h:31
SimulationInfo::GetSimulationTime
virtual double GetSimulationTime() const =0
LearningUnit::LogFinalStats
virtual void LogFinalStats(StatCollection *s)
log any final one-time stats before a simulation is ended
Definition: LearningUnit.h:63
LearningAlgorithm
Definition: LearningAlgorithm.h:15
LearningUnit
Definition: LearningUnit.h:18
PublicUnitInfo::nextTime
double nextTime
Definition: SimulationInfo.h:66
PublicUnitInfo::currentState
state currentState
Definition: SimulationInfo.h:61
FPUtil.h
LearningUnit::currentLoc
state currentLoc
Definition: LearningUnit.h:74
LearningUnit::GetGoal
void GetGoal(state &s)
Definition: LearningUnit.h:61
LearningUnit::totalLearned
double totalLearned
Definition: LearningUnit.h:72
LearningUnit::~LearningUnit
virtual ~LearningUnit()
Definition: LearningUnit.h:30
LearningUnit::goalLoc
state goalLoc
Definition: LearningUnit.h:74
LearningUnit::nodesTouched
long nodesTouched
Definition: LearningUnit.h:70
PublicUnitInfo::lastState
state lastState
Definition: SimulationInfo.h:62
LearningUnit::nodesExpanded
long nodesExpanded
Definition: LearningUnit.h:70
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
SimulationInfo::GetPublicUnitInfo
virtual void GetPublicUnitInfo(unsigned int unitnum, PublicUnitInfo< state, action, environment > &) const =0
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
LearningUnit::algorithm
LearningAlgorithm< state, action, environment > * algorithm
Definition: LearningUnit.h:71
LearningAlgorithm.h
LearningUnit::Done
bool Done()
Definition: LearningUnit.h:32
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
LearningUnit::GetLocation
void GetLocation(state &s)
Definition: LearningUnit.h:62
PublicUnitInfo::lastTime
double lastTime
Definition: SimulationInfo.h:65
SimulationInfo::GetCurrentUnit
virtual unsigned int GetCurrentUnit() const =0
LearningUnit::LearningUnit
LearningUnit(state &s, state &target, LearningAlgorithm< state, action, environment > *alg)
Definition: LearningUnit.h:21
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
PublicUnitInfo
Definition: SimulationInfo.h:9
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
LearningUnit::OpenGLDraw
virtual void OpenGLDraw(const environment *e, const SimulationInfo< state, action, environment > *) const
Definition: LearningUnit.h:100
LearningUnit::StartNewTrial
void StartNewTrial(StatCollection *s)
Definition: LearningUnit.h:33
LearningUnit::UpdateLocation
virtual void UpdateLocation(environment *, state &s, bool success, SimulationInfo< state, action, environment > *)
Definition: LearningUnit.h:50
Unit.h
Unit
Definition: Unit.h:23
OccupancyInterface
Definition: OccupancyInterface.h:36