LCOV - code coverage report
Current view: top level - models/abm - personal_rng.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 7 100.0 %
Date: 2024-11-18 12:45:26 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* 
       2             : * Copyright (C) 2020-2024 MEmilio
       3             : *
       4             : * Authors: Daniel Abele, Elisabeth Kluth, David Kerkmann, Khoa Nguyen, Rene Schmieding
       5             : *
       6             : * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
       7             : *
       8             : * Licensed under the Apache License, Version 2.0 (the "License");
       9             : * you may not use this file except in compliance with the License.
      10             : * You may obtain a copy of the License at
      11             : *
      12             : *     http://www.apache.org/licenses/LICENSE-2.0
      13             : *
      14             : * Unless required by applicable law or agreed to in writing, software
      15             : * distributed under the License is distributed on an "AS IS" BASIS,
      16             : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      17             : * See the License for the specific language governing permissions and
      18             : * limitations under the License.
      19             : */
      20             : 
      21             : #ifndef MIO_ABM_PERSONAL_RNG_H
      22             : #define MIO_ABM_PERSONAL_RNG_H
      23             : 
      24             : #include "memilio/utils/random_number_generator.h"
      25             : #include "abm/person_id.h"
      26             : 
      27             : namespace mio
      28             : {
      29             : namespace abm
      30             : {
      31             : 
      32             : class Person;
      33             : 
      34             : /**
      35             :  * Random number generator of individual persons.
      36             :  * Increments the random number generator counter of the person when used.
      37             :  * Does not store its own key or counter.
      38             :  * Instead the key needs to be provided from the outside, so that the RNG
      39             :  * for all persons share the same key.
      40             :  * The counter is taken from the person.
      41             :  * PersonalRandomNumberGenerator is cheap to construct and transparent
      42             :  * for the compiler to optimize, so we don't store the RNG persistently, only the
      43             :  * counter, so we don't need to store the key in each person. This increases
      44             :  * consistency (if the key is changed after the person is created) and
      45             :  * reduces the memory required per person.
      46             :  * @see mio::RandomNumberGeneratorBase
      47             :  */
      48             : class PersonalRandomNumberGenerator : public mio::RandomNumberGeneratorBase<PersonalRandomNumberGenerator>
      49             : {
      50             : public:
      51             :     /**
      52             :      * Creates a RandomNumberGenerator for a person.
      53             :      * @param key Key to be used by the generator.
      54             :      * @param id Id of the Person.
      55             :      * @param counter Reference to the Person's RNG Counter.
      56             :      */
      57             :     PersonalRandomNumberGenerator(mio::Key<uint64_t> key, PersonId id, mio::Counter<uint32_t>& counter);
      58             : 
      59             :     /**
      60             :      * Creates a RandomNumberGenerator for a person.
      61             :      * Uses the same key as another RandomNumberGenerator.
      62             :      * @param rng RandomNumberGenerator who's key will be used.
      63             :      * @param person Reference to the Person who's counter will be used.
      64             :      */
      65             :     PersonalRandomNumberGenerator(const mio::RandomNumberGenerator& rng, Person& person);
      66             : 
      67             :     /**
      68             :      * @return Get the key.
      69             :      */
      70        2808 :     mio::Key<uint64_t> get_key() const
      71             :     {
      72        2808 :         return m_key;
      73             :     }
      74             : 
      75             :     /**
      76             :      * @return Get the current counter.
      77             :      */
      78        2826 :     mio::Counter<uint64_t> get_counter() const
      79             :     {
      80        5652 :         return mio::rng_totalsequence_counter<uint64_t>(m_person_id.get(), m_counter);
      81             :     }
      82             : 
      83             :     /**
      84             :      * Increment the counter.
      85             :      */
      86        2808 :     void increment_counter()
      87             :     {
      88        2808 :         ++m_counter;
      89        2808 :     }
      90             : 
      91             : private:
      92             :     mio::Key<uint64_t> m_key; ///< Global RNG Key
      93             :     PersonId m_person_id; ///< Id of the Person
      94             :     mio::Counter<uint32_t>& m_counter; ///< Reference to the Person's rng counter
      95             : };
      96             : 
      97             : } // namespace abm
      98             : } // namespace mio
      99             : 
     100             : #endif // MIO_ABM_PERSONAL_RNG_H

Generated by: LCOV version 1.14