10 #ifndef BEST_FIRST_SEARCH_H_
11 #define BEST_FIRST_SEARCH_H_
13 #include "../../utils/combinatorics.h"
14 #include "../../generic_defs/search_engine.h"
16 #include "../../generic_defs/heuristic.h"
49 template<
class state_t,
class action_t>
106 virtual double nodeEval(
const state_t &state,
double g_cost,
double h_cost) = 0;
132 template<
class state_t,
class action_t>
134 : heur_func(0), hash_func(0), unique_goal_tests(0)
138 template<
class state_t,
class action_t>
143 template<
class state_t,
class action_t>
149 template<
class state_t,
class action_t>
155 template<
class state_t,
class action_t>
160 heur_func->prepareToCompute();
161 double init_h = heur_func->getHValue(init_state);
163 incrementHCompCount();
165 double init_eval = nodeEval(init_state, 0.0, init_h);
167 open_closed_list.addInitialNodeToOpen(init_state, op_system->getDummyAction(), hash_func->getStateHash(init_state),
171 exp_result = nodeExpansion();
178 template<
class state_t,
class action_t>
182 unique_goal_tests = 0;
185 template<
class state_t,
class action_t>
188 if(open_closed_list.isOpenEmpty())
191 NodeID to_expand_id = open_closed_list.getBestNodeAndClose();
196 if(hitGoalTestLimit())
199 incrementGoalTestCount();
203 if(goal_test->isGoal(to_expand_node.
state)) {
204 extractSolutionPath(to_expand_id);
208 double parent_g = to_expand_node.
g_cost;
210 if(hitSuccFuncLimit())
213 incrementSuccFuccCalls();
216 op_system->getActions(to_expand_node.
state, app_actions);
217 increaseActionGenCount(app_actions.size());
219 for(
unsigned i = 0; i < app_actions.size(); i++) {
221 double edge_cost = op_system->getActionCost(to_expand_node.
state, app_actions[i]);
222 double child_g = parent_g + edge_cost;
224 state_t child_state = to_expand_node.
state;
225 op_system->applyAction(child_state, app_actions[i]);
226 incrementStateGenCount();
228 StateHash child_hash = hash_func->getStateHash(child_state);
230 StateLocation child_loc = open_closed_list.getStateLocation(child_state, child_hash, child_id);
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];
241 open_closed_list.openNodeEvalChanged(child_id);
243 open_closed_list.reopenNode(child_id);
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);
256 open_closed_list.addNewNodeToOpen(child_state, app_actions[i], child_hash, child_g, child_h, child_eval,
264 template<
class state_t,
class action_t>
267 if(!heur_func || !hash_func)
272 template<
class state_t,
class action_t>
275 return unique_goal_tests;
278 template<
class state_t,
class action_t>
281 open_closed_list.clear();
285 template<
class state_t,
class action_t>
288 incumbent_cost = 0.0;
289 incumbent_plan.clear();
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());
298 assert(!
fp_greater(incumbent_cost, open_closed_list.getNode(path_end_id).g_cost));
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