HOG2
DFID.h
Go to the documentation of this file.
1 /*
2  * DFID.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 5/22/07.
6  * Copyright 2007 Nathan Sturtevant, University of Alberta. All rights reserved.
7  *
8  */
9 
10 #ifndef DFID_H
11 #define DFID_H
12 
13 #include <iostream>
14 #include "SearchEnvironment.h"
15 #include <unordered_map>
16 #include "FPUtil.h"
17 
18 typedef std::unordered_map<uint64_t, double> NodeHashTable;
19 
20 template <class state, class action>
21 class DFID {
22 public:
23  DFID() { useHashTable = usePathMax = false; }
24  virtual ~DFID() {}
25  void GetPath(SearchEnvironment<state, action> *env, state from, state to,
26  std::vector<state> &thePath);
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 SetUseBDPathMax(bool val) { usePathMax = val; }
34 private:
35  unsigned long nodesExpanded, nodesTouched;
36 
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 
44  void UpdateNextBound(double currBound, double gCost);
45  state goal;
46  double nextBound;
48  bool usePathMax;
50 };
51 
52 template <class state, class action>
54  state from, state to,
55  std::vector<state> &thePath)
56 {
57  nextBound = 0;
58  nodesExpanded = nodesTouched = 0;
59  thePath.resize(0);
60  UpdateNextBound(0, 0);
61  goal = to;
62  double lastBound;
63  while (thePath.size() == 0)
64  {
65  nodeTable.clear();
66 // printf("Starting iteration with bound %f\n", nextBound);
67  lastBound = nextBound;
68  DoIteration(env, from, from, thePath, nextBound, 0);
69  std::cout << nodesExpanded << " after next iteration with cost limit " << nextBound << std::endl;
70  if (fequal(lastBound, nextBound))
71  break;
72  }
73 }
74 
75 template <class state, class action>
77  state from, state to,
78  std::vector<action> &thePath)
79 {
80  nextBound = 0;
81  nodesExpanded = nodesTouched = 0;
82  thePath.resize(0);
83  UpdateNextBound(0, 0);
84  goal = to;
85  std::vector<action> act;
86  env->GetActions(from, act);
87  double lastBound;
88 
89  while (thePath.size() == 0)
90  {
91  nodeTable.clear();
92 // printf("Starting iteration with bound %f\n", nextBound);
93  lastBound = nextBound;
94  DoIteration(env, act[0], from, thePath, nextBound, 0);
95  std::cout << nodesExpanded << " after iteration with cost limit " << nextBound << std::endl;
96  if (fequal(lastBound, nextBound))
97  break;
98  }
99 }
100 
101 template <class state, class action>
103  state parent, state currState,
104  std::vector<state> &thePath, double bound, double g)
105 {
106  if (fgreater(g, bound))
107  {
108  UpdateNextBound(bound, g);
109  //printf("Stopping at (%d, %d). g=%f h=%f\n", currState>>16, currState&0xFFFF, g, h);
110  return false;
111  }
112 // if (env->GoalTest(currState, goal))
113 // return true;
114 
115  nodesExpanded++;
116 
117  std::vector<state> neighbors;
118  env->GetSuccessors(currState, neighbors);
119  nodesTouched += neighbors.size();
120 
121  //std::cout << "Expanding " << currState << std::endl;
122 
123  for (unsigned int x = 0; x < neighbors.size(); x++)
124  {
125  if (neighbors[x] == parent)
126  continue;
127  thePath.push_back(neighbors[x]);
128  double edgeCost = env->GCost(currState, neighbors[x]);
129  if (DoIteration(env, currState, neighbors[x], thePath, bound, g+edgeCost))
130  return true;
131  thePath.pop_back();
132  }
133  return false;
134 }
135 
136 template <class state, class action>
138  action forbiddenAction, state &currState,
139  std::vector<action> &thePath, double bound, double g)
140 {
141  if (fgreater(g, bound))
142  {
143  UpdateNextBound(bound, g);
144  //printf("Stopping at (%d, %d). g=%f h=%f\n", currState>>16, currState&0xFFFF, g, h);
145  return false;
146  }
147 // if (env->GoalTest(currState, goal))
148 // return true; // found goal
149 
150  nodesExpanded++;
151 
152  std::vector<action> actions;
153  env->GetActions(currState, actions);
154  nodesTouched += actions.size();
155  int depth = thePath.size();
156 
157  for (unsigned int x = 0; x < actions.size(); x++)
158  {
159  if ((depth != 0) && (actions[x] == forbiddenAction))
160  continue;
161 
162  thePath.push_back(actions[x]);
163 
164  double edgeCost = env->GCost(currState, actions[x]);
165  env->ApplyAction(currState, actions[x]);
166  env->InvertAction(actions[x]);
167  if (DoIteration(env, actions[x], currState, thePath, bound, g+edgeCost))
168  {
169  env->ApplyAction(currState, actions[x]);
170  return true;
171  }
172  env->ApplyAction(currState, actions[x]);
173 
174  thePath.pop_back();
175  }
176  return false;
177 }
178 
179 
180 template <class state, class action>
181 void DFID<state, action>::UpdateNextBound(double currBound, double gCost)
182 {
183  if (!fgreater(nextBound, currBound))
184  {
185  nextBound = gCost;
186  //printf("Updating next bound to %f\n", nextBound);
187  }
188  else if (fgreater(gCost, currBound) && fless(gCost, nextBound))
189  {
190  nextBound = gCost;
191  //printf("Updating next bound to %f\n", nextBound);
192  }
193 }
194 
195 
196 #endif
DFID::GetPath
void GetPath(SearchEnvironment< state, action > *env, state from, state to, std::vector< state > &thePath)
Definition: DFID.h:53
DFID::nodesExpanded
unsigned long nodesExpanded
Definition: DFID.h:35
DFID::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: DFID.h:30
SearchEnvironment::InvertAction
virtual bool InvertAction(action &a) const =0
DFID::UpdateNextBound
void UpdateNextBound(double currBound, double gCost)
Definition: DFID.h:181
FPUtil.h
DFID::nodeTable
NodeHashTable nodeTable
Definition: DFID.h:47
SearchEnvironment::GCost
virtual double GCost(const state &node1, const state &node2) const =0
NodeHashTable
std::unordered_map< uint32_t, double > NodeHashTable
Definition: GenericIDAStar.h:18
DFID::GetNodesTouched
uint64_t GetNodesTouched()
Definition: DFID.h:31
SearchEnvironment::GetActions
virtual void GetActions(const state &nodeID, std::vector< action > &actions) const =0
DFID::nextBound
double nextBound
Definition: DFID.h:46
DFID::~DFID
virtual ~DFID()
Definition: DFID.h:24
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
DFID::DFID
DFID()
Definition: DFID.h:23
DFID
Definition: DFID.h:21
SearchEnvironment::GetSuccessors
virtual void GetSuccessors(const state &nodeID, std::vector< state > &neighbors) const =0
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
DFID::ResetNodeCount
void ResetNodeCount()
Definition: DFID.h:32
NodeHashTable
std::unordered_map< uint64_t, double > NodeHashTable
Definition: DFID.h:18
DFID::DoIteration
bool DoIteration(SearchEnvironment< state, action > *env, state parent, state currState, std::vector< state > &thePath, double bound, double g)
Definition: DFID.h:102
DFID::nodesTouched
unsigned long nodesTouched
Definition: DFID.h:35
DFID::usePathMax
bool usePathMax
Definition: DFID.h:48
SearchEnvironment::ApplyAction
virtual void ApplyAction(state &s, action a) const =0
DFID::goal
state goal
Definition: DFID.h:45
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
DFID::SetUseBDPathMax
void SetUseBDPathMax(bool val)
Definition: DFID.h:33
SearchEnvironment
Definition: SearchEnvironment.h:30
DFID::useHashTable
bool useHashTable
Definition: DFID.h:49
SearchEnvironment.h