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

          Line data    Source code
       1             : /*
       2             : * Copyright (C) 2020-2024 MEmilio
       3             : *
       4             : * Authors: Daniel Abele, Sascha Korf, Khoa Nguyen
       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             : #include "abm/household.h"
      22             : #include "abm/person_id.h"
      23             : #include "abm/location.h"
      24             : #include "memilio/utils/random_number_generator.h"
      25             : 
      26             : namespace mio
      27             : {
      28             : namespace abm
      29             : {
      30             : 
      31             : namespace
      32             : {
      33             : /**
      34             :  * @brief Picks an age from a CustomIndexArray with a weight for each AgeGroup according to a discrete distribution.
      35             :  * @param[in] age_groups A CustomIndexArray with the weights.
      36             :  * @return The picked AgeGroup.
      37             :  */
      38         936 : AgeGroup pick_age_group_from_age_distribution(RandomNumberGenerator& rng,
      39             :                                               const CustomIndexArray<int, AgeGroup>& age_groups)
      40             : {
      41         936 :     auto age_group_weights = age_groups.array().cast<double>().eval();
      42         936 :     size_t age_group       = DiscreteDistribution<size_t>::get_instance()(rng, age_group_weights);
      43        1872 :     return (AgeGroup)age_group;
      44         936 : }
      45             : } // namespace
      46             : 
      47          54 : void Household::add_members(HouseholdMember household_member, int number_of_members)
      48             : {
      49          54 :     m_household_member_list.push_back(std::make_tuple(household_member, number_of_members));
      50          54 :     m_number_of_members += number_of_members;
      51          54 : }
      52             : 
      53          18 : void HouseholdGroup::add_households(Household household, int number_of_households)
      54             : {
      55          18 :     m_household_list.push_back(std::make_tuple(household, number_of_households));
      56          18 :     m_number_of_households += number_of_households;
      57          18 : }
      58             : 
      59         144 : void add_household_to_model(Model& model, const Household& household)
      60             : {
      61         144 :     auto home    = model.add_location(LocationType::Home);
      62         144 :     auto members = household.get_members();
      63         288 :     model.get_location(home).set_capacity(household.get_total_number_of_members(),
      64         144 :                                           household.get_total_number_of_members() * household.get_space_per_member());
      65             : 
      66         432 :     for (auto& memberTouple : members) {
      67         288 :         int count;
      68         288 :         HouseholdMember member  = HouseholdMember(model.parameters.get_num_groups());
      69         288 :         std::tie(member, count) = memberTouple;
      70        1224 :         for (int j = 0; j < count; j++) {
      71         936 :             auto age_group = pick_age_group_from_age_distribution(model.get_rng(), member.get_age_weights());
      72         936 :             auto person    = model.add_person(home, age_group);
      73         936 :             model.assign_location(person, home);
      74             :         }
      75         288 :     }
      76         288 : }
      77             : 
      78           9 : void add_household_group_to_model(Model& model, const HouseholdGroup& household_group)
      79             : {
      80           9 :     auto households = household_group.get_households();
      81             : 
      82          27 :     for (auto& householdTuple : households) {
      83          18 :         int count;
      84          18 :         Household household;
      85          18 :         std::tie(household, count) = householdTuple;
      86         153 :         for (int j = 0; j < count; j++) {
      87         135 :             add_household_to_model(model, household);
      88             :         }
      89          18 :     }
      90          18 : }
      91             : 
      92             : } // namespace abm
      93             : } // namespace mio

Generated by: LCOV version 1.14