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 /****************************************************************************/
8 // Calculators for route costs and probabilities
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
11 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <limits>
33 #include <utils/common/StdDefs.h>
34 #include <utils/geom/GeomHelper.h>
35 #include <utils/common/SUMOTime.h>
37 #include "ROEdge.h"
38 #include "RORoute.h"
39 #include "ROVehicle.h"
40 #include "ROCostCalculator.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // static member definitions
49 // ===========================================================================
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
58  myMaxRouteNumber = oc.getInt("max-alternatives");
59  myKeepRoutes = oc.getBool("keep-all-routes");
60  mySkipRouteCalculation = oc.getBool("skip-new-routes");
61 }
62 
63 
65 
66 
69  if (myInstance == 0) {
71  if (oc.getBool("logit")) {
72  myInstance = new ROLogitCalculator(oc.getFloat("logit.beta"), oc.getFloat("logit.gamma"), oc.getFloat("logit.theta"));
73  } else {
74  myInstance = new ROGawronCalculator(oc.getFloat("gawron.beta"), oc.getFloat("gawron.a"));
75  }
76  }
77  return *myInstance;
78 }
79 
80 
81 void
83  delete myInstance;
84  myInstance = 0;
85 }
86 
87 
89  : myBeta(beta), myA(a) {}
90 
91 
93 
94 
95 void
96 ROGawronCalculator::setCosts(RORoute* route, const SUMOReal costs, const bool isActive) const {
97  if (isActive) {
98  route->setCosts(costs);
99  } else {
100  route->setCosts(myBeta * costs + ((SUMOReal) 1.0 - myBeta) * route->getCosts());
101  }
102 }
103 
104 
105 void
106 ROGawronCalculator::calculateProbabilities(const ROVehicle* const /* veh */, std::vector<RORoute*> alternatives) {
107  for (std::vector<RORoute*>::iterator i = alternatives.begin(); i != alternatives.end() - 1; i++) {
108  RORoute* pR = *i;
109  for (std::vector<RORoute*>::iterator j = i + 1; j != alternatives.end(); j++) {
110  RORoute* pS = *j;
111  // see [Gawron, 1998] (4.2)
112  const SUMOReal delta =
113  (pS->getCosts() - pR->getCosts()) /
114  (pS->getCosts() + pR->getCosts());
115  // see [Gawron, 1998] (4.3a, 4.3b)
116  SUMOReal newPR = gawronF(pR->getProbability(), pS->getProbability(), delta);
117  SUMOReal newPS = pR->getProbability() + pS->getProbability() - newPR;
118  if (ISNAN(newPR) || ISNAN(newPS)) {
119  newPR = pS->getCosts() > pR->getCosts()
120  ? (SUMOReal) 1. : 0;
121  newPS = pS->getCosts() > pR->getCosts()
122  ? 0 : (SUMOReal) 1.;
123  }
124  newPR = MIN2((SUMOReal) MAX2(newPR, (SUMOReal) 0), (SUMOReal) 1);
125  newPS = MIN2((SUMOReal) MAX2(newPS, (SUMOReal) 0), (SUMOReal) 1);
126  pR->setProbability(newPR);
127  pS->setProbability(newPS);
128  }
129  }
130 }
131 
132 
133 SUMOReal
134 ROGawronCalculator::gawronF(const SUMOReal pdr, const SUMOReal pds, const SUMOReal x) const {
135  if (pdr * gawronG(myA, x) + pds == 0) {
137  }
138  return (pdr * (pdr + pds) * gawronG(myA, x)) /
139  (pdr * gawronG(myA, x) + pds);
140 }
141 
142 
143 SUMOReal
145  if (((1.0 - (x * x)) == 0)) {
147  }
148  return (SUMOReal) exp((a * x) / (1.0 - (x * x)));
149 }
150 
151 
152 
153 
155  const SUMOReal theta)
156  : myBeta(beta), myGamma(gamma), myTheta(theta) {}
157 
158 
160 
161 
162 void
163 ROLogitCalculator::setCosts(RORoute* route, const SUMOReal costs, const bool /* isActive */) const {
164  route->setCosts(costs);
165 }
166 
167 
168 void
169 ROLogitCalculator::calculateProbabilities(const ROVehicle* const veh, std::vector<RORoute*> alternatives) {
170  const SUMOReal theta = myTheta >= 0 ? myTheta : getThetaForCLogit(alternatives);
171  const SUMOReal beta = myBeta >= 0 ? myBeta : getBetaForCLogit(alternatives);
172  if (beta > 0) {
173  // calculate commonalities
174  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
175  const RORoute* pR = *i;
176  SUMOReal lengthR = 0;
177  const std::vector<const ROEdge*>& edgesR = pR->getEdgeVector();
178  for (std::vector<const ROEdge*>::const_iterator edge = edgesR.begin(); edge != edgesR.end(); ++edge) {
179  //@todo we should use costs here
180  lengthR += (*edge)->getTravelTime(veh, STEPS2TIME(veh->getDepartureTime()));
181  }
182  SUMOReal overlapSum = 0;
183  for (std::vector<RORoute*>::const_iterator j = alternatives.begin(); j != alternatives.end(); j++) {
184  const RORoute* pS = *j;
185  SUMOReal overlapLength = 0.;
186  SUMOReal lengthS = 0;
187  const std::vector<const ROEdge*>& edgesS = pS->getEdgeVector();
188  for (std::vector<const ROEdge*>::const_iterator edge = edgesS.begin(); edge != edgesS.end(); ++edge) {
189  lengthS += (*edge)->getTravelTime(veh, STEPS2TIME(veh->getDepartureTime()));
190  if (std::find(edgesR.begin(), edgesR.end(), *edge) != edgesR.end()) {
191  overlapLength += (*edge)->getTravelTime(veh, STEPS2TIME(veh->getDepartureTime()));
192  }
193  }
194  overlapSum += pow(overlapLength / sqrt(lengthR * lengthS), myGamma);
195  }
196  myCommonalities[pR] = beta * log(overlapSum);
197  }
198  }
199  for (std::vector<RORoute*>::iterator i = alternatives.begin(); i != alternatives.end(); i++) {
200  RORoute* pR = *i;
201  SUMOReal weightedSum = 0;
202  for (std::vector<RORoute*>::iterator j = alternatives.begin(); j != alternatives.end(); j++) {
203  RORoute* pS = *j;
204  weightedSum += exp(theta * (pR->getCosts() - pS->getCosts() + myCommonalities[pR] - myCommonalities[pS]));
205  }
206  pR->setProbability(1. / weightedSum);
207  }
208 }
209 
210 
211 SUMOReal
212 ROLogitCalculator::getBetaForCLogit(const std::vector<RORoute*> alternatives) const {
214  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
215  const SUMOReal cost = (*i)->getCosts() / 3600.;
216  if (cost < min) {
217  min = cost;
218  }
219  }
220  return min;
221 }
222 
223 
224 SUMOReal
225 ROLogitCalculator::getThetaForCLogit(const std::vector<RORoute*> alternatives) const {
226  // @todo this calculation works for travel times only
227  SUMOReal sum = 0.;
228  SUMOReal diff = 0.;
230  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
231  const SUMOReal cost = (*i)->getCosts() / 3600.;
232  sum += cost;
233  if (cost < min) {
234  min = cost;
235  }
236  }
237  const SUMOReal meanCost = sum / SUMOReal(alternatives.size());
238  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
239  diff += pow((*i)->getCosts() / 3600. - meanCost, 2);
240  }
241  const SUMOReal cvCost = sqrt(diff / SUMOReal(alternatives.size())) / meanCost;
242  // @todo re-evaluate function
243 // if (cvCost > 0.04) { // Magic numbers from Lohse book
244  return PI / (sqrt(6.) * cvCost * (min + 1.1)) / 3600.;
245 // }
246 // return 1./3600.;
247 }
248 
249 
250 /****************************************************************************/