SUMO - Simulation of Urban MObility
RODFRouteCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A container for routes
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <fstream>
33 #include <cassert>
34 #include "RODFRouteDesc.h"
35 #include "RODFRouteCont.h"
36 #include "RODFNet.h"
37 #include <router/ROEdge.h>
38 #include <utils/common/ToString.h>
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46 
47 
49 }
50 
51 
52 void
54  // routes may be duplicate as in-between routes may have different starting points
55  if (find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc)) == myRoutes.end()) {
56  // compute route id
57  setID(desc);
58  myRoutes.push_back(desc);
59  } else {
60  RODFRouteDesc& prev = *find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
61  prev.overallProb += desc.overallProb;
62  }
63 }
64 
65 
66 bool
68  std::vector<RODFRouteDesc>::const_iterator j = find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
69  if (j == myRoutes.end()) {
70  return false;
71  }
72  return true;
73 }
74 
75 
76 bool
77 RODFRouteCont::save(std::vector<std::string>& saved,
78  const std::string& prependix, OutputDevice& out) {
79  bool haveSavedOneAtLeast = false;
80  for (std::vector<RODFRouteDesc>::const_iterator j = myRoutes.begin(); j != myRoutes.end(); ++j) {
81  const RODFRouteDesc& desc = (*j);
82  if (find(saved.begin(), saved.end(), desc.routename) != saved.end()) {
83  continue;
84  }
85  saved.push_back((*j).routename);
86  assert(desc.edges2Pass.size() >= 1);
87  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, prependix + desc.routename);
88  out << " edges=\"";
89  for (ROEdgeVector::const_iterator k = desc.edges2Pass.begin(); k != desc.edges2Pass.end(); k++) {
90  if (k != desc.edges2Pass.begin()) {
91  out << ' ';
92  }
93  out << (*k)->getID();
94  }
95  out << '"';
96  out.closeTag();
97  haveSavedOneAtLeast = true;
98  }
99  return haveSavedOneAtLeast;
100 }
101 
102 
103 void
105  sort(myRoutes.begin(), myRoutes.end(), by_distance_sorter());
106 }
107 
108 
109 void
110 RODFRouteCont::removeIllegal(const std::vector<ROEdgeVector >& illegals) {
111  for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end();) {
112  RODFRouteDesc& desc = *i;
113  bool remove = false;
114  for (std::vector<ROEdgeVector >::const_iterator j = illegals.begin(); !remove && j != illegals.end(); ++j) {
115  int noFound = 0;
116  for (ROEdgeVector::const_iterator k = (*j).begin(); !remove && k != (*j).end(); ++k) {
117  if (find(desc.edges2Pass.begin(), desc.edges2Pass.end(), *k) != desc.edges2Pass.end()) {
118  noFound++;
119  if (noFound > 1) {
120  remove = true;
121  }
122  }
123  }
124  }
125  if (remove) {
126  i = myRoutes.erase(i);
127  } else {
128  ++i;
129  }
130  }
131 }
132 
133 
134 void
136  std::pair<ROEdge*, ROEdge*> c(desc.edges2Pass[0], desc.edges2Pass.back());
137  desc.routename = c.first->getID() + "_to_" + c.second->getID();
138  if (myConnectionOccurences.find(c) == myConnectionOccurences.end()) {
139  myConnectionOccurences[c] = 0;
140  } else {
142  desc.routename = desc.routename + "_" + toString(myConnectionOccurences[c]);
143  }
144 }
145 
146 
147 
148 /****************************************************************************/
149 
void removeIllegal(const std::vector< ROEdgeVector > &illegals)
Removes "illegal" routes.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
std::vector< RODFRouteDesc > myRoutes
Stored route descriptions.
A class for sorting route descriptions by their length.
~RODFRouteCont()
Destructor.
ROEdgeVector edges2Pass
The edges the route is made of.
Definition: RODFRouteDesc.h:55
begin/end of the description of a route
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.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
void sortByDistance()
Sorts routes by their distance (length)
std::map< std::pair< ROEdge *, ROEdge * >, int > myConnectionOccurences
Counts how many routes connecting the key-edges were already stored.
double overallProb
Definition: RODFRouteDesc.h:66
A route within the DFROUTER.
Definition: RODFRouteDesc.h:53
RODFRouteCont()
Constructor.
std::string routename
The name of the route.
Definition: RODFRouteDesc.h:57
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.
void setID(RODFRouteDesc &desc) const
Computes and sets the id of a route.
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.