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