Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
LCEselection.h
Go to the documentation of this file.
1 
30 #ifndef LCE_SELECTION_H
31 #define LCE_SELECTION_H
32 
33 #include <cmath>
34 #include "lifecycleevent.h"
35 #include "filehandler.h"
36 #ifdef HAS_GSL
37 #include <gsl/gsl_vector.h>
38 #include <gsl/gsl_matrix.h>
39 #endif
40 
41 class LCE_SelectionSH;
42 class LCE_SelectionFH;
43 //CLASS LCE_Selection_base
44 //
50 class LCE_Selection_base : public virtual LifeCycleEvent
51 {
54 
55  vector< TMatrix* > _selection_matrix;
56  vector< gsl_matrix* > _gsl_selection_matrix;
57  gsl_vector *_diffs,
65 
67  //double *_phe; // do we need this here?
68 
70 
74  vector<double> _mean_trait_value;
75  unsigned int _linearSelTraitIndex;
78 
83 
89 
92 
93  double _letheq;
94 
96  double _Fpedigree[5];
97 
100  double _FitnessFixModel[5];
101 
103 
104 
106  friend class LCE_SelectionSH;
107  friend class LCE_SelectionFH;
108  friend class LCE_Breed_Selection;
109 
110 protected:
111 
114  bool _is_local,
116 
118  double _fitness[5], _survival[5], _ind_cntr[5];
119 
122 
125 
127  vector<unsigned int> _selectTraitMapping;
128 
130  vector< double > _selection_variance;
131 
133  double _eVariance;
135 
137  vector< string > _Traits;
138 
140  vector< unsigned int > _TraitIndices;
141 
143  vector< string > _SelectionModels;
144 
147 
148  vector< double (LCE_Selection_base::* ) (Individual*, unsigned int, unsigned int) > _getRawFitness;
149 
152  double (LCE_Selection_base::* _getFitness) (Individual*, unsigned int);
153 
157  void (LCE_Selection_base::* _setScalingFactor) (age_idx, unsigned int);
158 
162 
164 
167 
168 public:
169 
170  LCE_Selection_base ( );
171 
172  virtual ~LCE_Selection_base ( );
173 
174  bool setSelectionMatrix();
175  bool setSelectTraitMapping(unsigned int num_quanti_traits);
176  bool setSelectionOffset(double default_val, double min_val);
177  bool setLocalOptima ();
178  const TMatrix& getLocalOptima () const {return _local_optima;}
179  bool set_sel_model ();
180  bool set_fit_model ();
181  bool set_local_optima ();
183 
184  void set_std_rate_of_change();
185  void addPhenotypicSD (unsigned int deme, double *stDev);
186  void changeLocalOptima ();
187 
189  void resetCounters ();
190 
194  void setMeans (unsigned int tot_ind);
195 
201  void updateFitnessCounters (double fitness, unsigned int ped_class, bool survived);
202 
205  double getMeanFitness (age_idx age);
206 
210  double getMeanPatchFitness (age_idx age, unsigned int p);
211 
214  double getMaxFitness (age_idx age);
215 
219  double getMaxPatchFitness (age_idx age, unsigned int p);
220 
221 
224  double setMeanFitness (age_idx age) { return (_mean_fitness = getMeanFitness(age));}
225 
230  double getFitness (Individual* ind, unsigned int patch)
231  {
232  return (this->*_getFitness)(ind, patch);
233  }
234 
236  double getFitnessFixedEffect (Individual* offsprg, unsigned int patch, unsigned int trait)
237  {
238  return _FitnessFixModel[ offsprg->getPedigreeClass() ];
239  }
240 
242  double getFitnessDirect (Individual* offsprg, unsigned int patch, unsigned int trait)
243  {
244  return *(double*)offsprg->getTraitValue(trait);
245  }
247  double getFitnessUnivariateQuadratic ( Individual* ind, unsigned int patch, unsigned int trait);
248 
250  double getFitnessUnivariateLinear (Individual* offsprg, unsigned int patch, unsigned int trait);
251 
253  double getFitnessUnivariateDisruptive (Individual* offsprg, unsigned int patch, unsigned int trait);
254 
256  double getFitnessMultivariateDisruptive (Individual* offsprg, unsigned int patch, unsigned int trait);
257 
260  double getFitnessTruncation (Individual* offsprg, unsigned int patch, unsigned int trait);
261 
263  double getFitnessMultivariateGaussian (Individual* offsprg, unsigned int patch, unsigned int trait);
264 
266  double getFitnessUnivariateGaussian (Individual* offsprg, unsigned int patch, unsigned int trait);
267 
269  double getFitnessMultivariateGaussian_VE (Individual* offsprg, unsigned int patch, unsigned int trait);
270 
272  double getFitnessUnivariateGaussian_VE (Individual* offsprg, unsigned int patch, unsigned int trait);
273 
274 
279  double getFitnessAbsolute (Individual* ind, unsigned int patch);
280 
288  double getFitnessRelative (Individual* ind, unsigned int patch)
289  {
290  return getFitnessAbsolute(ind, patch) * _scaling_factor;
291  }
292 
296  void setScalingFactorLocal (age_idx age, unsigned int p);
297 
303  void setScalingFactorGlobal (age_idx age, unsigned int p);
304 
308  void setScalingFactorMaxLocal (age_idx age, unsigned int p);
309 
315  void setScalingFactorMaxGlobal (age_idx age, unsigned int p);
316 
321  void setScalingFactorForLinearSelection (age_idx age, unsigned int p);
322 
326  void setScalingFactorAbsolute (age_idx age, unsigned int p) { _scaling_factor = 1; }
327 
335  void doViabilitySelection (sex_t SEX, age_idx AGE, Patch* patch, unsigned int p);
336 
338  void checkChangeLocalOptima();
339 
342  virtual bool setParameters ();
343  virtual void execute ();
344  virtual void loadStatServices (StatServices* loader);
345  virtual void loadFileServices (FileServices* loader);
346  virtual bool resetParameterFromSource (std::string param, SimComponent* cmpt) {return false;}
347  virtual LifeCycleEvent* clone () {return new LCE_Selection_base();}
348  virtual age_t removeAgeClass () {return NONE;}
349  virtual age_t addAgeClass () {return NONE;}
350  virtual age_t requiredAgeClass () {return OFFSPRG;}
352 };
353 
354 
356 class LCE_SelectionSH : public EventStatHandler< LCE_Selection_base, LCE_SelectionSH >
357 {
358 
359  vector< vector< double > > _phenoTable;
361 
362 public:
363 
366  _table_set_gen(999999), _table_set_age(999999), _table_set_repl(999999)
367  {}
368 
369  virtual ~LCE_SelectionSH() {}
370 
371  virtual bool setStatRecorders (string& token);
372 
373  void addMeanPerPatch (age_t AGE);
374  void addVarPerPatch (age_t AGE);
375  void setDataTable (age_t AGE);
378 
379 
381  double getFitness (unsigned int i) {return _SHLinkedEvent->_fitness[i];}
382  double getSurvival (unsigned int i) {return _SHLinkedEvent->_survival[i];}
383  double getPedProp (unsigned int i) {return _SHLinkedEvent->_ind_cntr[i];}
384  double getMeanPatchFitness (unsigned int i, unsigned int int_agex);
385  double getMeanPatchFitness (unsigned int i);
386  double getVarPatchFitness (unsigned int i, unsigned int int_agex);
387 };
388 
389 class LCE_SelectionFH : public virtual EventFileHandler< LCE_Selection_base >
390 {
391 
392  public:
393 
395 
396  virtual ~LCE_SelectionFH() {}
397 
398  virtual void FHwrite();
399 
400  virtual void FHread(string& filename) {}
401 
402  void print(ofstream& FH, sex_t SEX, age_idx AGE, unsigned int i, Patch* patch, unsigned int ntraits);
403 };
404 
405 
406 #endif
407 
Template class for the LCEs StatHandler classes.
Definition: filehandler.h:258
Template class for the LCEs StatHandler classes.
Definition: stathandler.h:190
LCE_Selection_base * _SHLinkedEvent
Pointer to the linked LCE.
Definition: stathandler.h:193
A class to manage the files associated with each components of the simulation.
Definition: fileservices.h:52
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:49
void * getTraitValue(IDX T)
Accessor to the value (phenotype) of a particular trait.
Definition: individual.h:271
unsigned int getPedigreeClass()
Returns the pedigree class of the individual, as set during offspring creation.
Definition: individual.h:179
Composite LCE implementing breeding and viability selection on a given trait type.
Definition: LCEcomposite.h:238
Definition: LCEselection.h:390
virtual void FHwrite()
Definition: LCEselection.cc:1811
LCE_SelectionFH(LCE_Selection_base *event)
Definition: LCEselection.h:394
virtual void FHread(string &filename)
Definition: LCEselection.h:400
void print(ofstream &FH, sex_t SEX, age_idx AGE, unsigned int i, Patch *patch, unsigned int ntraits)
Definition: LCEselection.cc:1850
virtual ~LCE_SelectionFH()
Definition: LCEselection.h:396
StatHandler class for the LCE_Selection class.
Definition: LCEselection.h:357
double getVarPatchFitness(unsigned int i, unsigned int int_agex)
Definition: LCEselection.cc:1789
double getSurvival(unsigned int i)
Definition: LCEselection.h:382
unsigned int _table_set_age
Definition: LCEselection.h:360
double getMeanPatchFitness(unsigned int i, unsigned int int_agex)
Definition: LCEselection.cc:1762
void addVarPerPatch(age_t AGE)
Definition: LCEselection.cc:1657
unsigned int _table_set_repl
Definition: LCEselection.h:360
void setOffsprgTable()
Definition: LCEselection.h:377
double getMeanFitness()
Definition: LCEselection.h:380
void setAdultTable()
Definition: LCEselection.h:376
vector< vector< double > > _phenoTable
Definition: LCEselection.h:359
virtual ~LCE_SelectionSH()
Definition: LCEselection.h:369
virtual bool setStatRecorders(string &token)
Definition: LCEselection.cc:1565
void addMeanPerPatch(age_t AGE)
Definition: LCEselection.cc:1623
double getPedProp(unsigned int i)
Definition: LCEselection.h:383
double getFitness(unsigned int i)
Definition: LCEselection.h:381
unsigned int _table_set_gen
Definition: LCEselection.h:360
LCE_SelectionSH(LCE_Selection_base *event)
Definition: LCEselection.h:364
void setDataTable(age_t AGE)
Definition: LCEselection.cc:1691
Base class performing (viability) selection on an arbitrary trait.
Definition: LCEselection.h:51
double getFitnessUnivariateGaussian_VE(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following the Gaussian selection model with several traits under...
Definition: LCEselection.cc:1097
double getFitnessMultivariateDisruptive(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following a Disruptive (inverted Gaussian) selection model with ...
Definition: LCEselection.cc:1002
int _selectTraitDimension
Number of quantitative traits under selection.
Definition: LCEselection.h:124
double getFitnessRelative(Individual *ind, unsigned int patch)
Returns the relative fitness of the individual, adjusted by a scaling factor.
Definition: LCEselection.h:288
bool setSelectionMatrix()
Definition: LCEselection.cc:661
double _fitness[5]
Fitness counters, one for each pedigree class.
Definition: LCEselection.h:118
double _base_fitness
Definition: LCEselection.h:112
double(LCE_Selection_base::* _getFitness)(Individual *, unsigned int)
Pointer to the function returning the individual fitness.
Definition: LCEselection.h:152
const TMatrix & getLocalOptima() const
Definition: LCEselection.h:178
virtual void loadFileServices(FileServices *loader)
Definition: LCEselection.cc:147
double getFitnessFixedEffect(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual in the fixed selection model.
Definition: LCEselection.h:236
double getFitnessDirect(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following the direct selection model.
Definition: LCEselection.h:242
void setScalingFactorAbsolute(age_idx age, unsigned int p)
Resets the fitness scaling factor equal to one.
Definition: LCEselection.h:326
double _max_fitness
Definition: LCEselection.h:113
double _FitnessFixModel[5]
Absolute fitness values of the five pedigree class for the fixed selection model (lethal equivalents ...
Definition: LCEselection.h:100
double getMeanFitness(age_idx age)
Computes the mean fitness of the whole population for a given age class.
Definition: LCEselection.cc:1146
vector< gsl_matrix * > _gsl_selection_matrix
Definition: LCEselection.h:56
double getFitnessUnivariateQuadratic(Individual *ind, unsigned int patch, unsigned int trait)
Quadratic fitness surface, approximates the Gaussian model for weak selection and/or small deviation ...
Definition: LCEselection.cc:1042
double getFitnessTruncation(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following a Truncation selection model.
Definition: LCEselection.cc:1021
void addPhenotypicSD(unsigned int deme, double *stDev)
Definition: LCEselection.cc:1512
void setScalingFactorMaxGlobal(age_idx age, unsigned int p)
Sets the fitness scaling factor equal to the inverse of the maximum population fitness value.
Definition: LCEselection.cc:1242
TMatrix _selection_offset
Definition: LCEselection.h:81
void doViabilitySelection(sex_t SEX, age_idx AGE, Patch *patch, unsigned int p)
Selectively removes individuals in the population depending on their fitness.
Definition: LCEselection.cc:1358
vector< double > _selection_variance
Patch-specific selection variance.
Definition: LCEselection.h:130
LCE_SelectionFH * _writer
Definition: LCEselection.h:166
unsigned int _linearSelTraitIndex
Definition: LCEselection.h:75
vector< unsigned int > _TraitIndices
The indices of the traits under selection.
Definition: LCEselection.h:140
gsl_vector * _diffs
Definition: LCEselection.h:57
bool set_param_rate_of_change()
Definition: LCEselection.cc:888
void(LCE_Selection_base::* _setScalingFactor)(age_idx, unsigned int)
Pointer to the function used to set the fitness scaling factor when fitness is relative.
Definition: LCEselection.h:157
bool _is_absolute
Definition: LCEselection.h:115
double getMaxFitness(age_idx age)
Computes the maximum fitness value of the whole population for a given age class.
Definition: LCEselection.cc:1180
double _eVariance
Evironmental variance.
Definition: LCEselection.h:133
void setScalingFactorMaxLocal(age_idx age, unsigned int p)
Sets the fitness scaling factor equal to the inverse of the maximum local patch fitness value.
Definition: LCEselection.cc:1234
TMatrix _rate_of_change_local_optima
Definition: LCEselection.h:60
vector< double > _mean_trait_value
Definition: LCEselection.h:74
double _scaling_factor
Definition: LCEselection.h:113
bool _do_change_local_opt
Definition: LCEselection.h:61
bool _is_local
Definition: LCEselection.h:114
TMatrix _truncation_threshold
Definition: LCEselection.h:86
double getMeanPatchFitness(age_idx age, unsigned int p)
Computes the mean fitness in a given patch for a given age class.
Definition: LCEselection.cc:1164
void checkChangeLocalOptima()
Check is rate of change in local optima has been set and must be applied.
Definition: LCEselection.cc:1305
virtual age_t addAgeClass()
Definition: LCEselection.h:349
virtual void execute()
Definition: LCEselection.cc:1324
double getFitness(Individual *ind, unsigned int patch)
Calls the fitness function according to the fitness model.
Definition: LCEselection.h:230
void setMeans(unsigned int tot_ind)
Computes the average fitness of each pedigree class.
Definition: LCEselection.cc:1400
virtual ~LCE_Selection_base()
Definition: LCEselection.cc:121
bool setSelectionOffset(double default_val, double min_val)
Definition: LCEselection.cc:824
void resetCounters()
Resets the fitness counters.
Definition: LCEselection.cc:1389
double getFitnessMultivariateGaussian_VE(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following the Gaussian selection model with one trait under sele...
Definition: LCEselection.cc:1113
gsl_vector * _res1
Definition: LCEselection.h:58
bool set_sel_model()
Definition: LCEselection.cc:312
double getFitnessMultivariateGaussian(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following the Gaussian selection model with one trait under sele...
Definition: LCEselection.cc:1077
virtual age_t removeAgeClass()
Definition: LCEselection.h:348
double _ind_cntr[5]
Definition: LCEselection.h:118
virtual bool setParameters()
Definition: LCEselection.cc:186
unsigned int _set_std_rate_at_generation
Definition: LCEselection.h:63
double _mean_fitness
Definition: LCEselection.h:113
bool set_fit_model()
Definition: LCEselection.cc:254
virtual bool resetParameterFromSource(std::string param, SimComponent *cmpt)
Definition: LCEselection.h:346
LCE_Selection_base()
Definition: LCEselection.cc:46
LCE_SelectionSH * _stater
Definition: LCEselection.h:165
vector< unsigned int > _selectTraitMapping
Mapping from selection dimension index to quanti trait index (0-based).
Definition: LCEselection.h:127
void setScalingFactorForLinearSelection(age_idx age, unsigned int p)
Sets the fitness scaling factor as the mean trait value in the patch to compute the fitness value in ...
Definition: LCEselection.cc:1251
int _std_rate_reference_patch
Definition: LCEselection.h:64
TMatrix _linear_selection_strength
Definition: LCEselection.h:73
void changeLocalOptima()
Definition: LCEselection.cc:1295
void(LCE_Selection_base::* _setNewLocalOptima)(void)
Pointer to the function used to change the local phenotypic optima or its rate of change.
Definition: LCEselection.h:161
double _Fpedigree[5]
Array of pedigree values used in the fixed-selection model.
Definition: LCEselection.h:96
TMatrix _local_optima
Definition: LCEselection.h:59
bool setSelectTraitMapping(unsigned int num_quanti_traits)
Definition: LCEselection.cc:615
vector< double(LCE_Selection_base::*)(Individual *, unsigned int, unsigned int) > _getRawFitness
A vector containing pointers to fitness function related to each trait under selection.
Definition: LCEselection.h:148
bool _truncation_upper
Definition: LCEselection.h:87
bool set_local_optima()
Definition: LCEselection.cc:860
double _letheq
Definition: LCEselection.h:93
void setScalingFactorGlobal(age_idx age, unsigned int p)
Sets the fitness scaling factor equal to the inverse of the mean population fitness.
Definition: LCEselection.cc:1224
double getMaxPatchFitness(age_idx age, unsigned int p)
Computes the maximum fitness value in a given patch for a given age class.
Definition: LCEselection.cc:1196
vector< TMatrix * > _selection_matrix
Definition: LCEselection.h:55
vector< string > _SelectionModels
The selection models associated with each trait under selection.
Definition: LCEselection.h:143
bool _rate_of_change_is_std
Definition: LCEselection.h:62
virtual LifeCycleEvent * clone()
Definition: LCEselection.h:347
double getFitnessUnivariateLinear(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following a Linear selection model with a single trait under sel...
Definition: LCEselection.cc:973
virtual age_t requiredAgeClass()
Definition: LCEselection.h:350
void setScalingFactorLocal(age_idx age, unsigned int p)
Sets the fitness scaling factor equal to the inverse of the mean local patch fitness.
Definition: LCEselection.cc:1216
double _survival[5]
Definition: LCEselection.h:118
vector< string > _Traits
The list of trait types under selection.
Definition: LCEselection.h:137
double getFitnessAbsolute(Individual *ind, unsigned int patch)
Returns the raw fitness of the individual, without adjustment (absolute fitness).
Definition: LCEselection.cc:1134
age_idx _linearSelAgeClass
Definition: LCEselection.h:76
virtual void loadStatServices(StatServices *loader)
Definition: LCEselection.cc:139
void set_std_rate_of_change()
Definition: LCEselection.cc:1428
double setMeanFitness(age_idx age)
Sets the _mean_fitness variable to the value of the mean population fitness.
Definition: LCEselection.h:224
double getFitnessUnivariateGaussian(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following the Gaussian selection model with several traits under...
Definition: LCEselection.cc:1062
void updateFitnessCounters(double fitness, unsigned int ped_class, bool survived)
Updates the fitness and survival mean counters.
Definition: LCEselection.cc:1416
double getFitnessUnivariateDisruptive(Individual *offsprg, unsigned int patch, unsigned int trait)
Returns the fitness of an individual following a Disruptive (inverted Gaussian) selection model with ...
Definition: LCEselection.cc:990
Base class of the Life Cycle Events, declares the LCE interface.
Definition: lifecycleevent.h:73
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:432
Interface to all basic components of a simulation (traits, life cycle events, pop,...
Definition: simcomponent.h:45
The Service class used to manage the StatHandler objects.
Definition: statservices.h:50
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:50
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:36
#define NONE
No age flag.
Definition: types.h:48
unsigned int age_t
Age class flags.
Definition: types.h:46
#define ADULTS
Adults age class flag (breeders).
Definition: types.h:54
#define OFFSPRG
Offspring age class flag.
Definition: types.h:50
age_idx
Array index of the age classes in the patch sizes and containers arrays.
Definition: types.h:41

Generated for Nemo v2.4.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR