HOG2
IDAStarCR.h
Go to the documentation of this file.
1 /*
2  * IDAStarCR.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 6/6/19
6  *
7  */
8 
9 #ifndef IDAStarCR_H
10 #define IDAStarCR_H
11 
12 #include <iostream>
13 #include <functional>
14 #include "SearchEnvironment.h"
15 #include <unordered_map>
16 #include "FPUtil.h"
17 #include "vectorCache.h"
18 
19 
20 typedef std::unordered_map<uint64_t, double> NodeHashTable;
21 
22 template <class state, class action, int buckets = 50, bool verbose = true>
23 class IDAStarCR {
24 public:
25  IDAStarCR() { storedHeuristic = false; }
26  virtual ~IDAStarCR() {}
27  void GetPath(SearchEnvironment<state, action> *env, state from, state to,
28  std::vector<action> &thePath);
29 
30  uint64_t GetNodesExpanded() { return nodesExpanded; }
31  uint64_t GetNodesTouched() { return nodesTouched; }
33  void SetHeuristic(Heuristic<state> *heur) { heuristic = heur; if (heur != 0) storedHeuristic = true; else storedHeuristic = false; }
34 private:
35  unsigned long long nodesExpanded, nodesTouched;
36 
37  // void DoIteration(SearchEnvironment<state, action> *env,
38  // state parent, state currState,
39  // std::vector<state> &thePath, double bound, double g);
41  action forbiddenAction, state &currState,
42  std::vector<action> &thePath, double bound, double g);
43  state start, goal;
44  double growth;
45  double solutionCost;
49  std::vector<action> solution;
50  std::array<int, buckets> bucketCount;
51 };
52 
53 template <class state, class action, int buckets, bool verbose>
55  state from, state to,
56  std::vector<action> &thePath)
57 {
58  if (!storedHeuristic)
59  heuristic = env;
60  solutionCost = DBL_MAX;
61  nodesExpanded = nodesTouched = 0;
62  thePath.resize(0);
63  solution.resize(0);
64 
65  if (env->GoalTest(from, to))
66  return;
67 
68  goal = to;
69  start = from;
70  std::vector<action> act;
71  env->GetActions(from, act);
72  uint64_t k = 1;
73 
74  double currBound = heuristic->HCost(from, to);
75  while (solution.size() == 0)
76  {
77  bucketCount.fill(0);
78  uint64_t oldNodes = nodesExpanded;
79  if (verbose)
80  printf("Starting iteration with bound %f; %llu expanded, %llu generated\n", currBound, nodesExpanded, nodesTouched);
81  fflush(stdout);
82  DoIteration(env, act[0], from, thePath, currBound, 0);
83  if (solution.size() != 0)
84  break;
85 
86  uint64_t newNodes = nodesExpanded-oldNodes;
87  uint64_t total = bucketCount[0];
88  double nextBound = currBound*(1.0+1.0/100.0);
89  for (int i = 1; i < buckets; i++)
90  {
91  total+=bucketCount[i];
92  if (bucketCount[i] > 0)
93  nextBound = currBound*(1.0+static_cast<double>(i)/100.0);
94  // This is the exponential bound for the paper. But, it would likely be better to grow based on
95  // the last iteration, not the absolute size of a 2^k tree for iteration k.
96  if (total >= (1<<k))
97  break;
98  }
99  printf(" Ending iteration next bound %f; %llu new exp %llu predicted\n", nextBound, newNodes, total);
100  currBound = nextBound;
101  k++;
102  }
103  thePath = solution;
104 }
105 
106 template <class state, class action, int buckets, bool verbose>
108  action forbiddenAction, state &currState,
109  std::vector<action> &thePath, double bound, double g)
110 {
111  double h = heuristic->HCost(currState, goal);
112  if (fgreater(g+h, bound) || fgreater(g+h, solutionCost))
113  {
114  unsigned int index = ((g+h)/bound-1)*100;
115  if (index < buckets)
116  bucketCount[index]++;
117  return;
118  }
119  // must do this after we check the f-cost bound
120  if (env->GoalTest(currState, goal))
121  {
122  if (fless(env->GetPathLength(start, thePath), solutionCost))
123  {
124  solution = thePath;
125  solutionCost = env->GetPathLength(start, thePath);
126  printf("Improved solution to %1.5f\n", solutionCost);
127  }
128  return;
129  }
130 
131  std::vector<action> &actions = *actCache.getItem();
132  env->GetActions(currState, actions);
133  nodesTouched += actions.size();
134  nodesExpanded++;
135  int depth = (int)thePath.size();
136 
137  for (unsigned int x = 0; x < actions.size(); x++)
138  {
139  if ((depth != 0) && (actions[x] == forbiddenAction))
140  continue;
141 
142  thePath.push_back(actions[x]);
143  double edgeCost = env->GCost(currState, actions[x]);
144  env->ApplyAction(currState, actions[x]);
145  action a = actions[x];
146  env->InvertAction(a);
147  DoIteration(env, a, currState, thePath, bound, g+edgeCost);
148  env->UndoAction(currState, actions[x]);
149  thePath.pop_back();
150  }
151  actCache.returnItem(&actions);
152 }
153 
154 #endif
IDAStarCR::IDAStarCR
IDAStarCR()
Definition: IDAStarCR.h:25
Heuristic
Definition: Heuristic.h:30
SearchEnvironment::InvertAction
virtual bool InvertAction(action &a) const =0
IDAStarCR::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: IDAStarCR.h:30
SearchEnvironment::UndoAction
virtual void UndoAction(state &s, action a) const
Definition: SearchEnvironment.h:40
FPUtil.h
vectorCache.h
SearchEnvironment::GCost
virtual double GCost(const state &node1, const state &node2) const =0
IDAStarCR::~IDAStarCR
virtual ~IDAStarCR()
Definition: IDAStarCR.h:26
IDAStarCR::DoIteration
void DoIteration(SearchEnvironment< state, action > *env, action forbiddenAction, state &currState, std::vector< action > &thePath, double bound, double g)
Definition: IDAStarCR.h:107
IDAStarCR::ResetNodeCount
void ResetNodeCount()
Definition: IDAStarCR.h:32
IDAStarCR::solution
std::vector< action > solution
Definition: IDAStarCR.h:49
IDAStarCR::nodesExpanded
unsigned long long nodesExpanded
Definition: IDAStarCR.h:35
IDAStarCR::GetNodesTouched
uint64_t GetNodesTouched()
Definition: IDAStarCR.h:31
SearchEnvironment::GetActions
virtual void GetActions(const state &nodeID, std::vector< action > &actions) const =0
IDAStarCR::GetPath
void GetPath(SearchEnvironment< state, action > *env, state from, state to, std::vector< action > &thePath)
Definition: IDAStarCR.h:54
IDAStarCR
Definition: IDAStarCR.h:23
IDAStarCR::bucketCount
std::array< int, buckets > bucketCount
Definition: IDAStarCR.h:50
SearchEnvironment::GetPathLength
virtual double GetPathLength(std::vector< state > &neighbors)
Definition: SearchEnvironment.h:144
verbose
const static int verbose
Definition: ClusterAbstraction.cpp:81
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
IDAStarCR::actCache
vectorCache< action > actCache
Definition: IDAStarCR.h:47
IDAStarCR::goal
state goal
Definition: IDAStarCR.h:43
IDAStarCR::SetHeuristic
void SetHeuristic(Heuristic< state > *heur)
Definition: IDAStarCR.h:33
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
NodeHashTable
std::unordered_map< uint64_t, double > NodeHashTable
Definition: IDAStarCR.h:20
vectorCache< action >
IDAStarCR::heuristic
Heuristic< state > * heuristic
Definition: IDAStarCR.h:48
IDAStarCR::growth
double growth
Definition: IDAStarCR.h:44
SearchEnvironment::ApplyAction
virtual void ApplyAction(state &s, action a) const =0
IDAStarCR::solutionCost
double solutionCost
Definition: IDAStarCR.h:45
IDAStarCR::nodesTouched
unsigned long long nodesTouched
Definition: IDAStarCR.h:35
SearchEnvironment::GoalTest
virtual bool GoalTest(const state &node, const state &goal) const =0
SearchEnvironment
Definition: SearchEnvironment.h:30
IDAStarCR::storedHeuristic
bool storedHeuristic
Definition: IDAStarCR.h:46
SearchEnvironment.h
IDAStarCR::start
state start
Definition: IDAStarCR.h:43