HOG2
WeightedUnitGroup.h
Go to the documentation of this file.
1 
7 #include "UnitGroup.h"
9 
10 template <class state, class action, class environment>
11 class WeightedUnitGroup : public UnitGroup<state,action,environment>
12 {
13  public:
14  WeightedUnitGroup(environment* e){wme = new WeightedMap2DEnvironment(e); wt = -1; prop = -1; useWindow = false; windowSize = -1; localWeights = false; currDrawEnv = 0; noweighting = false; updateOnQuery = false; updateSurrounding = false; usePerceptron = false; learningRate = 0; }
15 // WeightedUnitGroup(double weight, double proportion) {wt = weight; prop = proportion;}
16 
17  void SetWeight(double wght) {wt = wght;}
18  void SetProportion(double proport) { prop=proport;}
19  void SetUseWindow(bool b) {useWindow = b;}
20  void SetWindowSize(double d) {windowSize = d;}
21  void UseLocalWeights(bool b) {localWeights = b;}
22  void SetLocalWeightRadius (double r) {localRadius = r;}
23  void SetNoWeighting(bool b) {noweighting = b;}
24  void SetUpdateOnQuery(double d) {updateOnQuery = true; queryProp = d; }
25  void SetUpdateSurrounding(double d) { updateSurrounding = true; surrProp = d; }
26  void UsePerceptron(double lr) { usePerceptron = true; learningRate = lr; }
27 
29  virtual ~WeightedUnitGroup()
30  {
31  delete wme;
32  for (unsigned int i=0; i<unitWme.size(); i++)
33  delete unitWme[i];
34  }
35 
36  virtual const char *GetName() { return "WeightedUnitGroup"; }
37 
39  {
40  if (myE != e)
41  {
43 
44  if (localWeights)
45  {
46  for (unsigned int x = 0; x < this->GetMembers().size(); x++)
47  {
48  unitWme.push_back(new WeightedMap2DEnvironment(e));
49  if (wt != -1)
50  unitWme[unitWme.size()-1]->SetWeight(wt);
51  if (prop != -1)
52  unitWme[unitWme.size()-1]->SetProportionOld(prop);
53  if ((useWindow)&&(windowSize >= 0))
54  {
55  unitWme[unitWme.size()-1]->UseWindow(true);
56  unitWme[unitWme.size()-1]->SetWindowSize(windowSize);
57  }
58  if (noweighting)
59  unitWme[unitWme.size()-1]->SetNoWeighting(true);
60 
61  if (updateOnQuery)
62  {
63  unitWme[unitWme.size()-1]->SetUpdateOnQuery(true);
64  unitWme[unitWme.size()-1]->SetQueryProportionOld(queryProp);
65  }
67  {
68  unitWme[unitWme.size()-1]->SetUpdateSurrounding(true);
69  unitWme[unitWme.size()-1]->SetSurroundingProportion(surrProp);
70  }
71  if (usePerceptron)
72  {
73  unitWme[unitWme.size()-1]->UsePerceptron(learningRate);
74  }
75 
76  environment* uenv = unitWme[unitWme.size()-1];
77  unitEnv.push_back(uenv);
78  }
79  currDrawEnv = unitWme.size();
80  }
81 
82  if (wt != -1)
83  wme->SetWeight(wt);
84  if (prop != -1)
86  if ((useWindow)&&(windowSize >= 0))
87  {
88  wme->UseWindow(true);
90  }
91  if (noweighting)
92  wme->SetNoWeighting(true);
93 
94  if (updateOnQuery)
95  {
96  wme->SetUpdateOnQuery(true);
98  }
100  {
101  wme->SetUpdateSurrounding(true);
103  }
104  if (usePerceptron)
105  {
107  }
108  env = wme;
109  myE = e;
110  }
111  if (useWindow) // Set the unit's current location as center for the window
112  {
113  xyLoc x;
114  u->GetLocation(x);
115  wme->SetWindowCenter(x);
116  }
117  if (localWeights)
118  {
119  while(unitWme.size() < this->GetMembers().size())
120  {
121  unitWme.push_back(new WeightedMap2DEnvironment(e));
122  environment* uenv = unitWme[unitWme.size()-1];
123  unitEnv.push_back(uenv);
124  }
125  // Update local weights in this unit's environment
127 
128  environment* thisUnitsEnv = unitEnv[GetUnitIndex(u)];
129 
130  return (u->MakeMove(thisUnitsEnv, thisUnitsEnv->GetOccupancyInfo(), si,a));
131  }
132 
133 
134  //u->GetAlgorithm()->SetWeightedEnvironment(wme);
135 
136  return (u->MakeMove(env, env->GetOccupancyInfo(), si,a));
137  }
138 
140  {
142  xyLoc currLoc;
143  u->GetLocation(currLoc);
144 
145  //std::cout<<std::endl<<"I'm at "<<currLoc<<std::endl;
146  for (int x = currLoc.x - (int)localRadius; x<=currLoc.x + (int)localRadius; x++)
147  {
148  if ((x<0)||(x>=uwme->GetMap()->GetMapWidth()))
149  continue;
150  for (int y = currLoc.y - (int)localRadius; y<=currLoc.y + (int)localRadius; y++)
151  {
152  if ((y<0)||(y>=uwme->GetMap()->GetMapHeight()))
153  continue;
154 
155  // if this location is within localRadius, update angle
156  xyLoc thisloc;
157  thisloc.x = x;
158  thisloc.y = y;
159 
160  if (uwme->HCost(currLoc, thisloc) <= localRadius)
161  {
162  //std::cout<<"updating "<<thisloc<<std::endl;
163  Vector2D angle = wme->GetAngle(thisloc);
164  uwme->SetAngle(thisloc, angle);
165  }
166 
167  }
168  }
169  }
170 
172  {
173  for (unsigned int x = 0; x < this->GetMembers().size(); x++)
174  {
175  if (this->GetMembers()[x] == u)
176  return x;
177  }
178  return -1;
179  }
180 
181  virtual void UpdateLocation(Unit<state, action, environment> *u, environment *e, state &loc, bool success, SimulationInfo<state,action,environment> *si)
182  {
183  if (myE != e)
184  {
185  if (localWeights)
186  {
187  for (unsigned int x = 0; x < this->GetMembers().size(); x++)
188  {
189  unitWme.push_back(new WeightedMap2DEnvironment(e));
190  if (wt != -1)
191  unitWme[unitWme.size()-1]->SetWeight(wt);
192  if (prop != -1)
193  unitWme[unitWme.size()-1]->SetProportionOld(prop);
194  if ((useWindow)&&(windowSize >= 0))
195  {
196  unitWme[unitWme.size()-1]->UseWindow(true);
197  unitWme[unitWme.size()-1]->SetWindowSize(windowSize);
198  }
199  if (noweighting)
200  unitWme[unitWme.size()-1]->SetNoWeighting(true);
201 
202  if (updateOnQuery)
203  {
204  unitWme[unitWme.size()-1]->SetUpdateOnQuery(true);
205  unitWme[unitWme.size()-1]->SetQueryProportionOld(queryProp);
206  }
207  if (updateSurrounding)
208  {
209  unitWme[unitWme.size()-1]->SetUpdateSurrounding(true);
210  unitWme[unitWme.size()-1]->SetSurroundingProportion(surrProp);
211  }
212  if (usePerceptron)
213  {
214  unitWme[unitWme.size()-1]->UsePerceptron(learningRate);
215  }
216  environment* uenv = unitWme[unitWme.size()-1];
217  unitEnv.push_back(uenv);
218  }
219  currDrawEnv = unitWme.size();
220  }
221 
222  wme = new WeightedMap2DEnvironment(e);
223  if (wt != -1)
224  wme->SetWeight(wt);
225  if (prop != -1)
227  if ((useWindow)&&(windowSize >= 0))
228  {
229  wme->UseWindow(true);
231  }
232  if (noweighting)
233  wme->SetNoWeighting(true);
234 
235  if (updateOnQuery)
236  {
237  wme->SetUpdateOnQuery(true);
239  }
240  if (updateSurrounding)
241  {
242  wme->SetUpdateSurrounding(true);
244  }
245  if (usePerceptron)
246  {
248  }
249  env = wme;
250  myE = e;
251  }
252  xyLoc s;
253  u->GetLocation(s);
255  u->UpdateLocation(env, loc, success, si);
256  }
257 
259  {
260  if (currDrawEnv != unitWme.size())
261  this->GetMembers()[currDrawEnv]->SetColor(1,0,0);
262  currDrawEnv = (currDrawEnv + 1)%(unitWme.size()+1);
263  }
264 
265  virtual void OpenGLDraw(const environment *, const SimulationInfo<state,action,environment> *) const
266  {
267  if (currDrawEnv == unitWme.size())
268  wme->OpenGLDraw();
269  else
270  {
271  //this->GetMembers()[currDrawEnv]->SetColor(0,0,1);
272  unitWme[currDrawEnv]->OpenGLDraw();
273  }
274  }
275 
276  double ComputeArrowMetric(bool b, double t,bool b2, double mt)
277  {
278  return wme->ComputeArrowMetric(b,t,b2,mt);
279  }
280 
281 
282 
283 
284  private:
286  std::vector<WeightedMap2DEnvironment*> unitWme;
287  std::vector<environment*> unitEnv;
288  environment* env;
289  environment* myE;
290  double wt;
291  double prop;
292  bool useWindow;
293  double windowSize;
295  double localRadius;
296  unsigned int currDrawEnv;
299  double queryProp;
301  double surrProp;
303  double learningRate;
304 };
305 
WeightedUnitGroup::windowSize
double windowSize
Definition: WeightedUnitGroup.h:293
WeightedUnitGroup::ComputeArrowMetric
double ComputeArrowMetric(bool b, double t, bool b2, double mt)
Definition: WeightedUnitGroup.h:276
WeightedUnitGroup::SetUseWindow
void SetUseWindow(bool b)
Definition: WeightedUnitGroup.h:19
WeightedMap2DEnvironment.h
A 2D map environment with edge costs weighted according to the number of times a unit has passed over...
Vector2D
Definition: WeightedMap2DEnvironment.h:40
WeightedUnitGroup::DrawNextEnvironment
void DrawNextEnvironment()
Definition: WeightedUnitGroup.h:258
UnitGroup
Definition: UnitGroup.h:31
WeightedUnitGroup::UpdateLocation
virtual void UpdateLocation(Unit< state, action, environment > *u, environment *e, state &loc, bool success, SimulationInfo< state, action, environment > *si)
Definition: WeightedUnitGroup.h:181
WeightedUnitGroup::GetUnitIndex
int GetUnitIndex(Unit< state, action, environment > *u)
Definition: WeightedUnitGroup.h:171
SimulationInfo
Definition: SimulationInfo.h:13
WeightedUnitGroup::OpenGLDraw
virtual void OpenGLDraw(const environment *, const SimulationInfo< state, action, environment > *) const
Definition: WeightedUnitGroup.h:265
WeightedUnitGroup::unitEnv
std::vector< environment * > unitEnv
Definition: WeightedUnitGroup.h:287
Unit::UpdateLocation
virtual void UpdateLocation(environment *, state &, bool success, SimulationInfo< state, action, environment > *)=0
WeightedUnitGroup::MakeMove
virtual bool MakeMove(Unit< state, action, environment > *u, environment *e, SimulationInfo< state, action, environment > *si, action &a)
Definition: WeightedUnitGroup.h:38
WeightedMap2DEnvironment::SetUpdateOnQuery
void SetUpdateOnQuery(bool b)
Definition: WeightedMap2DEnvironment.h:144
SimulationInfo::GetSimulationTime
virtual double GetSimulationTime() const =0
WeightedUnitGroup::SetLocalWeightRadius
void SetLocalWeightRadius(double r)
Definition: WeightedUnitGroup.h:22
xyLoc::y
uint16_t y
Definition: Map2DEnvironment.h:42
WeightedUnitGroup::noweighting
bool noweighting
Definition: WeightedUnitGroup.h:297
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
WeightedUnitGroup::prop
double prop
Definition: WeightedUnitGroup.h:291
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
WeightedUnitGroup::localRadius
double localRadius
Definition: WeightedUnitGroup.h:295
xyLoc
Definition: Map2DEnvironment.h:37
UnitGroup::GetMembers
virtual std::vector< Unit< state, action, environment > * > GetMembers()
Definition: UnitGroup.h:93
WeightedMap2DEnvironment::GetAngle
Vector2D GetAngle(xyLoc &l)
Definition: WeightedMap2DEnvironment.cpp:627
WeightedMap2DEnvironment::OpenGLDraw
void OpenGLDraw() const
Definition: WeightedMap2DEnvironment.cpp:473
xyLoc::x
uint16_t x
Definition: Map2DEnvironment.h:41
WeightedMap2DEnvironment::SetAngle
void SetAngle(xyLoc &l, Vector2D angle)
Definition: WeightedMap2DEnvironment.cpp:621
WeightedUnitGroup::SetUpdateSurrounding
void SetUpdateSurrounding(double d)
Definition: WeightedUnitGroup.h:25
WeightedMap2DEnvironment::UsePerceptron
void UsePerceptron(double lr)
Definition: WeightedMap2DEnvironment.h:162
WeightedUnitGroup::SetWeight
void SetWeight(double wght)
Definition: WeightedUnitGroup.h:17
WeightedUnitGroup::wt
double wt
Definition: WeightedUnitGroup.h:290
WeightedMap2DEnvironment::SetWindowSize
void SetWindowSize(double d)
Definition: WeightedMap2DEnvironment.h:154
WeightedUnitGroup::updateOnQuery
bool updateOnQuery
Definition: WeightedUnitGroup.h:298
WeightedMap2DEnvironment::SetWeight
void SetWeight(double wt)
Set the cost of moving in the "wrong" direction.
Definition: WeightedMap2DEnvironment.h:140
WeightedMap2DEnvironment
Definition: WeightedMap2DEnvironment.h:120
loc
Definition: MapGenerators.cpp:296
WeightedUnitGroup::SetWindowSize
void SetWindowSize(double d)
Definition: WeightedUnitGroup.h:20
WeightedUnitGroup::~WeightedUnitGroup
virtual ~WeightedUnitGroup()
Definition: WeightedUnitGroup.h:29
WeightedUnitGroup::learningRate
double learningRate
Definition: WeightedUnitGroup.h:303
WeightedUnitGroup::usePerceptron
bool usePerceptron
Definition: WeightedUnitGroup.h:302
Unit::GetLocation
virtual void GetLocation(state &)=0
WeightedUnitGroup::UsePerceptron
void UsePerceptron(double lr)
Definition: WeightedUnitGroup.h:26
WeightedMap2DEnvironment::SetProportionOld
void SetProportionOld(double prop)
Set the weight (proportion) of the old angle.
Definition: WeightedMap2DEnvironment.h:142
WeightedUnitGroup::UpdateLocalWeights
void UpdateLocalWeights(Unit< state, action, environment > *u)
Definition: WeightedUnitGroup.h:139
WeightedMap2DEnvironment::SetWindowCenter
void SetWindowCenter(xyLoc l)
Definition: WeightedMap2DEnvironment.h:153
WeightedUnitGroup::UseLocalWeights
void UseLocalWeights(bool b)
Definition: WeightedUnitGroup.h:21
WeightedUnitGroup::localWeights
bool localWeights
Definition: WeightedUnitGroup.h:294
WeightedMap2DEnvironment::SetNoWeighting
void SetNoWeighting(bool b)
Definition: WeightedMap2DEnvironment.h:159
WeightedUnitGroup::SetNoWeighting
void SetNoWeighting(bool b)
Definition: WeightedUnitGroup.h:23
WeightedUnitGroup::WeightedUnitGroup
WeightedUnitGroup(environment *e)
Definition: WeightedUnitGroup.h:14
WeightedUnitGroup::updateSurrounding
bool updateSurrounding
Definition: WeightedUnitGroup.h:300
WeightedUnitGroup::env
environment * env
Definition: WeightedUnitGroup.h:288
WeightedUnitGroup::unitWme
std::vector< WeightedMap2DEnvironment * > unitWme
Definition: WeightedUnitGroup.h:286
WeightedUnitGroup::wme
WeightedMap2DEnvironment * wme
Definition: WeightedUnitGroup.h:285
WeightedMap2DEnvironment::UpdateAngle
void UpdateAngle(const xyLoc &old, const xyLoc &s, double t)
Definition: WeightedMap2DEnvironment.cpp:367
UnitGroup.h
WeightedUnitGroup::queryProp
double queryProp
Definition: WeightedUnitGroup.h:299
WeightedMap2DEnvironment::SetQueryProportionOld
void SetQueryProportionOld(double prop)
Set the weight (proportion) of the old angle for queried edge updates.
Definition: WeightedMap2DEnvironment.h:146
Unit::MakeMove
virtual bool MakeMove(environment *, OccupancyInterface< state, action > *, SimulationInfo< state, action, environment > *, action &a)=0
WeightedUnitGroup::SetUpdateOnQuery
void SetUpdateOnQuery(double d)
Definition: WeightedUnitGroup.h:24
WeightedUnitGroup
Definition: WeightedUnitGroup.h:11
WeightedUnitGroup::myE
environment * myE
Definition: WeightedUnitGroup.h:289
WeightedUnitGroup::GetWeightedEnvironment
WeightedMap2DEnvironment * GetWeightedEnvironment()
Definition: WeightedUnitGroup.h:28
WeightedUnitGroup::GetName
virtual const char * GetName()
Definition: WeightedUnitGroup.h:36
WeightedUnitGroup::useWindow
bool useWindow
Definition: WeightedUnitGroup.h:292
WeightedUnitGroup::currDrawEnv
unsigned int currDrawEnv
Definition: WeightedUnitGroup.h:296
WeightedUnitGroup::surrProp
double surrProp
Definition: WeightedUnitGroup.h:301
WeightedMap2DEnvironment::SetSurroundingProportion
void SetSurroundingProportion(double prop)
Definition: WeightedMap2DEnvironment.h:149
WeightedUnitGroup::SetProportion
void SetProportion(double proport)
Definition: WeightedUnitGroup.h:18
WeightedMap2DEnvironment::SetUpdateSurrounding
void SetUpdateSurrounding(bool b)
Definition: WeightedMap2DEnvironment.h:148
Unit
Definition: Unit.h:23