LCOV - code coverage report
Current view: top level - src/utils/router - GawronCalculator.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 93.1 % 29 27
Test Date: 2024-11-22 15:46:21 Functions: 80.0 % 5 4

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    GawronCalculator.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @author  Jakob Erdmann
      18              : /// @date    Sept 2002
      19              : ///
      20              : // Calculators for route costs and probabilities
      21              : /****************************************************************************/
      22              : #pragma once
      23              : #include <config.h>
      24              : 
      25              : #include <vector>
      26              : #include <map>
      27              : 
      28              : 
      29              : // ===========================================================================
      30              : // class definitions
      31              : // ===========================================================================
      32              : /**
      33              :  * @class GawronCalculator
      34              :  * @brief Cost calculation with Gawron's method.
      35              :  */
      36              : template<class R, class E, class V>
      37              : class GawronCalculator : public RouteCostCalculator<R, E, V> {
      38              : public:
      39              :     /// Constructor
      40         3569 :     GawronCalculator(const double beta, const double a) : myBeta(beta), myA(a) {}
      41              : 
      42              :     /// Destructor
      43         3569 :     virtual ~GawronCalculator() {}
      44              : 
      45       225839 :     void setCosts(R* route, const double costs, const bool isActive = false) const {
      46       225839 :         if (isActive) {
      47        85154 :             route->setCosts(costs);
      48              :         } else {
      49       140685 :             route->setCosts(myBeta * costs + ((double) 1.0 - myBeta) * route->getCosts());
      50              :         }
      51       225839 :     }
      52              : 
      53              :     /** @brief calculate the probabilities */
      54        85154 :     void calculateProbabilities(std::vector<R*> alternatives, const V* const /* veh */, const SUMOTime /* time */) {
      55       225839 :         for (typename std::vector<R*>::iterator i = alternatives.begin(); i != alternatives.end() - 1; i++) {
      56       140685 :             R* pR = *i;
      57       345286 :             for (typename std::vector<R*>::iterator j = i + 1; j != alternatives.end(); j++) {
      58       204601 :                 R* pS = *j;
      59              :                 // see [Gawron, 1998] (4.2)
      60       204601 :                 const double delta =
      61       204601 :                     (pS->getCosts() - pR->getCosts()) /
      62       204601 :                     (pS->getCosts() + pR->getCosts());
      63              :                 // see [Gawron, 1998] (4.3a, 4.3b)
      64       204601 :                 double newPR = gawronF(pR->getProbability(), pS->getProbability(), delta);
      65       204601 :                 double newPS = pR->getProbability() + pS->getProbability() - newPR;
      66       204601 :                 if (std::isnan(newPR) || std::isnan(newPS)) {
      67              :                     newPR = pS->getCosts() > pR->getCosts()
      68            0 :                             ? (double) 1. : 0;
      69              :                     newPS = pS->getCosts() > pR->getCosts()
      70            0 :                             ? 0 : (double) 1.;
      71              :                 }
      72              :                 newPR = MIN2((double) MAX2(newPR, (double) 0), (double) 1);
      73              :                 newPS = MIN2((double) MAX2(newPS, (double) 0), (double) 1);
      74       204601 :                 pR->setProbability(newPR);
      75       204601 :                 pS->setProbability(newPS);
      76              :             }
      77              :         }
      78        85154 :     }
      79              : 
      80              : private:
      81              :     /** @brief Performs the gawron - f() function
      82              :         From "Dynamic User Equilibria..." */
      83       204601 :     double gawronF(const double pdr, const double pds, const double x) const {
      84       204601 :         if (pdr * gawronG(myA, x) + pds == 0) {
      85              :             return std::numeric_limits<double>::max();
      86              :         }
      87       409202 :         return (pdr * (pdr + pds) * gawronG(myA, x)) /
      88       204601 :                (pdr * gawronG(myA, x) + pds);
      89              :     }
      90              : 
      91              :     /** @brief Performs the gawron - g() function
      92              :         From "Dynamic User Equilibria..." */
      93              :     double gawronG(const double a, const double x) const {
      94       613803 :         if (((1.0 - (x * x)) == 0)) {
      95              :             return std::numeric_limits<double>::max();
      96              :         }
      97       613803 :         return (double) exp((a * x) / (1.0 - (x * x)));
      98              :     }
      99              : 
     100              : private:
     101              :     /// @brief gawron beta - value
     102              :     const double myBeta;
     103              : 
     104              :     /// @brief gawron a - value
     105              :     const double myA;
     106              : 
     107              : private:
     108              :     /** @brief invalidated assignment operator */
     109              :     GawronCalculator& operator=(const GawronCalculator& s);
     110              : 
     111              : };
        

Generated by: LCOV version 2.0-1