HOG2
DFS.h
Go to the documentation of this file.
1 /*
2  * DFS.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 10/15/10.
6  * Copyright 2010 University of Denver. All rights reserved.
7  *
8  */
9 
10 #ifndef DFS_H
11 #define DFS_H
12 
13 #include <iostream>
14 #include "SearchEnvironment.h"
15 #include <unordered_map>
16 #include "FPUtil.h"
17 
18 template <class state, class action>
19 class DFS {
20 public:
21  DFS() { }
22  virtual ~DFS() {}
23  void GetPath(SearchEnvironment<state, action> *env, state from, state to,
24  std::vector<state> &thePath);
25  void GetPath(SearchEnvironment<state, action> *env, state from, state to,
26  std::vector<action> &thePath);
27 
28  uint64_t GetNodesExpanded() { return nodesExpanded; }
29  uint64_t GetNodesTouched() { return nodesTouched; }
30 private:
31  typedef std::unordered_map<uint64_t, bool, Hash64> DFSClosedList;
32 
34  state parent, state currState,
35  std::vector<state> &thePath, double bound, double g);
37  action forbiddenAction, state &currState,
38  std::vector<action> &thePath, double bound, double g);
39 
40  unsigned long nodesExpanded, nodesTouched;
41  double nextBound;
42  std::deque<state> mOpen;
43  std::deque<int> depth;
44 // DFSClosedList mClosed; // store parent id!
45 };
46 
47 template <class state, class action>
49  state from, state to,
50  std::vector<state> &thePath)
51 {
52  nextBound = DBL_MAX;
53  nodesExpanded = nodesTouched = 0;
54  thePath.resize(0);
55 // goal = to;
56  DoIteration(env, from, from, thePath, nextBound, 0);
57 }
58 
59 template <class state, class action>
61  state from, state to,
62  std::vector<action> &thePath)
63 {
64  nextBound = DBL_MAX;
65  nodesExpanded = nodesTouched = 0;
66  thePath.resize(0);
67 // goal = to;
68  std::vector<action> act;
69  env->GetActions(from, act);
70  DoIteration(env, act[0], from, thePath, nextBound, 0);
71 }
72 
73 template <class state, class action>
75  state parent, state currState,
76  std::vector<state> &thePath, double bound, double g)
77 {
78  nodesExpanded++;
79 
80  std::vector<state> neighbors;
81  env->GetSuccessors(currState, neighbors);
82  nodesTouched += neighbors.size();
83 
84  for (unsigned int x = 0; x < neighbors.size(); x++)
85  {
86  if (neighbors[x] == parent)
87  continue;
88 // if (mClosed.find(env->GetStateHash(neighbors[x])) != mClosed.end()) // check for duplicates on path
89 // continue;
90 
91  thePath.push_back(neighbors[x]);
92  double edgeCost = env->GCost(currState, neighbors[x]);
93 // mClosed[env->GetStateHash(neighbors[x])] = true;
94  DoIteration(env, currState, neighbors[x], thePath, bound, g+edgeCost);
95 // mClosed.erase(mClosed.find(env->GetStateHash(neighbors[x])));
96  // if (env->GoalTest(thePath.back(), goal))
97 // return 0;
98  thePath.pop_back();
99  }
100 }
101 
102 template <class state, class action>
104  action forbiddenAction, state &currState,
105  std::vector<action> &thePath, double bound, double g)
106 {
107  nodesExpanded++;
108 
109 // if (env->GoalTest(currState, goal))
110 // return -1; // found goal
111 
112  std::vector<action> actions;
113  env->GetActions(currState, actions);
114  nodesTouched += actions.size();
115  int depth = thePath.size();
116 
117  for (unsigned int x = 0; x < actions.size(); x++)
118  {
119  if ((depth != 0) && (actions[x] == forbiddenAction))
120  continue;
121 
122  thePath.push_back(actions[x]);
123 
124  double edgeCost = env->GCost(currState, actions[x]);
125  env->ApplyAction(currState, actions[x]);
126  env->InvertAction(actions[x]);
127 
128 // if (mClosed.find(env->GetStateHash(currState)) == mClosed.end()) // check for duplicates on path
129 // {
130 // mClosed[env->GetStateHash(currState)] = true;
131  DoIteration(env, actions[x], currState, thePath, bound, g+edgeCost);
132 // mClosed.erase(mClosed.find(env->GetStateHash(currState)));
133 // }
134 
135  env->ApplyAction(currState, actions[x]);
136 // if (fequal(childH, -1)) // found goal
137 // return -1;
138 
139  thePath.pop_back();
140 
141  }
142 }
143 
144 
145 #endif
DFS::nextBound
double nextBound
Definition: DFS.h:41
DFS::nodesExpanded
unsigned long nodesExpanded
Definition: DFS.h:40
DFS::depth
std::deque< int > depth
Definition: DFS.h:43
SearchEnvironment::InvertAction
virtual bool InvertAction(action &a) const =0
FPUtil.h
SearchEnvironment::GCost
virtual double GCost(const state &node1, const state &node2) const =0
SearchEnvironment::GetActions
virtual void GetActions(const state &nodeID, std::vector< action > &actions) const =0
DFS::GetPath
void GetPath(SearchEnvironment< state, action > *env, state from, state to, std::vector< state > &thePath)
Definition: DFS.h:48
DFS::nodesTouched
unsigned long nodesTouched
Definition: DFS.h:40
DFS::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: DFS.h:28
DFS::GetNodesTouched
uint64_t GetNodesTouched()
Definition: DFS.h:29
DFS::DFS
DFS()
Definition: DFS.h:21
DFS
Definition: DFS.h:19
SearchEnvironment::GetSuccessors
virtual void GetSuccessors(const state &nodeID, std::vector< state > &neighbors) const =0
DFS::DoIteration
void DoIteration(SearchEnvironment< state, action > *env, state parent, state currState, std::vector< state > &thePath, double bound, double g)
Definition: DFS.h:74
DFS::~DFS
virtual ~DFS()
Definition: DFS.h:22
DFS::DFSClosedList
std::unordered_map< uint64_t, bool, Hash64 > DFSClosedList
Definition: DFS.h:31
SearchEnvironment::ApplyAction
virtual void ApplyAction(state &s, action a) const =0
DFS::mOpen
std::deque< state > mOpen
Definition: DFS.h:42
SearchEnvironment
Definition: SearchEnvironment.h:30
SearchEnvironment.h