RODFRouteCont.cpp

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

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6