SUMO - Simulation of Urban MObility
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-2015 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 <od/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 (edge->getLaneNo() == 0) {
81  // TAZ have no cost
82  return 0;
83  } else if (roadClass == 0 || roadClass == 1) {
84  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 2000.*1.3)) * 2.); //CR13 in table.py
85  } else if (roadClass == 2 && edge->getSpeed() <= 11.) {
86  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1333.33 * 0.9)) * 3.); //CR5 in table.py
87  } else if (roadClass == 2 && edge->getSpeed() > 11. && edge->getSpeed() <= 16.) {
88  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1500.*1.)) * 2.); //CR3 in table.py
89  } else if (roadClass == 2 && edge->getSpeed() > 16.) {
90  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 2000.*1.3)) * 2.); //CR13 in table.py
91  } else if (roadClass == 3 && edge->getSpeed() <= 11.) {
92  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 800.*0.9)) * 3.); //CR5 in table.py
93  } else if (roadClass == 3 && edge->getSpeed() > 11. && edge->getSpeed() <= 13.) {
94  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 875.*0.9)) * 3.); //CR5 in table.py
95  } else if (roadClass == 3 && edge->getSpeed() > 13. && edge->getSpeed() <= 16.) {
96  return edge->getLength() / edge->getSpeed() * (1. + 1.7 * (flow / (edge->getLaneNo() * 1500.*1.)) * 2.); //CR4 in table.py
97  } else if (roadClass == 3 && edge->getSpeed() > 16.) {
98  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1800.*1.3)) * 2.); //CR13 in table.py
99  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() <= 5.) {
100  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 200.*0.5)) * 3.); //CR7 in table.py
101  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 5. && edge->getSpeed() <= 7.) {
102  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 412.5 * 0.5)) * 3.); //CR7 in table.py
103  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 7. && edge->getSpeed() <= 9.) {
104  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 600.*0.8)) * 3.); //CR6 in table.py
105  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 9. && edge->getSpeed() <= 11.) {
106  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 800.*0.9)) * 3.); //CR5 in table.py
107  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 11. && edge->getSpeed() <= 13.) {
108  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1125.*0.9)) * 3.); //CR5 in table.py
109  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 13. && edge->getSpeed() <= 16.) {
110  return edge->getLength() / edge->getSpeed() * (1. + 1.7 * (flow / (edge->getLaneNo() * 1583.*1.)) * 2.); //CR4 in table.py
111  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 16. && edge->getSpeed() <= 18.) {
112  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1100.*1.)) * 2.); //CR3 in table.py
113  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 18. && edge->getSpeed() <= 22.) {
114  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1200.*1.)) * 2.); //CR3 in table.py
115  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 22. && edge->getSpeed() <= 26.) {
116  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1300.*1.)) * 2.); //CR3 in table.py
117  } else if ((roadClass >= 4 || roadClass == -1) && edge->getSpeed() > 26.) {
118  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 1400.*1.)) * 2.); //CR3 in table.py
119  }
120  return edge->getLength() / edge->getSpeed() * (1. + 1.*(flow / (edge->getLaneNo() * 800.*0.9)) * 3.); //CR5 in table.py
121 }
122 
123 
124 bool
125 ROMAAssignments::addRoute(ConstROEdgeVector& edges, std::vector<RORoute*>& paths, std::string routeId, SUMOReal costs, SUMOReal prob) {
126  RORoute* dup = 0;
127  for (std::vector<RORoute*>::const_iterator p = paths.begin(); p != paths.end(); p++) {
128  if (edges == (*p)->getEdgeVector()) {
129  dup = *p;
130  break;
131  }
132  }
133  if (dup == 0) {
134  paths.push_back(new RORoute(routeId, costs, prob, edges, 0, std::vector<SUMOVehicleParameter::Stop>()));
135  return true;
136  }
137  dup->addProbability(prob);
138  return false;
139 }
140 
141 
142 void
143 ROMAAssignments::getKPaths(const int kPaths, const SUMOReal penalty) {
144  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
145  ODCell* c = *i;
146  myPenalties.clear();
147  for (int k = 0; k < kPaths; k++) {
148  ConstROEdgeVector edges;
149  myRouter.compute(myNet.getEdge(c->origin + "-source"), myNet.getEdge(c->destination + "-sink"), myDefaultVehicle, 0, edges);
150  for (ConstROEdgeVector::iterator e = edges.begin(); e != edges.end(); e++) {
151  myPenalties[*e] = penalty;
152  }
153  addRoute(edges, c->pathsVector, c->origin + c->destination + toString(c->pathsVector.size()), 0, 0);
154  }
155  }
156  myPenalties.clear();
157 }
158 
159 
160 void
161 ROMAAssignments::incremental(const int numIter) {
162  for (int t = 0; t < numIter; t++) {
163  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); i++) {
164  ODCell* c = *i;
165  ConstROEdgeVector edges;
166  SUMOReal linkFlow = c->vehicleNumber / numIter;
167  myRouter.compute(myNet.getEdge(c->origin + "-source"), myNet.getEdge(c->destination + "-sink"), myDefaultVehicle, 0, edges);
168  SUMOReal costs = 0.;
169  for (ConstROEdgeVector::iterator e = edges.begin(); e != edges.end(); e++) {
170  ROEdge* edge = myNet.getEdge((*e)->getID());
171  edge->addEffort(linkFlow, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
172  const SUMOReal travelTime = capacityConstraintFunction(edge, linkFlow);
173  edge->addTravelTime(travelTime, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
174  costs += travelTime;
175  }
176  addRoute(edges, c->pathsVector, c->origin + c->destination + toString(c->pathsVector.size()), costs, linkFlow);
177  }
178  }
179 }
180 
181 
182 void
183 ROMAAssignments::sue(const int maxOuterIteration, const int maxInnerIteration, const int kPaths, const SUMOReal penalty, const SUMOReal tolerance, const std::string /* routeChoiceMethod */) {
184  getKPaths(kPaths, penalty);
185  for (int outer = 0; outer < maxOuterIteration; outer++) {
186  for (int inner = 0; inner < maxInnerIteration; inner++) {
187  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
188  ODCell* c = *i;
189  // update path cost
190  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
191  RORoute* r = *j;
193 // std::cout << std::setprecision(20) << r->getID() << ":" << r->getCosts() << std::endl;
194  }
195  // calculate route utilities and probabilities
197  // calculate route flows
198  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
199  RORoute* r = *j;
200  const SUMOReal pathFlow = r->getProbability() * c->vehicleNumber;
201  // assign edge flow deltas
202  for (ConstROEdgeVector::const_iterator e = r->getEdgeVector().begin(); e != r->getEdgeVector().end(); e++) {
203  ROMAEdge* edge = static_cast<ROMAEdge*>(myNet.getEdge((*e)->getID()));
204  edge->setHelpFlow(edge->getHelpFlow() + pathFlow);
205  }
206  }
207  }
208  // calculate new edge flows and check for stability
209  int unstableEdges = 0;
210  for (std::map<std::string, ROEdge*>::const_iterator i = myNet.getEdgeMap().begin(); i != myNet.getEdgeMap().end(); ++i) {
211  ROMAEdge* edge = static_cast<ROMAEdge*>((*i).second);
212  const SUMOReal oldFlow = edge->getEffort(myDefaultVehicle, 0.);
213  SUMOReal newFlow = oldFlow;
214  if (inner == 0 && outer == 0) {
215  newFlow += edge->getHelpFlow();
216  } else {
217  newFlow += (edge->getHelpFlow() - oldFlow) / (inner + 1);
218  }
219 // if not lohse:
220  if (newFlow > 0.) {
221  if (abs(newFlow - oldFlow) / newFlow > tolerance) {
222  unstableEdges++;
223  }
224  } else if (newFlow == 0.) {
225  if (oldFlow != 0. && (abs(newFlow - oldFlow) / oldFlow > tolerance)) {
226  unstableEdges++;
227  }
228  } else { // newFlow < 0.
229  unstableEdges++;
230  newFlow = 0.;
231  }
232  edge->addEffort(newFlow, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
233  const SUMOReal travelTime = capacityConstraintFunction(edge, newFlow);
234  edge->addTravelTime(travelTime, STEPS2TIME(myBegin), STEPS2TIME(myEnd));
235  edge->setHelpFlow(0.);
236  }
237  // if stable break
238  if (unstableEdges == 0) {
239  break;
240  }
241  // additional stability check from python script: if notstable < math.ceil(net.geteffEdgeCounts()*0.005) or notstable < 3: stable = True
242  }
243  // check for a new route, if none available, break
244  // several modifications about when a route is new and when to break are in the original script
245  bool newRoute = false;
246  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
247  ODCell* c = *i;
248  ConstROEdgeVector edges;
249  myRouter.compute(myNet.getEdge(c->origin + "-source"), myNet.getEdge(c->destination + "-sink"), myDefaultVehicle, 0, edges);
250  newRoute |= addRoute(edges, c->pathsVector, c->origin + c->destination + toString(c->pathsVector.size()), 0, 0);
251  }
252  if (!newRoute) {
253  break;
254  }
255  }
256  // final round of assignment
257  for (std::vector<ODCell*>::const_iterator i = myMatrix.getCells().begin(); i != myMatrix.getCells().end(); ++i) {
258  ODCell* c = *i;
259  // update path cost
260  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
261  RORoute* r = *j;
263  }
264  // calculate route utilities and probabilities
266  // calculate route flows
267  for (std::vector<RORoute*>::const_iterator j = c->pathsVector.begin(); j != c->pathsVector.end(); ++j) {
268  RORoute* r = *j;
270  }
271  }
272 }
273 
274 
275 SUMOReal
276 ROMAAssignments::getPenalizedEffort(const ROEdge* const e, const ROVehicle* const v, SUMOReal t) {
277  const std::map<const ROEdge* const, SUMOReal>::const_iterator i = myPenalties.find(e);
278  return i == myPenalties.end() ? e->getEffort(v, t) : e->getEffort(v, t) + i->second;
279 }
280 
281 
282 SUMOReal
283 ROMAAssignments::getPenalizedTT(const ROEdge* const e, const ROVehicle* const v, SUMOReal t) {
284  const std::map<const ROEdge* const, SUMOReal>::const_iterator i = myPenalties.find(e);
285  return i == myPenalties.end() ? e->getTravelTime(v, t) : e->getTravelTime(v, t) + i->second;
286 }
287 
288 
289 SUMOReal
290 ROMAAssignments::getTravelTime(const ROEdge* const e, const ROVehicle* const v, SUMOReal t) {
291  return e->getTravelTime(v, t);
292 }
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:175
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:138
const SUMOTime myBegin
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:155
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:206
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:59
std::vector< RORoute * > pathsVector
the list of paths / routes
Definition: ODCell.h:76
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:541
const SUMOTime myEnd
void addProbability(SUMOReal prob)
add additional vehicles/probability
Definition: RORoute.cpp:91
A vehicle as used by router.
Definition: ROVehicle.h:60
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:123
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:83
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:73
The router'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:416
SUMOReal getLength() const
Returns the length of the edge.
Definition: ROEdge.h:183
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
bool addRoute(ConstROEdgeVector &edges, std::vector< RORoute * > &paths, std::string routeId, SUMOReal costs, SUMOReal prob)
add a route and check for duplicates
#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:217
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:198
A complete router's route.
Definition: RORoute.h:62
ODMatrix & myMatrix