Vivaldi.cc

Go to the documentation of this file.
00001 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016 //
00017 
00025 #include <cfloat>
00026 
00027 #include <NeighborCache.h>
00028 
00029 #include "Vivaldi.h"
00030 
00031 
00032 void Vivaldi::initVivaldi(NeighborCache* neighborCache)
00033 {
00034     this->neighborCache = neighborCache;
00035 
00036     useSVivaldi = neighborCache->par("useSVivaldi");
00037     errorC = neighborCache->par("vivaldiErrorConst");
00038     coordC = neighborCache->par("vivaldiCoordConst");
00039     dimension = neighborCache->par("vivaldiDimConst");
00040     lossC = neighborCache->par("vivaldiLossConst");
00041     effectiveSample = neighborCache->par("vivaldiEffectiveSample");
00042     showVivaldiPosition = neighborCache->par("showVivaldiPosition");
00043 
00044     // init variables
00045     xi.resize(dimension);
00046     errori = 1.0;
00047     loss = 0;
00048 
00049     for (uint32_t i = 0; i < dimension; i++) {
00050         xi[i] = uniform(-.2, .2); //TODO
00051     }
00052 
00053     WATCH(errori);
00054     WATCH_VECTOR(xi);
00055     WATCH(loss);
00056 
00057     globalStatistics = GlobalStatisticsAccess().get();
00058 };
00059 
00060 void Vivaldi::processCoordinates(simtime_t rtt,
00061                                  const std::vector<double>& xj,
00062                                  double errorj)
00063 {
00064     //assert(rtt > 0.0);
00065     if (rtt <= 0.0) return;
00066 
00067     // init variables
00068     double dist = 0, sum = 0, delta = 0;
00069     double weight = (((errori + errorj) == 0) ?
00070                      0 : (errori / (errori + errorj)));
00071 
00072     // calculate distance
00073     if (rtt != 0) {
00074         for (uint32_t i = 0; i < dimension; i++) {
00075             dist += pow(xi[i] - xj[i], 2);
00076         }
00077         dist = sqrt(dist);
00078     }
00079 
00080     // choose Vivaldi version
00081     if (useSVivaldi) {
00082         // get avg absolute prediction error
00083         sum = neighborCache->getAvgAbsPredictionError(xi, effectiveSample);
00084 
00085         // update weighted moving average of local error
00086         errori = (sum * errorC) + errori * (1 - errorC);
00087 
00088         // calculate loss factor
00089         loss = lossC + (1 - lossC) * loss;
00090         delta = coordC * weight * (1 - loss);
00091     } else {
00092         double relErr = 0;
00093         if (rtt != 0) {
00094             //eSample computes the relative error for this sample
00095             relErr = fabs(dist - rtt) / rtt;
00096         }
00097         // update weighted moving average of local error
00098         errori = (relErr * errorC * weight) + errori * (1 - errorC * weight);
00099         // estimates the delta factor
00100         delta = coordC * weight;
00101     }
00102 
00103     // update local coordinates
00104     if (dist > 0) {
00105         for (uint32_t i = 0; i < dimension; i++) {
00106             xi[i] += (delta * (SIMTIME_DBL(rtt) - dist)) * ((xi[i] - xj[i]) / dist);
00107         }
00108     }
00109     updateDisplay();
00110 }
00111 
00112 void Vivaldi::updateDisplay()
00113 {
00114     char buf[60];
00115     sprintf(buf, "xi[0]: %f xi[1]: %f ", xi[0], xi[1]);
00116     neighborCache->getDisplayString().setTagArg("t", 0, buf);
00117 
00118     // show nodes at estimated position TODO
00119     if (showVivaldiPosition) {
00120         for (uint32_t i = 0; i < dimension; i++)
00121             neighborCache->getParentModule()
00122                 ->getDisplayString().setTagArg("p", i, xi[i] * 1000);
00123     }
00124 }
00125 
00126 void Vivaldi::finishVivaldi()
00127 {
00128     globalStatistics->addStdDev("Vivaldi: Errori(ei)",errori);
00129 }

Generated on Wed Mar 25 16:32:50 2009 for OverSim by  doxygen 1.5.5