SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RODFRouteCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A container for routes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <fstream>
34 #include <cassert>
35 #include "RODFRouteDesc.h"
36 #include "RODFRouteCont.h"
37 #include "RODFNet.h"
38 #include <router/ROEdge.h>
39 #include <utils/common/ToString.h>
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51 
52 
54 }
55 
56 
57 void
59  // routes may be duplicate as in-between routes may have different starting points
60  if (find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc)) == myRoutes.end()) {
61  // compute route id
62  setID(desc);
63  myRoutes.push_back(desc);
64  } else {
65  RODFRouteDesc& prev = *find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
66  prev.overallProb += desc.overallProb;
67  }
68 }
69 
70 
71 bool
73  std::vector<RODFRouteDesc>::const_iterator j = find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
74  if (j == myRoutes.end()) {
75  return false;
76  }
77  return true;
78 }
79 
80 
81 bool
82 RODFRouteCont::save(std::vector<std::string>& saved,
83  const std::string& prependix, OutputDevice& out) {
84  bool haveSavedOneAtLeast = false;
85  for (std::vector<RODFRouteDesc>::const_iterator j = myRoutes.begin(); j != myRoutes.end(); ++j) {
86  const RODFRouteDesc& desc = (*j);
87  if (find(saved.begin(), saved.end(), desc.routename) != saved.end()) {
88  continue;
89  }
90  saved.push_back((*j).routename);
91  assert(desc.edges2Pass.size() >= 1);
92  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, prependix + desc.routename);
93  out << " edges=\"";
94  for (std::vector<ROEdge*>::const_iterator k = desc.edges2Pass.begin(); k != desc.edges2Pass.end(); k++) {
95  if (k != desc.edges2Pass.begin()) {
96  out << ' ';
97  }
98  out << (*k)->getID();
99  }
100  out << '"';
101  out.closeTag();
102  haveSavedOneAtLeast = true;
103  }
104  return haveSavedOneAtLeast;
105 }
106 
107 
108 void
110  sort(myRoutes.begin(), myRoutes.end(), by_distance_sorter());
111 }
112 
113 
114 void
115 RODFRouteCont::removeIllegal(const std::vector<std::vector<ROEdge*> >& illegals) {
116  for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end();) {
117  RODFRouteDesc& desc = *i;
118  bool remove = false;
119  for (std::vector<std::vector<ROEdge*> >::const_iterator j = illegals.begin(); !remove && j != illegals.end(); ++j) {
120  int noFound = 0;
121  for (std::vector<ROEdge*>::const_iterator k = (*j).begin(); !remove && k != (*j).end(); ++k) {
122  if (find(desc.edges2Pass.begin(), desc.edges2Pass.end(), *k) != desc.edges2Pass.end()) {
123  noFound++;
124  if (noFound > 1) {
125  remove = true;
126  }
127  }
128  }
129  }
130  if (remove) {
131  i = myRoutes.erase(i);
132  } else {
133  ++i;
134  }
135  }
136 }
137 
138 
139 void
141  std::vector<RODFRouteDesc> newRoutes;
142  for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end(); ++i) {
143  RODFRouteDesc& desc = *i;
144  ROEdge* last = *(desc.edges2Pass.end() - 1);
145  if (last->getNoFollowing() == 0) {
146  newRoutes.push_back(desc);
147  continue;
148  }
149  for (unsigned int j = 0; j < last->getNoFollowing(); ++j) {
150  RODFRouteDesc ndesc(desc);
151  ndesc.edges2Pass.push_back(last->getFollower(j));
152  setID(ndesc);
153  newRoutes.push_back(ndesc);
154  }
155  }
156  myRoutes = newRoutes;
157 }
158 
159 
160 void
162  std::pair<ROEdge*, ROEdge*> c(desc.edges2Pass[0], desc.edges2Pass.back());
163  desc.routename = c.first->getID() + "_to_" + c.second->getID();
164  if (myConnectionOccurences.find(c) == myConnectionOccurences.end()) {
165  myConnectionOccurences[c] = 0;
166  } else {
168  desc.routename = desc.routename + "_" + toString(myConnectionOccurences[c]);
169  }
170 }
171 
172 
173 
174 /****************************************************************************/
175 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
ROEdge * getFollower(unsigned int pos) const
Returns the edge at the given position from the list of reachable edges.
Definition: ROEdge.h:270
std::vector< RODFRouteDesc > myRoutes
Stored route descriptions.
std::vector< ROEdge * > edges2Pass
The edges the route is made of.
Definition: RODFRouteDesc.h:55
A class for sorting route descriptions by their length.
~RODFRouteCont()
Destructor.
A class for finding a same route (one that passes the same edges)
bool removeRouteDesc(RODFRouteDesc &desc)
Removes the given route description from the container.
void setID(RODFRouteDesc &desc) const
Computes and sets the id of a route.
void sortByDistance()
Sorts routes by their distance (length)
SUMOReal overallProb
Definition: RODFRouteDesc.h:66
std::map< std::pair< ROEdge *, ROEdge * >, int > myConnectionOccurences
Counts how many routes connecting the key-edges were already stored.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
A route within the DFROUTER.
Definition: RODFRouteDesc.h:53
A basic edge for routing applications.
Definition: ROEdge.h:67
RODFRouteCont()
Constructor.
void addAllEndFollower()
All routes are replaced by their versions extended by follower edges.
unsigned int getNoFollowing() const
Returns the number of edges this edge is connected to.
Definition: ROEdge.cpp:292
std::string routename
The name of the route.
Definition: RODFRouteDesc.h:57
void removeIllegal(const std::vector< std::vector< ROEdge * > > &illegals)
Removes &quot;illegal&quot; routes.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
void addRouteDesc(RODFRouteDesc &desc)
Adds a route to the container.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool save(std::vector< std::string > &saved, const std::string &prependix, OutputDevice &out)
Saves routes.