SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROMAAssignments.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Assignment methods
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2014 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 <vector>
34 #include <router/ROEdge.h>
36 #include <router/RONet.h>
37 #include <router/RORoute.h>
39 #include <od2trips/ODMatrix.h>
40 #include <utils/common/SUMOTime.h>
42 #include "ROMAEdge.h"
43 #include "ROMAAssignments.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // static member variables
52 // ===========================================================================
53 std::map<const ROEdge* const, SUMOReal> ROMAAssignments::myPenalties;
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 
62  RONet& net, ODMatrix& matrix,
63  SUMOAbstractRouter<ROEdge, ROVehicle>& router) : myBegin(begin), myEnd(end), myNet(net), myMatrix(matrix), myRouter(router) {
65 }
66 
67 
69  delete myDefaultVehicle;
70 }
71 
72 // based on the definitions in PTV-Validate and in the VISUM-Köln network
75  if (edge->getType() == ROEdge::ET_DISTRICT) {
76  return 0;
77  }
78  const int roadClass = -edge->getPriority();
79  // TODO: differ road class 1 from the unknown road class 1!!!
80  if (roadClass == 0 || roadClass == 1) {
81  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 2000.*1.3)) * 2.); //CR13 in table.py
82  } else if (roadClass == 2 && edge->getSpeed() <= 11.) {
83  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1333.33 * 0.9)) * 3.); //CR5 in table.py
84  } else if (roadClass == 2 && edge->getSpeed() > 11. && edge->getSpeed() <= 16.) {
85  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1500.*1.)) * 2.); //CR3 in table.py
86  } else if (roadClass == 2 && edge->getSpeed() > 16.) {
87  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 2000.*1.3)) * 2.); //CR13 in table.py
88  } else if (roadClass == 3 && edge->getSpeed() <= 11.) {
89  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 800.*0.9)) * 3.); //CR5 in table.py
90  } else if (roadClass == 3 && edge->getSpeed() > 11. && edge->getSpeed() <= 13.) {
91  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 875.*0.9)) * 3.); //CR5 in table.py
92  } else if (roadClass == 3 && edge->getSpeed() > 13. && edge->getSpeed() <= 16.) {
93  return edge->getLength() / edge->getSpeed() * (1. + 1.7 * (flow / (edge->getLaneNo() * 1500.*1.)) * 2.); //CR4 in table.py
94  } else if (roadClass == 3 && edge->getSpeed() > 16.) {
95  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1800.*1.3)) * 2.); //CR13 in table.py
96  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() <= 5.) {
97  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 200.*0.5)) * 3.); //CR7 in table.py
98  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 5. && edge->getSpeed() <= 7.) {
99  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 412.5 * 0.5)) * 3.); //CR7 in table.py
100  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 7. && edge->getSpeed() <= 9.) {
101  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 600.*0.8)) * 3.); //CR6 in table.py
102  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 9. && edge->getSpeed() <= 11.) {
103  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 800.*0.9)) * 3.); //CR5 in table.py
104  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 11. && edge->getSpeed() <= 13.) {
105  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1125.*0.9)) * 3.); //CR5 in table.py
106  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 13. && edge->getSpeed() <= 16.) {
107  return edge->getLength() / edge->getSpeed() * (1. + 1.7 * (flow / (edge->getLaneNo() * 1583.*1.)) * 2.); //CR4 in table.py
108  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 16. && edge->getSpeed() <= 18.) {
109  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1100.*1.)) * 2.); //CR3 in table.py
110  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 18. && edge->getSpeed() <= 22.) {
111  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1200.*1.)) * 2.); //CR3 in table.py
112  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 22. && edge->getSpeed() <= 26.) {
113  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1300.*1.)) * 2.); //CR3 in table.py
114  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 26.) {
115  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1400.*1.)) * 2.); //CR3 in table.py
116  }
117  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 800.*0.9)) * 3.); //CR5 in table.py
118 }
119 
120 
121 bool
122 ROMAAssignments::addRoute(std::vector<const ROEdge*>& edges, std::vector<RORoute*>& paths, std::string routeId, SUMOReal costs, SUMOReal prob) {
123  RORoute* dup = 0;
124  for (std::vector<RORoute*>::const_iterator p = paths.begin(); p != paths.end(); p++) {
125  if (edges == (*p)->getEdgeVector()) {
126  dup = *p;
127  break;
128  }
129  }
130  if (dup == 0) {
131  paths.push_back(new RORoute(routeId, costs, prob, edges, 0, std::vector<SUMOVehicleParameter::Stop>()));
132  return true;
133  }
134  dup->addProbability(prob);
135  return false;
136 }
137 
138 
139 void
140 ROMAAssignments::getKPaths(const int kPaths, const SUMOReal penalty) {
141  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
142  ODCell* c = *i;
143  myPenalties.clear();
144  for (int k = 0; k < kPaths; k++) {
145  std::vector<const ROEdge*> edges;
146  myRouter.compute(myNet.getEdge(c->origin + "-source"), myNet.getEdge(c->destination + "-sink"), myDefaultVehicle, 0, edges);
147  for (std::vector<const ROEdge*>::iterator e = edges.begin(); e != edges.end(); e++) {
148  myPenalties[*e] = penalty;
149  }
150  addRoute(edges, c->pathsVector, c->origin + c->destination + toString(c->pathsVector.size()), 0, 0);
151  }
152  }
153  myPenalties.clear();
154 }
155 
156 
157 void
158 ROMAAssignments::incremental(const int numIter) {
159  for (int t = 0; t < numIter; t++) {
160  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); i++) {
161  ODCell* c = *i;
162  std::vector<const ROEdge*> edges;
163  SUMOReal linkFlow = c->vehicleNumber / numIter;
164  myRouter.compute(myNet.getEdge(c->origin + "-source"), myNet.getEdge(c->destination + "-sink"), myDefaultVehicle, 0, edges);
165  SUMOReal costs = 0.;
166  for (std::vector<const ROEdge*>::iterator e = edges.begin(); e != edges.end(); e++) {
167  ROEdge* edge = myNet.getEdge((*e)->getID());
168  edge->addEffort(linkFlow, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
169  const SUMOReal travelTime = capacityConstraintFunction(edge, linkFlow);
170  edge->addTravelTime(travelTime, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
171  costs += travelTime;
172  }
173  addRoute(edges, c->pathsVector, c->origin + c->destination + toString(c->pathsVector.size()), costs, linkFlow);
174  }
175  }
176 }
177 
178 
179 void
180 ROMAAssignments::sue(const int maxOuterIteration, const int maxInnerIteration, const int kPaths, const SUMOReal penalty, const SUMOReal tolerance, const std::string /* routeChoiceMethod */) {
181  getKPaths(kPaths, penalty);
182  for (int outer = 0; outer < maxOuterIteration; outer++) {
183  for (int inner = 0; inner < maxInnerIteration; inner++) {
184  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
185  ODCell* c = *i;
186  // update path cost
187  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
188  RORoute* r = *j;
190  }
191  // calculate route utilities and probabilities
193  // calculate route flows
194  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
195  RORoute* r = *j;
196  const SUMOReal pathFlow = r->getProbability() * c->vehicleNumber;
197  // assign edge flow deltas
198  for (std::vector<const ROEdge*>::const_iterator e = r->getEdgeVector().begin(); e != r->getEdgeVector().end(); e++) {
199  ROMAEdge* edge = static_cast<ROMAEdge*>(myNet.getEdge((*e)->getID()));
200  edge->setHelpFlow(edge->getHelpFlow() + pathFlow);
201  }
202  }
203  }
204  // calculate new edge flows and check for stability
205  int unstableEdges = 0;
206  for (std::map<std::string, ROEdge*>::const_iterator i = myNet.getEdgeMap().begin(); i != myNet.getEdgeMap().end(); ++i) {
207  ROMAEdge* edge = static_cast<ROMAEdge*>((*i).second);
208  const SUMOReal oldFlow = edge->getEffort(myDefaultVehicle, 0.);
209  SUMOReal newFlow = oldFlow;
210  if (inner == 0 && outer == 0) {
211  newFlow += edge->getHelpFlow();
212  } else {
213  newFlow += (edge->getHelpFlow() - oldFlow) / (inner + 1);
214  }
215 // if not lohse:
216  if (newFlow > 0.) {
217  if (abs(newFlow - oldFlow) / newFlow > tolerance) {
218  unstableEdges++;
219  }
220  } else if (newFlow == 0.) {
221  if (oldFlow != 0. && (abs(newFlow - oldFlow) / oldFlow > tolerance)) {
222  unstableEdges++;
223  }
224  } else { // newFlow < 0.
225  unstableEdges++;
226  newFlow = 0.;
227  }
228  edge->addEffort(newFlow, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
229  const SUMOReal travelTime = capacityConstraintFunction(edge, newFlow);
230  edge->addTravelTime(travelTime, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
231  edge->setHelpFlow(0.);
232  }
233  // if stable break
234  if (unstableEdges == 0) {
235  break;
236  }
237  // additional stability check from python script: if notstable < math.ceil(net.geteffEdgeCounts()*0.005) or notstable < 3: stable = True
238  }
239  // check for a new route, if none available, break
240  // several modifications about when a route is new and when to break are in the original script
241  bool newRoute = false;
242  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
243  ODCell* c = *i;
244  std::vector<const ROEdge*> edges;
245  myRouter.compute(myNet.getEdge(c->origin + "-source"), myNet.getEdge(c->destination + "-sink"), myDefaultVehicle, 0, edges);
246  newRoute |= addRoute(edges, c->pathsVector, c->origin + c->destination + toString(c->pathsVector.size()), 0, 0);
247  }
248  if (!newRoute) {
249  break;
250  }
251  }
252  // final round of assignment
253  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
254  ODCell* c = *i;
255  // update path cost
256  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
257  RORoute* r = *j;
259  }
260  // calculate route utilities and probabilities
262  // calculate route flows
263  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
264  RORoute* r = *j;
266  }
267  }
268 }
269 
270 
271 SUMOReal
272 ROMAAssignments::getPenalizedEffort(const ROEdge* const e, const ROVehicle* const v, SUMOReal t) {
273  const std::map<const ROEdge* const, SUMOReal>::const_iterator i = myPenalties.find(e);
274  return i == myPenalties.end() ? e->getEffort(v, t) : e->getEffort(v, t) + i->second;
275 }
276 
277 
278 SUMOReal
279 ROMAAssignments::getPenalizedTT(const ROEdge* const e, const ROVehicle* const v, SUMOReal t) {
280  const std::map<const ROEdge* const, SUMOReal>::const_iterator i = myPenalties.find(e);
281  return i == myPenalties.end() ? e->getTravelTime(v, t) : e->getTravelTime(v, t) + i->second;
282 }
283 
284 
285 SUMOReal
286 ROMAAssignments::getTravelTime(const ROEdge* const e, const ROVehicle* const v, SUMOReal t) {
287  return e->getTravelTime(v, t);
288 }
void incremental(const int numIter)
const std::vector< ODCell * > & getCells()
Definition: ODMatrix.h:219
void setHelpFlow(const SUMOReal flow)
Definition: ROMAEdge.h:89
SUMOAbstractRouter< ROEdge, ROVehicle > & myRouter
EdgeType getType() const
Returns the type of the edge.
Definition: ROEdge.h:172
SUMOReal getHelpFlow() const
Definition: ROMAEdge.h:93
ROMAAssignments(const SUMOTime begin, const SUMOTime end, RONet &net, ODMatrix &matrix, SUMOAbstractRouter< ROEdge, ROVehicle > &router)
Constructor.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:107
const SUMOTime myBegin
void setProbability(SUMOReal prob)
Sets the probability of the route.
Definition: RORoute.cpp:80
unsigned int getLaneNo() const
Returns the number of lanes this edge has.
Definition: ROEdge.h:203
std::vector< RORoute * > pathsVector
the list of paths / routes
Definition: ODCell.h:76
bool addRoute(std::vector< const ROEdge * > &edges, std::vector< RORoute * > &paths, std::string routeId, SUMOReal costs, SUMOReal prob)
add a route and check for duplicates
const std::string DEFAULT_VTYPE_ID
void addTravelTime(SUMOReal value, SUMOReal timeBegin, SUMOReal timeEnd)
Adds a travel time value.
Definition: ROEdge.cpp:119
#define abs(a)
Definition: polyfonts.c:67
const std::map< std::string, ROEdge * > & getEdgeMap() const
Definition: RONet.cpp:460
const SUMOTime myEnd
void addProbability(SUMOReal prob)
add additional vehicles/probability
Definition: RORoute.cpp:91
A vehicle as used by router.
Definition: ROVehicle.h:59
A single O/D-matrix cell.
Definition: ODCell.h:56
std::string origin
Name of the origin district.
Definition: ODCell.h:67
static SUMOReal getPenalizedTT(const ROEdge *const e, const ROVehicle *const v, SUMOReal t)
Returns the traveltime on an edge including penalties.
void sue(const int maxOuterIteration, const int maxInnerIteration, const int kPaths, const SUMOReal penalty, const SUMOReal tolerance, const std::string routeChoiceMethod)
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:74
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
SUMOReal getProbability() const
Returns the probability the driver will take this route with.
Definition: RORoute.h:122
void setCosts(SUMOReal costs)
Sets the costs of the route.
Definition: RORoute.cpp:74
~ROMAAssignments()
Destructor.
SUMOReal vehicleNumber
The number of vehicles.
Definition: ODCell.h:58
An edge representing a whole district.
Definition: ROEdge.h:80
static std::map< const ROEdge *const, SUMOReal > myPenalties
static SUMOReal getTravelTime(const ROEdge *const e, const ROVehicle *const v, SUMOReal t)
Returns the traveltime on an edge without penalties.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
static SUMOReal getPenalizedEffort(const ROEdge *const e, const ROVehicle *const v, SUMOReal t)
Returns the effort to pass an edge including penalties.
A basic edge for routing applications.
Definition: ROEdge.h:70
const std::vector< const ROEdge * > & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:154
The router&#39;s network representation.
Definition: RONet.h:72
Structure representing possible vehicle parameter.
void addEffort(SUMOReal value, SUMOReal timeBegin, SUMOReal timeEnd)
Adds a weight value.
Definition: ROEdge.cpp:112
int getPriority() const
get edge priority (road class)
Definition: ROEdge.h:409
SUMOReal getLength() const
Returns the length of the edge.
Definition: ROEdge.h:180
SUMOReal getEffort(const ROVehicle *const veh, SUMOReal time) const
Returns the effort for this edge.
Definition: ROEdge.cpp:126
void getKPaths(const int kPaths, const SUMOReal penalty)
get the k shortest paths
int SUMOTime
Definition: SUMOTime.h:43
virtual SUMOReal recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime) const =0
std::string destination
Name of the destination district.
Definition: ODCell.h:70
#define SUMOReal
Definition: config.h:218
virtual void compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into)=0
Builds the route between the given edges using the minimum effort at the given time The definition of...
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:167
SUMOReal getTravelTime(const ROVehicle *const veh, SUMOReal time) const
Returns the travel time for this edge.
Definition: ROEdge.cpp:147
static RouteCostCalculator< R, E, V > & getCalculator()
SUMOReal capacityConstraintFunction(const ROEdge *edge, const SUMOReal flow) const
A basic edge for routing applications.
Definition: ROMAEdge.h:65
static ROVehicle * myDefaultVehicle
SUMOReal getSpeed() const
Returns the speed allowed on this edge.
Definition: ROEdge.h:195
A complete router&#39;s route.
Definition: RORoute.h:61
ODMatrix & myMatrix