📄 code_death_birth.html
字号:
<html><title>Organism Placement the Population</title><body bgcolor="#FFFFFF" text="#000000" link="#0000AA" alink="#0000FF" vlink="#000044"><h2 align=center>Organism Placement the Population</h2><p>This document describes the interaction between organisms and thepopulation.<h3>The Death of an Organism</h3><p>When an organism is killed off, its location in the population (that is,its population cell) needs to be emptied, the scheduler needs to be notified,and various statistics need to be update to reflect the recently deceased. All of this is handled with themethod below. In a default avida setup, this method will only be called whena cell needs to be cleared out for a new occupant, but you've already seenthis method used when you implemented the kill-related events in a previoushomework. We'll see it also called from code displayed in the next sectionof this document.<pre> void <font color="#880000">cPopulation</font>::<font color="#008800">KillOrganism</font>(<font color="#880000">cPopulationCell</font> & <font color="#000088">in_cell</font>) { <font color="#886600">// do we actually have something to kill?</font> if (<font color="#000088">in_cell</font>.<font color="#008800">IsOccupied</font>() == false) { return; } <font color="#886600">// Statistics...</font> <font color="#880000">cOrganism</font> * <font color="#000088">organism</font> = <font color="#000088">in_cell</font>.<font color="#008800">GetOrganism</font>(); <font color="#000088">stats</font>.<font color="#008800">RecordDeath</font>(<font color="#000088">in_cell</font>.<font color="#008800">GetID</font>(), <font color="#000088">organism</font>-><font color="#008800">GetGenotype</font>()-><font color="#008800">GetID</font>(), <font color="#000088">organism</font>-><font color="#008800">GetPhenotype</font>().<font color="#008800">GetAge</font>()); <font color="#000088">num_organisms</font>--; if (<font color="#000088">organism</font>-><font color="#008800">GetPhenotype</font>().<font color="#008800">IsParasite</font>() == true) { <font color="#000088">organism</font>-><font color="#008800">GetGenotype</font>()-><font color="#008800">AddParasite</font>(); } <font color="#000088">organism</font>-><font color="#008800">GetGenotype</font>()-><font color="#008800">RemoveOrganism</font>(); <font color="#886600">// And clear it!</font> <font color="#000088">in_cell</font>.<font color="#008800">RemoveOrganism</font>(); delete <font color="#000088">organism</font>; <font color="#886600">// adjust the scheduler</font> <font color="#000088">schedule</font>-><font color="#008800">Adjust</font>( <font color="#000088">in_cell</font>.<font color="#008800">GetID</font>(), <font color="#880000">cMerit</font>(0) ); }</pre><p>This method takes as an argument the cell that needs to be emptied. Itstarts off by making sure that the cell in question actually hasan organism in it to be killed. If not, it stops right there. If so,it records some statistics about that organism's life, and updates itscounter of living organisms to reflect that there is one fewer. If theorganism is a parasite, it also lets the genotype know this. The genotypeobject keeps track of the number of organisms of that genotype that haveexhibited parasitic behavior since some organisms may only moonlight asparasites, but be perfectly normal at other times. Note that this currentlyonly works for old-style parasites that are separate organisms. The newstyle parasites that exist within other organisms and cannot be so easilydetected, but I will likely add new code here in the future to monitorthe "infection rates" of genotypes.<p>Once the statistics are finished, the cell itself is cleared with the<font color="#880000">cPopulationCell</font>::<font color="#008800">RemoveOrganism</font>()method (in which the pointer to the organism it once contained is set toNULL), and the old organism is deleted. Finally, the scheduler (which is theobject that doles out CPU cycles to the individual organisms) is updated toreflect that this cell is now empty.<h3>Activating an Organism in a Specific Cell</h3><p>If an organism is going to be placed into a specific cell of the population,the method ActivateOrganism can be called on the population, telling it thelocation in memory of the organism to be placed and the cell to place it in.This method will call the <font color="#008800">KillOrganism</font>() methodto make sure the cell is unoccupied. In turn, this method is called from the<font color="#008800">Inject</font>() method as well as <fontcolor="#008800">ActivateOffspring</font>(), described below.Here is the ActivateOrganism method:<pre> void <font color="#880000">cPopulation</font>::<font color="#008800">ActivateOrganism</font>(<font color="#880000">cOrganism</font> * <font color="#000088">in_organism</font>, <font color="#880000">cPopulationCell</font> & <font color="#000088">target_cell</font>) { <font color="#008800">assert</font>(<font color="#000088">in_organism</font> != NULL); <font color="#886600">// If the organism does not have a genotype, give it one!</font> if (<font color="#000088">in_organism</font>-><font color="#008800">GetGenotype</font>() == NULL) { <font color="#880000">cGenotype</font> * <font color="#000088">new_genotype</font> = <font color="#000088">genebank</font>-><font color="#008800">AddGenotype</font>(<font color="#000088">in_organism</font>-><font color="#008800">GetGenome</font>()); <font color="#000088">in_organism</font>-><font color="#008800">SetGenotype</font>(<font color="#000088">new_genotype</font>); } <font color="#880000">cGenotype</font> * <font color="#000088">in_genotype</font> = <font color="#000088">in_organism</font>-><font color="#008800">GetGenotype</font>(); <font color="#886600">// Save the old genotype from this cell...</font> <font color="#880000">cGenotype</font> * <font color="#000088">old_genotype</font> = (<font color="#000088">target_cell</font>.<font color="#008800">IsOccupied</font>()) ? <font color="#000088">target_cell</font>.<font color="#008800">GetOrganism</font>()-><font color="#008800">GetGenotype</font>() : NULL; <font color="#886600">// Update the contents of the target cell.</font> <font color="#008800">KillOrganism</font>(<font color="#000088">target_cell</font>); <font color="#000088">target_cell</font>.<font color="#008800">InsertOrganism</font>(*<font color="#000088">in_organism</font>); <font color="#886600">// Update the genebank...</font> <font color="#000088">in_genotype</font>-><font color="#008800">AddOrganism</font>(); if (<font color="#000088">old_genotype</font> != NULL) <font color="#000088">genebank</font>-><font color="#008800">AdjustGenotype</font>(*<font color="#000088">old_genotype</font>); <font color="#000088">genebank</font>-><font color="#008800">AdjustGenotype</font>(*<font color="#000088">in_genotype</font>); <font color="#886600">// Initialize the time-slice for this new organism.</font> <font color="#000088">schedule</font>-><font color="#008800">Adjust</font>(<font color="#000088">target_cell</font>.<font color="#008800">GetID</font>(),<font color="#000088">in_organism</font>-><font color="#008800">GetPhenotype</font>().<font color="#008800">GetMerit</font>()); <font color="#886600">// Special handling for certain birth methods.</font> if (<font color="#880000">cConfig</font>::<font color="#008800">GetBirthMethod</font>() == POSITION_CHILD_FULL_SOUP_ELDEST) { <font color="#000088">reaper_queue</font>.<font color="#008800">Push</font>(&<font color="#000088">target_cell</font>); } <font color="#000088">num_organisms</font>++; <font color="#886600">// Statistics...</font> <font color="#000088">stats</font>.<font color="#008800">RecordBirth</font>(<font color="#000088">target_cell</font>.<font color="#008800">GetID</font>(), <font color="#000088">in_genotype</font>-><font color="#008800">GetID</font>(), <font color="#000088">in_organism</font>-><font color="#008800">GetPhenotype</font>().<font color="#008800">ParentTrue</font>()); }</pre><p>After asserting that we are indeed injecting a legal organism, the first thingwe do is check to see if that organism has already been assigned its genotype.If an organism was born from a parent in the population, it will have beenassigned a genotype by the time this method is called. If it does not have agenotype, however, the genebank object will be called to look up anygenotypes that match this genome. The genebank will either return an exactmatch, or else create a new genotype, add it to the genebank, and return itspointer. In either case, we now have a genotype for this organism.<p>Before we erase the organism currently in this cell, we want to keep track ofwhat genotype it was part of for use in updating the genebank later. We thenkill the organism in the cell (as described above) and insert the new one.The <font color="#880000">cPopulationCell</font>::<font color="#008800">InsertOrganism</font>()method will setup the organism based on the environmental conditions of thiscell (mutation rate, tasks rewarded, etc), and store the organism for futureuse.<p>We then adjust the genotype to let it know a new organism of its type hasbeen created, and tell the genebank that it should also adjust the genotypesto reflect their new abundances (one genotype has grown by one, the other hasshrunk, so the genotype ordering may change). Other maintenance we need todo at this point includes adjusting the scheduler to let it know the merit ofthis new organism, and the "reaper_queue" if we keep track of the birth orderof organisms so that we can always kill off the oldest in the population.<p>Finally, we adjust some more statistics by incrementing the number oforganisms in the population and let the statistics object know that a neworganism was born, with all of its information.Remember, if this cell was already occupied, KillOrganism() would havedecremented it, so this will properly reflect the number of organisms alivein the population at any moment.<h3>Placing an Offspring in the Population</h3><p>When an organism gives birth, we must collect some relevent statistics,which can best be accomplished in the population object. Then we mustplace the offspring into its own cell in the population. This is all donewith the following method:<pre> void <font color="#880000">cPopulation</font>::<font color="#008800">ActivateOffspring</font>(<font color="#880000">cOrganism</font> * <font color="#000088">child_organism</font>, <font color="#880000">cOrganism</font> & <font color="#000088">parent_organism</font>) { <font color="#008800">assert</font>(<font color="#000088">child_organism</font> != NULL); <font color="#008800">assert</font>(&<font color="#000088">parent_organism</font> != NULL); <font color="#008800">assert</font>(<font color="#000088">child_organism</font> != &<font color="#000088">parent_organism</font>); <font color="#886600">// First, setup the genotype of the offspring.</font> <font color="#880000">cGenotype</font> * <font color="#000088">parent_genotype</font> = <font color="#000088">parent_organism</font>.<font color="#008800">GetGenotype</font>(); <font color="#880000">cGenotype</font> * <font color="#000088">child_genotype</font> = <font color="#000088">parent_genotype</font>; <font color="#886600">// If the parent genotype is not correct for the child, adjust it.</font> if (<font color="#000088">parent_organism</font>.<font color="#008800">GetPhenotype</font>().<font color="#008800">CopyTrue</font>() == false) { <font color="#000088">child_genotype</font> = <font color="#000088">genebank</font>-><font color="#008800">AddGenotype</font>(<font color="#000088">child_organism</font>-><font color="#008800">GetGenome</font>(), <font color="#000088">parent_genotype</font>); } <font color="#886600">// And set the genotype now that we know it.</font> <font color="#000088">child_organism</font>-><font color="#008800">SetGenotype</font>(<font color="#000088">child_genotype</font>);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -