GraphSearch.hpp
Go to the documentation of this file.
1 #ifndef GRAPHSEARCH_HPP_INCLUDED
2 #define GRAPHSEARCH_HPP_INCLUDED
3 
4 #include <set>
5 #include <list>
6 #include <stack>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 
11 #include "SearchProblem.hpp"
12 
13 using namespace std;
14 
15 template <class StateType, class ActionType>
17 {
18 private:
20 public:
22  {
23  _problem = problem;
24  }
25 
27  {
28  double c1 = _problem->getPathCost(&p1);
29  c1 += _problem->getHeuristicCost(p1.getLastState());
30 
31  double c2 = _problem->getPathCost(&p2);
32  c2 += _problem->getHeuristicCost(p2.getLastState());
33 
34  return ( c1 > c2 );
35  }
36 };
37 
39 {
40 
41 public:
43  template <class StateType, class ActionType>
45  {
46  set<StateType> expanded;
47  stack< Path<StateType, ActionType> > frontier;
48 
49  {
51  p.addState(problem.getStartState());
52  frontier.push(p);
53  }
54 
55  while(!frontier.empty())
56  {
57  Path<StateType, ActionType> path = frontier.top();
58  frontier.pop();
59 
60  if( expanded.find(path.getLastState()) == expanded.end() )// expanded does not contain path's last state
61  {
62  expanded.insert(path.getLastState());
63 
64  if(problem.isGoal(path.getLastState()))
65  {
66  return path;
67  }
68  list<ActionType> legalActions = problem.getActions(path.getLastState());
69  StateType last = path.getLastState();
70 
71  for( typename list<ActionType>::iterator it = legalActions.begin(); it != legalActions.end(); it++)
72  {
73  ActionType action = (*it);
74  StateType result = problem.getResult(last, action);
75 
77  newPath.addAction(action);
78  newPath.addState(result);
79  frontier.push(newPath);
80  }
81  }
82  }
83 
84  cout << __func__ << " Error: Could not find a solution." << endl;
86  return empty;
87  }
88 
90  template <class StateType, class ActionType>
92  {
93  set<StateType> expanded;
94  queue< Path<StateType, ActionType> > frontier;
95 
96  {
98  p.addState(problem.getStartState());
99  frontier.push(p);
100  }
101 
102  while(!frontier.empty())
103  {
104  Path<StateType, ActionType> path = frontier.front();
105  frontier.pop();
106 
107  if( expanded.find(path.getLastState()) == expanded.end() ) // expanded does not contain path's last state
108  {
109 
110  expanded.insert(path.getLastState());
111 
112  if(problem.isGoal(path.getLastState()))
113  {
114  return path;
115  }
116  list<ActionType> legalActions = problem.getActions(path.getLastState());
117  StateType last = path.getLastState();
118 
119  for( typename list<ActionType>::iterator it = legalActions.begin(); it != legalActions.end(); it++)
120  {
121  ActionType action = (*it);
122  StateType result = problem.getResult(last, action);
123 
125  newPath.addAction(action);
126  newPath.addState(result);
127  frontier.push(newPath);
128  }
129  }
130  }
131 
132  cout << __func__ << " Error: Could not find a solution." << endl;
134  return empty;
135  }
136 
138  template <class StateType, class ActionType>
140  {
141 
142  set<StateType> expanded;
143  priority_queue< Path<StateType, ActionType>, vector<Path<StateType, ActionType> >, PathComparator<StateType, ActionType> > frontier((PathComparator<StateType,ActionType>(&problem)));
144 
145  {
147  p.addState(problem.getStartState());
148  frontier.push(p);
149  }
150 
151  while(!frontier.empty())
152  {
153  Path<StateType, ActionType> path = frontier.top();
154  frontier.pop();
155 
156  if( expanded.find(path.getLastState()) == expanded.end() ) // expanded does not contain path's last state
157  {
158  StateType last = path.getLastState();
159  expanded.insert(last);
160 
161  if(problem.isGoal(last))
162  {
163  return path;
164  }
165  list<ActionType> legalActions = problem.getActions(last);
166 
167  for( typename list<ActionType>::iterator it = legalActions.begin(); it != legalActions.end(); it++)
168  {
169  ActionType action = (*it);
170  StateType result = problem.getResult(last, action);
172  newPath.addAction(action);
173  newPath.addState(result);
174  frontier.push(newPath);
175  }
176  }
177  }
178 
179  cout << __func__ << " Error: Could not find a solution." << endl;
181  return empty;
182  }
183 };
184 
185 #endif // GRAPHSEARCH_HPP_INCLUDED
static Path< StateType, ActionType > BFS(SearchProblem< StateType, ActionType > &problem)
Definition: GraphSearch.hpp:91
void addAction(ActionType action)
void newPath(const action_pathConstPtr &msg)
action_pathConstPtr path
virtual StateType getStartState()=0
virtual bool isGoal(StateType tate)=0
static Path< StateType, ActionType > AStar(SearchProblem< StateType, ActionType > &problem)
virtual StateType getResult(StateType state, ActionType action)=0
SearchProblem< StateType, ActionType > * _problem
Definition: GraphSearch.hpp:19
void addState(StateType state)
static Path< StateType, ActionType > DFS(SearchProblem< StateType, ActionType > &problem)
Definition: GraphSearch.hpp:44
StateType getLastState()
virtual std::list< ActionType > getActions(StateType state)=0
double getPathCost(Path< StateType, ActionType > *path)
PathComparator(SearchProblem< StateType, ActionType > *problem)
Definition: GraphSearch.hpp:21


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45