Evolutionary Diversification in Anolis Lizards
individual.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 #ifndef INDIVIDUAL_HPP_
6 #define INDIVIDUAL_HPP_
7 
8 #include <cmath>
9 #include <iosfwd>
10 #include <string>
11 #include <vector>
12 #include <map>
13 #include <bitset>
14 
15 namespace wtl {class sfmt19937_64;}
16 
18 namespace edal {
19 
20 namespace trait {
21 enum {
24  limb_length,
25 
28  diameter_preference,
29 
32  female_trait,
33  choosiness,
34 
37 
40 };
41 } // namespace trait
42 
43 
45 
50 
51 
53  double BETA_PARAM = 3.0;
54 
56  size_t CARRYING_CAPACITY = 1000u;
57 
59  double AVG_NUM_OFFSPINRGS = 4.0;
60 
62  double HEIGHT_PREFERENCE = 2.0;
63 
65  double DIAMETER_PREFERENCE = 2.0;
66 
68  double TOEPAD_SELECTION = 2.0;
69 
71  double LIMB_SELECTION = 2.0;
72 
74  double PREF_COMPETITION = 2.0;
75 
77  double MORPH_COMPETITION = 2.0;
78 
80  double MATING_SIGMA = 0.05;
81 
83 
86  double MU_LOCUS = 1e-4;
87 
89 
91  unsigned long MUTATION_MASK = 0;
92 
94  double MIGRATION_RATE = 0.005;
95 
97  constexpr static double NORMAL_SIGMA = 0.3;
98 
100  constexpr static double C0 = 1.0;
101 
103  constexpr static double C1 = 0.5;
105 };
106 
125 class Individual {
126  public:
130  using URBG = wtl::sfmt19937_64;
131 
132  private:
134  constexpr static size_t NUM_LOCI_ = 8;
135 
137  constexpr static unsigned long FULL_BITS = (1u << NUM_LOCI_) - 1u;
138 
140  constexpr static unsigned long HALF_BITS = (1u << (NUM_LOCI_ / 2u)) - 1u;
141 
143  constexpr static double INV_NUM_LOCI_ = 0.5 / NUM_LOCI_;
144 
146  constexpr static size_t NUM_STEPS_ = 32;
147 
149  static std::map<std::vector<double>, double> KE_CACHE_;
150 
152  public:
154  using Loci = std::bitset<NUM_LOCI_>;
155 
160 
162  Individual(const std::vector<Loci>& egg,
163  const std::vector<Loci>& sperm,
164  const bool is_male=false):
165  genotype_{egg, sperm},
167  sex_(is_male),
169 
171  Individual(const std::vector<unsigned long>&);
172 
174 
177 
185 
187 
190 
192 
195 
197  double effective_carrying_capacity_cache() const;
198 
200 
211  double preference_overlap(const Individual& other) const;
212 
214 
224  double morphology_overlap(const Individual& other) const;
225 
227 
229  double resource_overlap(const Individual& other) const {
230  return std::exp(preference_overlap(other) + morphology_overlap(other));
231  }
232 
234 
250  double survival_probability(const double effective_num_competitors) const;
251 
253 
265  double mating_probability(const Individual& male) const;
266 
268 
280  double mating_probability_debarre(const Individual& male) const;
281 
283 
295  double mating_probability_TPG2013(const Individual& male) const;
296 
298 
301  std::vector<Loci> gametogenesis(URBG&) const;
302 
304 
307  bool is_male() const {return sex_;}
308 
310  void change_sex() {sex_ = !sex_;}
311 
313  bool operator<(const Individual& other) const {
314  return genotype_ < other.genotype_;
315  }
316 
318  static std::string header();
319 
321  std::string str_detail() const;
322 
323  friend std::ostream& operator<< (std::ostream& ost, const Individual& ind);
324 
326  static void write_resource_abundance();
328  static std::string possible_phenotypes();
330  static std::string possible_geographic();
331 
333  static void param(const param_type& p) {PARAM_ = p;}
335  static const param_type& param() {return PARAM_;}
336 
338  private:
340 
343 
353  double habitat_preference_exp(const double height, const double diameter) const;
354 
356 
364  double habitat_preference_quadratic(const double height, const double diameter) const;
365 
367 
379  double calc_DI_analytical() const;
380 
382 
388  double calc_DI_numerical() const;
389 
391 
400  double calc_Dxi_analytical() const;
401 
403 
409  double calc_Dxi_numerical() const;
410 
412 
421  double fitness(const double height, const double diameter) const;
422 
424 
427 
429  std::vector<double> calc_phenotype() const {
430  constexpr size_t n = trait::size;
431  std::vector<double> output(n);
432  for (size_t i=0; i<n; ++i) {
433  output[i] = (genotype_.first[i].count() + genotype_.second[i].count()) * INV_NUM_LOCI_;
434  }
435  return output;
436  }
437 
439  std::vector<double> intermediate_phenotypes() const;
440 
442  static const std::vector<std::string> INTERMEDIATE_KEYS_;
443 
444  friend double pdf_beta(const double height, const double diameter);
445  friend double pdf_normal(const double height, const double diameter);
446  friend double pdf_exp(const double height, const double diameter);
447  template <class Func> friend double integrate_triangle(Func&& func);
448  template <class Func> friend double integrate_square(Func&& func);
449 
451  // data member
452 
454  std::pair<std::vector<Loci>, std::vector<Loci> > genotype_;
455 
457  std::vector<double> phenotype_;
458 
460  bool sex_ = false;
461 
463  double ke_;
464 
467 };
468 
470 extern std::ostream& operator<<(std::ostream& ost, const Individual::Loci& bs);
471 
472 } // namespace edal
473 
474 namespace std {
476 inline bool operator< (const edal::Individual::Loci& lhs, const edal::Individual::Loci& rhs) {
477  return lhs.to_ulong() < rhs.to_ulong();
478 }
479 }
480 
481 #endif /* INDIVIDUAL_HPP_ */
sexual, diploid, additive, unlinked, diallelic
Definition: individual.hpp:125
double mating_probability_debarre(const Individual &male) const
with Debarre 2012
Definition: individual.cpp:273
morphological characters
Definition: individual.hpp:23
double DIAMETER_PREFERENCE
in
Definition: individual.hpp:65
double calc_DI_analytical() const
analytical computation by Mathematica (fast but limited)
Definition: individual.cpp:135
void change_sex()
Change sex_.
Definition: individual.hpp:310
double survival_probability(const double effective_num_competitors) const
Probability of survival .
Definition: individual.cpp:239
double MORPH_COMPETITION
in
Definition: individual.hpp:77
double morphology_overlap(const Individual &other) const
Exponent of : competition on morphology.
Definition: individual.cpp:230
double TOEPAD_SELECTION
in
Definition: individual.hpp:68
friend double pdf_beta(const double height, const double diameter)
Beta distribution function for resource abundance.
Definition: individual.cpp:27
static constexpr unsigned long HALF_BITS
Genotype that produce trait value = 0.5, i.e., 00001111
Definition: individual.hpp:140
friend std::ostream & operator<<(std::ostream &ost, const Individual &ind)
put CSV on ostream
Definition: individual.cpp:348
friend double pdf_normal(const double height, const double diameter)
Distribution of tree height given diameter.
Definition: individual.cpp:52
Definition: individual.hpp:474
size_t CARRYING_CAPACITY
in
Definition: individual.hpp:56
static constexpr size_t NUM_STEPS_
Precision of numerical integration.
Definition: individual.hpp:146
double HEIGHT_PREFERENCE
in
Definition: individual.hpp:62
double effective_carrying_capacity_exp_unnormalized() const
with exponential
Definition: individual.cpp:193
double BETA_PARAM
of Beta distribution in
Definition: individual.hpp:53
double MIGRATION_RATE
Migration rate (i.e., makes the expected # of migrants)
Definition: individual.hpp:94
to calculate genetic divergence
Definition: individual.hpp:36
double MATING_SIGMA
in
Definition: individual.hpp:80
static param_type PARAM_
Parameters shared among instances.
Definition: individual.hpp:466
static std::string possible_phenotypes()
test function
Definition: individual.cpp:442
double ke_
calculated in advance
Definition: individual.hpp:463
double habitat_preference_exp(const double height, const double diameter) const
Exponent of in anolis_v2.
Definition: individual.cpp:116
double calc_DI_numerical() const
numerical computation (slow)
Definition: individual.cpp:149
double MU_LOCUS
Mutation rate per locus per generation.
Definition: individual.hpp:86
Individual()
Default constructor for original individuals.
Definition: individual.hpp:157
double preference_overlap(const Individual &other) const
Exponent of : competition on habitat preference.
Definition: individual.cpp:221
friend double pdf_exp(const double height, const double diameter)
Distribution of twig diameter given height.
Definition: individual.cpp:58
static void write_resource_abundance()
test function
Definition: individual.cpp:432
double mating_probability_TPG2013(const Individual &male) const
with Thibert-Plante and Gavrilets 2013
Definition: individual.cpp:299
double LIMB_SELECTION
in
Definition: individual.hpp:71
static std::map< std::vector< double >, double > KE_CACHE_
Store with ecological traits as keys.
Definition: individual.hpp:149
bool sex_
female=0, male=1
Definition: individual.hpp:460
Definition: individual.hpp:15
double habitat_preference_quadratic(const double height, const double diameter) const
quadratic approximation in anolis_v3
Definition: individual.cpp:123
std::vector< double > phenotype_
All traits are scaled to be between 0 and 1.
Definition: individual.hpp:457
std::ostream & operator<<(std::ostream &ost, const Individual &ind)
put CSV on ostream
Definition: individual.cpp:348
double PREF_COMPETITION
in
Definition: individual.hpp:74
std::string str_detail() const
detailed str() for testing/debugging
Definition: individual.cpp:399
mating compatibility characters
Definition: individual.hpp:31
Parameters for Individual class.
Definition: individual.hpp:47
double calc_Dxi_analytical() const
Calculate quadratic normalizer with analytical solution.
Definition: individual.cpp:156
Definition: individual.cpp:15
double AVG_NUM_OFFSPINRGS
in
Definition: individual.hpp:59
friend double integrate_square(Func &&func)
Integral over .
Definition: individual.cpp:83
static void param(const param_type &p)
Set PARAM_.
Definition: individual.hpp:333
static constexpr double C0
of Normal distribution in v2
Definition: individual.hpp:100
double effective_carrying_capacity_old_exp_unnormalized() const
with old resource distribution and exponential before normalization
Definition: individual.cpp:202
std::vector< double > intermediate_phenotypes() const
aggregation of intermediate phenotype values
Definition: individual.cpp:377
double effective_carrying_capacity_cache() const
Find in KE_CACHE_ or calculate.
Definition: individual.cpp:211
double mating_probability(const Individual &male) const
Definition: individual.cpp:248
for computational convenience
Definition: individual.hpp:39
std::vector< Loci > gametogenesis(URBG &) const
Gametogenesis with free recombination and mutation.
Definition: individual.cpp:324
static const std::vector< std::string > INTERMEDIATE_KEYS_
labels for intermediate phenotypes
Definition: individual.hpp:442
double resource_overlap(const Individual &other) const
for competition
Definition: individual.hpp:229
unsigned long MUTATION_MASK
Flag set to protect specific traits from mutations.
Definition: individual.hpp:91
static std::string possible_geographic()
test function
Definition: individual.cpp:464
static constexpr double NORMAL_SIGMA
of Normal distribution in v2
Definition: individual.hpp:97
Individual(const std::vector< Loci > &egg, const std::vector< Loci > &sperm, const bool is_male=false)
Constructor for sexual reproduction.
Definition: individual.hpp:162
std::bitset< NUM_LOCI_ > Loci
Alias for diallelic loci of a trait.
Definition: individual.hpp:154
habitat preference characters
Definition: individual.hpp:27
double fitness(const double height, const double diameter) const
Exponent of : measure adaptation to habitat.
Definition: individual.cpp:177
static const param_type & param()
Get PARAM_.
Definition: individual.hpp:335
friend double integrate_triangle(Func &&func)
Integral over and .
Definition: individual.cpp:73
static constexpr unsigned long FULL_BITS
Genotype that produce trait value = 1.0, i.e., 11111111
Definition: individual.hpp:137
static std::string header()
The header correspongs to str()
Definition: individual.cpp:356
bool operator<(const Individual &other) const
Operator required to be std::map key.
Definition: individual.hpp:313
static constexpr size_t NUM_LOCI_
The number of loci per trait.
Definition: individual.hpp:134
std::vector< double > calc_phenotype() const
calculates phenotypic values from genotype
Definition: individual.hpp:429
std::pair< std::vector< Loci >, std::vector< Loci > > genotype_
Unlinked diallelic loci with equal effects.
Definition: individual.hpp:454
wtl::sfmt19937_64 URBG
Uniform Random Number Generator.
Definition: individual.hpp:130
double effective_carrying_capacity_quad_unnormalized() const
with quadratic before normalization (v3u)
Definition: individual.cpp:184
double calc_Dxi_numerical() const
Numerical integration of quadratic .
Definition: individual.cpp:171
static constexpr double INV_NUM_LOCI_
Compile-time constant value used in init_phenotype()
Definition: individual.hpp:143
bool is_male() const
Get sex_.
Definition: individual.hpp:307
static constexpr double C1
of Normal distribution in v2
Definition: individual.hpp:103