SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROCostCalculator.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Calculators for route costs and probabilities
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2002-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 <limits>
34 #include <utils/common/StdDefs.h>
35 #include <utils/geom/GeomHelper.h>
36 #include <utils/common/SUMOTime.h>
38 #include "ROEdge.h"
39 #include "RORoute.h"
40 #include "ROVehicle.h"
41 #include "ROCostCalculator.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // static member definitions
50 // ===========================================================================
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
59  myMaxRouteNumber = oc.getInt("max-alternatives");
60  myKeepRoutes = oc.getBool("keep-all-routes");
61  mySkipRouteCalculation = oc.getBool("skip-new-routes");
62 }
63 
64 
66 
67 
70  if (myInstance == 0) {
72  if (oc.getString("route-choice-method") == "logit") {
73  myInstance = new ROLogitCalculator(oc.getFloat("logit.beta"), oc.getFloat("logit.gamma"), oc.getFloat("logit.theta"));
74  } else if (oc.getString("route-choice-method") == "gawron") {
75  myInstance = new ROGawronCalculator(oc.getFloat("gawron.beta"), oc.getFloat("gawron.a"));
76  }
77  }
78  return *myInstance;
79 }
80 
81 
82 void
84  delete myInstance;
85  myInstance = 0;
86 }
87 
88 
90  : myBeta(beta), myA(a) {}
91 
92 
94 
95 
96 void
97 ROGawronCalculator::setCosts(RORoute* route, const SUMOReal costs, const bool isActive) const {
98  if (isActive) {
99  route->setCosts(costs);
100  } else {
101  route->setCosts(myBeta * costs + ((SUMOReal) 1.0 - myBeta) * route->getCosts());
102  }
103 }
104 
105 
106 void
107 ROGawronCalculator::calculateProbabilities(std::vector<RORoute*> alternatives, const ROVehicle* const /* veh */, const SUMOTime /* time */) {
108  for (std::vector<RORoute*>::iterator i = alternatives.begin(); i != alternatives.end() - 1; i++) {
109  RORoute* pR = *i;
110  for (std::vector<RORoute*>::iterator j = i + 1; j != alternatives.end(); j++) {
111  RORoute* pS = *j;
112  // see [Gawron, 1998] (4.2)
113  const SUMOReal delta =
114  (pS->getCosts() - pR->getCosts()) /
115  (pS->getCosts() + pR->getCosts());
116  // see [Gawron, 1998] (4.3a, 4.3b)
117  SUMOReal newPR = gawronF(pR->getProbability(), pS->getProbability(), delta);
118  SUMOReal newPS = pR->getProbability() + pS->getProbability() - newPR;
119  if (ISNAN(newPR) || ISNAN(newPS)) {
120  newPR = pS->getCosts() > pR->getCosts()
121  ? (SUMOReal) 1. : 0;
122  newPS = pS->getCosts() > pR->getCosts()
123  ? 0 : (SUMOReal) 1.;
124  }
125  newPR = MIN2((SUMOReal) MAX2(newPR, (SUMOReal) 0), (SUMOReal) 1);
126  newPS = MIN2((SUMOReal) MAX2(newPS, (SUMOReal) 0), (SUMOReal) 1);
127  pR->setProbability(newPR);
128  pS->setProbability(newPS);
129  }
130  }
131 }
132 
133 
134 SUMOReal
135 ROGawronCalculator::gawronF(const SUMOReal pdr, const SUMOReal pds, const SUMOReal x) const {
136  if (pdr * gawronG(myA, x) + pds == 0) {
138  }
139  return (pdr * (pdr + pds) * gawronG(myA, x)) /
140  (pdr * gawronG(myA, x) + pds);
141 }
142 
143 
144 SUMOReal
146  if (((1.0 - (x * x)) == 0)) {
148  }
149  return (SUMOReal) exp((a * x) / (1.0 - (x * x)));
150 }
151 
152 
153 
154 
156  const SUMOReal theta)
157  : myBeta(beta), myGamma(gamma), myTheta(theta) {}
158 
159 
161 
162 
163 void
164 ROLogitCalculator::setCosts(RORoute* route, const SUMOReal costs, const bool /* isActive */) const {
165  route->setCosts(costs);
166 }
167 
168 
169 void
170 ROLogitCalculator::calculateProbabilities(std::vector<RORoute*> alternatives, const ROVehicle* const veh, const SUMOTime time) {
171  const SUMOReal theta = myTheta >= 0 ? myTheta : getThetaForCLogit(alternatives);
172  const SUMOReal beta = myBeta >= 0 ? myBeta : getBetaForCLogit(alternatives);
173  if (beta > 0) {
174  // calculate commonalities
175  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
176  const RORoute* pR = *i;
177  SUMOReal lengthR = 0;
178  const std::vector<const ROEdge*>& edgesR = pR->getEdgeVector();
179  for (std::vector<const ROEdge*>::const_iterator edge = edgesR.begin(); edge != edgesR.end(); ++edge) {
180  //@todo we should use costs here
181  lengthR += (*edge)->getTravelTime(veh, STEPS2TIME(time));
182  }
183  SUMOReal overlapSum = 0;
184  for (std::vector<RORoute*>::const_iterator j = alternatives.begin(); j != alternatives.end(); j++) {
185  const RORoute* pS = *j;
186  SUMOReal overlapLength = 0.;
187  SUMOReal lengthS = 0;
188  const std::vector<const ROEdge*>& edgesS = pS->getEdgeVector();
189  for (std::vector<const ROEdge*>::const_iterator edge = edgesS.begin(); edge != edgesS.end(); ++edge) {
190  lengthS += (*edge)->getTravelTime(veh, STEPS2TIME(time));
191  if (std::find(edgesR.begin(), edgesR.end(), *edge) != edgesR.end()) {
192  overlapLength += (*edge)->getTravelTime(veh, STEPS2TIME(time));
193  }
194  }
195  overlapSum += pow(overlapLength / sqrt(lengthR * lengthS), myGamma);
196  }
197  myCommonalities[pR] = beta * log(overlapSum);
198  }
199  }
200  for (std::vector<RORoute*>::iterator i = alternatives.begin(); i != alternatives.end(); i++) {
201  RORoute* pR = *i;
202  SUMOReal weightedSum = 0;
203  for (std::vector<RORoute*>::iterator j = alternatives.begin(); j != alternatives.end(); j++) {
204  RORoute* pS = *j;
205  weightedSum += exp(theta * (pR->getCosts() - pS->getCosts() + myCommonalities[pR] - myCommonalities[pS]));
206  }
207  pR->setProbability(1. / weightedSum);
208  }
209 }
210 
211 
212 SUMOReal
213 ROLogitCalculator::getBetaForCLogit(const std::vector<RORoute*> alternatives) const {
215  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
216  const SUMOReal cost = (*i)->getCosts() / 3600.;
217  if (cost < min) {
218  min = cost;
219  }
220  }
221  return min;
222 }
223 
224 
225 SUMOReal
226 ROLogitCalculator::getThetaForCLogit(const std::vector<RORoute*> alternatives) const {
227  // @todo this calculation works for travel times only
228  SUMOReal sum = 0.;
229  SUMOReal diff = 0.;
231  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
232  const SUMOReal cost = (*i)->getCosts() / 3600.;
233  sum += cost;
234  if (cost < min) {
235  min = cost;
236  }
237  }
238  const SUMOReal meanCost = sum / SUMOReal(alternatives.size());
239  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
240  diff += pow((*i)->getCosts() / 3600. - meanCost, 2);
241  }
242  const SUMOReal cvCost = sqrt(diff / SUMOReal(alternatives.size())) / meanCost;
243  // @todo re-evaluate function
244 // if (cvCost > 0.04) { // Magic numbers from Lohse book
245  return M_PI / (sqrt(6.) * cvCost * (min + 1.1)) / 3600.;
246 // }
247 // return 1./3600.;
248 }
249 
250 
251 /****************************************************************************/