Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A container for routes
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <fstream>
26 #include <cassert>
27 #include "RODFRouteDesc.h"
28 #include "RODFRouteCont.h"
29 #include "RODFNet.h"
30 #include <router/ROEdge.h>
31 #include <utils/common/ToString.h>
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
39 
40 
42 }
43 
44 
45 void
47  // routes may be duplicate as in-between routes may have different starting points
48  if (find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc)) == myRoutes.end()) {
49  // compute route id
50  setID(desc);
51  myRoutes.push_back(desc);
52  } else {
53  RODFRouteDesc& prev = *find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
54  prev.overallProb += desc.overallProb;
55  }
56 }
57 
58 
59 bool
61  std::vector<RODFRouteDesc>::const_iterator j = find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
62  if (j == myRoutes.end()) {
63  return false;
64  }
65  return true;
66 }
67 
68 
69 bool
70 RODFRouteCont::save(std::vector<std::string>& saved,
71  const std::string& prependix, OutputDevice& out) {
72  bool haveSavedOneAtLeast = false;
73  for (std::vector<RODFRouteDesc>::const_iterator j = myRoutes.begin(); j != myRoutes.end(); ++j) {
74  const RODFRouteDesc& desc = (*j);
75  if (find(saved.begin(), saved.end(), desc.routename) != saved.end()) {
76  continue;
77  }
78  saved.push_back((*j).routename);
79  assert(desc.edges2Pass.size() >= 1);
80  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, prependix + desc.routename);
81  out << " edges=\"";
82  for (ROEdgeVector::const_iterator k = desc.edges2Pass.begin(); k != desc.edges2Pass.end(); k++) {
83  if (k != desc.edges2Pass.begin()) {
84  out << ' ';
85  }
86  out << (*k)->getID();
87  }
88  out << '"';
89  out.closeTag();
90  haveSavedOneAtLeast = true;
91  }
92  return haveSavedOneAtLeast;
93 }
94 
95 
96 void
98  sort(myRoutes.begin(), myRoutes.end(), by_distance_sorter());
99 }
100 
101 
102 void
103 RODFRouteCont::removeIllegal(const std::vector<ROEdgeVector >& illegals) {
104  for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end();) {
105  RODFRouteDesc& desc = *i;
106  bool remove = false;
107  for (std::vector<ROEdgeVector >::const_iterator j = illegals.begin(); !remove && j != illegals.end(); ++j) {
108  int noFound = 0;
109  for (ROEdgeVector::const_iterator k = (*j).begin(); !remove && k != (*j).end(); ++k) {
110  if (find(desc.edges2Pass.begin(), desc.edges2Pass.end(), *k) != desc.edges2Pass.end()) {
111  noFound++;
112  if (noFound > 1) {
113  remove = true;
114  }
115  }
116  }
117  }
118  if (remove) {
119  i = myRoutes.erase(i);
120  } else {
121  ++i;
122  }
123  }
124 }
125 
126 
127 void
129  std::pair<ROEdge*, ROEdge*> c(desc.edges2Pass[0], desc.edges2Pass.back());
130  desc.routename = c.first->getID() + "_to_" + c.second->getID();
131  if (myConnectionOccurences.find(c) == myConnectionOccurences.end()) {
132  myConnectionOccurences[c] = 0;
133  } else {
135  desc.routename = desc.routename + "_" + toString(myConnectionOccurences[c]);
136  }
137 }
138 
139 
140 
141 /****************************************************************************/
142 
ToString.h
RODFRouteCont::myConnectionOccurences
std::map< std::pair< ROEdge *, ROEdge * >, int > myConnectionOccurences
Counts how many routes connecting the key-edges were already stored.
Definition: RODFRouteCont.h:179
RODFRouteCont::myRoutes
std::vector< RODFRouteDesc > myRoutes
Stored route descriptions.
Definition: RODFRouteCont.h:176
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
RODFRouteCont::addRouteDesc
void addRouteDesc(RODFRouteDesc &desc)
Adds a route to the container.
Definition: RODFRouteCont.cpp:46
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:378
RODFRouteCont::removeRouteDesc
bool removeRouteDesc(RODFRouteDesc &desc)
Removes the given route description from the container.
Definition: RODFRouteCont.cpp:60
RODFRouteDesc::overallProb
double overallProb
Definition: RODFRouteDesc.h:59
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
RODFRouteCont::RODFRouteCont
RODFRouteCont()
Constructor.
Definition: RODFRouteCont.cpp:38
RODFRouteCont::route_finder
A class for finding a same route (one that passes the same edges)
Definition: RODFRouteCont.h:153
RODFRouteDesc
A route within the DFROUTER.
Definition: RODFRouteDesc.h:46
RODFRouteCont::by_distance_sorter
A class for sorting route descriptions by their length.
Definition: RODFRouteCont.h:140
RODFRouteCont::~RODFRouteCont
~RODFRouteCont()
Destructor.
Definition: RODFRouteCont.cpp:41
OutputDevice.h
RODFRouteCont::save
bool save(std::vector< std::string > &saved, const std::string &prependix, OutputDevice &out)
Saves routes.
Definition: RODFRouteCont.cpp:70
RODFRouteCont::setID
void setID(RODFRouteDesc &desc) const
Computes and sets the id of a route.
Definition: RODFRouteCont.cpp:128
RODFRouteDesc::routename
std::string routename
The name of the route.
Definition: RODFRouteDesc.h:50
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
RODFRouteDesc::edges2Pass
ROEdgeVector edges2Pass
The edges the route is made of.
Definition: RODFRouteDesc.h:48
RODFRouteDesc.h
SUMO_TAG_ROUTE
begin/end of the description of a route
Definition: SUMOXMLDefinitions.h:125
config.h
RODFNet.h
ROEdge.h
RODFRouteCont.h
RODFRouteCont::removeIllegal
void removeIllegal(const std::vector< ROEdgeVector > &illegals)
Removes "illegal" routes.
Definition: RODFRouteCont.cpp:103
RODFRouteCont::sortByDistance
void sortByDistance()
Sorts routes by their distance (length)
Definition: RODFRouteCont.cpp:97