HOG2
IncrementalIDA.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 #include "Heuristic.h"
13 
14 #include <algorithm>
15 
16 template <class state, class action>
18 public:
20  { ResetNodeCount(); previousBound = 0; }
22  std::vector<state> &thePath);
23  void GetPath(SearchEnvironment<state, action> *env, state from, state to, Heuristic<state> *h,
24  std::vector<state> &thePath);
25  void GetPath(SearchEnvironment<state, action> *env, state from, state to,
26  std::vector<action> &thePath);
27  bool DoSingleSearchStep(std::vector<state> &thePath);
28  uint64_t GetNodesExpanded() { return nodesExpanded; }
29  uint64_t GetNodesTouched() { return nodesTouched; }
31  {
34  }
36  history.clear(); search.clear(); ResetNodeCount(); previousBound = 0; }
37  void OpenGLDraw();
38  void Draw(Graphics::Display &display) const;
39  state GetCurrentState() const { if (search.size() > 0) return search.back().currState; return start; }
40  void GetCurrentPath(std::vector<state> &p) const
41  { p.clear(); for (auto &i : search) p.push_back(i.currState); }
42  double GetCurrentFLimit() { return bound; }
43  double GetNextFLimit() { return nextBound; }
45 private:
49  };
50  struct currSearchState {
51  state currState;
53  double pathCost;
54  std::vector<state> succ;
55  };
56  std::vector<currSearchState> search;
57  bool IterationComplete() { return search.size() == 0; }
58  unsigned long nodesExpanded, nodesTouched;
59 
60  void SetupIteration(double cost);
61  bool StepIteration();
62 
63  std::vector<std::pair<state, double>> history;
64  std::vector<state> path;
65  state start, goal;
66  double previousBound;
67  double bound;
68  double initialBound;
69  double nextBound;
72  std::vector<state> succ;
74 };
75 
76 template <class state, class action>
78  Heuristic<state> *h, std::vector<state> &thePath)
79 {
80  if (InitializeSearch(e, from, to, h, thePath))
81  return;
82  while (!DoSingleSearchStep(thePath))
83  {}
84 }
85 
86 template <class state, class action>
88  std::vector<action> &thePath)
89 {
90 
91 }
92 
93 template <class state, class action>
95  std::vector<state> &thePath)
96 {
97  Reset();
98  if (from==to)
99  {
100  thePath.clear();
101  thePath.push_back(from);
102  return true;
103  }
104  start = from;
105  goal = to;
106  this->env = e;
107  start = from;
108  this->h = h;
109  SetupIteration(h->HCost(start, goal));
110  return false;
111 }
112 
113 template <class state, class action>
115 {
116  search.resize(1);
117  search.back().currState = start;
118  search.back().status = kGoingDown;
119  search.back().pathCost = 0;
120  previousBound = bound;
121  bound = cost;//nextBound;
122  nextBound = -1;
123  path.clear();
124 // printf("Starting iteration bound %1.1f\n", bound);
125  newNodesLastIteration = newNodeCount;
126  newNodeCount = 0;
127 }
128 
129 template <class state, class action>
131 {
132  if (env->GoalTest(search.back().currState, goal) && flesseq(search.back().pathCost, bound))
133  {
134  printf("Done!");
135  path.resize(0);
136  for (size_t x = 0; x < search.size(); x++)
137  path.push_back(search[x].currState);
138  return true;
139  }
140 
141  if (search.back().status == kGoingDown)
142  {
143  double f = search.back().pathCost+h->HCost(search.back().currState, goal);
144  // exceeded path cost bound
145  if (fgreater(f, bound))
146  {
147 // printf("Above bound: %f/%f\n", bound, f);
148  if (nextBound == -1)
149  nextBound = f;
150  else if (fless(f, nextBound))
151  nextBound = f;
152 
153  search.pop_back();
154  return false;
155  }
156 
157  if (fgreater(f, previousBound) && flesseq(f, bound))
158  newNodeCount++;
159 
160  // continue search
161 // printf("Generating next set of successors\n");
162  search.back().status = kGoingAcross;
163  env->GetSuccessors(search.back().currState, search.back().succ);
164  nodesExpanded++;
165  for (size_t x = 0; x < search.back().succ.size(); x++)
166  {
167  if (search.size() > 1 && search.back().succ[x] == search[search.size()-2].currState)
168  {
169  search.back().succ.erase(search.back().succ.begin()+x);
170  x--;
171  }
172  }
173 
174  // reverse and then handle them from back to front
175  std::reverse(search.back().succ.begin(), search.back().succ.end());
176  //return false;
177  }
178 
179  if (search.back().status == kGoingAcross)
180  {
181  // no more succ to go down - go up
182  if (search.back().succ.size() == 0)
183  {
184 // printf("Out of successors\n");
185  search.pop_back();
186  return false;
187  }
188 
189 // printf("Taking next successors\n");
190  // going down - generate next successor
191  search.resize(search.size()+1);
192  auto &s = search[search.size()-2];
193  search.back().currState = s.succ.back();
194  search.back().status = kGoingDown;
195  search.back().pathCost = s.pathCost+env->GCost(s.currState, s.succ.back());
196  s.succ.pop_back();
197  return false;
198  }
199  assert(false);
200  return false;
201 
202 }
203 
204 
205 template <class state, class action>
206 bool IncrementalIDA<state, action>::DoSingleSearchStep(std::vector<state> &thePath)
207 {
208  // starting new iteration
209  if (IterationComplete())
210  {
211  SetupIteration(nextBound);
212  // pause between iterations
213  return false;
214  }
215 
216  return StepIteration();
217 }
218 
219 template <class state, class action>
221 {
222  for (int x = 1; x < search.size(); x++)
223  {
224  env->DrawLine(display, search[x-1].currState, search[x].currState, 10);
225  }
226 }
227 
228 template <class state, class action>
230 {
231 // for (auto x : history)
232 // env->OpenGLDraw(x.first);
233  for (int x = 1; x < search.size(); x++)
234  env->GLDrawLine(search[x-1].currState, search[x].currState, 10);
235 // for (int x = 1; x < path.size(); x++)
236 // env->GLDrawLine(path[x-1], path[x]);
237 }
238 
239 
240 #endif /* IncrementalDFID_h */
IncrementalIDA::kGoingAcross
@ kGoingAcross
Definition: IncrementalIDA.h:48
IncrementalIDA::currSearchState::status
kSearchStatus status
Definition: IncrementalIDA.h:52
IncrementalIDA::newNodeCount
uint64_t newNodeCount
Definition: IncrementalIDA.h:73
IncrementalIDA::currSearchState::succ
std::vector< state > succ
Definition: IncrementalIDA.h:54
IncrementalIDA::SetupIteration
void SetupIteration(double cost)
Definition: IncrementalIDA.h:114
IncrementalIDA::IncrementalIDA
IncrementalIDA(double initialBound=0)
Definition: IncrementalIDA.h:19
IncrementalIDA::GetPath
void GetPath(SearchEnvironment< state, action > *env, state from, state to, Heuristic< state > *h, std::vector< state > &thePath)
Definition: IncrementalIDA.h:77
IncrementalIDA::Reset
void Reset()
Definition: IncrementalIDA.h:35
Heuristic
Definition: Heuristic.h:30
Heuristic.h
IncrementalIDA::StepIteration
bool StepIteration()
Definition: IncrementalIDA.h:130
IncrementalIDA::InitializeSearch
bool InitializeSearch(SearchEnvironment< state, action > *env, state from, state to, Heuristic< state > *h, std::vector< state > &thePath)
Definition: IncrementalIDA.h:94
IncrementalIDA::newNodesLastIteration
uint64_t newNodesLastIteration
Definition: IncrementalIDA.h:73
IncrementalIDA::initialBound
double initialBound
Definition: IncrementalIDA.h:68
IncrementalIDA::previousBound
double previousBound
Definition: IncrementalIDA.h:66
IncrementalIDA::GetNextFLimit
double GetNextFLimit()
Definition: IncrementalIDA.h:43
IncrementalIDA::nextBound
double nextBound
Definition: IncrementalIDA.h:69
IncrementalIDA::Draw
void Draw(Graphics::Display &display) const
Definition: IncrementalIDA.h:220
IncrementalIDA
Definition: IncrementalIDA.h:17
IncrementalIDA::goal
state goal
Definition: IncrementalIDA.h:65
IncrementalIDA::currSearchState::currState
state currState
Definition: IncrementalIDA.h:51
Graphics::Display
Definition: Graphics.h:146
IncrementalIDA::GetNodesTouched
uint64_t GetNodesTouched()
Definition: IncrementalIDA.h:29
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
IncrementalIDA::kGoingDown
@ kGoingDown
Definition: IncrementalIDA.h:47
Heuristic::HCost
virtual double HCost(const state &a, const state &b) const
Definition: Heuristic.h:73
IncrementalIDA::GetCurrentPath
void GetCurrentPath(std::vector< state > &p) const
Definition: IncrementalIDA.h:40
IncrementalIDA::nodesExpanded
unsigned long nodesExpanded
Definition: IncrementalIDA.h:58
IncrementalIDA::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: IncrementalIDA.h:28
IncrementalIDA::path
std::vector< state > path
Definition: IncrementalIDA.h:64
IncrementalIDA::GetCurrentFLimit
double GetCurrentFLimit()
Definition: IncrementalIDA.h:42
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
IncrementalIDA::currSearchState::pathCost
double pathCost
Definition: IncrementalIDA.h:53
IncrementalIDA::kSearchStatus
kSearchStatus
Definition: IncrementalIDA.h:46
IncrementalIDA::h
Heuristic< state > * h
Definition: IncrementalIDA.h:71
IncrementalIDA::GetNewNodesLastIteration
uint64_t GetNewNodesLastIteration()
Definition: IncrementalIDA.h:44
IncrementalIDA::IterationComplete
bool IterationComplete()
Definition: IncrementalIDA.h:57
IncrementalIDA::ResetNodeCount
void ResetNodeCount()
Definition: IncrementalIDA.h:30
IncrementalIDA::OpenGLDraw
void OpenGLDraw()
Definition: IncrementalIDA.h:229
IncrementalIDA::currSearchState
Definition: IncrementalIDA.h:50
IncrementalIDA::GetCurrentState
state GetCurrentState() const
Definition: IncrementalIDA.h:39
IncrementalIDA::DoSingleSearchStep
bool DoSingleSearchStep(std::vector< state > &thePath)
Definition: IncrementalIDA.h:206
IncrementalIDA::bound
double bound
Definition: IncrementalIDA.h:67
IncrementalIDA::nodesTouched
unsigned long nodesTouched
Definition: IncrementalIDA.h:58
IncrementalIDA::history
std::vector< std::pair< state, double > > history
Definition: IncrementalIDA.h:63
IncrementalIDA::env
SearchEnvironment< state, action > * env
Definition: IncrementalIDA.h:70
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
IncrementalIDA::succ
std::vector< state > succ
Definition: IncrementalIDA.h:72
IncrementalIDA::search
std::vector< currSearchState > search
Definition: IncrementalIDA.h:56
IncrementalIDA::start
state start
Definition: IncrementalIDA.h:65
SearchEnvironment
Definition: SearchEnvironment.h:30
flesseq
bool flesseq(double a, double b)
Definition: FPUtil.h:30