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 // ===========================================================================
57 
58 
60 
61 
64  if (myInstance == 0) {
66  if (oc.getBool("logit")) {
67  myInstance = new ROLogitCalculator(oc.getFloat("logit.beta"), oc.getFloat("logit.gamma"), oc.getFloat("logit.theta"));
68  } else {
69  myInstance = new ROGawronCalculator(oc.getFloat("gawron.beta"), oc.getFloat("gawron.a"));
70  }
71  }
72  return *myInstance;
73 }
74 
75 
76 void
78  delete myInstance;
79  myInstance = 0;
80 }
81 
82 
84 : myBeta(beta), myA(a) {}
85 
86 
88 
89 
90 void
91 ROGawronCalculator::setCosts(RORoute* route, const SUMOReal costs, const bool isActive) const {
92  if (isActive) {
93  route->setCosts(costs);
94  } else {
95  route->setCosts(myBeta * costs + ((SUMOReal) 1.0 - myBeta) * route->getCosts());
96  }
97 }
98 
99 
100 void
101 ROGawronCalculator::calculateProbabilities(const ROVehicle* const veh, std::vector<RORoute*> alternatives) {
102  for (std::vector<RORoute*>::iterator i = alternatives.begin(); i != alternatives.end() - 1; i++) {
103  RORoute* pR = *i;
104  for (std::vector<RORoute*>::iterator j = i + 1; j != alternatives.end(); j++) {
105  RORoute* pS = *j;
106  // see [Gawron, 1998] (4.2)
107  const SUMOReal delta =
108  (pS->getCosts() - pR->getCosts()) /
109  (pS->getCosts() + pR->getCosts());
110  // see [Gawron, 1998] (4.3a, 4.3b)
111  SUMOReal newPR = gawronF(pR->getProbability(), pS->getProbability(), delta);
112  SUMOReal newPS = pR->getProbability() + pS->getProbability() - newPR;
113  if (ISNAN(newPR) || ISNAN(newPS)) {
114  newPR = pS->getCosts() > pR->getCosts()
115  ? (SUMOReal) 1. : 0;
116  newPS = pS->getCosts() > pR->getCosts()
117  ? 0 : (SUMOReal) 1.;
118  }
119  newPR = MIN2((SUMOReal) MAX2(newPR, (SUMOReal) 0), (SUMOReal) 1);
120  newPS = MIN2((SUMOReal) MAX2(newPS, (SUMOReal) 0), (SUMOReal) 1);
121  pR->setProbability(newPR);
122  pS->setProbability(newPS);
123  }
124  }
125 }
126 
127 
128 SUMOReal
129 ROGawronCalculator::gawronF(const SUMOReal pdr, const SUMOReal pds, const SUMOReal x) const {
130  if (pdr * gawronG(myA, x) + pds == 0) {
132  }
133  return (pdr * (pdr + pds) * gawronG(myA, x)) /
134  (pdr * gawronG(myA, x) + pds);
135 }
136 
137 
138 SUMOReal
140  if (((1.0 - (x * x)) == 0)) {
142  }
143  return (SUMOReal) exp((a * x) / (1.0 - (x * x)));
144 }
145 
146 
147 
148 
150  const SUMOReal theta)
151  : myBeta(beta), myGamma(gamma), myTheta(theta) {}
152 
153 
155 
156 
157 void
158 ROLogitCalculator::setCosts(RORoute* route, const SUMOReal costs, const bool isActive) const {
159  route->setCosts(costs);
160 }
161 
162 
163 void
164 ROLogitCalculator::calculateProbabilities(const ROVehicle* const veh, std::vector<RORoute*> alternatives) {
165  const SUMOReal theta = myTheta >= 0 ? myTheta : getThetaForCLogit(alternatives);
166  const SUMOReal beta = myBeta >= 0 ? myBeta : getBetaForCLogit(alternatives);
167  if (beta > 0) {
168  // calculate commonalities
169  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
170  const RORoute* pR = *i;
171  SUMOReal lengthR = 0;
172  const std::vector<const ROEdge*> &edgesR = pR->getEdgeVector();
173  for (std::vector<const ROEdge*>::const_iterator edge = edgesR.begin(); edge != edgesR.end(); ++edge) {
174  //@todo we should use costs here
175  lengthR += (*edge)->getTravelTime(veh, STEPS2TIME(veh->getDepartureTime()));
176  }
177  SUMOReal overlapSum = 0;
178  for (std::vector<RORoute*>::const_iterator j = alternatives.begin(); j != alternatives.end(); j++) {
179  const RORoute* pS = *j;
180  SUMOReal overlapLength = 0.;
181  SUMOReal lengthS = 0;
182  const std::vector<const ROEdge*> &edgesS = pS->getEdgeVector();
183  for (std::vector<const ROEdge*>::const_iterator edge = edgesS.begin(); edge != edgesS.end(); ++edge) {
184  lengthS += (*edge)->getTravelTime(veh, STEPS2TIME(veh->getDepartureTime()));
185  if (std::find(edgesR.begin(), edgesR.end(), *edge) != edgesR.end()) {
186  overlapLength += (*edge)->getTravelTime(veh, STEPS2TIME(veh->getDepartureTime()));
187  }
188  }
189  overlapSum += pow(overlapLength / sqrt(lengthR * lengthS), myGamma);
190  }
191  myCommonalities[pR] = beta * log(overlapSum);
192  }
193  }
194  for (std::vector<RORoute*>::iterator i = alternatives.begin(); i != alternatives.end(); i++) {
195  RORoute* pR = *i;
196  SUMOReal weightedSum = 0;
197  for (std::vector<RORoute*>::iterator j = alternatives.begin(); j != alternatives.end(); j++) {
198  RORoute* pS = *j;
199  weightedSum += exp(theta * (pR->getCosts() - pS->getCosts() + myCommonalities[pR] - myCommonalities[pS]));
200  }
201  pR->setProbability(1. / weightedSum);
202  }
203 }
204 
205 
206 SUMOReal
207 ROLogitCalculator::getBetaForCLogit(const std::vector<RORoute*> alternatives) const {
209  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
210  const SUMOReal cost = (*i)->getCosts()/3600.;
211  if (cost < min) {
212  min = cost;
213  }
214  }
215  return min;
216 }
217 
218 
219 SUMOReal
220 ROLogitCalculator::getThetaForCLogit(const std::vector<RORoute*> alternatives) const {
221  // @todo this calculation works for travel times only
222  SUMOReal sum = 0.;
223  SUMOReal diff = 0.;
225  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
226  const SUMOReal cost = (*i)->getCosts()/3600.;
227  sum += cost;
228  if (cost < min) {
229  min = cost;
230  }
231  }
232  const SUMOReal meanCost = sum / SUMOReal(alternatives.size());
233  for (std::vector<RORoute*>::const_iterator i = alternatives.begin(); i != alternatives.end(); i++) {
234  diff += pow((*i)->getCosts()/3600. - meanCost, 2);
235  }
236  const SUMOReal cvCost = sqrt(diff / SUMOReal(alternatives.size())) / meanCost;
237  // @todo re-evaluate function
238 // if (cvCost > 0.04) { // Magic numbers from Lohse book
239  return PI / (sqrt(6.) * cvCost * (min + 1.1))/3600.;
240 // }
241 // return 1./3600.;
242 }
243 
244 
245 /****************************************************************************/