HOG2
SearchEnvironment.h
Go to the documentation of this file.
1 /*
2  * SearchEnvironment.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 5/15/07.
6  * Copyright 2007 Nathan Sturtevant, University of Alberta. All rights reserved.
7  *
8  */
9 
10 #ifndef SEARCHENVIRONMENT_H
11 #define SEARCHENVIRONMENT_H
12 
13 #include <stdint.h>
14 #include <vector>
15 //#include "ReservationProvider.h"
16 #include <assert.h>
17 #include "Heuristic.h"
18 #include "OccupancyInterface.h"
19 #include "GLUtil.h"
20 #include "Graphics.h"
21 
22 
23 struct Hash64 {
24  size_t operator()(const uint64_t &x) const
25  { return (size_t)(x); }
26 };
27 
28 
29 template <class state, class action>
30 class SearchEnvironment : public Heuristic<state> {
31 public:
32  virtual ~SearchEnvironment() {}
33  virtual void GetSuccessors(const state &nodeID, std::vector<state> &neighbors) const = 0;
34  virtual void GetActions(const state &nodeID, std::vector<action> &actions) const = 0;
35  virtual int GetNumSuccessors(const state &stateID) const
36  { std::vector<state> neighbors; GetSuccessors(stateID, neighbors); return (int)neighbors.size(); }
37 
38  virtual action GetAction(const state &s1, const state &s2) const;
39  virtual void ApplyAction(state &s, action a) const = 0;
40  virtual void UndoAction(state &s, action a) const
41  { bool success = InvertAction(a); assert(success); ApplyAction(s, a); }
42 
43  virtual void GetNextState(const state &s1, action a, state &s2) const
44  {
45  s2 = s1;
46  ApplyAction(s2, a);
47  };
48 
49  virtual bool InvertAction(action &a) const = 0;
50 
52  virtual void StoreGoal(state &s)
53  { bValidSearchGoal = true; searchGoal = s; }
54 
56  virtual void ClearGoal()
57  { bValidSearchGoal = false; }
58 
60  virtual bool IsGoalStored() const
61  { return bValidSearchGoal; }
62 
64  virtual double HCost(const state &node1, const state &node2) const = 0;
65  virtual double HCost(const state &node1, const state &node2, double parentHCost) const
66  { return HCost(node1, node2); }
69  virtual double HCost(const state &node) const
70  { assert(bValidSearchGoal); return HCost(node, searchGoal); }
71 
72  virtual double GCost(const state &node1, const state &node2) const = 0;
73  virtual double GCost(const state &node, const action &act) const = 0;
74  virtual bool GoalTest(const state &node, const state &goal) const = 0;
75 
77  virtual bool GoalTest(const state &node) const
78  { return bValidSearchGoal&&(node == searchGoal); }
79 
80  virtual uint64_t GetMaxHash() const { return 0; }
81  virtual uint64_t GetStateHash(const state &node) const = 0;
82  virtual void GetStateFromHash(uint64_t parent, state &s) const { assert(false); }
83 
84  virtual uint64_t GetActionHash(action act) const = 0;
85 
86  virtual double GetPathLength(std::vector<state> &neighbors);
87  virtual double GetPathLength(const state &start, std::vector<action> &neighbors);
88 
90  { return 0; }
92  { }
93 
94  virtual void OpenGLDraw() const {};
95  virtual void OpenGLDraw(const state&) const {};
97  virtual void OpenGLDraw(const state&, const state&, float) const {}
98  virtual void OpenGLDraw(const state&, const action&) const {};
99  virtual void GLLabelState(const state&, const char *) const {} // draw label over state
100  virtual void GLDrawLine(const state &x, const state &y) const {}
101  virtual void GLDrawPath(const std::vector<state> &x) const;
102  virtual void SetColor(const rgbColor &r) const { color = r; }
103  virtual void SetColor(GLfloat rr, GLfloat g, GLfloat b, GLfloat t = 1.0) const { color.r = rr; color.g = g; color.b = b; transparency = t; }
104  virtual void GetColor(GLfloat& rr, GLfloat& g, GLfloat& b, GLfloat &t) const { rr=color.r; g=color.g; b=color.b; t = transparency;}
105  virtual rgbColor GetColor() const { return color; }
106 
107  virtual void Draw(Graphics::Display &display) const {}
108  virtual void Draw(Graphics::Display &display, const state&) const {}
109  virtual void DrawLine(Graphics::Display &display, const state &x, const state &y, float width = 1.0) const {}
110 
111 protected:
113  state searchGoal;
114  mutable rgbColor color;
115  mutable GLfloat transparency;
116 };
117 
118 
119 template <class state, class action>
120 action SearchEnvironment<state,action>::GetAction(const state &s1, const state &s2) const
121 {
122  std::vector<action> a;
123  GetActions(s1, a);
124  for (size_t x = 0; x < a.size(); x++)
125  {
126  state s = s1;
127  ApplyAction(s, a[x]);
128  if (s == s2)
129  {
130  return a[x];
131  }
132  }
133  fprintf(stderr, "No legal move found.");
134  action act;
135  return act;
136 }
137 
138 /*
139  * \param neighbors A vector of adjacent states
140  *
141  * \return The cost of the path
142  */
143 template <class state, class action>
144 double SearchEnvironment<state,action>::GetPathLength(std::vector<state> &neighbors)
145 {
146  double length = 0;
147  for (unsigned int x = 1; x < neighbors.size(); x++)
148  {
149  length += GCost(neighbors[x-1], neighbors[x]);
150  }
151  return length;
152 }
153 
154 template <class state, class action>
155 double SearchEnvironment<state,action>::GetPathLength(const state &start, std::vector<action> &neighbors)
156 {
157  state tmp = start;
158  double length = 0;
159  for (unsigned int x = 0; x < neighbors.size(); x++)
160  {
161  length += GCost(tmp, neighbors[x]);
162  ApplyAction(tmp, neighbors[x]);
163  }
164  return length;
165 }
166 
167 
168 
169 template <class state, class action>
170 void SearchEnvironment<state,action>::GLDrawPath(const std::vector<state> &path) const
171 {
172  for (unsigned int x = 0; x+1 < path.size(); x++)
173  {
174  GLDrawLine(path[x], path[x+1]);
175  }
176 }
177 
178 #endif
SearchEnvironment::DrawLine
virtual void DrawLine(Graphics::Display &display, const state &x, const state &y, float width=1.0) const
Definition: SearchEnvironment.h:109
SearchEnvironment::StoreGoal
virtual void StoreGoal(state &s)
Stores the goal for use by single-state HCost.
Definition: SearchEnvironment.h:52
SearchEnvironment::SetColor
virtual void SetColor(GLfloat rr, GLfloat g, GLfloat b, GLfloat t=1.0) const
Definition: SearchEnvironment.h:103
SearchEnvironment::GLDrawPath
virtual void GLDrawPath(const std::vector< state > &x) const
Definition: SearchEnvironment.h:170
SearchEnvironment::Draw
virtual void Draw(Graphics::Display &display) const
Definition: SearchEnvironment.h:107
SearchEnvironment::GLLabelState
virtual void GLLabelState(const state &, const char *) const
Definition: SearchEnvironment.h:99
rgbColor::b
float b
Definition: Colors.h:71
rgbColor
A color; r/g/b are between 0...1.
Definition: Colors.h:17
SearchEnvironment::transparency
GLfloat transparency
Definition: SearchEnvironment.h:115
Heuristic
Definition: Heuristic.h:30
SearchEnvironment::GetAction
virtual action GetAction(const state &s1, const state &s2) const
Definition: SearchEnvironment.h:120
SearchEnvironment::Draw
virtual void Draw(Graphics::Display &display, const state &) const
Definition: SearchEnvironment.h:108
Heuristic.h
SearchEnvironment::InvertAction
virtual bool InvertAction(action &a) const =0
OccupancyInterface.h
SearchEnvironment::IsGoalStored
virtual bool IsGoalStored() const
Returns true if the goal is stored and false otherwise.
Definition: SearchEnvironment.h:60
SearchEnvironment::UndoAction
virtual void UndoAction(state &s, action a) const
Definition: SearchEnvironment.h:40
width
int width
Definition: SFML_HOG.cpp:54
SearchEnvironment::GCost
virtual double GCost(const state &node1, const state &node2) const =0
rgbColor::g
float g
Definition: Colors.h:71
SearchEnvironment::OpenGLDraw
virtual void OpenGLDraw(const state &, const state &, float) const
Draw the transition at some percentage 0...1 between two states.
Definition: SearchEnvironment.h:97
SearchEnvironment::GetNumSuccessors
virtual int GetNumSuccessors(const state &stateID) const
Definition: SearchEnvironment.h:35
SearchEnvironment::GetActions
virtual void GetActions(const state &nodeID, std::vector< action > &actions) const =0
SearchEnvironment::SetColor
virtual void SetColor(const rgbColor &r) const
Definition: SearchEnvironment.h:102
SearchEnvironment::GetColor
virtual void GetColor(GLfloat &rr, GLfloat &g, GLfloat &b, GLfloat &t) const
Definition: SearchEnvironment.h:104
SearchEnvironment::OpenGLDraw
virtual void OpenGLDraw(const state &) const
Definition: SearchEnvironment.h:95
SearchEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: SearchEnvironment.h:94
SearchEnvironment::searchGoal
state searchGoal
Definition: SearchEnvironment.h:113
SearchEnvironment::color
rgbColor color
Definition: SearchEnvironment.h:114
SearchEnvironment::bValidSearchGoal
bool bValidSearchGoal
Definition: SearchEnvironment.h:112
SearchEnvironment::GetPathLength
virtual double GetPathLength(std::vector< state > &neighbors)
Definition: SearchEnvironment.h:144
Hash64
Definition: SearchEnvironment.h:23
Graphics::Display
Definition: Graphics.h:146
SearchEnvironment::GetColor
virtual rgbColor GetColor() const
Definition: SearchEnvironment.h:105
SearchEnvironment::GetSuccessors
virtual void GetSuccessors(const state &nodeID, std::vector< state > &neighbors) const =0
SearchEnvironment::~SearchEnvironment
virtual ~SearchEnvironment()
Definition: SearchEnvironment.h:32
SearchEnvironment::GetStateFromHash
virtual void GetStateFromHash(uint64_t parent, state &s) const
Definition: SearchEnvironment.h:82
SearchEnvironment::GetActionHash
virtual uint64_t GetActionHash(action act) const =0
GLUtil.h
Hash64::operator()
size_t operator()(const uint64_t &x) const
Definition: SearchEnvironment.h:24
Graphics.h
rgbColor::r
float r
Definition: Colors.h:71
SearchEnvironment::HCost
virtual double HCost(const state &node1, const state &node2) const =0
Heuristic value between two arbitrary nodes.
SearchEnvironment::GoalTest
virtual bool GoalTest(const state &node) const
Goal Test if the goal is stored.
Definition: SearchEnvironment.h:77
SearchEnvironment::SetOccupancyInfo
virtual void SetOccupancyInfo(OccupancyInterface< state, action > *)
Definition: SearchEnvironment.h:91
SearchEnvironment::OpenGLDraw
virtual void OpenGLDraw(const state &, const action &) const
Definition: SearchEnvironment.h:98
SearchEnvironment::GetMaxHash
virtual uint64_t GetMaxHash() const
Definition: SearchEnvironment.h:80
SearchEnvironment::GetOccupancyInfo
virtual OccupancyInterface< state, action > * GetOccupancyInfo()
Definition: SearchEnvironment.h:89
SearchEnvironment::HCost
virtual double HCost(const state &node) const
Heuristic value between node and the stored goal.
Definition: SearchEnvironment.h:69
SearchEnvironment::HCost
virtual double HCost(const state &node1, const state &node2, double parentHCost) const
Definition: SearchEnvironment.h:65
SearchEnvironment::GetStateHash
virtual uint64_t GetStateHash(const state &node) const =0
SearchEnvironment::ClearGoal
virtual void ClearGoal()
Clears the goal from memory.
Definition: SearchEnvironment.h:56
SearchEnvironment::ApplyAction
virtual void ApplyAction(state &s, action a) const =0
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
SearchEnvironment::GetNextState
virtual void GetNextState(const state &s1, action a, state &s2) const
Definition: SearchEnvironment.h:43
SearchEnvironment::GoalTest
virtual bool GoalTest(const state &node, const state &goal) const =0
SearchEnvironment
Definition: SearchEnvironment.h:30
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
OccupancyInterface
Definition: OccupancyInterface.h:36
SearchEnvironment::GLDrawLine
virtual void GLDrawLine(const state &x, const state &y) const
Definition: SearchEnvironment.h:100