Metapop Class Reference

Top class of the metapopulation structure, contains the patches. More...

#include <metapop.h>

Inheritance diagram for Metapop:

Inheritance graph
[legend]
Collaboration diagram for Metapop:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 Metapop ()
virtual ~Metapop ()
bool init ()
 Inits the population parameters from the ParamSet and builds the pop (adds patches), the prototypes and the life cycle.
virtual bool setParameters ()
 Default interface needed to initialize the component's variables from its input parameters value.
bool setPopulationParameters ()
 Population's size parameters initializing procedure.
bool setSourceParameters ()
 Setter for source population parameters.
bool updatePopulationParameters ()
 Called during simulation to change the population's parameters (temporal argument).
void reset ()
 Called to empty the patches, individuals are move to the garbage collector.
void clear ()
 Called at the end of each simulation, empties the pop and the garbage collector; the Individuals are destroyed.
void setMPImanager (MPImanager *mgr)
void store_trait (int trait_idx, BinaryStorageBuffer *saver)
 Iterates through the individuals containers to store the trait data to a binary file.
void read_trait (int trait_idx, BinaryStorageBuffer *loader)
 Iterates through the individuals containers to retrieve the trait data from a binary file.
void show_up ()
Population builders
void resizePatchArray ()
 Resets the patch container to the right number of patches as set by _patchNbr.
void buildPatchArray ()
 Builds the new population from parameter values.
void updatePatchArray ()
 Called during simulation to modify the meta-population size.
void updatePatchState ()
 Update the patch capacities and patch ID (reset to array position).
void setPatchCapacities ()
 Sets the deme capacity matrix from parameter values.
void setPatchCapacities (string param)
 Builds the new population from a single matrix of deme sizes.
void setPatchCapacities (sex_t SEX, string param)
 Builds the new population from a matrix of deme sizes but for one sex only.
void setPatchCapacities (string paramfem, string parammal)
 Builds the new population from matrices of deme sizes.
void loadSourcePopulation ()
 Loads a population from a soure population.
void loadPopFromBinarySource (string &filename)
 Loads the population from a binary data file when setting the first generation of a replicate.
void loadPopFromTraitFile (string &filename)
 Loads a population from a trait's data file (text file).
void setPopulation (unsigned int currentReplicate, unsigned int replicates)
 Sets the population for the first generation of each replicates.
void setPopulationFromSourceInPreserveMode ()
void setPopulationFromSource ()
void fillPopulationFromSource (age_idx AGE, sex_t SEX, deque< Individual * > &src_pool)
 Fills the population of the first generation of each replicates with individuals from a population source.
void fillPatchFromSource (sex_t SEX, Patch *src, Patch *patch, age_t AGE)
 Fills a patch from a source patch loaded from a binary file, used when setting the population in preserve mode.
Implementations
virtual void loadFileServices (FileServices *loader)
 Loads the component's FileHandler onto the FileServices.
virtual void loadStatServices (StatServices *loader)
 Loads the component's StatHandler onto the StatServices.
virtual void store_data (BinaryStorageBuffer *saver)
 Interface to store the component data (e.g. gene values) into a binary buffer.
virtual bool retrieve_data (BinaryStorageBuffer *reader)
 Interface to retrieve the same data from the binary buffer.
Getters
PatchgetPatch (unsigned int i)
 Patch accessor, return the ith+1 patch in the metapop.
PatchgetPatchPtr (unsigned int patch)
 A secure version of the getPatch() method.
deque< Patch * > * getPatchArray ()
unsigned int getPatchArraySize ()
void getAllIndividuals (age_idx AGE, deque< Individual * > &fem_pool, deque< Individual * > &mal_pool)
void setGenerations (unsigned int gen)
unsigned int getGenerations ()
void setReplicates (unsigned int repl)
unsigned int getReplicates ()
unsigned int getPatchNbr ()
unsigned int getPatchKFem ()
unsigned int getPatchKMal ()
unsigned int getPatchCapacity ()
unsigned int getPatchCapacity (sex_t SEX, unsigned int patch)
TMatrixgetPatchCapacities ()
Population state interface
unsigned int getCurrentReplicate ()
unsigned int getCurrentGeneration ()
void setCurrentReplicate (unsigned int repl)
void setCurrentGeneration (unsigned int gen)
age_t getCurrentAge ()
void setCurrentAge (age_t age)
 Sets the age flag.
void setCurrentAge (LifeCycleEvent *LCE)
 Set the age flag from a LifeCycleEvent object.
bool isAlive ()
 Checks if the population still contains at least one individual in any sex or age class.
unsigned int size ()
 Get the total number of individuals present in the population, all sex and age classes together.
unsigned int size (sex_t SEX, age_t AGE)
 Interface to get the size of a praticular age and sex class(es).
unsigned int size (sex_t SEX, age_t AGE, unsigned int deme)
 Interface to get the size of a praticular age and sex class within a patch.
unsigned int size (age_t AGE)
 Simplified interface to get the size of both sexes of the appropriate age class(es) in the whole population.
unsigned int size (age_t AGE, unsigned int deme)
 Simplified interface to get the size of both sexes of the appropriate age class(es) in one patch.
Individualget (sex_t SEX, age_idx AGE, unsigned int at, unsigned int deme)
 Returns a pointer to the appropriate individual.
void move (sex_t SEX, age_idx from_age, unsigned int from_deme, age_idx to_age, unsigned int to_deme, unsigned int at)
 Moves an individual from a deme to an other one, both demes sizes are modified.
void flush ()
 Removes all individual pointers and flush them into the recycling pool.
void flush (sex_t SEX, age_idx AGE)
 Removes all individual pointers of the appropriate sex and age class and flush them into the recycling pool.
void flush (age_idx AGE)
 Removes all individual pointers of both sexes and specified age class and flush them into the recycling pool.
void flush (age_t AGE)
 Removes all individual pointers of both sexes and specified age class(es) and flush them into the recycling pool.
PatchremovePatch (unsigned int i)
 Removes a patch from the patch array and returns it pointer.
void deletePatch (unsigned int i)
 Removes a patch from the patch array and deletes it and its content.
void addPatch (Patch *patch)
 Adds a patch to the population.
void addPatch (unsigned int num)
 Adds num patches to the population.

Private Attributes

MPImanager_mpimgr
MPStatHandler _statHandler
 The stat handler for the population stats.
deque< Patch * > _vPatch
 The Patch container.
BinaryDataLoader _loader
 A BinaryDataLoader to load a population from a binary data file.
Metapop_source
 A source population as loaded from a binary data file, used to build a population.
bool _source_preserve
 Flag to specify the loading mode, true means the source pop is the actual population.
bool _source_load
 Flage to specify that the population should be built from the binary loaded source population.
unsigned int _source_replicates
 The number of source files, each file being a different replicate of the source population.
unsigned int _source_replicate_digits
 Number of digits in the replicate counter filename extension.
unsigned int _source_start_at_replicate
 The replicate number to start loading from.
unsigned int _source_generation
 The generation to load from the binary file source file.
std::string _source_name
 The base filename of source population files.
std::string _source_filetype
 The trait type to load from.
std::string _source_required_age
 The age class to fill with the source population.
age_t _requiredAge
 The age class flag that is required to fill the population at the beginning of a replicate.
unsigned int _patchNbr
 Number of patches in the population.
unsigned int _patchK
 Patch carrying capacity.
unsigned int _patchKfem
 Sex specific carrying capacities.
unsigned int _patchKmal
TMatrix _patchSizes
 Matrix of the deme sizes, row 0 for the males, row 1 for the females.
unsigned int _generations
 Patch init sizes.
unsigned int _replicates
 Number of replicates to iterate.
unsigned int _currentGeneration
 The current generation in the generation loop, starts at 1.
unsigned int _currentReplicate
 The current replicate in the replicate loop, starts at 1.
age_t _currentAge
 The current age class, might be changed by the LCEs.

Friends

MetapopBinaryDataLoader::extractPop (std::string &, unsigned int, SimBuilder *, Metapop *)


Detailed Description

Top class of the metapopulation structure, contains the patches.

The basic design for the metapopulation structure is a top-down chain of responsibility where the Metapop class takes care of the patches it contains which are themselves concerned by the management of their individual containers. The Individual class is only concerned by the management of its traits. Thereby, a metapopulation can be viewed as an interleaving of containers where one container class takes care of its directly contained class only, without knowledge of the upward container state.

The Metapop class thus implements methods used to manage and get information from the patches, to manage the parameters necessary to build a population and to get the population state information. It also implements the methods used to load a population from different source files.

Population states: given by the number and the position of the individuals (both spatially in demes and temporally in age class containers). The Metapop::_currentAge flag is set according to the age state of the metapopulation. The life cycle events modify that state by moving individuals among individuals containers within the metatpopulation. The Metapop::_currentAge flag can contain the following age class bits as defined in types.h. The OFFSPRNG (=1) age class bit is set whenever the offspring containers are not empty. The ADULTS (=4) age class bit is set whenever the adult containers are not empty. The POSTDISP (=2) age class bit informs about the content of the post-dispersal containers. The ALL (=7) age class is the addition of the previous tags and NONE (=0) is the negation of them. The individual containers are stored in the patches and handled through the Patch class interface. Each age class is represented by two containers, one for the males (index 0) and the other for the females (index 1). These containers are store in a table and are accessed through their age class index as defined by the age_idx enum (see types.h). These indexes are as follows: The OFFSPRNG age class has index OFFSx = 0, the POSTDISP age class has index PDISPx = 1 and the ADULTS age class has index ADLTx = 2.

Definition at line 76 of file metapop.h.


Constructor & Destructor Documentation

Metapop::Metapop (  ) 

Definition at line 55 of file metapop.cc.

Referenced by loadPopFromTraitFile().

00055                  : _mpimgr(0), _statHandler(), _loader(), _source(0), _source_preserve(0), _source_load(0),
00056 _source_replicates(0), _source_replicate_digits(0), _source_generation(0), _patchNbr(0), _patchK(0),
00057 _patchKfem(0), _patchKmal(0), _generations(0), _replicates(0), _currentGeneration(0), _currentReplicate(0),
00058 _currentAge(NONE)
00059 {
00060   set_paramset("population", true, this);
00061   ParamUpdater<Metapop>* upd = new ParamUpdater<Metapop>( &Metapop::updatePopulationParameters );
00062   add_parameter("patch_capacity",INT,false,false,0,0, upd);
00063   add_parameter("patch_nbfem",INT,false,false,0,0, upd);
00064   add_parameter("patch_nbmal",INT,false,false,0,0, upd);
00065   add_parameter("patch_number",INT,false,false,0,0, upd);
00066   
00067   upd = new ParamUpdater<Metapop>( &Metapop::setSourceParameters );
00068   add_parameter("source_pop",STR,false,false,0,0, upd);
00069   add_parameter("source_file_type",STR,false,false,0,0, upd);
00070   add_parameter("source_preserve",BOOL,false,false,0,0, upd);
00071   add_parameter("source_replicates",INT,false,false,0,0, upd);
00072   add_parameter("source_replicate_digit",INT,false,false,0,0, upd);
00073   add_parameter("source_start_at_replicate",INT,false,false,0,0, upd);
00074   add_parameter("source_generation",INT,false,false,0,0, upd);
00075   add_parameter("source_fill_age_class",STR,false,false,0,0, upd);
00076 }

Metapop::~Metapop (  )  [virtual]

Definition at line 80 of file metapop.cc.

00081 {
00082 #ifdef _DEBUG_
00083     message("Metapop::~Metapop\n");
00084 #endif
00085   clear();
00086 }


Member Function Documentation

void Metapop::addPatch ( unsigned int  num  )  [inline]

Adds num patches to the population.

Definition at line 748 of file metapop.h.

00749 {
00750   for (unsigned int i = 0; i < num; i++)
00751     _vPatch.push_back(new Patch());
00752 }

void Metapop::addPatch ( Patch patch  )  [inline]

Adds a patch to the population.

The patch is added at the end of the array and it is empty.

Definition at line 743 of file metapop.h.

Referenced by LCE_Resize::buildNewPatchArrayWithBackup(), and LCE_Resize::removeDesignatedPatch().

00744 {
00745   _vPatch.push_back(patch);
00746 }

void Metapop::buildPatchArray (  ) 

Builds the new population from parameter values.

Supernumerary patches are deleted. All patches are empty.

Definition at line 240 of file metapop.cc.

Referenced by BinaryDataLoader::extractPop(), init(), and setPopulation().

00241 {
00242   resizePatchArray();
00243 
00244   //set the population capacities:
00245   for(unsigned int i = 0; i < _patchNbr; ++i) {
00246     _vPatch[i]->flush(this);
00247     _vPatch[i]->init(_patchSizes.get(FEM,i), _patchSizes.get(MAL,i), i);
00248   }
00249 }

void Metapop::clear (  ) 

Called at the end of each simulation, empties the pop and the garbage collector; the Individuals are destroyed.

Definition at line 959 of file metapop.cc.

Referenced by SimRunner::Replicate_LOOP(), and ~Metapop().

00960 {
00961   unsigned int i;
00962   
00963   for(i = 0; i < _vPatch.size(); ++i) delete _vPatch[i];
00964   
00965   _vPatch.clear();
00966 
00967   _patchNbr = 0;
00968   
00969 //  purgeRecyclingPOOL();
00970   
00971 //  if(_source) delete _source;
00972 //  _source = NULL;
00973 }

void Metapop::deletePatch ( unsigned int  i  )  [inline]

Removes a patch from the patch array and deletes it and its content.

The IDs of the remaining patches are updated.

Parameters:
i the index of the patch to remove.

Definition at line 733 of file metapop.h.

Referenced by LCE_Resize::removeDesignatedPatch().

00734 {
00735   delete _vPatch[i];
00736   for (unsigned int k = i; k < _vPatch.size() -1; k++) {
00737     _vPatch[k] = _vPatch[k + 1];
00738     _vPatch[k]->setID(k);
00739   }
00740   _vPatch.pop_back();
00741 }

void Metapop::fillPatchFromSource ( sex_t  SEX,
Patch src,
Patch patch,
age_t  AGE 
)

Fills a patch from a source patch loaded from a binary file, used when setting the population in preserve mode.

Parameters:
SEX sex of the individuals to fetch from the source pop
src the source patch that will be copied.
patch the local patch to be filled with individuals copied from the source patch.
AGE age class to copy individuals from the source.

Definition at line 742 of file metapop.cc.

Referenced by setPopulationFromSourceInPreserveMode().

00743 {
00744   age_idx in_age = (AGE == OFFSPRG? OFFSx : ADLTx);
00745   age_idx to_age = (_requiredAge == OFFSPRG? OFFSx : ADLTx);
00746   
00747   Individual* new_ind;
00748   
00749   for(unsigned int j = 0; j < src->size(SEX, in_age); ++j) {
00750     new_ind = _source->getNewIndividual();
00751     (*new_ind) = (*src->get(SEX, in_age, j));
00752     patch->add(SEX , to_age, new_ind );
00753   }
00754 }

void Metapop::fillPopulationFromSource ( age_idx  AGE,
sex_t  SEX,
deque< Individual * > &  src_pool 
)

Fills the population of the first generation of each replicates with individuals from a population source.

Parameters:
AGE age of the individuals to fetch from the source pop
SEX sex of the individuals to fetch from the source pop
src_pool the container where the source individuals will be placed, they are not removed from the source.

Definition at line 805 of file metapop.cc.

Referenced by setPopulationFromSource().

00806 {
00807   unsigned int Ktot = 0;
00808   unsigned int ind_pos ;
00809   Individual* new_ind;
00810   
00811   for(unsigned int i = 0; i < _patchNbr; ++i) 
00812     Ktot += _vPatch[i]->get_K(SEX);
00813   
00814   if(src_pool.size() < Ktot)
00815     warning("Number of %s %s in source metapop (%i) not enough to fill current metapop (%i).\n",
00816             (SEX ? "female" : "male"), (AGE == OFFSx ? "offspring" : "adults"),
00817             src_pool.size(), Ktot);
00818   
00819   for(unsigned int i = 0; i < _patchNbr; ++i) {
00820     
00821     for(unsigned int j = 0, psize = _vPatch[i]->get_K(SEX); j < psize && src_pool.size() != 0; ++j) {
00822       
00823       new_ind = _source->getNewIndividual();
00824       
00825       ind_pos = RAND::Uniform( src_pool.size() );
00826       
00827       _vPatch[i]->add( SEX, AGE, &( (*new_ind) = *(src_pool[ ind_pos ]) ) );
00828       
00829       src_pool.erase( src_pool.begin() + ind_pos );
00830       
00831     }
00832   }
00833   
00834 }

void Metapop::flush ( age_t  AGE  )  [inline]

Removes all individual pointers of both sexes and specified age class(es) and flush them into the recycling pool.

Container sizes are reset to null values.

See also:
Patch::flush()
Parameters:
AGE the age class(es) to be flushed

Definition at line 721 of file metapop.h.

00722 {
00723   for(unsigned int i = 0; i < _patchNbr; i++) _vPatch[i]->flush(AGE, this);
00724 }

void Metapop::flush ( age_idx  AGE  )  [inline]

Removes all individual pointers of both sexes and specified age class and flush them into the recycling pool.

Container sizes are reset to null values.

See also:
Patch::flush()
Parameters:
AGE the index of the age class to be flushed

Definition at line 716 of file metapop.h.

00717 {
00718   for(unsigned int i = 0; i < _patchNbr; i++) _vPatch[i]->flush(AGE, this);
00719 }

void Metapop::flush ( sex_t  SEX,
age_idx  AGE 
) [inline]

Removes all individual pointers of the appropriate sex and age class and flush them into the recycling pool.

Container sizes are reset to null values.

See also:
Patch::flush()
Parameters:
SEX the sex class of the individual
AGE the index of the age class to be flushed

Definition at line 711 of file metapop.h.

00712 {
00713   for(unsigned int i = 0; i < _patchNbr; i++) _vPatch[i]->flush(SEX, AGE, this);
00714 }

void Metapop::flush (  )  [inline]

Removes all individual pointers and flush them into the recycling pool.

Container sizes are reset to null values.

See also:
Patch::flush()

Definition at line 706 of file metapop.h.

Referenced by LCE_Cross::execute(), LCE_Breed_Selection::execute(), LCE_Breed_Wolbachia::execute(), LCE_Breed::execute(), and flush().

00707 {
00708   for(unsigned int i = 0; i < _patchNbr; i++) _vPatch[i]->flush(this);
00709 }

Individual * Metapop::get ( sex_t  SEX,
age_idx  AGE,
unsigned int  at,
unsigned int  deme 
) [inline]

Returns a pointer to the appropriate individual.

Parameters:
SEX sex class container index
AGE age class container index
at the index of the individual in its container
deme the patch where to grab the individual

Definition at line 696 of file metapop.h.

00697 { return getPatchPtr(deme)->get(SEX, AGE, at); }

void Metapop::getAllIndividuals ( age_idx  AGE,
deque< Individual * > &  fem_pool,
deque< Individual * > &  mal_pool 
)

Definition at line 985 of file metapop.cc.

Referenced by setPopulationFromSource().

00986 {
00987   
00988   for(unsigned int i = 0; i < _vPatch.size(); ++i) {
00989     
00990     for(unsigned int j = 0, psize = _vPatch[i]->size(MAL, AGE); j < psize; ++j)
00991       mal_pool.push_back( _vPatch[i]->get(MAL, AGE, j) );
00992     
00993     for(unsigned int j = 0, psize = _vPatch[i]->size(FEM, AGE); j < psize; ++j)
00994       fem_pool.push_back( _vPatch[i]->get(FEM, AGE, j) );
00995     
00996   }
00997   
00998 }

age_t Metapop::getCurrentAge (  )  [inline]

unsigned int Metapop::getCurrentGeneration (  )  [inline]

unsigned int Metapop::getCurrentReplicate (  )  [inline]

unsigned int Metapop::getGenerations (  )  [inline]

Patch* Metapop::getPatch ( unsigned int  i  )  [inline]

Patch accessor, return the ith+1 patch in the metapop.

Definition at line 239 of file metapop.h.

Referenced by LCE_Breed_Selection_Disperse::breed_selection_disperse(), LCE_Breed_Disperse::do_breed_disperse(), LCE_Disperse_EvolDisp::evoldisp(), LCE_Selection_base::execute(), LCE_Cross::execute(), LCE_Patch_Extinction::execute(), LCE_Regulation::execute(), LCE_Aging::execute(), LCE_Disperse_EvolDisp::execute(), LCE_Disperse_ConstDisp::execute(), LCE_Breed_Selection::execute(), LCE_Breed_Selection_Disperse::execute(), LCE_Breed_Disperse::execute(), LCE_Breed_Wolbachia::execute(), LCE_Breed::execute(), TTNeutralGenesFH::FHread(), TTDeletMutBitstrFH::FHread(), TTDeletMutBitstrFH::FHwrite(), LCE_Disperse_EvolDisp::fixdisp(), LCE_Breed_Disperse::get_parent(), TTWolbachiaSH::getMeanFemaleInfection_perPatch(), LCE_Selection_base::getMeanFitness(), TTWolbachiaSH::getMeanMaleInfection_perPatch(), TTWolbachiaSH::getMeanOffsprgFemaleInfection_perPatch(), TTWolbachiaSH::getMeanOffsprgInfection(), TTWolbachiaSH::getMeanOffsprgMaleInfection_perPatch(), LCE_Selection_base::getMeanPatchFitness(), LCE_Breed_Wolbachia::hasInfectedFemale(), LCE_Breed_Wolbachia::inoculate_wolbachia(), LCE_Disperse_ConstDisp::Migrate(), LCE_Disperse_ConstDisp::MigratePatch(), LCE_Disperse_ConstDisp::MigratePatch_AbsorbingBorder(), LCE_Disperse_base::reset_counters(), TTWolbachiaSH::setInfectionStats(), size(), LCE_Disperse_base::swapPostDisp(), TTNeutralGenesFH::write_FSTAT(), and TTNeutralGenesFH::write_varcompWC().

00239 {return (i > _vPatch.size() -1 ? 0 : _vPatch[i]);}

deque< Patch* >* Metapop::getPatchArray (  )  [inline]

Definition at line 251 of file metapop.h.

00251 {return &_vPatch;}

unsigned int Metapop::getPatchArraySize (  )  [inline]

Definition at line 252 of file metapop.h.

Referenced by LCE_Resize::buildNewPatchArrayWithBackup(), LCE_Resize::execute(), and LCE_Resize::removeDesignatedPatch().

00252 {return _vPatch.size();}

TMatrix* Metapop::getPatchCapacities (  )  [inline]

Definition at line 263 of file metapop.h.

Referenced by LCE_Resize::updatePatchCapacities().

00263 {return &_patchSizes;}

unsigned int Metapop::getPatchCapacity ( sex_t  SEX,
unsigned int  patch 
) [inline]

Definition at line 262 of file metapop.h.

00262 {return (unsigned int)_patchSizes.get(SEX, patch);}

unsigned int Metapop::getPatchCapacity (  )  [inline]

Definition at line 261 of file metapop.h.

00261 {return _patchK;}

unsigned int Metapop::getPatchKFem (  )  [inline]

Definition at line 259 of file metapop.h.

Referenced by LCE_Resize::execute(), loadPopFromBinarySource(), and setPopulation().

00259 {return _patchKfem;}

unsigned int Metapop::getPatchKMal (  )  [inline]

Definition at line 260 of file metapop.h.

Referenced by LCE_Resize::execute(), loadPopFromBinarySource(), and setPopulation().

00260 {return _patchKmal;}

unsigned int Metapop::getPatchNbr (  )  [inline]

Definition at line 258 of file metapop.h.

Referenced by TTNeutralGenesSH::allocateTables(), LCE_Breed_Selection_Disperse::breed_selection_disperse(), LCE_Resize::buildNewPatchArrayWithBackup(), LCE_Disperse_base::checkBackwardDispersalMatrix(), LCE_Disperse_base::checkForwardDispersalMatrix(), LCE_Breed_Disperse::do_breed_disperse(), LCE_Selection_base::execute(), LCE_Resize::execute(), LCE_Cross::execute(), LCE_Patch_Extinction::execute(), LCE_Regulation::execute(), LCE_Aging::execute(), LCE_Disperse_EvolDisp::execute(), LCE_Disperse_ConstDisp::execute(), LCE_Breed_Selection::execute(), LCE_Breed_Selection_Disperse::execute(), LCE_Breed_Disperse::execute(), LCE_Breed_Wolbachia::execute(), LCE_Breed::execute(), TTNeutralGenesFH::FHread(), TTDeletMutBitstrFH::FHread(), TTDeletMutBitstrFH::FHwrite(), LCE_Resize::fillPop(), LCE_Selection_base::getMeanFitness(), TTWolbachiaSH::getMeanOffsprgInfection(), LCE_Breed_Wolbachia::hasInfectedFemale(), LCE_Breed_Wolbachia::inoculate_wolbachia(), loadPopFromBinarySource(), LCE_Breed_Disperse::mate_selfing(), LCE_Resize::regulate(), LCE_Patch_Extinction::set_matrix_param(), TTNeutralGenesSH::setCoaMatrixRecorders(), TTNeutralGenesSH::setFstMatrixRecorders(), TTWolbachiaSH::setInfectionStats(), TTNeutralGenesSH::setNeiGeneticDistanceRecorders(), LCE_Disperse_EvolDisp::setParameters(), LCE_Disperse_base::setParameters(), setPopulation(), LCE_Disperse_base::setPropaguleTargets(), LCE_Selection_base::setSelectionMatrix(), LCE_Selection_base::setSpatialMatrix(), LCE_SelectionSH::setStatRecorders(), LCE_Resize::updateParameters(), LCE_Resize::updatePatchCapacities(), TTNeutralGenesFH::write_freq(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), and TTNeutralGenesFH::write_varcompWC().

00258 {return _patchNbr;}

Patch* Metapop::getPatchPtr ( unsigned int  patch  )  [inline]

A secure version of the getPatch() method.

Definition at line 242 of file metapop.h.

Referenced by LCE_Resize::buildNewPatchArrayWithBackup(), LCE_Resize::fillPatchNoBackup(), LCE_Resize::fillPatchWithBackup(), get(), LCE_SelectionSH::getPatchFitness(), LCE_Resize::regulate(), LCE_Resize::removeDesignatedPatch(), removePatch(), setPopulationFromSourceInPreserveMode(), and LCE_Resize::updatePatchCapacities().

00242                                                          {
00243     if(!(patch < _vPatch.size())) 
00244       fatal("Metapop::getPatchPtr()::_vPatch overflow (id=%i nb=%i)\n", patch, _vPatch.size());
00245     
00246     if (_vPatch[patch] == NULL) fatal("Metapop::getPatchPtr()::NULL ptr\n");
00247     
00248     return _vPatch[patch];
00249   }

unsigned int Metapop::getReplicates (  )  [inline]

bool Metapop::init (  ) 

Inits the population parameters from the ParamSet and builds the pop (adds patches), the prototypes and the life cycle.

Called at the start of each simulation, resets the individual garbage collector.

Definition at line 90 of file metapop.cc.

Referenced by SimRunner::init_components(), and loadPopFromTraitFile().

00091 {
00092   if( !(_paramSet->isSet()) ) {
00093         error("parameters in \"population\" are not properly set!\n");
00094         return false;
00095   }
00096   
00097   if(!setParameters()) return false;
00098   
00099   buildPatchArray();
00100   
00101   //empty and clean the RecyclingPOOL, safer...
00102   purgeRecyclingPOOL();
00103     
00104   return true;
00105 }

bool Metapop::isAlive (  )  [inline]

Checks if the population still contains at least one individual in any sex or age class.

Definition at line 282 of file metapop.h.

Referenced by SimRunner::Cycle(), LCE_StatSH::get_isAlive(), and SimRunner::Replicate_LOOP().

00282 {return size() != 0;}

virtual void Metapop::loadFileServices ( FileServices loader  )  [inline, virtual]

Loads the component's FileHandler onto the FileServices.

Parameters:
loader the file service

Implements SimComponent.

Definition at line 218 of file metapop.h.

00218 {}

void Metapop::loadPopFromBinarySource ( string &  filename  ) 

Loads the population from a binary data file when setting the first generation of a replicate.

Definition at line 883 of file metapop.cc.

Referenced by loadSourcePopulation().

00884 {
00885   Individual  *new_ind, *src_tmp;
00886   
00887   if(!(_source = _loader.extractPop(filename,_source_generation,SIMenv::MainSim, this)))
00888     fatal("Metapop::loadPopFromBinarySource:could not extract pop from binary file \"%s\"\n",filename.c_str());
00889   
00890   //check traits parameters (mainly sizes)
00891   new_ind = getNewIndividual();
00892   src_tmp = _source->getNewIndividual();
00893   
00894   if( (*new_ind) != (*src_tmp) ) {
00895       fatal("loading population from source file failed: trait genetic architecture differs.\n");
00896   }
00897   
00898   delete new_ind;
00899   delete src_tmp;
00900   
00901   if(_source_preserve) {
00902     
00903     //basic checks:
00904     if(_source->getPatchNbr() != _patchNbr)
00905       fatal("loading population from source file failed: preserve mode forbids difference in number of patches\n");
00906     
00907     if(_source->getPatchKFem() != _patchKfem || _source->getPatchKMal() != _patchKmal)
00908       fatal("loading population from source file failed: preserve mode forbids difference in patch sizes\n");
00909   }
00910   
00911 }

void Metapop::loadPopFromTraitFile ( string &  filename  ) 

Loads a population from a trait's data file (text file).

Definition at line 916 of file metapop.cc.

Referenced by loadSourcePopulation().

00917 {
00918   if(_source != NULL) delete _source;
00919   
00920   _source = new Metapop();
00921   _source->setMPImanager( _mpimgr );
00922 
00923   //the population's parameters from current params
00924   _source->set_paramsetFromCopy((*this->get_paramset()));
00925  
00926   //we need this to be able to create new individuals in the source:
00927   _source->init( );
00928   _source->makePrototype(this->getTraitPrototypes());
00929   
00930   //now read data from genotype file:
00931   FileServices *FS = SIMenv::MainSim->get_FileServices();
00932   
00933   FileHandler* file = FS->getReader(_source_filetype);
00934   
00935   if(!file) fatal("no file reader exists with source file extension \"%s\".\n",_source_filetype.c_str());
00936   
00937   file->set_pop_ptr(_source);
00938   
00939   file->FHread(filename);
00940   
00941   if(_source->size() == 0) fatal("source population in file \"%s\" is empty.\n",filename.c_str());
00942 }

void Metapop::loadSourcePopulation (  ) 

Loads a population from a soure population.

Definition at line 838 of file metapop.cc.

Referenced by setPopulation().

00839 {  
00840   std::string filename;
00841   
00842   if(_source_replicates != 0) {
00843     //find the replicate string of the source file
00844     ostringstream rpl;
00845     rpl.fill('0');
00846     rpl.width(_source_replicate_digits);
00847     
00848     if(_source_replicates >= _replicates)
00849       rpl << _currentReplicate + _source_start_at_replicate - 1;
00850     else if (_source_replicates != 1)
00851       //less replicates in source, reload the same repl every `source_replicates'
00852       rpl << ((_currentReplicate-1) / (_replicates/_source_replicates)) + 1;
00853     else
00854       rpl << 1; //a bit dumb but just in case...
00855     
00856     filename = _source_name + "_" + rpl.str() + _source_filetype;
00857     
00858   } else {
00859     
00860     filename = _source_name;
00861     
00862   }
00863   
00864   if(_source_filetype.compare(".bin") == 0) 
00865     loadPopFromBinarySource(filename);
00866   else
00867     loadPopFromTraitFile(filename);
00868   
00869   if(_source->size() == 0)  fatal("source population is empty!\n");
00870   else {
00871     if(_source->size(OFFSPRG) != 0) _source->setCurrentAge( (_source->getCurrentAge() | OFFSPRG) );
00872     if(_source->size(ADULTS)  != 0) _source->setCurrentAge( (_source->getCurrentAge() | ADULTS)  );
00873   }
00874   
00875 #ifdef _DEBUG_
00876   message("source pop loaded: offspring: %i f, %i m; adults: %i f, %i m\n", _source->size(FEM,OFFSPRG),
00877           _source->size(MAL,OFFSPRG), _source->size(FEM,ADULTS), _source->size(MAL,ADULTS));
00878 #endif
00879 }

virtual void Metapop::loadStatServices ( StatServices loader  )  [inline, virtual]

Loads the component's StatHandler onto the StatServices.

Parameters:
loader the stat service

Implements SimComponent.

Definition at line 220 of file metapop.h.

00220 {loader->attach(&_statHandler);}

void Metapop::move ( sex_t  SEX,
age_idx  from_age,
unsigned int  from_deme,
age_idx  to_age,
unsigned int  to_deme,
unsigned int  at 
) [inline]

Moves an individual from a deme to an other one, both demes sizes are modified.

Parameters:
SEX sex class container index
from_age age class container index in the deme of origin
from_deme index of the deme of origin
to_age age class container index in the destination deme
to_deme index of the destination deme
at index of the focal individual in the 'from' deme

Definition at line 699 of file metapop.h.

Referenced by LCE_Disperse_EvolDisp::evoldisp(), LCE_Disperse_EvolDisp::fixdisp(), LCE_Disperse_ConstDisp::MigratePatch(), and LCE_Disperse_ConstDisp::MigratePatch_AbsorbingBorder().

00701 {
00702   _vPatch[to_deme]->add( SEX, to_age, get(SEX, from_age, at, from_deme));
00703   _vPatch[from_deme]->remove(SEX, from_age, at);
00704 }

void Metapop::read_trait ( int  trait_idx,
BinaryStorageBuffer loader 
)

Iterates through the individuals containers to retrieve the trait data from a binary file.

Definition at line 612 of file metapop.cc.

Referenced by retrieve_data().

00613 { 
00614   for(unsigned int i = 0; i < _patchNbr; ++i) {
00615     
00616         //first offspring:
00617         for(unsigned int j = 0; j < _vPatch[i]->size(FEM, OFFSx); ++j){
00618           _vPatch[i]->get(FEM, OFFSx, j)->getTrait(trait_idx)->retrieve_data(loader);   
00619     }
00620     
00621         for(unsigned int j = 0; j < _vPatch[i]->size(MAL, OFFSx); ++j){
00622           _vPatch[i]->get(MAL, OFFSx, j)->getTrait(trait_idx)->retrieve_data(loader);   
00623     }
00624     
00625         //then adults:
00626         for(unsigned int j = 0; j < _vPatch[i]->size(FEM, ADLTx); ++j)
00627           _vPatch[i]->get(FEM, ADLTx, j)->getTrait(trait_idx)->retrieve_data(loader);
00628     
00629     for(unsigned int j = 0; j < _vPatch[i]->size(MAL, ADLTx); ++j)
00630       _vPatch[i]->get(MAL, ADLTx, j)->getTrait(trait_idx)->retrieve_data(loader);
00631   }
00632 }

Patch * Metapop::removePatch ( unsigned int  i  )  [inline]

Removes a patch from the patch array and returns it pointer.

The patch and its content are NOT deleted. The IDs of the remaining patches are *NOT* updated.

Parameters:
i the index of the patch to remove.

Definition at line 726 of file metapop.h.

Referenced by LCE_Resize::buildNewPatchArrayWithBackup(), and LCE_Resize::removeDesignatedPatch().

00727 {
00728   Patch* patch = getPatchPtr(i);
00729   _vPatch.erase(_vPatch.begin() + i);
00730   return patch;
00731 }

void Metapop::reset (  ) 

Called to empty the patches, individuals are move to the garbage collector.

********************************************************************* Metapop::reset resets each Patch, all individuals are moved to the POOL

Definition at line 947 of file metapop.cc.

Referenced by LCE_Breed_Wolbachia::execute().

00948 {
00949   unsigned int i;
00950 
00951   for(i = 0; i < _patchNbr; ++i) {
00952     _vPatch[i]->flush(this);
00953     _vPatch[i]->reset_counters();
00954   }
00955 }

void Metapop::resizePatchArray (  ) 

Resets the patch container to the right number of patches as set by _patchNbr.

Called at the beginning of each new simulation. Extra patches are destroyed and new ones are added if missing.

Definition at line 253 of file metapop.cc.

Referenced by buildPatchArray(), and updatePatchArray().

00254 {
00255   //reset the right number of patches for the new simulation
00256   if(_vPatch.size() > _patchNbr) {
00257         while(_vPatch.size() > _patchNbr) {
00258       delete _vPatch[0];
00259       _vPatch.pop_front();
00260         }
00261   } else while(_vPatch.size() < _patchNbr) _vPatch.push_back(new Patch());
00262 }

bool Metapop::retrieve_data ( BinaryStorageBuffer reader  )  [virtual]

Interface to retrieve the same data from the binary buffer.

Implements StorableComponent.

Definition at line 487 of file metapop.cc.

Referenced by BinaryDataLoader::extractPop().

00488 {
00489 #ifdef _DEBUG_ 
00490     message("Metapop::retrieve_data, %iB of data read so far\n",loader->getBytesOut()); 
00491 #endif
00492   unsigned int *sizes[3], dummy_int;
00493   unsigned char separator[2];
00494 
00495   sizes[0] = new unsigned int [_patchNbr]; 
00496   sizes[1] = new unsigned int [_patchNbr]; 
00497   sizes[2] = new unsigned int [_patchNbr];
00498   
00499   loader->read(&separator, 2 * sizeof(unsigned char));
00500   
00501   if(separator[0] != '@' || separator[1] != 'P') {
00502     error("Metapop::retrieve_data::wrong population seprarator\n");
00503     return false;
00504   }
00505     
00506   loader->read(&dummy_int, sizeof(unsigned int));
00507   
00508   if(dummy_int != _patchNbr) {
00509     error("Metapop::retrieve_data:number of Patch differ from parameter value\n");
00510     return false;
00511   }
00512   //get the Patch sizes
00513   //offspring
00514   loader->read(sizes[0], _patchNbr * sizeof(unsigned int));
00515   //females adults
00516   loader->read(sizes[1], _patchNbr * sizeof(unsigned int));
00517   //males adults
00518   loader->read(sizes[2], _patchNbr * sizeof(unsigned int));
00519   
00520   Individual* ind;
00521   unsigned int bytes_cnt = loader->getBytesOut();
00522   //retrieve all individual informations: IDs, matings, etc.
00523   for(unsigned int i = 0; i < _patchNbr; ++i) {
00524         //first, offspring:
00525         for(unsigned int j = 0; j < sizes[0][i]; ++j) {
00526       ind = this->getNewIndividual();
00527       ind->retrieve_data(loader);
00528           _vPatch[i]->add(ind->getSex(), OFFSx, ind);   
00529     }
00530         //then adult females:
00531         for(unsigned int j = 0; j < sizes[1][i]; ++j) {
00532       ind = this->getNewIndividual();
00533       ind->retrieve_data(loader);
00534           _vPatch[i]->add(FEM, ADLTx, ind);
00535     }
00536         //and adult males:
00537     for(unsigned int j = 0; j < sizes[2][i]; ++j) {
00538       ind = this->getNewIndividual();
00539       ind->retrieve_data(loader);
00540       _vPatch[i]->add(MAL, ADLTx, ind);
00541     }
00542         
00543   }
00544   
00545 #ifdef _DEBUG_ 
00546   message("Metapop::retrieve_data::retrieved %ikB of ind data (%i individuals)\n",
00547           (loader->getBytesOut()-bytes_cnt)/1024, size()); 
00548 #endif
00549     //retrieve traits sequences
00550     map<trait_t, TraitPrototype *>::iterator tt = _protoTraits.begin();
00551 
00552     char trait_name[6] = {'\0','\0','\0','\0','\0','\0'};
00553 
00554     unsigned int trait_cntr = 0;
00555   
00556     loader->read(&separator, 2 * sizeof(unsigned char));
00557   
00558     if(separator[0] != '@' || separator[1] != 'T') {
00559         error("Metapop::retrieve_data::wrong trait seprarator\n");
00560         return false;
00561     }
00562   
00563     bytes_cnt = loader->getBytesOut();
00564   
00565     do {
00566         //get the trait type:
00567 
00568         loader->read(&trait_name[0], TRAIT_T_MAX);
00569         string dummy_trait(trait_name);
00570 
00571         //get the prototype:
00572         if( (tt = _protoTraits.find(dummy_trait)) == _protoTraits.end() ) {
00573             error("Metapop::retrieve_data::trait in file not present in protoype\n");
00574             return false;
00575         }
00576         //then ask the prototype to retrieve its data:
00577         tt->second->retrieve_data(loader);
00578     
00579 #ifdef _DEBUG_ 
00580     message("%iB of trait data read so far (trait %s)\n",loader->getBytesOut()-bytes_cnt,tt->first.c_str()); 
00581 #endif
00582     
00583     //get the traits data:
00584     read_trait(tt->second->get_index(), loader);
00585     
00586         trait_cntr++;
00587 
00588         //get the next separator, should be present if right number of bytes have been read
00589         loader->read(&separator, 2 * sizeof(unsigned char));
00590     
00591     
00592     } while (separator[0] == '@' && separator[1] == 'T') ;
00593   
00594 #ifdef _DEBUG_ 
00595     message("Metapop::retrieve_data::retrieved %ikB of trait data\n",(loader->getBytesOut()-bytes_cnt)/1024); 
00596 #endif
00597   
00598   if(trait_cntr != _protoTraits.size())
00599     error("Metapop::retrieve_data::could not load all traits in prototype!\n");
00600   
00601   if(separator[0] != '@') 
00602     error("Metapop::retrieve_data::separator not found at end of pop record!\n");
00603   
00604   for(unsigned int i = 0; i < 3; i++)
00605     delete [] sizes[i];
00606   
00607   return true;
00608 }

void Metapop::setCurrentAge ( LifeCycleEvent LCE  ) 

Set the age flag from a LifeCycleEvent object.

Parameters:
LCE the LifeCycleEvent object.

Definition at line 977 of file metapop.cc.

00978 {
00979   _currentAge ^= LCE->removeAgeClass();
00980   _currentAge |= LCE->addAgeClass();
00981 }

void Metapop::setCurrentAge ( age_t  age  )  [inline]

Sets the age flag.

Parameters:
age the current age.

Definition at line 276 of file metapop.h.

Referenced by loadSourcePopulation(), and SimRunner::step().

00276 {_currentAge = age;}

void Metapop::setCurrentGeneration ( unsigned int  gen  )  [inline]

void Metapop::setCurrentReplicate ( unsigned int  repl  )  [inline]

Definition at line 270 of file metapop.h.

Referenced by SimRunner::Replicate_LOOP(), and SimRunner::setCurrentReplicate().

00270 {_currentReplicate = repl;}

void Metapop::setGenerations ( unsigned int  gen  )  [inline]

Definition at line 254 of file metapop.h.

Referenced by SimRunner::init_components(), and SimRunner::setGenerations().

00254 {_generations = gen;}

void Metapop::setMPImanager ( MPImanager mgr  )  [inline]

Definition at line 158 of file metapop.h.

Referenced by BinaryDataLoader::extractPop(), and loadPopFromTraitFile().

00158 {_mpimgr = mgr;}

bool Metapop::setParameters (  )  [virtual]

Default interface needed to initialize the component's variables from its input parameters value.

Formerly called 'init'.

Implements SimComponent.

Definition at line 109 of file metapop.cc.

Referenced by BinaryDataLoader::extractPop(), and init().

00110 {
00111   if(!setPopulationParameters()) return false;
00112   if(!setSourceParameters()) return false;
00113   return true;
00114 }

void Metapop::setPatchCapacities ( string  paramfem,
string  parammal 
)

Builds the new population from matrices of deme sizes.

Parameters:
paramfem the name of the parameter to take the matrix argument from.
parammal the name of the parameter to take the matrix argument from.

Definition at line 354 of file metapop.cc.

00355 {
00356   double *size_fem, *size_mal;
00357   unsigned int KFnum, KMnum;
00358   TMatrix popKfem, popKmal;
00359   
00360   _paramSet->getMatrix(paramfem,&popKfem);
00361   _paramSet->getMatrix(parammal,&popKmal);
00362   
00363   KFnum = popKfem.length();
00364   KMnum = popKmal.length();
00365   
00366   if(_patchNbr == 0 && KFnum != KMnum){
00367         warning("not same number of elements in females and males capacity matrices!\n");
00368     warning("setting the number of populations from size of longest capacity array (= %i).\n",max(KFnum, KMnum));
00369   }
00370   if(_patchNbr < max(KFnum, KMnum)) _patchNbr = max(KFnum, KMnum);
00371     
00372   _patchSizes.reset(2, _patchNbr);
00373 
00374   size_fem = popKfem.get();
00375   size_mal = popKmal.get();
00376   
00377   for(unsigned int i = 0; i < _patchNbr; ++i) {
00378     _patchSizes.set(FEM, i, (unsigned int)size_fem[i % KFnum]);
00379         _patchSizes.set(MAL, i, (unsigned int)size_mal[i % KMnum]);
00380   }   
00381  }

void Metapop::setPatchCapacities ( sex_t  SEX,
string  param 
)

Builds the new population from a matrix of deme sizes but for one sex only.

Sizes for the other sex class(es) must be given seperately.

Parameters:
SEX the sex class of the given deme sizes.
param the name of the parameter to take the matrix argument from.

Definition at line 325 of file metapop.cc.

00326 {
00327   double* size_array;
00328   unsigned int size_, Knum;
00329   TMatrix popK;
00330   
00331   _paramSet->getMatrix(param, &popK);
00332   
00333   Knum = popK.length();
00334 
00335   if(_patchNbr == 0 || _patchNbr < Knum)  _patchNbr = Knum;
00336   
00337   _patchSizes.reset(2, _patchNbr);
00338 
00339   size_array = popK.get();
00340   
00341   if(SEX == FEM)
00342     size_ = _patchKmal;
00343   else
00344     size_ = _patchKfem;
00345   
00346   for(unsigned int i = 0; i < _patchNbr; ++i) {
00347     _patchSizes.set(SEX, i, (unsigned int)size_array[i % Knum]);
00348         _patchSizes.set(!SEX, i, size_);
00349   }
00350 }

void Metapop::setPatchCapacities ( string  param  ) 

Builds the new population from a single matrix of deme sizes.

The sex-specific patch capacities will be half of the numbers given in the matrix.

Parameters:
param the name of the parameter to take the matrix argument from.

Definition at line 301 of file metapop.cc.

00302 {
00303   double* size_array;
00304   unsigned int Knum;
00305   TMatrix popK;
00306   
00307   _paramSet->getMatrix(param, &popK);
00308   
00309   Knum = popK.length();
00310 
00311   if(_patchNbr == 0 || _patchNbr < Knum)  _patchNbr = Knum;
00312   
00313   _patchSizes.reset(2, _patchNbr);
00314     
00315   size_array = popK.get();
00316   
00317   for(unsigned int i = 0; i < _patchNbr; ++i) {
00318     _patchSizes.set(FEM, i, (unsigned int)size_array[i % Knum]/2);
00319         _patchSizes.set(MAL, i, (unsigned int)size_array[i % Knum]/2);
00320   }
00321 }

void Metapop::setPatchCapacities (  ) 

Sets the deme capacity matrix from parameter values.

Definition at line 289 of file metapop.cc.

Referenced by setPopulationParameters().

00290 {
00291   _patchSizes.reset(2, _patchNbr);
00292     
00293   for(unsigned int i = 0; i < _patchNbr; ++i) {
00294     _patchSizes.set(FEM, i, _patchKfem);
00295         _patchSizes.set(MAL, i, _patchKmal);
00296   }
00297 }

void Metapop::setPopulation ( unsigned int  currentReplicate,
unsigned int  replicates 
)

Sets the population for the first generation of each replicates.

Definition at line 636 of file metapop.cc.

Referenced by SimRunner::Replicate_LOOP().

00637 {
00638 #ifdef _DEBUG_
00639   message("Metapop::setPopulation: ");
00640   fflush(stdout);
00641 #endif
00642   
00643   _currentReplicate = currentReplicate;
00644   
00645   //reset the population parameters, they may have changed during the life cycle
00646   setPopulationParameters();
00647   //reset the patch array, flush the patches and reset the patch capacities.
00648   buildPatchArray();
00649     
00650   _currentAge = NONE;
00651 
00652   //find the age class required to start the life cycle with:
00653   if(_requiredAge == NONE) _requiredAge = SIMenv::MainSim->getFirstRequiredAgeInLifeCycle();
00654 
00655 #ifdef _DEBUG_
00656   message("required age is: %i ", _requiredAge);
00657   fflush(stdout);
00658 #endif
00659   //load first generation from a source population
00660   if(_source_load) {    
00661     
00662     //delete ind in recycler, needed when dealing with large source pop and lots of replicates:
00663     purgeRecyclingPOOL();
00664     
00665     if(currentReplicate == 1 ||
00666        _source_replicates >= replicates || //load from a different source every replicate
00667        !(_source_replicates != 0 && (currentReplicate - 1) % (replicates/_source_replicates)) ) //change source every _source_replicates-nth replicates
00668       loadSourcePopulation();
00669     
00670     //load in preserve mode, i.e. make a copy of the saved pop
00671     if(_source_preserve) { 
00672       //basic checks:
00673       if(_source->getPatchNbr() != _patchNbr)
00674         fatal("loading population from source file failed: preserve mode forbids difference in number of patches\n");
00675       
00676       if(_source->getPatchKFem() != _patchKfem || _source->getPatchKMal() != _patchKmal)
00677         fatal("loading population from source file failed: preserve mode forbids difference in patch sizes\n");
00678 
00679       setPopulationFromSourceInPreserveMode();
00680     
00681     } else setPopulationFromSource();
00682     
00683   } else { //not source_load
00684     
00685     for(unsigned int i = 0; i < _patchNbr; ++i)
00686       _vPatch[i]->setNewGeneration( _requiredAge, this );
00687     
00688     _currentAge = _requiredAge;
00689     
00690   }
00691   
00692 #ifdef _DEBUG_
00693   message("loaded age is: %i \n",_currentAge);
00694 #endif
00695   
00696   if(_currentAge == NONE) warning("Metapop::setPopulation: generation 0 is empty!!\n");
00697 }

void Metapop::setPopulationFromSource (  ) 

Definition at line 758 of file metapop.cc.

Referenced by setPopulation().

00759 {
00760   //load in non-preserve mode, i.e. use saved pop as a source of new individuals
00761   //individuals are drawn without replacement, sex and age are preserved.
00762   deque< Individual* > src_fem_pool;
00763   deque< Individual* > src_mal_pool;
00764   
00765   if( _requiredAge & OFFSPRG ) {
00766     
00767     if(_source->size(OFFSPRG) != 0)
00768       _source->getAllIndividuals(OFFSx, src_fem_pool, src_mal_pool);
00769     else {
00770       warning("source population does not contain offspring individuals, using adults instead.\n"); 
00771       //total size of source has been checked before
00772       _source->getAllIndividuals(ADLTx, src_fem_pool, src_mal_pool);
00773     }
00774     
00775     fillPopulationFromSource(OFFSx, FEM, src_fem_pool);
00776     fillPopulationFromSource(OFFSx, MAL, src_mal_pool);
00777     src_fem_pool.clear();
00778     src_mal_pool.clear();
00779     
00780     _currentAge |= OFFSPRG;
00781   }
00782   
00783   if( _requiredAge & ADULTS ) {
00784     
00785     if(_source->size(ADULTS) != 0)
00786       _source->getAllIndividuals(ADLTx, src_fem_pool, src_mal_pool);
00787     else {
00788       warning("source population does not contain adult individuals, using offspring instead.\n"); 
00789       //total size of source has been checked before
00790       _source->getAllIndividuals(OFFSx, src_fem_pool, src_mal_pool);
00791     }
00792     
00793     fillPopulationFromSource(ADLTx, FEM, src_fem_pool);
00794     fillPopulationFromSource(ADLTx, MAL, src_mal_pool);
00795     src_fem_pool.clear();
00796     src_mal_pool.clear();
00797     
00798     _currentAge |= ADULTS;
00799   }
00800   
00801 } 

void Metapop::setPopulationFromSourceInPreserveMode (  ) 

Definition at line 701 of file metapop.cc.

Referenced by setPopulation().

00702 {
00703   Patch* src_patch;
00704   age_t source_age;
00705   
00706   if(_source->size(_requiredAge) == 0 && _source->size() != 0) {
00707     source_age = _source->getCurrentAge();
00708     warning("required age %i not present in source, using age class %i instead (preserve mode).\n"
00709             ,_requiredAge,source_age);
00710     
00711     for(unsigned int i = 0; i < _patchNbr; ++i) {
00712       src_patch = _source->getPatchPtr(i);
00713       fillPatchFromSource(FEM, src_patch, _vPatch[i], source_age);
00714       fillPatchFromSource(MAL, src_patch, _vPatch[i], source_age);
00715     }
00716     
00717   } else {
00718     
00719     for(unsigned int i = 0; i < _patchNbr; ++i) {
00720       src_patch = _source->getPatchPtr(i);
00721       if(_requiredAge & OFFSPRG) {
00722         fillPatchFromSource(FEM, src_patch, _vPatch[i], OFFSPRG);
00723         fillPatchFromSource(MAL, src_patch, _vPatch[i], OFFSPRG);
00724       }
00725       if(_requiredAge & ADULTS) {
00726         fillPatchFromSource(FEM, src_patch, _vPatch[i], ADULTS);
00727         fillPatchFromSource(MAL, src_patch, _vPatch[i], ADULTS);
00728       }
00729     }
00730   }
00731   
00732   if(size( ADULTS ) != 0)
00733     _currentAge |= ADULTS;
00734   
00735   if(size( OFFSPRG ) != 0)
00736     _currentAge |= OFFSPRG;
00737   
00738 } 

bool Metapop::setPopulationParameters (  ) 

Population's size parameters initializing procedure.

Definition at line 167 of file metapop.cc.

Referenced by LCE_Resize::execute(), setParameters(), setPopulation(), and updatePopulationParameters().

00168 {
00169   if(_paramSet->isSet("patch_number")) 
00170         _patchNbr = (unsigned int)_paramSet->getValue("patch_number");
00171   else _patchNbr = 0;
00172 
00173   if(_paramSet->isSet("patch_capacity")) {
00174         
00175         if( _paramSet->isMatrix("patch_capacity") ) {
00176           
00177       setPatchCapacities("patch_capacity");
00178           
00179         } else if( !(_paramSet->isSet("patch_number")) ) {
00180           
00181           error("param \"patch_number\" is missing!\n");
00182           return false;
00183       
00184         } else {
00185       _patchK = (unsigned int)_paramSet->getValue("patch_capacity");
00186       _patchKfem = _patchKmal = _patchK/2;
00187       setPatchCapacities();
00188         }
00189         
00190   } else if(_paramSet->isSet("patch_nbfem") && _paramSet->isSet("patch_nbmal")) {
00191         
00192         if( !(_paramSet->isMatrix("patch_nbfem")) && !(_paramSet->isMatrix("patch_nbmal")) ) {
00193           
00194       if( !(_paramSet->isSet("patch_number")) ) {
00195                 
00196                 error("param \"patch_number\" is missing!\n");
00197                 return false;
00198                 
00199       } else {
00200                 _patchKfem = (unsigned int)_paramSet->getValue("patch_nbfem");
00201                 _patchKmal = (unsigned int)_paramSet->getValue("patch_nbmal");
00202                 _patchK = _patchKfem + _patchKmal;
00203                 setPatchCapacities();
00204       }
00205           
00206         } else {
00207           
00208       if( !(_paramSet->isMatrix("patch_nbfem")) && _paramSet->isMatrix("patch_nbmal") ) {
00209                 _patchKfem = (unsigned int)_paramSet->getValue("patch_nbfem");
00210                 setPatchCapacities(MAL,"patch_nbmal");
00211       } else if( _paramSet->isMatrix("patch_nbfem") && !(_paramSet->isMatrix("patch_nbmal")) ) {
00212                 _patchKmal = (unsigned int)_paramSet->getValue("patch_nbmal");
00213                 setPatchCapacities(FEM,"patch_nbfem");
00214       } else
00215                 setPatchCapacities("patch_nbfem","patch_nbmal");
00216         }
00217         
00218   } else {
00219         error("population parameters are not properly set!\n");
00220         return false;
00221   }
00222   
00223   return true;
00224 }

void Metapop::setReplicates ( unsigned int  repl  )  [inline]

Definition at line 256 of file metapop.h.

Referenced by SimRunner::init_components(), and SimRunner::setReplicates().

00256 {_replicates = repl;}

bool Metapop::setSourceParameters (  ) 

Setter for source population parameters.

Definition at line 118 of file metapop.cc.

Referenced by Metapop(), and setParameters().

00119 {
00120   if(_paramSet->isSet("source_pop")) {
00121     _source_load = true;
00122     _source_name = _paramSet->getArg("source_pop");
00123     _source_preserve = _paramSet->isSet("source_preserve");
00124     _source_replicates = _paramSet->isSet("source_replicates") ? 
00125                         (unsigned int)_paramSet->getValue("source_replicates") : 0;
00126     
00127     _source_replicate_digits = _paramSet->isSet("source_replicate_digit") ? 
00128                                (unsigned int)_paramSet->getValue("source_replicate_digit") : 1;
00129     
00130     _source_start_at_replicate = _paramSet->isSet("source_start_at_replicate") ? 
00131                                 (unsigned int)_paramSet->getValue("source_start_at_replicate") : 1;
00132 
00133     _source_generation = _paramSet->isSet("source_generation") ? 
00134                         (unsigned int)_paramSet->getValue("source_generation") : 0;
00135     
00136     _source_filetype = _paramSet->getArg("source_file_type");
00137     
00138     if(_source_filetype.length() == 0)  _source_filetype = ".bin";
00139     
00140     _source_required_age = _paramSet->getArg("source_fill_age_class");
00141     
00142     if(_source_required_age.length() == 0) _requiredAge = NONE;
00143     else if(_source_required_age.compare("OFFSPRG") == 0 || _source_required_age.compare("offspring") == 0 ||
00144             _source_required_age.compare("0") == 0){
00145       _requiredAge = OFFSPRG;
00146     } else if(_source_required_age.compare("ADULTS") == 0 || _source_required_age.compare("adults") == 0 ||
00147               _source_required_age.compare("1") == 0) {
00148        _requiredAge = ADULTS;
00149     } else
00150        _requiredAge = NONE; //for now...
00151 
00152   
00153   } else {
00154     _source = NULL;
00155     _source_preserve = false;
00156     _source_load = false;
00157     _source_replicates = 0;
00158     _source_generation = 0;
00159     _source_name = "";
00160     _source_filetype = ".bin";
00161   }
00162   return true;
00163 }

void Metapop::show_up (  ) 

Definition at line 1000 of file metapop.cc.

01001 {
01002   message("Metapop:\n");
01003   message("nbre. of patches: %i(%i)\n", _vPatch.size(),_patchNbr);
01004   message("population size : %i\n", size());
01005   message("K = %i, K_fem = %i, K_mal = %i\n",_patchK, _patchKfem, _patchKmal);
01006   message("patch capacities: \n");
01007   _patchSizes.show_up();
01008   message("Patches:\n");
01009   for(unsigned int i = 0; i < _vPatch.size(); i++)
01010     _vPatch[i]->show_up();
01011 }

unsigned int Metapop::size ( age_t  AGE,
unsigned int  deme 
) [inline]

Simplified interface to get the size of both sexes of the appropriate age class(es) in one patch.

Parameters:
AGE age class flags
deme the focal deme

Definition at line 310 of file metapop.h.

Referenced by size().

00311   { return size( FEM, AGE, deme ) + size( MAL, AGE, deme );}

unsigned int Metapop::size ( age_t  AGE  )  [inline]

Simplified interface to get the size of both sexes of the appropriate age class(es) in the whole population.

Parameters:
AGE age class flags

Definition at line 303 of file metapop.h.

Referenced by size().

00304   { return size( FEM, AGE ) + size( MAL, AGE );}

unsigned int Metapop::size ( sex_t  SEX,
age_t  AGE,
unsigned int  deme 
) [inline]

Interface to get the size of a praticular age and sex class within a patch.

Parameters:
AGE age class flags
SEX sex class
deme the focal patch

Definition at line 690 of file metapop.h.

00691 {
00692   Patch* patch = getPatch(deme);
00693   return (patch!=0? patch->size(SEX, AGE) : 0);
00694 }  

unsigned int Metapop::size ( sex_t  SEX,
age_t  AGE 
) [inline]

Interface to get the size of a praticular age and sex class(es).

Parameters:
AGE age class flags
SEX sex class

Definition at line 682 of file metapop.h.

00683 {
00684   unsigned int s = 0;
00685   for(unsigned int i = 0; i < _patchNbr; i++)
00686     s += _vPatch[i]->size(SEX, AGE);
00687   return s;
00688 }

unsigned int Metapop::size (  )  [inline]

void Metapop::store_data ( BinaryStorageBuffer saver  )  [virtual]

Interface to store the component data (e.g. gene values) into a binary buffer.

Implements StorableComponent.

Definition at line 385 of file metapop.cc.

Referenced by BinaryDataSaver::storeData().

00386 {
00387   unsigned int *sizes[3];
00388   unsigned char separator[2] = {'@','P'};
00389   
00390   sizes[0] = new unsigned int [_patchNbr]; 
00391   sizes[1] = new unsigned int [_patchNbr]; 
00392   sizes[2] = new unsigned int [_patchNbr];
00393   
00394   for(unsigned int i = 0; i < _patchNbr; ++i) {
00395         sizes[0][i] = size(OFFSPRG,i);
00396         sizes[1][i] = size(FEM,ADULTS,i);
00397         sizes[2][i] = size(MAL,ADULTS,i);
00398   }
00399   //store the data, begin with pop separator and number of patches:
00400   saver->store(&separator, 2 * sizeof(unsigned char));
00401   
00402   saver->store(&_patchNbr, sizeof(unsigned int));
00403   //store the Patch sizes
00404   //offspring
00405   saver->store(sizes[0], _patchNbr * sizeof(unsigned int));
00406   //females adults
00407   saver->store(sizes[1], _patchNbr * sizeof(unsigned int));
00408   //males adults
00409   saver->store(sizes[2], _patchNbr * sizeof(unsigned int));
00410   
00411   int byte_count = saver->getByteLength();
00412   //record all individual informations: IDs, matings, etc.
00413   for(unsigned int i = 0; i < _patchNbr; ++i) {
00414         //first offspring:
00415         for(unsigned int j = 0; j < _vPatch[i]->size(FEM,OFFSx); ++j)
00416           _vPatch[i]->get(FEM, OFFSx, j)->store_data(saver);    
00417     
00418         for(unsigned int j = 0; j < _vPatch[i]->size(MAL,OFFSx); ++j)
00419           _vPatch[i]->get(MAL, OFFSx, j)->store_data(saver);    
00420     
00421         //then adults:
00422         for(unsigned int j = 0; j < _vPatch[i]->size(FEM, ADLTx); ++j)
00423           _vPatch[i]->get(FEM, ADLTx, j)->store_data(saver);
00424     
00425     for(unsigned int j = 0; j < _vPatch[i]->size(MAL, ADLTx); ++j)
00426       _vPatch[i]->get(MAL, ADLTx, j)->store_data(saver);
00427   }
00428   
00429 #ifdef _DEBUG_
00430     message("Metapop::store_data :stored %ikB of individual data (%i individuals)\n",
00431             (saver->getByteLength()-byte_count)/1024, size());
00432 #endif
00433   
00434     //records the trait sequences:
00435     map<trait_t, TraitPrototype *> traits = this->getTraitPrototypes();
00436     map<trait_t, TraitPrototype *>::iterator tt = traits.begin();
00437     //  trait_t type;
00438     separator[1] = 'T'; //trait separator = '@T'
00439   
00440     byte_count = saver->getByteLength();
00441 
00442     while(tt != traits.end()) {
00443         saver->store(&separator, 2 * sizeof(unsigned char));
00444         //store the trait type:
00445         saver->store((void*)tt->first.c_str(), TRAIT_T_MAX); 
00446 
00447         //then ask the prototype to store its data:
00448         tt->second->store_data(saver);
00449         //store the traits data:
00450         store_trait(tt->second->get_index(), saver);
00451 
00452         tt++;
00453     }
00454 #ifdef _DEBUG_
00455     message("Metapop::store_data :stored %ikB of traits data\n",
00456             (saver->getByteLength()-byte_count)/1024);
00457 #endif
00458   
00459   for(unsigned int i = 0; i < 3; i++)
00460     delete [] sizes[i];
00461 }

void Metapop::store_trait ( int  trait_idx,
BinaryStorageBuffer saver 
)

Iterates through the individuals containers to store the trait data to a binary file.

Definition at line 465 of file metapop.cc.

Referenced by store_data().

00466 {  
00467   for(unsigned int i = 0; i < _patchNbr; ++i) {
00468         //first offspring:
00469         for(unsigned int j = 0; j < _vPatch[i]->size(FEM, OFFSx); ++j) {
00470           _vPatch[i]->get(FEM, OFFSx, j)->getTrait(trait_idx)->store_data(saver);       
00471     }
00472         
00473         for(unsigned int j = 0; j < _vPatch[i]->size(MAL, OFFSx); ++j) {
00474           _vPatch[i]->get(MAL, OFFSx, j)->getTrait(trait_idx)->store_data(saver);       
00475     }
00476         //then adults:
00477         for(unsigned int j = 0; j < _vPatch[i]->size(FEM, ADLTx); ++j)
00478           _vPatch[i]->get(FEM, ADLTx, j)->getTrait(trait_idx)->store_data(saver);
00479         
00480     for(unsigned int j = 0; j < _vPatch[i]->size(MAL, ADLTx); ++j)
00481       _vPatch[i]->get(MAL, ADLTx, j)->getTrait(trait_idx)->store_data(saver);
00482   }
00483 }

void Metapop::updatePatchArray (  ) 

Called during simulation to modify the meta-population size.

Patch number and capacities are updated, new patches are empty, existing patches containers are untouched.

Definition at line 266 of file metapop.cc.

Referenced by LCE_Resize::buildNewPatchArrayNoBackup(), and updatePopulationParameters().

00267 {
00268   //remove or add patches as needed:
00269   resizePatchArray();
00270   //reset the patch capacities and patch ID:
00271   updatePatchState();
00272 }

void Metapop::updatePatchState (  ) 

Update the patch capacities and patch ID (reset to array position).

Definition at line 276 of file metapop.cc.

Referenced by LCE_Resize::buildNewPatchArrayWithBackup(), and updatePatchArray().

00277 {
00278   //reset the patch capacities and patch ID:
00279   for(unsigned int i = 0; i < _patchNbr; ++i) {
00280     _vPatch[i]->setID(i);
00281     _vPatch[i]->set_K(_patchSizes.get(FEM,i) + _patchSizes.get(MAL,i));
00282     _vPatch[i]->set_KFem(_patchSizes.get(FEM,i));
00283     _vPatch[i]->set_KMal(_patchSizes.get(MAL,i));
00284   }
00285 }

bool Metapop::updatePopulationParameters (  ) 

Called during simulation to change the population's parameters (temporal argument).

Definition at line 228 of file metapop.cc.

Referenced by Metapop().

00229 {
00230   if(!setPopulationParameters()) return false;
00231   updatePatchArray();
00232   //if we added patches, they will be empty
00233   //fusion of existing pop is not possible here, 
00234   //the individuals are flushed when deleting the patches
00235   return true;
00236 }


Friends And Related Function Documentation

Metapop* BinaryDataLoader::extractPop ( std::string &  ,
unsigned  int,
SimBuilder ,
Metapop  
) [friend]


Member Data Documentation

The current age class, might be changed by the LCEs.

Definition at line 136 of file metapop.h.

Referenced by getCurrentAge(), setCurrentAge(), setPopulation(), setPopulationFromSource(), and setPopulationFromSourceInPreserveMode().

unsigned int Metapop::_currentGeneration [private]

The current generation in the generation loop, starts at 1.

Definition at line 131 of file metapop.h.

Referenced by getCurrentGeneration(), and setCurrentGeneration().

unsigned int Metapop::_currentReplicate [private]

The current replicate in the replicate loop, starts at 1.

Definition at line 133 of file metapop.h.

Referenced by getCurrentReplicate(), loadSourcePopulation(), setCurrentReplicate(), and setPopulation().

unsigned int Metapop::_generations [private]

Patch init sizes.

Number of generations to iterate.

Definition at line 126 of file metapop.h.

Referenced by getGenerations(), and setGenerations().

A BinaryDataLoader to load a population from a binary data file.

Definition at line 89 of file metapop.h.

Referenced by loadPopFromBinarySource().

Definition at line 81 of file metapop.h.

Referenced by BinaryDataLoader::extractPop(), loadPopFromTraitFile(), and setMPImanager().

unsigned int Metapop::_patchK [private]

Patch carrying capacity.

Definition at line 118 of file metapop.h.

Referenced by getPatchCapacity(), setPopulationParameters(), and show_up().

unsigned int Metapop::_patchKfem [private]

Sex specific carrying capacities.

Definition at line 120 of file metapop.h.

Referenced by getPatchKFem(), loadPopFromBinarySource(), setPatchCapacities(), setPopulation(), setPopulationParameters(), and show_up().

unsigned int Metapop::_patchKmal [private]

unsigned int Metapop::_patchNbr [private]

Matrix of the deme sizes, row 0 for the males, row 1 for the females.

Definition at line 122 of file metapop.h.

Referenced by buildPatchArray(), getPatchCapacities(), getPatchCapacity(), setPatchCapacities(), show_up(), and updatePatchState().

unsigned int Metapop::_replicates [private]

Number of replicates to iterate.

Definition at line 128 of file metapop.h.

Referenced by getReplicates(), loadSourcePopulation(), and setReplicates().

The age class flag that is required to fill the population at the beginning of a replicate.

Definition at line 112 of file metapop.h.

Referenced by fillPatchFromSource(), setPopulation(), setPopulationFromSource(), setPopulationFromSourceInPreserveMode(), and setSourceParameters().

std::string Metapop::_source_filetype [private]

The trait type to load from.

Definition at line 108 of file metapop.h.

Referenced by loadPopFromTraitFile(), loadSourcePopulation(), and setSourceParameters().

unsigned int Metapop::_source_generation [private]

The generation to load from the binary file source file.

Definition at line 104 of file metapop.h.

Referenced by loadPopFromBinarySource(), and setSourceParameters().

bool Metapop::_source_load [private]

Flage to specify that the population should be built from the binary loaded source population.

Definition at line 95 of file metapop.h.

Referenced by setPopulation(), and setSourceParameters().

std::string Metapop::_source_name [private]

The base filename of source population files.

Definition at line 106 of file metapop.h.

Referenced by loadSourcePopulation(), and setSourceParameters().

bool Metapop::_source_preserve [private]

Flag to specify the loading mode, true means the source pop is the actual population.

Definition at line 93 of file metapop.h.

Referenced by loadPopFromBinarySource(), setPopulation(), and setSourceParameters().

unsigned int Metapop::_source_replicate_digits [private]

Number of digits in the replicate counter filename extension.

Definition at line 100 of file metapop.h.

Referenced by loadSourcePopulation(), and setSourceParameters().

unsigned int Metapop::_source_replicates [private]

The number of source files, each file being a different replicate of the source population.

Used to automatically build the source filename.

Definition at line 98 of file metapop.h.

Referenced by loadSourcePopulation(), setPopulation(), and setSourceParameters().

std::string Metapop::_source_required_age [private]

The age class to fill with the source population.

Definition at line 110 of file metapop.h.

Referenced by setSourceParameters().

unsigned int Metapop::_source_start_at_replicate [private]

The replicate number to start loading from.

Definition at line 102 of file metapop.h.

Referenced by loadSourcePopulation(), and setSourceParameters().

The stat handler for the population stats.

Definition at line 83 of file metapop.h.

Referenced by loadStatServices().

deque< Patch* > Metapop::_vPatch [private]


The documentation for this class was generated from the following files:

Generated for Nemo v2.1.2 by  doxygen 1.5.8 -- Nemo is hosted by  SourceForge.net Logo