Assignment Search Framework
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
best_first_search.h
Go to the documentation of this file.
1 /*
2  * best_first_search.h
3  *
4  * LICENSE HERE
5  *
6  * Created on: 2016-09-11
7  * Author: Rick Valenzano
8  */
9 
10 #ifndef BEST_FIRST_SEARCH_H_
11 #define BEST_FIRST_SEARCH_H_
12 
13 #include "../../utils/combinatorics.h"
14 #include "../../generic_defs/search_engine.h"
15 #include "open_closed_list.h"
16 #include "../../generic_defs/heuristic.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <iostream>
20 
34 {
36 };
37 
49 template<class state_t, class action_t>
50 class BestFirstSearch: public SearchEngine<state_t, action_t>
51 {
56 
65 
66 public:
68  virtual ~BestFirstSearch();
69 
76 
82  void setHeuristic(Heuristic<state_t> *heur);
83 
89  uint64_t getUniqueGoalTests() const;
90 
91 protected:
92  // Overloaded functions
93  virtual SearchTermType searchForPlan(const state_t &init_state);
94  virtual void resetStatistics();
95  virtual bool isConfigured() const;
96  virtual void resetEngine();
97 
106  virtual double nodeEval(const state_t &state, double g_cost, double h_cost) = 0;
107 
114 
120  void extractSolutionPath(NodeID path_end_id);
121 
124 
126 
127  uint64_t unique_goal_tests;
128 
129  std::vector<action_t> app_actions;
130 };
131 
132 template<class state_t, class action_t>
134  : heur_func(0), hash_func(0), unique_goal_tests(0)
135 {
136 }
137 
138 template<class state_t, class action_t>
140 {
141 }
142 
143 template<class state_t, class action_t>
145 {
146  hash_func = hash;
147 }
148 
149 template<class state_t, class action_t>
151 {
152  heur_func = heur;
153 }
154 
155 template<class state_t, class action_t>
157 {
159 
160  heur_func->prepareToCompute();
161  double init_h = heur_func->getHValue(init_state);
162 
163  incrementHCompCount();
164 
165  double init_eval = nodeEval(init_state, 0.0, init_h);
166 
167  open_closed_list.addInitialNodeToOpen(init_state, op_system->getDummyAction(), hash_func->getStateHash(init_state),
168  init_h, init_eval);
169 
170  while(exp_result == BfsExpansionResult::no_solution)
171  exp_result = nodeExpansion();
172 
173  if(exp_result == BfsExpansionResult::res_limit)
176 }
177 
178 template<class state_t, class action_t>
180 {
182  unique_goal_tests = 0;
183 }
184 
185 template<class state_t, class action_t>
187 {
188  if(open_closed_list.isOpenEmpty())
190 
191  NodeID to_expand_id = open_closed_list.getBestNodeAndClose();
192 
193  BFSNode<state_t, action_t> to_expand_node = open_closed_list.getNode(to_expand_id);
194  //std::cout << "Expanding " << to_expand_id << " - " << to_expand_node.state << "," << to_expand_node.gen_action << std::endl;
195 
196  if(hitGoalTestLimit())
198 
199  incrementGoalTestCount();
200  if(!to_expand_node.reopened)
201  unique_goal_tests++; // change this to a function
202 
203  if(goal_test->isGoal(to_expand_node.state)) { // checks for goal
204  extractSolutionPath(to_expand_id);
206  }
207 
208  double parent_g = to_expand_node.g_cost;
209 
210  if(hitSuccFuncLimit())
212 
213  incrementSuccFuccCalls();
214 
215  app_actions.clear();
216  op_system->getActions(to_expand_node.state, app_actions);
217  increaseActionGenCount(app_actions.size());
218 
219  for(unsigned i = 0; i < app_actions.size(); i++) {
220 
221  double edge_cost = op_system->getActionCost(to_expand_node.state, app_actions[i]);
222  double child_g = parent_g + edge_cost;
223 
224  state_t child_state = to_expand_node.state;
225  op_system->applyAction(child_state, app_actions[i]);
226  incrementStateGenCount();
227 
228  StateHash child_hash = hash_func->getStateHash(child_state);
229  NodeID child_id;
230  StateLocation child_loc = open_closed_list.getStateLocation(child_state, child_hash, child_id);
231 
232  if(child_loc == StateLocation::open || child_loc == StateLocation::closed) {
233  if(fp_less(child_g, open_closed_list.getNode(child_id).g_cost)) {
234  open_closed_list.getNode(child_id).g_cost = child_g;
235  open_closed_list.getNode(child_id).eval = nodeEval(child_state, child_g,
236  open_closed_list.getNode(child_id).h_value);
237  open_closed_list.getNode(child_id).parent_id = to_expand_id;
238  open_closed_list.getNode(child_id).gen_action = app_actions[i];
239 
240  if(child_loc == StateLocation::open)
241  open_closed_list.openNodeEvalChanged(child_id);
242  else
243  open_closed_list.reopenNode(child_id);
244  }
245  } else {
246 
247  if(hitHCompLimit())
249 
250  incrementHCompCount();
251  heur_func->prepareToCompute();
252  double child_h = heur_func->getHValue(child_state);
253  double child_eval = nodeEval(child_state, child_g, child_h);
254 
255  //std::cout << "New Child " << child_state << " eval " << child_eval << std::endl;
256  open_closed_list.addNewNodeToOpen(child_state, app_actions[i], child_hash, child_g, child_h, child_eval,
257  to_expand_id);
258  }
259  }
260 
262 }
263 
264 template<class state_t, class action_t>
266 {
267  if(!heur_func || !hash_func)
268  return false;
270 }
271 
272 template<class state_t, class action_t>
274 {
275  return unique_goal_tests;
276 }
277 
278 template<class state_t, class action_t>
280 {
281  open_closed_list.clear();
283 }
284 
285 template<class state_t, class action_t>
287 {
288  incumbent_cost = 0.0;
289  incumbent_plan.clear();
290 
291  NodeID id = path_end_id;
292  while(open_closed_list.getNode(id).gen_action != op_system->getDummyAction()) {
293  incumbent_plan.push_back(open_closed_list.getNode(id).gen_action);
294  id = open_closed_list.getNode(id).parent_id;
295  incumbent_cost += op_system->getActionCost(open_closed_list.getNode(id).state, incumbent_plan.back());
296  }
297 
298  assert(!fp_greater(incumbent_cost, open_closed_list.getNode(path_end_id).g_cost));
299 }
300 
301 #endif /* BEST_FIRST_SEARCH_H_ */
virtual void resetEngine()
Definition: best_first_search.h:279
uint64_t StateHash
The hash value of a state.
Definition: state_hash_function.h:14
virtual void resetStatistics()
Definition: search_engine.h:428
uint64_t unique_goal_tests
The number of unique goal tests performed.
Definition: best_first_search.h:127
virtual void resetStatistics()
Definition: best_first_search.h:179
virtual double nodeEval(const state_t &state, double g_cost, double h_cost)=0
bool fp_greater(double a, double b)
Definition: floating_point_utils.cpp:17
Definition: state_hash_function.h:22
Definition: open_closed_list.h:92
const StateHashFunction< state_t > * hash_func
The hash function.
Definition: best_first_search.h:123
StateLocation
Definition: open_closed_list.h:32
virtual BfsExpansionResult nodeExpansion()
Definition: best_first_search.h:186
void setHashFunction(const StateHashFunction< state_t > *hash)
Definition: best_first_search.h:144
std::vector< action_t > app_actions
A vector to store the set of applicable actions.
Definition: best_first_search.h:129
uint64_t getUniqueGoalTests() const
Definition: best_first_search.h:273
SearchTermType
Definition: search_engine.h:51
unsigned NodeID
The ID of a node is the location of the node in the node table.
Definition: node_table.h:21
double g_cost
The g-cost of the node.
Definition: open_closed_list.h:73
void extractSolutionPath(NodeID path_end_id)
Definition: best_first_search.h:286
virtual SearchTermType searchForPlan(const state_t &init_state)
Definition: best_first_search.h:156
BestFirstSearch()
Definition: best_first_search.h:133
bool fp_less(double a, double b)
Definition: floating_point_utils.cpp:12
void setHeuristic(Heuristic< state_t > *heur)
Definition: best_first_search.h:150
Heuristic< state_t > * heur_func
The heuristic function.
Definition: best_first_search.h:122
Definition: search_engine.h:62
virtual bool isConfigured() const
Definition: best_first_search.h:265
bool reopened
If the node has been reopened.
Definition: open_closed_list.h:80
OpenClosedList< state_t, action_t > open_closed_list
The open and closed list.
Definition: best_first_search.h:125
virtual ~BestFirstSearch()
Definition: best_first_search.h:139
Definition: open_closed_list.h:43
Definition: heuristic.h:23
virtual bool isConfigured() const
Definition: search_engine.h:459
Definition: best_first_search.h:50
virtual void resetEngine()
Definition: search_engine.h:439
state_t state
The state corresponding to this node.
Definition: open_closed_list.h:69