OverSim
hotspotRoaming.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include "hotspotRoaming.h"
25 #include "StringConvert.h"
26 
27 hotspotRoaming::hotspotRoaming(double areaDimension, double speed, NeighborMap *Neighbors, GlobalCoordinator* coordinator, CollisionList* CollisionRect)
28  :MovementGenerator(areaDimension, speed, Neighbors, coordinator, CollisionRect)
29 {
30  double prob = 0;
31 
32  std::vector<std::string> hotspotvec = cStringTokenizer(coordinator->par("Hotspots"), ";").asVector();
33  for( std::vector<std::string>::iterator it = hotspotvec.begin(); it != hotspotvec.end(); ++it ){
34  std::vector<std::string> hstr = cStringTokenizer(it->c_str(), ",").asVector();
35  if( hstr.size() != 4 ) {
36  throw( cException("Error parsing Hotspots parameter") );
37  }
38  // parse string, convert to hotspot data
39  Hotspot h;
40  h.center.x = convertString<double>( hstr[0] );
41  h.center.y = convertString<double>( hstr[1] );
42  h.radius = convertString<double>( hstr[2] );
43  h.probability = convertString<double>( hstr[3] );
44  prob += h.probability;
45 
46  // check hotspot bounds
47  if( h.center.x - h.radius < 0 || h.center.y - h.radius < 0 ||
48  h.center.x + h.radius > areaDimension || h.center.y + h.radius > areaDimension ) {
49 
50  throw( cException("Error: Hotspot is outside the playground!") );
51  }
52  if( prob > 1 ){
53  throw( cException("Error: Hotspot probabilities add up to > 1!") );
54  }
55 
56  hotspots.push_back(h);
57  }
58  curHotspot = hotspots.end();
59  if( (double) coordinator->par("HotspotStayTime") == (double) 0.0 ) {
60  stayInHotspot = false;
61  } else {
62  stayInHotspot = true;
63  }
64  target.x = uniform(0.0, areaDimension);
65  target.y = uniform(0.0, areaDimension);
66 }
67 
69 {
70  double minDist=areaDimension;
71  for( std::vector<Hotspot>::iterator it = hotspots.begin(); it != hotspots.end(); ++it) {
72  double dist = sqrt(position.distanceSqr( it->center )) - it->radius;
73  if( dist < minDist ) minDist = dist;
74  }
75 
76  return minDist;
77 }
78 
80 {
81  flock();
83  if(testBounds()) {
84  position += direction * speed * 2;
85  testBounds();
86  }
87 
88  if(target.distanceSqr(position) < speed * speed) {
89  // arrived at current destination
90 
91  // if we are not inside a hotspot, or do not want to
92  // stay inside the current hotspot (any more) ...
93  if ( !stayInHotspot || curHotspot == hotspots.end() ||
94  ( hotspotStayTime > 0 && hotspotStayTime < simTime() ))
95  {
96  hotspotStayTime = 0;
97 
98  // ... select next target hotspot
99  double rnd = uniform(0, 1);
100  for( curHotspot = hotspots.begin(); curHotspot != hotspots.end(); ++curHotspot ){
101  rnd -= curHotspot->probability;
102  if( rnd <= 0 ) break;
103  }
104 
105  } else {
106  // stay in current hotspot
107  // start stayTimer if not already done
108  if ( hotspotStayTime == 0 ) {
109  hotspotStayTime = simTime() + coordinator->par("HotspotStayTime");
110  }
111  }
112 
113  // chose target inside hotspot, or random target if no hotspot was selected
114  if( curHotspot != hotspots.end() ){
115  Vector2D dev;
116  // randomly select point inside the hotspot
117  double r = uniform( 0, 1 );
118  double theta = uniform( 0, 2*M_PI );
119  dev.x = sqrt( r ) * cos( theta );
120  dev.y = sqrt( r ) * sin( theta );
121 
122  target = curHotspot->center + dev*curHotspot->radius;
123  } else {
124  target.x = uniform(0.0, areaDimension);
125  target.y = uniform(0.0, areaDimension);
126  }
127  }
128 }