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 /****************************************************************************/
17 // A container for routes
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <fstream>
27 #include <cassert>
28 #include "RODFRouteDesc.h"
29 #include "RODFRouteCont.h"
30 #include "RODFNet.h"
31 #include <router/ROEdge.h>
32 #include <utils/common/ToString.h>
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
40 
41 
43 }
44 
45 
46 void
48  // routes may be duplicate as in-between routes may have different starting points
49  if (find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc)) == myRoutes.end()) {
50  // compute route id
51  setID(desc);
52  myRoutes.push_back(desc);
53  } else {
54  RODFRouteDesc& prev = *find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
55  prev.overallProb += desc.overallProb;
56  }
57 }
58 
59 
60 bool
62  std::vector<RODFRouteDesc>::const_iterator j = find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
63  if (j == myRoutes.end()) {
64  return false;
65  }
66  return true;
67 }
68 
69 
70 bool
71 RODFRouteCont::save(std::vector<std::string>& saved,
72  const std::string& prependix, OutputDevice& out) {
73  bool haveSavedOneAtLeast = false;
74  for (std::vector<RODFRouteDesc>::const_iterator j = myRoutes.begin(); j != myRoutes.end(); ++j) {
75  const RODFRouteDesc& desc = (*j);
76  if (find(saved.begin(), saved.end(), desc.routename) != saved.end()) {
77  continue;
78  }
79  saved.push_back((*j).routename);
80  assert(desc.edges2Pass.size() >= 1);
81  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, prependix + desc.routename);
82  out << " edges=\"";
83  for (ROEdgeVector::const_iterator k = desc.edges2Pass.begin(); k != desc.edges2Pass.end(); k++) {
84  if (k != desc.edges2Pass.begin()) {
85  out << ' ';
86  }
87  out << (*k)->getID();
88  }
89  out << '"';
90  out.closeTag();
91  haveSavedOneAtLeast = true;
92  }
93  return haveSavedOneAtLeast;
94 }
95 
96 
97 void
99  sort(myRoutes.begin(), myRoutes.end(), by_distance_sorter());
100 }
101 
102 
103 void
104 RODFRouteCont::removeIllegal(const std::vector<ROEdgeVector >& illegals) {
105  for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end();) {
106  RODFRouteDesc& desc = *i;
107  bool remove = false;
108  for (std::vector<ROEdgeVector >::const_iterator j = illegals.begin(); !remove && j != illegals.end(); ++j) {
109  int noFound = 0;
110  for (ROEdgeVector::const_iterator k = (*j).begin(); !remove && k != (*j).end(); ++k) {
111  if (find(desc.edges2Pass.begin(), desc.edges2Pass.end(), *k) != desc.edges2Pass.end()) {
112  noFound++;
113  if (noFound > 1) {
114  remove = true;
115  }
116  }
117  }
118  }
119  if (remove) {
120  i = myRoutes.erase(i);
121  } else {
122  ++i;
123  }
124  }
125 }
126 
127 
128 void
130  std::pair<ROEdge*, ROEdge*> c(desc.edges2Pass[0], desc.edges2Pass.back());
131  desc.routename = c.first->getID() + "_to_" + c.second->getID();
132  if (myConnectionOccurences.find(c) == myConnectionOccurences.end()) {
133  myConnectionOccurences[c] = 0;
134  } else {
136  desc.routename = desc.routename + "_" + toString(myConnectionOccurences[c]);
137  }
138 }
139 
140 
141 
142 /****************************************************************************/
143 
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:256
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:49
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:48
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:60
A route within the DFROUTER.
Definition: RODFRouteDesc.h:47
RODFRouteCont()
Constructor.
std::string routename
The name of the route.
Definition: RODFRouteDesc.h:51
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
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.