SUMO - Simulation of Urban MObility
RODFRouteCont.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // A container for routes
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <fstream>
00034 #include <cassert>
00035 #include "RODFRouteDesc.h"
00036 #include "RODFRouteCont.h"
00037 #include "RODFNet.h"
00038 #include <router/ROEdge.h>
00039 #include <utils/common/ToString.h>
00040 #include <utils/iodevices/OutputDevice.h>
00041 
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045 
00046 
00047 // ===========================================================================
00048 // method definitions
00049 // ===========================================================================
00050 RODFRouteCont::RODFRouteCont() {}
00051 
00052 
00053 RODFRouteCont::~RODFRouteCont() {
00054 }
00055 
00056 
00057 void
00058 RODFRouteCont::addRouteDesc(RODFRouteDesc& desc) {
00059     // routes may be duplicate as in-between routes may have different starting points
00060     if (find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc)) == myRoutes.end()) {
00061         // compute route id
00062         setID(desc);
00063         myRoutes.push_back(desc);
00064     } else {
00065         RODFRouteDesc& prev = *find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
00066         prev.overallProb += desc.overallProb;
00067     }
00068 }
00069 
00070 
00071 bool
00072 RODFRouteCont::removeRouteDesc(RODFRouteDesc& desc) {
00073     std::vector<RODFRouteDesc>::const_iterator j = find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
00074     if (j == myRoutes.end()) {
00075         return false;
00076     }
00077     return true;
00078 }
00079 
00080 
00081 bool
00082 RODFRouteCont::save(std::vector<std::string> &saved,
00083                     const std::string& prependix, OutputDevice& out) {
00084     bool haveSavedOneAtLeast = false;
00085     for (std::vector<RODFRouteDesc>::const_iterator j = myRoutes.begin(); j != myRoutes.end(); ++j) {
00086         const RODFRouteDesc& desc = (*j);
00087         if (find(saved.begin(), saved.end(), desc.routename) != saved.end()) {
00088             continue;
00089         }
00090         saved.push_back((*j).routename);
00091         assert(desc.edges2Pass.size() >= 1);
00092         out << "   <route id=\"" << prependix << desc.routename << "\" edges=\"";
00093         for (std::vector<ROEdge*>::const_iterator k = desc.edges2Pass.begin(); k != desc.edges2Pass.end(); k++) {
00094             if (k != desc.edges2Pass.begin()) {
00095                 out << ' ';
00096             }
00097             out << (*k)->getID();
00098         }
00099         out << "\"/>\n";
00100         haveSavedOneAtLeast = true;
00101     }
00102     return haveSavedOneAtLeast;
00103 }
00104 
00105 
00106 void
00107 RODFRouteCont::sortByDistance() {
00108     sort(myRoutes.begin(), myRoutes.end(), by_distance_sorter());
00109 }
00110 
00111 
00112 void
00113 RODFRouteCont::removeIllegal(const std::vector<std::vector<ROEdge*> > &illegals) {
00114     for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end();) {
00115         RODFRouteDesc& desc = *i;
00116         bool remove = false;
00117         for (std::vector<std::vector<ROEdge*> >::const_iterator j = illegals.begin(); !remove && j != illegals.end(); ++j) {
00118             int noFound = 0;
00119             for (std::vector<ROEdge*>::const_iterator k = (*j).begin(); !remove && k != (*j).end(); ++k) {
00120                 if (find(desc.edges2Pass.begin(), desc.edges2Pass.end(), *k) != desc.edges2Pass.end()) {
00121                     noFound++;
00122                     if (noFound > 1) {
00123                         remove = true;
00124                     }
00125                 }
00126             }
00127         }
00128         if (remove) {
00129             i = myRoutes.erase(i);
00130         } else {
00131             ++i;
00132         }
00133     }
00134 }
00135 
00136 
00137 void
00138 RODFRouteCont::addAllEndFollower() {
00139     std::vector<RODFRouteDesc> newRoutes;
00140     for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end(); ++i) {
00141         RODFRouteDesc& desc = *i;
00142         ROEdge* last = *(desc.edges2Pass.end() - 1);
00143         if (last->getNoFollowing() == 0) {
00144             newRoutes.push_back(desc);
00145             continue;
00146         }
00147         for (unsigned int j = 0; j < last->getNoFollowing(); ++j) {
00148             RODFRouteDesc ndesc(desc);
00149             ndesc.edges2Pass.push_back(last->getFollower(j));
00150             setID(ndesc);
00151             newRoutes.push_back(ndesc);
00152         }
00153     }
00154     myRoutes = newRoutes;
00155 }
00156 
00157 
00158 void
00159 RODFRouteCont::setID(RODFRouteDesc& desc) const {
00160     std::pair<ROEdge*, ROEdge*> c(desc.edges2Pass[0], desc.edges2Pass.back());
00161     desc.routename = c.first->getID() + "_to_" + c.second->getID();
00162     if (myConnectionOccurences.find(c) == myConnectionOccurences.end()) {
00163         myConnectionOccurences[c] = 0;
00164     } else {
00165         myConnectionOccurences[c] = myConnectionOccurences[c] + 1;
00166         desc.routename = desc.routename + "_" + toString(myConnectionOccurences[c]);
00167     }
00168 }
00169 
00170 
00171 
00172 /****************************************************************************/
00173 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines