ConnectivityProbePR.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include "ConnectivityProbePR.h"
00025 
00026 Define_Module(ConnectivityProbePR);
00027 
00028 PRTopologyNode::PRTopologyNode(int moduleID)
00029 {
00030     this->moduleID = moduleID;
00031     visited = false;
00032 }
00033 
00034 QuonPR* PRTopologyNode::getModule() const
00035 {
00036     return check_and_cast<QuonPR*>(simulation.getModule(moduleID));
00037 }
00038 
00039 void ConnectivityProbePR::initialize()
00040 {
00041     globalStatistics = GlobalStatisticsAccess().get();
00042     probeIntervall = par("connectivityProbeIntervall");
00043     plotIntervall = par("visualizeNetworkIntervall");
00044     probeTimer = new cMessage("probeTimer");
00045     plotTimer = new cMessage("plotTimer");
00046     plotConnections = par("plotConnections");
00047 
00048     if(probeIntervall > 0.0f) {
00049         scheduleAt(simTime() + probeIntervall, probeTimer);
00050 
00051         cOV_NodeCount.setName("total node count");
00052         cOV_MaximumComponent.setName("largest connected component");
00053         cOV_MaxConnectivity.setName("connectivity in percent");
00054         cOV_ZeroMissingNeighbors.setName("neighbor-error free nodes");
00055         cOV_AverageMissingNeighbors.setName("average missing neighbors per node");
00056         cOV_MaxMissingNeighbors.setName("largest missing neighbors count");
00057         cOV_AverageDrift.setName("average drift");
00058     }
00059 
00060     if(plotIntervall > 0.0) {
00061         scheduleAt(simTime() + plotIntervall, plotTimer);
00062     }
00063 }
00064 
00065 void ConnectivityProbePR::handleMessage(cMessage* msg)
00066 {
00067     // fill topology with all QUONPR modules
00068     extractTopology();
00069 
00070     if(Topology.size() == 0) {
00071         return;
00072     }
00073 
00074     // catch self timer messages
00075     if(msg->isName("probeTimer")) {
00076         //reset timer
00077         cancelEvent(probeTimer);
00078         scheduleAt(simTime() + probeIntervall, msg);
00079 
00080         unsigned int maxComponent = 0;
00081         for(PRTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00082             unsigned int count = getComponentSize(itTopology->second.getModule()->getKey());
00083             if(count > maxComponent) {
00084                 maxComponent = count;
00085             }
00086             if(count == Topology.size()) {
00087                 break;
00088             }
00089         }
00090 
00091         cOV_NodeCount.record((double)Topology.size());
00092         cOV_MaximumComponent.record((double)maxComponent);
00093         cOV_MaxConnectivity.record((double)maxComponent * 100.0 / (double)Topology.size());
00094         RECORD_STATS (
00095             globalStatistics->addStdDev("ConnectivityProbe: max connectivity", (double)maxComponent * 100.0 / (double)Topology.size());
00096         );
00097 
00098         int mnMax = 0;
00099         int mnZero = 0;
00100         int driftCount = 0;
00101         double mnAverage = 0.0;
00102         double drift = 0.0;
00103 
00104         for(PRTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00105             QPRBoundingBox AOI(itTopology->second.getModule()->getPosition(), itTopology->second.getModule()->getAOI());
00106             int missing = 0;
00107             for(PRTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
00108                 if(itI != itTopology && AOI.collide(itI->second.getModule()->getPosition())) {
00109                     PRSiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getKey());
00110                     if(currentSite == itTopology->second.getModule()->Sites.end()) {
00111                         ++missing;
00112                     }
00113                     else {
00114                         drift += sqrt(currentSite->second->position.distanceSqr(itI->second.getModule()->getPosition()));
00115                         ++driftCount;
00116                     }
00117                 }
00118             }
00119 
00120             mnAverage += missing;
00121             if(mnMax < missing) {
00122                 mnMax = missing;
00123             }
00124             if(missing == 0) {
00125                 ++mnZero;
00126             }
00127         }
00128         mnAverage /= (double)Topology.size();
00129         if(driftCount > 0) {
00130             drift /= (double)driftCount;
00131         }
00132 
00133         cOV_ZeroMissingNeighbors.record((double)mnZero);
00134         RECORD_STATS (
00135             globalStatistics->addStdDev("ConnectivityProbe: percentage zero missing neighbors", (double)mnZero * 100.0 / (double)Topology.size());
00136         );
00137         cOV_AverageMissingNeighbors.record(mnAverage);
00138         cOV_MaxMissingNeighbors.record((double)mnMax);
00139         cOV_AverageDrift.record(drift);
00140     }
00141     else if(msg->isName("plotTimer")) {
00142         //reset timer
00143         cancelEvent(plotTimer);
00144         scheduleAt(simTime() + plotIntervall, msg);
00145 
00146         int range = (int)Topology.begin()->second.getModule()->getAreaDimension();
00147         std::stringstream oss;
00148         std::string filename;
00149         int simTimeInt, stellen = 1;
00150         simTimeInt = (int)SIMTIME_DBL(simTime());
00151         oss << "plot";
00152         for(int i=0; i<6; i++) {
00153             if(!(simTimeInt / stellen)) {
00154                 oss << "0";
00155             }
00156             stellen *= 10;
00157         }
00158         oss << simTimeInt;
00159 
00160         // open / write plot file
00161         filename = oss.str() + ".plot";
00162         pltNetwork.open(filename.c_str(), std::ios::out);
00163         pltNetwork << "set xrange [0:" << range << "]" << endl;
00164         pltNetwork << "set yrange [0:" << range << "]" << endl;
00165         pltNetwork << "set nokey" << endl;
00166 
00167         // open point file
00168         filename = oss.str() + ".point";
00169         pltData.open(filename.c_str(), std::ios::out);
00170 
00171         pltNetwork << "plot '" << filename << "' using 1:2 with points pointtype 7,\\" << endl;
00172 
00173         // open vector file
00174         filename = oss.str() + ".arrow";
00175         pltVector.open(filename.c_str(), std::ios::out);
00176 
00177         pltNetwork << "     '" << filename << "' using 1:2:3:4 with vectors linetype 1" << endl;
00178         pltNetwork.close();
00179 
00180         // write point data file
00181         for(PRTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00182             pltData << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << endl;
00183         }
00184         pltData.close();
00185 
00186         //write arrow data file
00187         for(PRTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00188             for(PRSiteMap::iterator itSites = itTopology->second.getModule()->Sites.begin(); itSites != itTopology->second.getModule()->Sites.end(); ++itSites) {
00189                 if(plotConnections) {
00190                     PRTopology::iterator destNode = Topology.find(itSites->second->address.key);
00191                     if(destNode != Topology.end()) {
00192                         Vector2D relPos = destNode->second.getModule()->getPosition() - itTopology->second.getModule()->getPosition();
00193                         pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
00194                                   << relPos.x << "\t" << relPos.y << endl;
00195                     }
00196                 }
00197                 else {
00198                     Vector2D relPos = itSites->second->position - itTopology->second.getModule()->getPosition();
00199                     pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
00200                               << relPos.x << "\t" << relPos.y << endl;
00201                 }
00202             }
00203         }
00204         pltVector.close();
00205     }
00206     Topology.clear();
00207 }
00208 
00209 void ConnectivityProbePR::extractTopology()
00210 {
00211     for(int i=0; i<=simulation.getLastModuleId(); i++) {
00212         cModule* module = simulation.getModule(i);
00213         if(module && dynamic_cast<QuonPR*>(module)) {
00214             QuonPR* quonpr = check_and_cast<QuonPR*>(module);
00215             if(quonpr->getState() == QREADY) {
00216                 PRTopologyNode temp(i);
00217                 Topology.insert(std::make_pair(quonpr->getKey(), temp));
00218             }
00219         }
00220     }
00221 }
00222 
00223 void ConnectivityProbePR::resetTopologyNodes()
00224 {
00225     for(PRTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00226         itTopology->second.visited = false;
00227     }
00228 }
00229 
00230 unsigned int ConnectivityProbePR::getComponentSize(OverlayKey key)
00231 {
00232     PRTopology::iterator itEntry = Topology.find(key);
00233     if(itEntry != Topology.end() && itEntry->second.visited == false) {
00234         int count = 1;
00235         itEntry->second.visited = true;
00236         QuonPR* quonpr = itEntry->second.getModule();
00237         for(PRSiteMap::iterator itSites = quonpr->Sites.begin(); itSites != quonpr->Sites.end(); ++itSites) {
00238             count += getComponentSize(itSites->first);
00239         }
00240         return count;
00241     }
00242     return 0;
00243 }
00244 
00245 ConnectivityProbePR::~ConnectivityProbePR()
00246 {
00247     // destroy self timer messages
00248     cancelAndDelete(probeTimer);
00249     cancelAndDelete(plotTimer);
00250 }

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