OverSim
Vivaldi.cc
Go to the documentation of this file.
1 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
25 #include <cfloat>
26 
27 #include <NeighborCache.h>
28 
29 #include "Vivaldi.h"
30 
31 
32 void Vivaldi::init(NeighborCache* neighborCache)
33 {
34  this->neighborCache = neighborCache;
35 
36  errorC = neighborCache->par("vivaldiErrorConst");
37  coordC = neighborCache->par("vivaldiCoordConst");
38  dimension = neighborCache->par("vivaldiDimConst");
39  enableHeightVector = neighborCache->par("vivaldiEnableHeightVector");
40  showPosition = neighborCache->par("vivaldiShowPosition");
41 
42  // init variables
45 
46  for (uint32_t i = 0; i < dimension; i++) {
47  //ownCoords->setCoords(i, uniform(-.2, .2));
48  ownCoords->setCoords(i, uniform(-200, 200));
49  }
51 
52  WATCH(*ownCoords);
53 
55 };
56 
57 void Vivaldi::processCoordinates(const simtime_t& rtt,
58  const AbstractNcsNodeInfo& nodeInfo)
59 {
60  if (rtt <= 0.0) {
61  std::cout << "Vivaldi::processCoordinates() called with rtt = "
62  << rtt << std::endl;
63  return;
64  }
65 
66  //double dblrtt = SIMTIME_DBL(rtt);// * 1000; // s -> ms
67 
68  if (!dynamic_cast<const VivaldiCoordsInfo*>(&nodeInfo)) {
69  throw cRuntimeError("Vivaldi coords needed!");
70  }
71  const VivaldiCoordsInfo& info =
72  *(static_cast<const VivaldiCoordsInfo*>(&nodeInfo));
73 
74  // calculate weight
75  double weight = (((ownCoords->getError() + info.getError()) == 0) ? 0 :
76  (ownCoords->getError() / (ownCoords->getError() + info.getError())));
77 
78  // calculate distance
79  double dist = ownCoords->getDistance(info).proximity;
80 
81  // ... own error
82  ownCoords->setError(calcError(rtt, dist, weight));
83 
84  // delta
85  double delta = calcDelta(rtt, dist, weight);
86 
87  // update local coordinates
88  if (dist > 0) {
89  for (uint8_t i = 0; i < dimension; i++) {
90  // old calculation based on s not ms
91  //ownCoords->setCoords(i, ownCoords->getCoords(i) +
92  // (delta * (SIMTIME_DBL(rtt) - dist)) *
93  // ((ownCoords->getCoords(i) - info.getCoords(i)) /
94  // dist));
95  ownCoords->setCoords(i, ((ownCoords->getCoords(i) / 1000) +
96  (delta * (SIMTIME_DBL(rtt) - dist)) *
97  ((ownCoords->getCoords(i) - info.getCoords(i)) /
98  (dist * 1000))) * 1000);
99  }
100  if(enableHeightVector) {
102  (delta * (SIMTIME_DBL(rtt) - dist)));
103  }
104  }
105 
106  updateDisplay();
107 }
108 
109 
110 double Vivaldi::calcError(const simtime_t& rtt, double dist, double weight)
111 {
112  double relErr = 0;
113  if (rtt != 0) {
114  //eSample computes the relative error for this sample
115  relErr = fabs(dist - rtt) / rtt;
116  }
117  // update weighted moving average of local error
118  return (relErr * errorC * weight) +
119  ownCoords->getError() * (1 - errorC * weight);
120 }
121 
122 
123 double Vivaldi::calcDelta(const simtime_t& rtt, double dist, double weight)
124 {
125  // estimates the delta factor
126  return coordC * weight;
127 }
128 
129 
131 {
132  return ownCoords->getDistance(abstractInfo);
133 }
134 
135 
136 AbstractNcsNodeInfo* Vivaldi::createNcsInfo(const std::vector<double>& coords) const
137 {
138  assert(coords.size() > 1);
139  VivaldiCoordsInfo* info = new VivaldiCoordsInfo();
140 
141  uint8_t i;
142  for (i = 0; i < coords.size() - (enableHeightVector ? 2 : 1); ++i) {
143  info->setCoords(i, coords[i]);
144  }
145  info->setError(coords[i++]);
146 
147  if (enableHeightVector) {
148  info->setHeightVector(coords[i]);
149  }
150 
151  return info;
152 }
153 
154 
156 {
157  char buf[60];
158  sprintf(buf, "xi[0]: %f xi[1]: %f ", ownCoords->getCoords(0),
159  ownCoords->getCoords(1));
160  neighborCache->getDisplayString().setTagArg("t", 0, buf);
161 
162  // show nodes at estimated position TODO
163  if (showPosition) {
164  for (uint32_t i = 0; i < dimension; i++)
165  neighborCache->getParentModule()
166  ->getDisplayString().setTagArg("p", i,
167  ownCoords->getCoords(i)/* * 1000*/);
168  }
169 }
170 
172 {
173  globalStatistics->addStdDev("Vivaldi: Errori(ei)", ownCoords->getError());
174 }