HOG2
GreedyDMUnit.h
Go to the documentation of this file.
1 #include "Unit.h"
3 
4 template <class environment>
5 class GreedyDMUnit : public Unit<xyLoc,tDirection,environment>
6 {
7  public:
8  GreedyDMUnit(xyLoc &startLoc) :loc(startLoc) {}
10 
11  virtual const char *GetName() { return "Greedy DM following unit"; }
12 
14  {
15 // if (random()%10)
16 // { a = kStay; return true; }
17 
18  std::vector<tDirection> directions;
19  directions.push_back(kN);
20  directions.push_back(kNE);
21  directions.push_back(kE);
22  directions.push_back(kSE);
23  directions.push_back(kS);
24  directions.push_back(kSW);
25  directions.push_back(kW);
26  directions.push_back(kNW);
27 
28  Vector2D nodeVec = wme->GetAngle(loc);
29 
30  // if no angle stored, pick random
31  if (nodeVec.x == 0 && nodeVec.y == 0)
32  {
33  a = directions[random()%directions.size()];
34  return true;
35  }
36 
37  double bestDotProd = -1;
38  double secondBestValue = -1;
39  tDirection bestDir = kStay;
40  tDirection secondBest = kStay;
41 
42  //std::vector<double> dotProds;
43 
44  for (unsigned int i=0; i<directions.size(); i++)
45  {
46  xyLoc nextThisDir;
47  wme->GetNextState(loc,directions[i],nextThisDir);
48  //if (prevLoc == nextThisDir)
49  // continue;
50  if (theEnv->GetMap()->CanStep(loc.x, loc.y, nextThisDir.x, nextThisDir.y))
51  {
52  if (theEnv->GetOccupancyInfo() && theEnv->GetOccupancyInfo()->GetStateOccupied(nextThisDir))
53  continue;
54  Vector2D dirVec = wme->GetAngleFromDirection(directions[i]);
55  Vector2D nextVec = wme->GetAngle(nextThisDir);
56 
57  double dotProd1 = (nodeVec.x * dirVec.x) + (nodeVec.y * dirVec.y);
58  double dotProd2 = (dirVec.x * nextVec.x) + (dirVec.y * nextVec.y);
59 
60  double dotProd = 0.5 * dotProd1 + 0.5 * dotProd2;
61 
62  if (nextVec.x == 0 && nextVec.y == 0)
63  dotProd = -1;
64 
65  //dotProds.push_back(dotProd);
66 
67  if (dotProd >= bestDotProd)
68  {
69  //if (abs(bestDotProd-dotProd) < 0.1)
70  secondBest = bestDir;
71  secondBestValue = bestDotProd;
72  bestDotProd = dotProd;
73 
74  bestDir = directions[i];
75  }
76 
77 
78  }
79  }
80 
81  // find all dot products within epsilon of best, then randomly choose one of them.
82 
83 
84 // xyLoc next;
85 // wme->GetNextState(loc, bestDir, next);
86 //
87 // //std::cout<<"loc "<<loc<<" next "<<next<<std::endl;
88 // if (!(env->GetMap()->CanStep(loc.x, loc.y, next.x, next.y))) // make best possible move
89 // {
90 //
91 // //std::cout<<"can't go here - random\n";
92 // a = directions[random()%directions.size()];
93 // }
94 // else
95 
96 
97  if (secondBest == kStay)
98  a = bestDir;
99  else
100  {
101  if ((bestDotProd - secondBestValue < 0.1) && (random()%4 == 1))
102  a = secondBest;
103  else
104  a = bestDir;
105  }
106 // else
107 // a = directions[random()%directions.size()];
108 
109  return true;
110  }
111 
112 
113  virtual void UpdateLocation(environment *, xyLoc &newloc, bool success, SimulationInfo<xyLoc,tDirection,environment> *)
114  {
115  if (success)
116  {
117  prevLoc = loc;
118  loc = newloc;
119  }
120  }
121 
122  virtual void GetLocation(xyLoc &l)
123  { l = loc; }
124 
125  virtual void OpenGLDraw(const environment *theEnv, const SimulationInfo<xyLoc,tDirection,environment> *) const
126  { theEnv->OpenGLDraw(loc); }
127 
128  virtual void GetGoal(xyLoc &s)
129  { s = loc; }
130 
132 
133  private:
136 };
WeightedMap2DEnvironment.h
A 2D map environment with edge costs weighted according to the number of times a unit has passed over...
GreedyDMUnit::GetLocation
virtual void GetLocation(xyLoc &l)
Definition: GreedyDMUnit.h:122
WeightedMap2DEnvironment::GetAngleFromDirection
Vector2D GetAngleFromDirection(tDirection dir) const
Definition: WeightedMap2DEnvironment.cpp:725
loc::x
int x
Definition: MapGenerators.cpp:296
Vector2D
Definition: WeightedMap2DEnvironment.h:40
kSE
@ kSE
Definition: Map2DEnvironment.h:79
SimulationInfo
Definition: SimulationInfo.h:13
Vector2D::x
float x
Definition: WeightedMap2DEnvironment.h:78
GreedyDMUnit::MakeMove
virtual bool MakeMove(environment *theEnv, OccupancyInterface< xyLoc, tDirection > *, SimulationInfo< xyLoc, tDirection, environment > *, tDirection &a)
Definition: GreedyDMUnit.h:13
loc::y
int y
Definition: MapGenerators.cpp:296
xyLoc::y
uint16_t y
Definition: Map2DEnvironment.h:42
kW
@ kW
Definition: Map2DEnvironment.h:78
GreedyDMUnit::prevLoc
xyLoc prevLoc
Definition: GreedyDMUnit.h:134
GreedyDMUnit
Definition: GreedyDMUnit.h:5
xyLoc
Definition: Map2DEnvironment.h:37
WeightedMap2DEnvironment::GetAngle
Vector2D GetAngle(xyLoc &l)
Definition: WeightedMap2DEnvironment.cpp:627
xyLoc::x
uint16_t x
Definition: Map2DEnvironment.h:41
kStay
@ kStay
Definition: Map2DEnvironment.h:79
WeightedMap2DEnvironment
Definition: WeightedMap2DEnvironment.h:120
loc
Definition: MapGenerators.cpp:296
kE
@ kE
Definition: Map2DEnvironment.h:78
tDirection
tDirection
Definition: Map2DEnvironment.h:77
GreedyDMUnit::~GreedyDMUnit
~GreedyDMUnit()
Definition: GreedyDMUnit.h:9
Vector2D::y
float y
Definition: WeightedMap2DEnvironment.h:78
kN
@ kN
Definition: Map2DEnvironment.h:78
GreedyDMUnit::UpdateLocation
virtual void UpdateLocation(environment *, xyLoc &newloc, bool success, SimulationInfo< xyLoc, tDirection, environment > *)
Definition: GreedyDMUnit.h:113
GreedyDMUnit::GetName
virtual const char * GetName()
Definition: GreedyDMUnit.h:11
GreedyDMUnit::GreedyDMUnit
GreedyDMUnit(xyLoc &startLoc)
Definition: GreedyDMUnit.h:8
GreedyDMUnit::GetGoal
virtual void GetGoal(xyLoc &s)
Definition: GreedyDMUnit.h:128
kS
@ kS
Definition: Map2DEnvironment.h:78
GreedyDMUnit::SetEnvironment
void SetEnvironment(WeightedMap2DEnvironment *w)
Definition: GreedyDMUnit.h:131
GreedyDMUnit::wme
WeightedMap2DEnvironment * wme
Definition: GreedyDMUnit.h:135
kNE
@ kNE
Definition: Map2DEnvironment.h:78
GreedyDMUnit::loc
xyLoc loc
Definition: GreedyDMUnit.h:134
kSW
@ kSW
Definition: Map2DEnvironment.h:79
GreedyDMUnit::OpenGLDraw
virtual void OpenGLDraw(const environment *theEnv, const SimulationInfo< xyLoc, tDirection, environment > *) const
Definition: GreedyDMUnit.h:125
Unit.h
Unit
Definition: Unit.h:23
kNW
@ kNW
Definition: Map2DEnvironment.h:78
OccupancyInterface< xyLoc, tDirection >