HOG2
WeightedMap2DEnvironment.h
Go to the documentation of this file.
1 
30 #ifndef WEIGHTEDMAP2DENVIRONMENT_H
31 #define WEIGHTEDMAP2DENVIRONMENT_H
32 
33 #include "Map2DEnvironment.h"
34 #include "BitVector.h"
35 #include "OccupancyInterface.h"
36 #include "Graph.h"
37 #include <cmath>
38 #include <unordered_map>
39 
40 class Vector2D {
41  public:
42  Vector2D(float _x,float _y):x(_x),y(_y),updateTime(0),accessTime(0) {/*Normalize();*/}
43  Vector2D():x(0),y(0),updateTime(0),accessTime(0) {}
44  void Set(float _x, float _y) { x=_x; y=_y; /*Normalize();*/}
45 
46  void SetUpdateTime(double t) {updateTime=t;}
47  double GetUpdateTime() {return updateTime;}
48 
49  void SetAccessTime(double t) {accessTime=t;}
50  double GetAccessTime() {return accessTime;}
51 
52  bool operator==(const Vector2D &rhs) {return ((x == rhs.x)&&(y==rhs.y));}
53  std::ostream& operator <<(std::ostream & out)
54  {
55  out << "(" << x << ", " << y << ")";
56  return out;
57  }
58 
59  friend Vector2D operator *(const Vector2D& vec, const double num)
60  {
61  Vector2D returnme(vec.x * num, vec.y * num);
62  return returnme;
63  }
64 
65  friend Vector2D operator *(const double num, const Vector2D& vec)
66  {
67  Vector2D returnme(vec.x * num, vec.y * num);
68  return returnme;
69  }
70 
71  friend Vector2D operator +(const Vector2D& v1, const Vector2D& v2)
72  {
73  Vector2D returnme(v1.x + v2.x, v1.y + v2.y);
74  //returnme.Normalize();
75  return returnme;
76  }
77  //private:
78  float x, y;
80 
81  void Normalize()
82  {
83  if ((x==0)&&(y==0))
84  return;
85  float magnitude = sqrt(x*x + y*y);
86  x /= magnitude;
87  y /= magnitude;
88  }
89 };
90 
91 
92 namespace AngleUtil {
93 
95  public:
96  AngleSearchNode(const xyLoc &s,uint64_t key)
97  :node(s), hashKey(key) {}
99  uint64_t hashKey;
100  };
101 
103  bool operator()(const AngleSearchNode &i1, const AngleSearchNode &i2) const
104  { return (i1.node == i2.node); } };
105 
106  struct SearchNodeHash {
107  size_t operator()(const AngleSearchNode &x) const
108  { return (size_t)(x.hashKey); } };
109 
110  typedef std::unordered_map<AngleUtil::AngleSearchNode,Vector2D, AngleUtil::SearchNodeHash, AngleUtil::SearchNodeEqual> AngleLookupTable;
111 };
112 
114 enum {
115  kForwardCount = 2, // The number of units passing from From to To
116  kBackwardCount = 3 // The number of units passing from To to From
117 };
118 
119 
120 class WeightedMap2DEnvironment : public AbsMapEnvironment
121 {
122 public:
123  WeightedMap2DEnvironment(MapAbstraction *ma);
124  WeightedMap2DEnvironment(AbsMapEnvironment *ame);
125  virtual ~WeightedMap2DEnvironment();
126  void ApplyAction(xyLoc &s, tDirection dir) const;
127  virtual double GCost(const xyLoc &node1, const xyLoc &node2) const;
128  virtual double GCost(const xyLoc &node1, const tDirection &act) const { return AbsMapEnvironment::GCost(node1, act); }
129  //virtual BaseMapOccupancyInterface* GetOccupancyInterface(){std::cout<<"Returning "<<oi<<std::endl;return oi;}
131  void OpenGLDraw() const;
132  void OpenGLDraw(const xyLoc &l) const{ AbsMapEnvironment::OpenGLDraw(l); }
133  void OpenGLDraw(const xyLoc& s, const tDirection &dir) const;
134  void OpenGLDraw(const xyLoc &l1, const xyLoc &l2, float v) const { MapEnvironment::OpenGLDraw(l1, l2, v); }
135  void DrawEdge(const edge* e) const;
136 
137  void UpdateAngle(const xyLoc &old, const xyLoc &s, double t);
138 
140  void SetWeight(double wt) { diffWeight = wt; }
142  void SetProportionOld(double prop) { assert((prop>=0) && (prop <=1)); oldProportion = prop; }
143 
144  void SetUpdateOnQuery(bool b) { updateOnQuery = b;}
146  void SetQueryProportionOld(double prop) { assert((prop>=0) && (prop <=1)); queryOldProportion = prop; }
147 
149  void SetSurroundingProportion(double prop) { assert((prop>=0) && (prop <=1)); surroundingProportion = prop;}
150 
151  // Use weights within window only
152  void UseWindow(bool b) {useWindow=b;}
154  void SetWindowSize(double d) {windowSize=d;}
155 
156  // Needed for local copy updating of weights
157  void SetAngle(xyLoc &l, Vector2D angle);
158  Vector2D GetAngle(xyLoc &l);
159  void SetNoWeighting(bool b) {noWeighting = b; }
160 
161  // For perceptron update rule
162  void UsePerceptron(double lr) { usePerceptron = true; learningRate = lr; }
163 
164  double ComputeArrowMetric(bool timed=false,double time=0, bool DoNormalize=false, double maxtime=0);
166 
167 private:
169 
170  //typedef std::unordered_map<AngleUtil::AngleSearchNode,Vector2D, AngleUtil::SearchNodeHash, AngleUtil::SearchNodeEqual>
171  typedef std::unordered_map<AngleUtil::AngleSearchNode,Vector2D, AngleUtil::SearchNodeHash, AngleUtil::SearchNodeEqual> AngleLookupTable;
173 
174  void UpdateAngle(const xyLoc &old, const xyLoc &s, double prop, double t);
175 
176  double diffWeight;
178 
179  // Only use weights within some window
181  bool useWindow;
182  double windowSize;
183 
185 
188 
191 
193  double learningRate;
194 };
195 
197 
198 // Weighting with node angles
199 
200 
201 
202 
203 #endif
AngleUtil::AngleSearchNode::node
xyLoc node
Definition: WeightedMap2DEnvironment.h:98
UnitWeightedMapSimulation
UnitSimulation< xyLoc, tDirection, WeightedMap2DEnvironment > UnitWeightedMapSimulation
Definition: WeightedMap2DEnvironment.h:196
WeightedMap2DEnvironment::useWindow
bool useWindow
Definition: WeightedMap2DEnvironment.h:181
WeightedMap2DEnvironment::GetAngleFromDirection
Vector2D GetAngleFromDirection(tDirection dir) const
Definition: WeightedMap2DEnvironment.cpp:725
Vector2D
Definition: WeightedMap2DEnvironment.h:40
Vector2D::GetUpdateTime
double GetUpdateTime()
Definition: WeightedMap2DEnvironment.h:47
Graph.h
WeightedMap2DEnvironment::GCost
virtual double GCost(const xyLoc &node1, const tDirection &act) const
Definition: WeightedMap2DEnvironment.h:128
Vector2D::x
float x
Definition: WeightedMap2DEnvironment.h:78
AngleUtil::SearchNodeEqual::operator()
bool operator()(const AngleSearchNode &i1, const AngleSearchNode &i2) const
Definition: WeightedMap2DEnvironment.h:103
WeightedMap2DEnvironment::GCost
virtual double GCost(const xyLoc &node1, const xyLoc &node2) const
Definition: WeightedMap2DEnvironment.cpp:379
WeightedMap2DEnvironment::SetUpdateOnQuery
void SetUpdateOnQuery(bool b)
Definition: WeightedMap2DEnvironment.h:144
BaseMapOccupancyInterface
Definition: Map2DEnvironment.h:82
WeightedMap2DEnvironment::OpenGLDraw
void OpenGLDraw(const xyLoc &l1, const xyLoc &l2, float v) const
Definition: WeightedMap2DEnvironment.h:134
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
WeightedMap2DEnvironment::windowSize
double windowSize
Definition: WeightedMap2DEnvironment.h:182
WeightedMap2DEnvironment::~WeightedMap2DEnvironment
virtual ~WeightedMap2DEnvironment()
Destructor for the WeightedMap2DEnvironment.
Definition: WeightedMap2DEnvironment.cpp:64
AngleUtil::SearchNodeHash::operator()
size_t operator()(const AngleSearchNode &x) const
Definition: WeightedMap2DEnvironment.h:107
WeightedMap2DEnvironment::UseWindow
void UseWindow(bool b)
Definition: WeightedMap2DEnvironment.h:152
WeightedMap2DEnvironment::ComputeArrowMetric
double ComputeArrowMetric(bool timed=false, double time=0, bool DoNormalize=false, double maxtime=0)
Definition: WeightedMap2DEnvironment.cpp:639
OccupancyInterface.h
Vector2D::SetAccessTime
void SetAccessTime(double t)
Definition: WeightedMap2DEnvironment.h:49
Vector2D::GetAccessTime
double GetAccessTime()
Definition: WeightedMap2DEnvironment.h:50
xyLoc
Definition: Map2DEnvironment.h:37
Map2DEnvironment.h
WeightedMap2DEnvironment::GetAngle
Vector2D GetAngle(xyLoc &l)
Definition: WeightedMap2DEnvironment.cpp:627
WeightedMap2DEnvironment::GetOccupancyInfo
virtual BaseMapOccupancyInterface * GetOccupancyInfo()
Definition: WeightedMap2DEnvironment.h:130
WeightedMap2DEnvironment::OpenGLDraw
void OpenGLDraw() const
Definition: WeightedMap2DEnvironment.cpp:473
MapEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: Map2DEnvironment.cpp:552
WeightedMap2DEnvironment::ApplyAction
void ApplyAction(xyLoc &s, tDirection dir) const
ApplyAction is called when an action is applied.
Definition: WeightedMap2DEnvironment.cpp:77
AngleUtil::AngleSearchNode
Definition: WeightedMap2DEnvironment.h:94
WeightedMap2DEnvironment::SetAngle
void SetAngle(xyLoc &l, Vector2D angle)
Definition: WeightedMap2DEnvironment.cpp:621
WeightedMap2DEnvironment::UsePerceptron
void UsePerceptron(double lr)
Definition: WeightedMap2DEnvironment.h:162
WeightedMap2DEnvironment::SetWindowSize
void SetWindowSize(double d)
Definition: WeightedMap2DEnvironment.h:154
AngleUtil
Definition: WeightedMap2DEnvironment.h:92
WeightedMap2DEnvironment::SetWeight
void SetWeight(double wt)
Set the cost of moving in the "wrong" direction.
Definition: WeightedMap2DEnvironment.h:140
WeightedMap2DEnvironment
Definition: WeightedMap2DEnvironment.h:120
WeightedMap2DEnvironment::noWeighting
bool noWeighting
Definition: WeightedMap2DEnvironment.h:184
WeightedMap2DEnvironment::DrawEdge
void DrawEdge(const edge *e) const
Definition: WeightedMap2DEnvironment.cpp:540
Vector2D::operator*
friend Vector2D operator*(const Vector2D &vec, const double num)
Definition: WeightedMap2DEnvironment.h:59
Vector2D::accessTime
double accessTime
Definition: WeightedMap2DEnvironment.h:79
WeightedMap2DEnvironment::windowCenter
xyLoc windowCenter
Definition: WeightedMap2DEnvironment.h:180
Vector2D::operator==
bool operator==(const Vector2D &rhs)
Definition: WeightedMap2DEnvironment.h:52
Vector2D::Vector2D
Vector2D(float _x, float _y)
Definition: WeightedMap2DEnvironment.h:42
WeightedMap2DEnvironment::SetProportionOld
void SetProportionOld(double prop)
Set the weight (proportion) of the old angle.
Definition: WeightedMap2DEnvironment.h:142
tDirection
tDirection
Definition: Map2DEnvironment.h:77
WeightedMap2DEnvironment::SetWindowCenter
void SetWindowCenter(xyLoc l)
Definition: WeightedMap2DEnvironment.h:153
WeightedMap2DEnvironment::SetNoWeighting
void SetNoWeighting(bool b)
Definition: WeightedMap2DEnvironment.h:159
WeightedMap2DEnvironment::queryOldProportion
double queryOldProportion
Definition: WeightedMap2DEnvironment.h:187
WeightedMap2DEnvironment::oldProportion
double oldProportion
Definition: WeightedMap2DEnvironment.h:177
WeightedMap2DEnvironment::diffWeight
double diffWeight
Definition: WeightedMap2DEnvironment.h:176
WeightedMap2DEnvironment::surroundingProportion
double surroundingProportion
Definition: WeightedMap2DEnvironment.h:190
Vector2D::Set
void Set(float _x, float _y)
Definition: WeightedMap2DEnvironment.h:44
Vector2D::y
float y
Definition: WeightedMap2DEnvironment.h:78
AngleUtil::AngleSearchNode::hashKey
uint64_t hashKey
Definition: WeightedMap2DEnvironment.h:99
Vector2D::Vector2D
Vector2D()
Definition: WeightedMap2DEnvironment.h:43
Vector2D::operator+
friend Vector2D operator+(const Vector2D &v1, const Vector2D &v2)
Definition: WeightedMap2DEnvironment.h:71
WeightedMap2DEnvironment::angleLookup
AngleLookupTable angleLookup
Definition: WeightedMap2DEnvironment.h:172
WeightedMap2DEnvironment::AngleLookupTable
std::unordered_map< AngleUtil::AngleSearchNode, Vector2D, AngleUtil::SearchNodeHash, AngleUtil::SearchNodeEqual > AngleLookupTable
Definition: WeightedMap2DEnvironment.h:171
WeightedMap2DEnvironment::WeightedMap2DEnvironment
WeightedMap2DEnvironment(MapAbstraction *ma)
Constructor for the WeightedMap2DEnvironment.
Definition: WeightedMap2DEnvironment.cpp:10
Vector2D::SetUpdateTime
void SetUpdateTime(double t)
Definition: WeightedMap2DEnvironment.h:46
UnitSimulation
The basic simulation class for the world.
Definition: UnitSimulation.h:85
kForwardCount
@ kForwardCount
Definition: WeightedMap2DEnvironment.h:115
Vector2D::operator<<
std::ostream & operator<<(std::ostream &out)
Definition: WeightedMap2DEnvironment.h:53
WeightedMap2DEnvironment::learningRate
double learningRate
Definition: WeightedMap2DEnvironment.h:193
BitVector.h
WeightedMap2DEnvironment::UpdateAngle
void UpdateAngle(const xyLoc &old, const xyLoc &s, double t)
Definition: WeightedMap2DEnvironment.cpp:367
AngleUtil::AngleSearchNode::AngleSearchNode
AngleSearchNode(const xyLoc &s, uint64_t key)
Definition: WeightedMap2DEnvironment.h:96
AngleUtil::SearchNodeHash
Definition: WeightedMap2DEnvironment.h:106
Vector2D::Normalize
void Normalize()
Definition: WeightedMap2DEnvironment.h:81
WeightedMap2DEnvironment::usePerceptron
bool usePerceptron
Definition: WeightedMap2DEnvironment.h:192
WeightedMap2DEnvironment::SetQueryProportionOld
void SetQueryProportionOld(double prop)
Set the weight (proportion) of the old angle for queried edge updates.
Definition: WeightedMap2DEnvironment.h:146
WeightedMap2DEnvironment::oi
BaseMapOccupancyInterface * oi
Definition: WeightedMap2DEnvironment.h:168
AngleUtil::AngleLookupTable
std::unordered_map< AngleUtil::AngleSearchNode, Vector2D, AngleUtil::SearchNodeHash, AngleUtil::SearchNodeEqual > AngleLookupTable
Definition: WeightedMap2DEnvironment.h:110
WeightedMap2DEnvironment::updateSurrounding
bool updateSurrounding
Definition: WeightedMap2DEnvironment.h:189
WeightedMap2DEnvironment::SetSurroundingProportion
void SetSurroundingProportion(double prop)
Definition: WeightedMap2DEnvironment.h:149
kBackwardCount
@ kBackwardCount
Definition: WeightedMap2DEnvironment.h:116
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
WeightedMap2DEnvironment::SetUpdateSurrounding
void SetUpdateSurrounding(bool b)
Definition: WeightedMap2DEnvironment.h:148
Vector2D::updateTime
double updateTime
Definition: WeightedMap2DEnvironment.h:79
WeightedMap2DEnvironment::OpenGLDraw
void OpenGLDraw(const xyLoc &l) const
Definition: WeightedMap2DEnvironment.h:132
AngleUtil::SearchNodeEqual
Definition: WeightedMap2DEnvironment.h:102
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129
WeightedMap2DEnvironment::updateOnQuery
bool updateOnQuery
Definition: WeightedMap2DEnvironment.h:186