Line data Source code
1 : /* 2 : * Copyright (C) 2020-2025 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 : #include <cstdint> 27 : 28 : namespace mio 29 : { 30 : namespace abm 31 : { 32 : 33 : class Person; 34 : 35 : /** 36 : * Random number generator of individual persons. 37 : * Increments the random number generator counter of the person when used. 38 : * Does not store its own key or counter. 39 : * Instead the key needs to be provided from the outside, so that the RNG 40 : * for all persons share the same key. 41 : * The counter is taken from the person. 42 : * PersonalRandomNumberGenerator is cheap to construct and transparent 43 : * for the compiler to optimize, so we don't store the RNG persistently, only the 44 : * counter, so we don't need to store the key in each person. This increases 45 : * consistency (if the key is changed after the person is created) and 46 : * reduces the memory required per person. 47 : * @see mio::RandomNumberGeneratorBase 48 : */ 49 : class PersonalRandomNumberGenerator : public mio::RandomNumberGeneratorBase<PersonalRandomNumberGenerator> 50 : { 51 : public: 52 : /** 53 : * Creates a RandomNumberGenerator for a person. 54 : * @param key Key to be used by the generator. 55 : * @param index index of the Person. 56 : * @param counter Reference to the Person's RNG Counter. 57 : */ 58 : PersonalRandomNumberGenerator(mio::Key<uint64_t> key, uint32_t index, mio::Counter<uint32_t>& counter); 59 : 60 : /** 61 : * Creates a RandomNumberGenerator for a person. 62 : * @param person Reference to the Person who's counter will be used. 63 : */ 64 : PersonalRandomNumberGenerator(Person& person); 65 : 66 : /** 67 : * @return Get the key. 68 : */ 69 2627 : mio::Key<uint64_t> get_key() const 70 : { 71 2627 : return m_key; 72 : } 73 : 74 : /** 75 : * @return Get the current counter. 76 : */ 77 2645 : mio::Counter<uint64_t> get_counter() const 78 : { 79 5290 : return mio::rng_totalsequence_counter<uint64_t>(m_person_index, m_counter); 80 : } 81 : 82 : /** 83 : * Increment the counter. 84 : */ 85 2627 : void increment_counter() 86 : { 87 2627 : ++m_counter; 88 2627 : } 89 : 90 : private: 91 : mio::Key<uint64_t> m_key; ///< Global RNG Key 92 : uint32_t m_person_index; ///< Index of the Person 93 : mio::Counter<uint32_t>& m_counter; ///< Reference to the Person's rng counter 94 : }; 95 : 96 : } // namespace abm 97 : } // namespace mio 98 : 99 : #endif // MIO_ABM_PERSONAL_RNG_H