tekka
individual.hpp
Go to the documentation of this file.
1
4#pragma once
5#ifndef PBT_INDIVIDUAL_HPP_
6#define PBT_INDIVIDUAL_HPP_
7
8#include "random_fwd.hpp"
9
10#include <cstdint>
11#include <iosfwd>
12#include <memory>
13#include <vector>
14#include <unordered_map>
15
17
18namespace pbf {
19
21
24 friend class Individual;
25 public:
29 void read(std::istream&);
31 std::ostream& write(std::ostream&) const;
32 private:
34 using RowMatrix = std::vector<std::vector<double>>;
37
39 std::vector<double> natural_mortality = {};
41 std::vector<double> fishing_mortality = {};
44 std::vector<double> fishing_coef = {};
46 std::vector<double> weight_for_age = {};
48 std::vector<RowMatrix> migration_matrices = {};
50};
51
55 public:
57 Individual() = delete;
59 explicit Individual(bool is_male) noexcept: is_male_(is_male) {}
61 Individual(const std::shared_ptr<Individual>& father,
62 const std::shared_ptr<Individual>& mother,
63 int_fast32_t year, bool is_male) noexcept
64 : father_(father), mother_(mother),
66 Individual(const Individual&) = delete;
67 ~Individual() = default;
68
70 double death_rate(const int_fast32_t year, const int_fast32_t season) const {
71 const auto q_age = 4 * age(year) + season;
72 return DEATH_RATE(q_age, year);
73 }
74
76 uint_fast32_t migrate(uint_fast32_t loc, int_fast32_t year, URBG&);
77
79 void trace_back(std::ostream& ost, std::unordered_map<const Individual*, uint_fast32_t>* ids,
80 uint_fast32_t loc, int_fast32_t year) const;
82 std::ostream& write(std::ostream&) const;
84 std::ostream& write(std::ostream&, const std::unordered_map<const Individual*, uint_fast32_t>&) const;
86 static std::ostream& write_trace_back_header(std::ostream&);
88 static std::ostream& write_names(std::ostream&);
89 friend std::ostream& operator<<(std::ostream&, const Individual&);
90
92 constexpr static inline int_fast32_t MAX_AGE = 80;
94 static inline IndividualJson JSON;
96 static void set_dependent_static(const uint_fast32_t years);
98 static bool is_ready(const uint_fast32_t years) {
99 return (
100 FISHING_COEF_.size() >= years
101 && JSON.natural_mortality.size() >= 4u * MAX_AGE
102 && JSON.fishing_mortality.size() >= 4u * MAX_AGE
103 && WEIGHT_FOR_AGE_.size() >= MAX_AGE
105 );
106 }
108 static double DEATH_RATE(const int_fast32_t q_age, const int_fast32_t year) {
109 const auto m = JSON.natural_mortality[q_age];
110 const auto f = JSON.fishing_mortality[q_age] * FISHING_COEF_[year];
111 return 1.0 - std::exp(-m - f);
112 }
113
116
118 int_fast32_t age(const int_fast32_t year) const noexcept {
119 return year - birth_year_;
120 }
122 double weight(int_fast32_t year) const noexcept {
123 return WEIGHT_FOR_AGE_[age(year)];
124 }
126 bool is_male() const noexcept {return is_male_;}
128
129 private:
131 using PairDestDist=std::pair<int_fast32_t, std::discrete_distribution<uint_fast32_t>>;
133 static void set_static_migration();
135 static void set_static_mortality();
137 static void set_static_weight();
139 static inline std::vector<double> FISHING_COEF_;
141 static inline std::vector<double> WEIGHT_FOR_AGE_;
143 static inline std::vector<std::vector<PairDestDist>> MIGRATION_DESTINATION_;
144
146 const std::shared_ptr<Individual> father_ = nullptr;
148 const std::shared_ptr<Individual> mother_ = nullptr;
150 int_fast32_t birth_year_ = -4;
152 const bool is_male_;
153};
154
155} // namespace pbf
156
157#endif /* PBT_INDIVIDUAL_HPP_ */
Individual class.
Definition individual.hpp:54
Individual(bool is_male) noexcept
for initial population
Definition individual.hpp:59
Individual()=delete
Alias.
static std::ostream & write_names(std::ostream &)
Write column names for write()
Definition individual.cpp:70
double death_rate(const int_fast32_t year, const int_fast32_t season) const
Finite death rate per quarter year: .
Definition individual.hpp:70
static std::vector< double > FISHING_COEF_
Fluctuation of fishing mortality by year.
Definition individual.hpp:139
static IndividualJson JSON
Parameters shared among instances (JSON file).
Definition individual.hpp:94
uint_fast32_t migrate(uint_fast32_t loc, int_fast32_t year, URBG &)
Generate random number for new location.
Definition individual.cpp:48
Individual(const std::shared_ptr< Individual > &father, const std::shared_ptr< Individual > &mother, int_fast32_t year, bool is_male) noexcept
for sexual reproduction
Definition individual.hpp:61
std::ostream & write(std::ostream &) const
Write all the data members in TSV.
Definition individual.cpp:77
static std::vector< std::vector< PairDestDist > > MIGRATION_DESTINATION_
Discrete distributions for migration.
Definition individual.hpp:143
static void set_static_migration()
Prepare MIGRATION_DESTINATION_.
Definition individual.cpp:96
const std::shared_ptr< Individual > mother_
Pointer to mother.
Definition individual.hpp:148
static void set_static_weight()
Prepare WEIGHT_FOR_AGE_.
Definition individual.cpp:117
int_fast32_t age(const int_fast32_t year) const noexcept
Age in the given year.
Definition individual.hpp:118
static double DEATH_RATE(const int_fast32_t q_age, const int_fast32_t year)
Finite death rate per quarter year.
Definition individual.hpp:108
int_fast32_t birth_year_
Year of birth.
Definition individual.hpp:150
static constexpr int_fast32_t MAX_AGE
No individual lives longer than this.
Definition individual.hpp:92
friend std::ostream & operator<<(std::ostream &, const Individual &)
Shortcut of Individual::write.
Definition individual.cpp:92
const bool is_male_
Sex.
Definition individual.hpp:152
bool is_male() const noexcept
Just returns is_male_.
Definition individual.hpp:126
static bool is_ready(const uint_fast32_t years)
Test if static variables are ready.
Definition individual.hpp:98
static void set_dependent_static(const uint_fast32_t years)
Set static variables that depend on others.
Definition individual.cpp:126
static std::ostream & write_trace_back_header(std::ostream &)
Write column names for trace_back()
Definition individual.cpp:66
static void set_static_mortality()
Prepare mortality vectors.
Definition individual.cpp:111
const std::shared_ptr< Individual > father_
Pointer to father.
Definition individual.hpp:146
static std::vector< double > WEIGHT_FOR_AGE_
year version of IndividualJson::weight_for_age
Definition individual.hpp:141
double weight(int_fast32_t year) const noexcept
Weight for year age.
Definition individual.hpp:122
std::pair< int_fast32_t, std::discrete_distribution< uint_fast32_t > > PairDestDist
Alias for readability.
Definition individual.hpp:131
void trace_back(std::ostream &ost, std::unordered_map< const Individual *, uint_fast32_t > *ids, uint_fast32_t loc, int_fast32_t year) const
Write ancestors recursively.
Definition individual.cpp:53
Parameters for Individual class (JSON file)
Definition individual.hpp:23
void read(std::istream &)
Read class variables from stream in json.
Definition individual.cpp:142
std::vector< std::vector< double > > RowMatrix
Alias for readability.
Definition individual.hpp:34
std::ostream & write(std::ostream &) const
Write class variables to stream in json.
Definition individual.cpp:152
IndividualJson()
Constructor.
Definition individual.cpp:137
std::vector< RowMatrix > migration_matrices
Transition matrix for migration.
Definition individual.hpp:48
std::vector< double > fishing_mortality
Array of for quarter age: instantaneous mortality due to fishing activities.
Definition individual.hpp:41
std::vector< double > fishing_coef
Definition individual.hpp:44
std::vector< double > weight_for_age
Weight in kg for quarter age.
Definition individual.hpp:46
std::vector< double > natural_mortality
Array of for quarter age: instantaneous mortality due to natural causes.
Definition individual.hpp:39