SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2017 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 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47 
48 
50 }
51 
52 
53 void
55  // routes may be duplicate as in-between routes may have different starting points
56  if (find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc)) == myRoutes.end()) {
57  // compute route id
58  setID(desc);
59  myRoutes.push_back(desc);
60  } else {
61  RODFRouteDesc& prev = *find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
62  prev.overallProb += desc.overallProb;
63  }
64 }
65 
66 
67 bool
69  std::vector<RODFRouteDesc>::const_iterator j = find_if(myRoutes.begin(), myRoutes.end(), route_finder(desc));
70  if (j == myRoutes.end()) {
71  return false;
72  }
73  return true;
74 }
75 
76 
77 bool
78 RODFRouteCont::save(std::vector<std::string>& saved,
79  const std::string& prependix, OutputDevice& out) {
80  bool haveSavedOneAtLeast = false;
81  for (std::vector<RODFRouteDesc>::const_iterator j = myRoutes.begin(); j != myRoutes.end(); ++j) {
82  const RODFRouteDesc& desc = (*j);
83  if (find(saved.begin(), saved.end(), desc.routename) != saved.end()) {
84  continue;
85  }
86  saved.push_back((*j).routename);
87  assert(desc.edges2Pass.size() >= 1);
88  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, prependix + desc.routename);
89  out << " edges=\"";
90  for (ROEdgeVector::const_iterator k = desc.edges2Pass.begin(); k != desc.edges2Pass.end(); k++) {
91  if (k != desc.edges2Pass.begin()) {
92  out << ' ';
93  }
94  out << (*k)->getID();
95  }
96  out << '"';
97  out.closeTag();
98  haveSavedOneAtLeast = true;
99  }
100  return haveSavedOneAtLeast;
101 }
102 
103 
104 void
106  sort(myRoutes.begin(), myRoutes.end(), by_distance_sorter());
107 }
108 
109 
110 void
111 RODFRouteCont::removeIllegal(const std::vector<ROEdgeVector >& illegals) {
112  for (std::vector<RODFRouteDesc>::iterator i = myRoutes.begin(); i != myRoutes.end();) {
113  RODFRouteDesc& desc = *i;
114  bool remove = false;
115  for (std::vector<ROEdgeVector >::const_iterator j = illegals.begin(); !remove && j != illegals.end(); ++j) {
116  int noFound = 0;
117  for (ROEdgeVector::const_iterator k = (*j).begin(); !remove && k != (*j).end(); ++k) {
118  if (find(desc.edges2Pass.begin(), desc.edges2Pass.end(), *k) != desc.edges2Pass.end()) {
119  noFound++;
120  if (noFound > 1) {
121  remove = true;
122  }
123  }
124  }
125  }
126  if (remove) {
127  i = myRoutes.erase(i);
128  } else {
129  ++i;
130  }
131  }
132 }
133 
134 
135 void
137  std::pair<ROEdge*, ROEdge*> c(desc.edges2Pass[0], desc.edges2Pass.back());
138  desc.routename = c.first->getID() + "_to_" + c.second->getID();
139  if (myConnectionOccurences.find(c) == myConnectionOccurences.end()) {
140  myConnectionOccurences[c] = 0;
141  } else {
143  desc.routename = desc.routename + "_" + toString(myConnectionOccurences[c]);
144  }
145 }
146 
147 
148 
149 /****************************************************************************/
150 
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:56
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:56
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:67
A route within the DFROUTER.
Definition: RODFRouteDesc.h:54
RODFRouteCont()
Constructor.
std::string routename
The name of the route.
Definition: RODFRouteDesc.h:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
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.