SearchProblem.hpp
Go to the documentation of this file.
1 #ifndef SEARCHPROBLEM_HPP_INCLUDED
2 #define SEARCHPROBLEM_HPP_INCLUDED
3 
4 #include <list>
5 #include <algorithm>
6 #include <iterator>
7 
8 template <class StateType, class ActionType>
9 class Path
10 {
11 private:
12  std::list<StateType> states;
13  std::list<ActionType> actions;
14 public:
15  Path(){ }
17  {
18  states = std::list<StateType>(p.states);
19  actions = std::list<ActionType>(p.actions);
20  }
21  void addState(StateType state)
22  {
23  states.push_back(state);
24  }
25  void addAction(ActionType action)
26  {
27  actions.push_back(action);
28  }
29  void setState(StateType state, int index)
30  {
31  if(index < 0 || index >= states.size())
32  throw "Index out of bounds!";
33  states[index] = state;
34  }
35  void setAction(ActionType action, int index)
36  {
37  if(index < 0 || index >= actions.size())
38  throw "Index out of bounds!";
39  actions[index] = action;
40  }
41  StateType getState(unsigned int index)
42  {
43  if(index < 0 || index >= states.size())
44  throw "Index out of bounds!";
45 
46  typename std::list<StateType>::iterator iter = states.begin();
47  std::advance(iter, index);
48  return *iter;
49  }
50  ActionType getAction(unsigned int index)
51  {
52  if(index < 0 || index >= actions.size())
53  throw "Index out of bounds!";
54  typename std::list<ActionType>::iterator iter = actions.begin();
55  std::advance(iter, index);
56  return *iter;
57  }
58  std::list<StateType>* getStates()
59  {
60  return &states;
61  }
62  std::list<ActionType>* getActions()
63  {
64  return &actions;
65  }
66  StateType getLastState()
67  {
68  return states.back();
69  }
71  {
72  return actions.size();
73  }
74  bool containsState(StateType state)
75  {
76  return (find(states.begin(), states.end(), state) != states.end());
77  }
78 };
79 
80 template <class StateType, class ActionType>
82 {
83 public:
84  virtual StateType getStartState() = 0;
85  virtual std::list<ActionType> getActions(StateType state) = 0;
86  virtual StateType getResult(StateType state, ActionType action) = 0;
87  virtual bool isGoal(StateType tate) = 0;
88  virtual double getStepCost(StateType state, ActionType action) { (void)state; (void)action; return 1; }
89  virtual double getHeuristicCost(StateType state) { (void)state; return 0; }
91  {
92  double cost = 0;
93 
94  for(int i=0; i < path->getNumberOfSteps(); i++)
95  {
96  cost += getStepCost(path->getState(i), path->getAction(i));
97  }
98 
99  return cost;
100  }
101 };
102 
103 #endif // SEARCHPROBLEM_HPP_INCLUDED
void addAction(ActionType action)
action_pathConstPtr path
virtual StateType getStartState()=0
virtual bool isGoal(StateType tate)=0
bool containsState(StateType state)
virtual double getStepCost(StateType state, ActionType action)
void setAction(ActionType action, int index)
virtual StateType getResult(StateType state, ActionType action)=0
Path(const Path< StateType, ActionType > &p)
std::list< ActionType > actions
virtual double getHeuristicCost(StateType state)
std::list< StateType > * getStates()
void addState(StateType state)
StateType getLastState()
ActionType getAction(unsigned int index)
igvc_msgs::lights state
std::list< StateType > states
virtual std::list< ActionType > getActions(StateType state)=0
double getPathCost(Path< StateType, ActionType > *path)
std::list< ActionType > * getActions()
int getNumberOfSteps()
StateType getState(unsigned int index)
void setState(StateType state, int index)


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