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