HOG2
GenericIDAStar.cpp
Go to the documentation of this file.
1 /*
2  * GenericIDAStar.cpp
3  * hog
4  *
5  * Created by Nathan Sturtevant on 1/3/07.
6  * Copyright 2007 __MyCompanyName__. All rights reserved.
7  *
8  */
9 
10 #include "GenericIDAStar.h"
11 #include <math.h>
12 #include "FPUtil.h"
13 
14 using namespace OldSearchCode;
15 
16 
17 void GenericIDAStar::GetPath(SearchEnvironment *env, uint32_t from, uint32_t to,
18  std::vector<uint32_t> &thePath)
19 {
20  nextBound = 0;
21  nodesExpanded = nodesTouched = 0;
22  thePath.resize(0);
23  //updateNextBound(0, env->heuristic(from, to));
24  UpdateNextBound(0, floor(env->heuristic(from, to)));
25  while (thePath.size() == 0)
26  {
27  nodeTable.clear();
28  printf("Starting iteration with bound %f\n", nextBound);
29  DoIteration(env, from, from, to, thePath, nextBound, 0, 0);
30  }
31 }
32 
34  uint32_t parent, uint32_t currState, uint32_t goal,
35  std::vector<uint32_t> &thePath, double bound, double g,
36  double maxH)
37 {
38  nodesExpanded++;
39  double h = floor(env->heuristic(currState, goal));
40  //double h = env->heuristic(currState, goal);
41 
42  // path max
43  if (usePathMax && fless(h, maxH))
44  h = maxH;
45  if (fgreater(g+h, bound))
46  {
47  UpdateNextBound(bound, g+h);
48  //printf("Stopping at (%d, %d). g=%f h=%f\n", currState>>16, currState&0xFFFF, g, h);
49  return h;
50  }
51  if (nodeTable.find(currState) != nodeTable.end()) // already seen
52  {
53  if (fless(g/*+h*/, nodeTable[currState])) // with lower g-cost
54  {
55  }
56  else {
57  return h;
58  }
59  }
60  nodeTable[currState] = g;//+h;
61 
62  std::vector<uint32_t> neighbors;
63  env->getNeighbors(currState, neighbors);
64  nodesTouched += neighbors.size();
65 
66  for (unsigned int x = 0; x < neighbors.size(); x++)
67  {
68  if (neighbors[x] == parent)
69  continue;
70  thePath.push_back(neighbors[x]);
71  double edgeCost = env->gcost(currState, neighbors[x]);
72  double childH = DoIteration(env, currState, neighbors[x], goal, thePath, bound,
73  g+edgeCost, maxH - edgeCost);
74  if (thePath.back() == goal)
75  return 0;
76  thePath.pop_back();
77  // pathmax
78  if (usePathMax && fgreater(childH-edgeCost, h))
79  {
80  nodeTable[currState] = g;//+h
81  h = childH-edgeCost;
82  if (fgreater(g+h, bound))
83  {
84  UpdateNextBound(bound, g+h);
85  return h;
86  }
87  }
88  }
89  return h;
90 }
91 
92 void GenericIDAStar::UpdateNextBound(double currBound, double fCost)
93 {
94  fCost = floor(fCost);
95  if (!fgreater(nextBound, currBound))
96  nextBound = fCost;
97  else if (fgreater(fCost, currBound) && fless(fCost, nextBound))
98  nextBound = fCost;
99 }
OldSearchCode::SearchEnvironment::heuristic
virtual double heuristic(uint32_t node1, uint32_t node2)=0
GenericIDAStar::DoIteration
double DoIteration(OldSearchCode::SearchEnvironment *env, uint32_t parent, uint32_t currState, uint32_t goal, std::vector< uint32_t > &thePath, double bound, double g, double maxH)
Definition: GenericIDAStar.cpp:33
OldSearchCode
Definition: OldSearchEnvironment.cpp:12
FPUtil.h
OldSearchCode::SearchEnvironment::getNeighbors
virtual void getNeighbors(uint32_t nodeID, std::vector< uint32_t > &neighbors)=0
OldSearchCode::SearchEnvironment
Definition: OldSearchEnvironment.h:18
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
GenericIDAStar::UpdateNextBound
void UpdateNextBound(double currBound, double fCost)
Definition: GenericIDAStar.cpp:92
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
OldSearchCode::SearchEnvironment::gcost
virtual double gcost(uint32_t node1, uint32_t node2)=0
GenericIDAStar::GetPath
void GetPath(OldSearchCode::SearchEnvironment *env, uint32_t from, uint32_t to, std::vector< uint32_t > &thePath)
Definition: GenericIDAStar.cpp:17
GenericIDAStar.h