HOG2
IncrementalDFID.h
Go to the documentation of this file.
1 //
2 // IncrementalDFID.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 3/24/16.
6 // Copyright © 2016 University of Denver. All rights reserved.
7 //
8 
9 #ifndef IncrementalDFID_h
10 #define IncrementalDFID_h
11 
12 template <class state, class action>
14 public:
16  void GetPath(SearchEnvironment<state, action> *env, state from, state to,
17  std::vector<state> &thePath);
18  void GetPath(SearchEnvironment<state, action> *env, state from, state to,
19  std::vector<action> &thePath);
20  bool DoSingleSearchStep(SearchEnvironment<state, action> *env, state from, state to,
21  std::vector<state> &thePath);
22  uint64_t GetNodesExpanded() { return nodesExpanded; }
23  uint64_t GetNodesTouched() { return nodesTouched; }
25  void Reset() { bound = initialBound; path.clear(); history.clear(); }
26  void OpenGLDraw();
27  void Draw(Graphics::Display &display) const;
28  state GetCurrentState() const { return path.back(); }
29 private:
30  unsigned long nodesExpanded, nodesTouched;
31 
33  state parent, state currState,
34  std::vector<state> &thePath, double bound, double g);
36  action forbiddenAction, state &currState,
37  std::vector<action> &thePath, double bound, double g);
38 
39  std::vector<std::pair<state, int>> history;
40  std::vector<state> path;
41  state goal;
42  int bound;
44  //double nextBound;
46  std::vector<state> succ;
47 };
48 
49 template <class state, class action>
51  std::vector<state> &thePath)
52 {
53  while (!DoSingleSearchStep(env, from, to, thePath))
54  {}
55 }
56 
57 template <class state, class action>
59  std::vector<action> &thePath)
60 {
61 
62 }
63 
64 template <class state, class action>
66  std::vector<state> &thePath)
67 {
68  if (history.size() == 0)
69  {
70  history.push_back({from, 0});
71  bound++;
72  }
73 
74  this->env = env;
75  int depth = history.back().second;
76  env->GetSuccessors(history.back().first, succ);
77  //printf("Working at depth %d, bound %d\n", depth, bound);
78  // backtracking step
79  if (path.size() > depth)
80  {
81  path.pop_back();
82 
83  // terminating on next step - for visualization only! (avoids flicker)
84  if (path.size() <= depth && env->GoalTest(history.back().first, to))
85  path.push_back(history.back().first);
86  return false;
87  }
88  path.push_back(history.back().first);
89 
90  // Later than normal - for the purposes of drawing nicely
91  if (env->GoalTest(history.back().first, to))
92  {
93  //printf("Done!");
94  return true;
95  }
96 
97  history.pop_back();
98  if (depth < bound)
99  {
100  for (int x = succ.size()-1; x >= 0; x--)
101  {
102  if (path.size() < 2 || path[path.size()-2] != succ[x])
103  history.push_back({succ[x], depth+1});
104  }
105  }
106  return false;
107 }
108 
109 template <class state, class action>
111 {
112 // for (auto x : history)
113 // env->OpenGLDraw(x.first);
114  for (int x = 1; x < path.size(); x++)
115  env->GLDrawLine(path[x-1], path[x]);
116 }
117 
118 template <class state, class action>
120 {
121  for (int x = 1; x < path.size(); x++)
122  env->DrawLine(display, path[x-1], path[x], 2.0);
123 }
124 
125 
126 
127 #endif /* IncrementalDFID_h */
IncrementalDFID::ResetNodeCount
void ResetNodeCount()
Definition: IncrementalDFID.h:24
IncrementalDFID::env
SearchEnvironment< state, action > * env
Definition: IncrementalDFID.h:45
IncrementalDFID::succ
std::vector< state > succ
Definition: IncrementalDFID.h:46
IncrementalDFID::nodesExpanded
unsigned long nodesExpanded
Definition: IncrementalDFID.h:30
IncrementalDFID::Reset
void Reset()
Definition: IncrementalDFID.h:25
IncrementalDFID::nodesTouched
unsigned long nodesTouched
Definition: IncrementalDFID.h:30
IncrementalDFID::goal
state goal
Definition: IncrementalDFID.h:41
IncrementalDFID::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: IncrementalDFID.h:22
IncrementalDFID::GetPath
void GetPath(SearchEnvironment< state, action > *env, state from, state to, std::vector< state > &thePath)
Definition: IncrementalDFID.h:50
Graphics::Display
Definition: Graphics.h:146
IncrementalDFID::OpenGLDraw
void OpenGLDraw()
Definition: IncrementalDFID.h:110
IncrementalDFID
Definition: IncrementalDFID.h:13
IncrementalDFID::DoSingleSearchStep
bool DoSingleSearchStep(SearchEnvironment< state, action > *env, state from, state to, std::vector< state > &thePath)
Definition: IncrementalDFID.h:65
IncrementalDFID::path
std::vector< state > path
Definition: IncrementalDFID.h:40
IncrementalDFID::GetNodesTouched
uint64_t GetNodesTouched()
Definition: IncrementalDFID.h:23
SearchEnvironment::GetSuccessors
virtual void GetSuccessors(const state &nodeID, std::vector< state > &neighbors) const =0
IncrementalDFID::bound
int bound
Definition: IncrementalDFID.h:42
IncrementalDFID::DoIteration
bool DoIteration(SearchEnvironment< state, action > *env, state parent, state currState, std::vector< state > &thePath, double bound, double g)
IncrementalDFID::initialBound
int initialBound
Definition: IncrementalDFID.h:43
IncrementalDFID::GetCurrentState
state GetCurrentState() const
Definition: IncrementalDFID.h:28
IncrementalDFID::history
std::vector< std::pair< state, int > > history
Definition: IncrementalDFID.h:39
IncrementalDFID::Draw
void Draw(Graphics::Display &display) const
Definition: IncrementalDFID.h:119
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
SearchEnvironment::GoalTest
virtual bool GoalTest(const state &node, const state &goal) const =0
IncrementalDFID::IncrementalDFID
IncrementalDFID(int initialBound=0)
Definition: IncrementalDFID.h:15
SearchEnvironment
Definition: SearchEnvironment.h:30