<h2><strong>Using sexual reproduction with mating types</strong></h2>
<p>Although by default, reproduction in Avida is asexual, Avida can be configured to allow organisms to reproduce with sexual reproduction. By default, when sexual reproduction is turned on, any organism can mate with any other organism. However, Avida is also capable of sexual reproduction with two distinct mating types (e.g., males and females), mimicking the mating systems found in many biological organisms, in which only matings between opposite mating types are successful. (Note: this page is intended as an overview of how to implement these distinct mating types and their associated configuration options, not as an exhaustive guide to all of the other options involved in sexual reproduction in general).</p>
<p><br />First, you will need to turn on mating types in avida.cfg:</p>
<p><code>MATING_TYPES 1</code></p>
<p>However, this change by itself is not enough. You will also need to modify your instruction set so that the organisms can set their mating types, by adding the following two instructions to your instruction set:</p>
<p><code>INST set-mating-type-male<br />INST set-mating-type-female</code></p>
<p>Once an organism sets its mating type, it is irreversible -- repeated execution of a different set-mating-type-X instruction will fail. If you want your organisms to be able to change their mating type during their lifetime, you can add another instruction that lets them de-differentiate, after which they can re-set their mating type to something else:</p>
<p><code>INST set-mating-type-juvenile</code></p>
<p>Then, you also need to make sure that your organisms are using the proper instruction to divide, or else their mating types may not be checked properly. So you should replace the divide instruction in your instruction set (probably <code>h-divide</code> or <code>divide-sex</code>) with div-sex-mating-type.</p>
<p>While we're in the instruction set, you might want to consider adding instructions allowing organisms to conditionally execute instructions based on their phenotypic sex (otherwise, they have no real mechanism to detect what their phenotypic sex is):</p>
<p><code>INST if-mating-type-male<br />INST if-mating-type-female<br />INST if-mating-type-juvenile</code></p>
<p>Next, you'll need to modify your events file to make sure that the population is initialized with a male and a female organism. You can create a new default organism by modifying default-heads-sex.org (make two, say, default-heads-sex-male.org and default-heads-sex-female.org). Simple add a set-mating-type-male or set-mating-type-female instruction to the beginning of each .org file (you'll probably want it to be the first instruction that they execute, but it doesn't necessarily have to be). Then replace the original divide instruction with the div-sex-mating-type instruction.</p>
<p>Now, modify your events file to inject both organisms at the start of the run:</p>
<p><code>u begin Inject default-heads-sex-male.org 0<br />u begin Inject default-heads-sex-female.org 1</code></p>
<p>At this point, you should be able to get a run going, but read on for some further options and points to consider.</p>
<p>-------------------------------------------</p>
<h2>What happens during sexual reproduction:</h2>
<p>When an organism reproduces sexually, it needs to find a mate. When the first organism in the population successfully divides, its 'offspring' goes into what is called the birth chamber, rather than being placed into the population. This is sort of a waiting area in which organisms can find mates. When another organism divides, Avida checks the birth chamber to see if there are any 'offspring' waiting to find a mate that were generated by a parent of the opposite mating type. If so, then they recombine, and the sexual offspring are placed into the population. If not, then this organism's 'offspring' goes into the birth chamber to wait as well. Because of this waiting process, you can almost think of these 'offspring' as being more like sperm or eggs, that need to join up with a gamete of the opposite type before they can form a zygote together.</p>
<p>There are a few options regarding the birth chamber. First, you'll want to set its maximum size -- how many 'offspring' (or gametes) can be waiting in the birth chamber at any given time? The default is 3600, the same as the size of the default population. This can be changed in avida.cfg:</p>
<p><code>MAX_GLOBAL_BIRTH_CHAMBER_SIZE 3600</code></p>
<p>Another thing to consider regarding the birth chamber is how long 'offspring' or gametes can wait there before dying. The default setting for this parameter (<code>MAX_BIRTH_WAIT_TIME</code>) is -1, meaning there is no limit -- they will remain in the birth chamber indefinitely, until they get picked by a mate. You will almost certainly want to change this. For example:</p>
<p><code>MAX_BIRTH_WAIT_TIME 3</code></p>
<p>would tell Avida that an 'offspring' or gamete can wait for three updates to be chosen as a mate, but any gametes that are not picked as mates within three updates after they go into the birth chamber will die. In practice, three seems to give good results.</p>
<p>One final thing to consider is spatial organization. Currently, the birth chamber has no spatial organization to it; any organism can mate with any other organism regardless of their "physical" location in the population. Since mating has no spatial structure, you therefore may not want any spatial structure in your population at all. To do so, change:</p>
<p><code>BIRTH_METHOD 4</code></p>
<p>-------------------------------------------</p>
<h2>Mate choice:</h2>
<p>By default, organisms will choose mates randomly. However, organisms can choose mates based on various phenotypic attributes by executing instructions that confer certain mating preferences. To enable this, simply add one or more of the following instructions to the instruction set:</p>
<p><code>INST set-mate-preference-highest-merit<br />INST set-mate-preference-random<br />INST set-mate-preference-highest-display-a<br />INST set-mate-preference-highest-display-b</code></p>
<p>For example, suppose a female organism executes a set-mate-preference-highest-merit instruction, and then later divides. If there is more than one male 'offspring' (or sperm) waiting in the birth chamber when she divides, she will pick the one that came from a parent with the highest merit. Likewise, a female that has declared a preference for males with higher values of display A will select the one with the highest value of display A from among the available offspring of males in the birth chamber at the time of her divide.</p>
<p>But wait -- what are these displays? Organisms can develop displays by executing instructions, which you will have to add to the instruction set:</p>
<p><code>increment-mating-display-a<br />increment-mating-display-b<br />set-mating-display-a<br />set-mating-display-b</code></p>
<p>When an organism is born, its value for both display A and display B are both set to 0. Executing an <code>increment-mating-display-A</code> instruction adds 1 unit to its display. Using this method, an organism can only develop a large display by repeatedly executing this increment instruction. The <code>set-mating-display-A</code> and <code>-B</code> instructions, on the other hand, will copy the value of the ?BX? register (which register is used can be nop-modified) into its mating display. So using these instructions, organisms can develop exaggerated displays relatively quickly by using mathematical operations to generate a large value in their registers and then copying it to their displays.</p>
<p>Of course, in nature, organisms don't always perceive signals with 100% accuracy. Thus, in Avida, you can introduce noise into an organism's perception of other organisms' signals or phenotypic traits, using the <code>NOISY_MATE_ASSESSMENT</code> config option (0 = noise off [default]; 1 = noise on):</p>
<p><code>NOISY_MATE_ASSESSMENT 1</code></p>
<p>How much noise is introduced? When an organism assesses a phenotypic trait of a potential mate, the perceived value is drawn from a normal distribution with the mean at the actual value, and with a coefficient of variation of 0.1. This coefficient of variation can be changed with the <code>MATE_ASSESSMENT_CV</code> config option:</p>
<p><code>MATE_ASSESSMENT_CV 0.15</code></p>
<p>-------------------------------------------</p>
<h2>Costs:</h2>
<p>In nature, males and females of many animals can differ in certain traits because of sex-specific selective pressures. Avida can reflect this by implementing sex-specific costs for instructions. For example, in many animals, females invest more in reproduction. In Avida, we can make females pay extra CPU cycles to execute an instruction. This is an especially important consideration for the <code>div-sex-mating-type</code> instruction. Making females pay extra CPU cycles to complete division not only reflects the biological characteristics of many species, but also slows down female reproductive rates. As a result of males dividing more quickly than females (on average), the offspring of male organisms (i.e., sperm) will accumulate in the birth chamber. Females, then, will usually not be mate-limited, and will have more opportunities for mate choice -- again reflecting patterns that are often seen in nature. You can do this as follows, by modifying the div-sex-mating-types instruction in the instruction set file:</p>
<p><code>INST div-sex-mating-type:female_cost=50</code></p>
<p>This will require females to pay an extra 50 CPU cycles to complete their divide statements. Note that another work-around to forcing females to be the choosy sex is with the <code>LEKKING</code> option in avida.cfg. If you set its value to 1, all the offspring of male organisms will always go into the birth chamber -- in other words, males can never choose females as mates; females are always the 'choosers'.</p>
<p>Another important element is the cost of mate choice. In nature, mate choice can impose substantial costs on organisms. (For example, spending a lot of time searching for mates rather than mating with the first guy that comes along can put you at greater risk of being eaten by a predator.) You can force females who mate non-randomly to pay even greater CPU costs as follows:</p>
<p><code>INST div-sex-mating-type:female_cost=50:choosy_female_cost=50</code></p>
<p>In this setup, males will pay 1 CPU cycle for executing a <code>div-sex-mating-type</code> instruction. Randomly mating females will pay an additional 50 CPU cycles. Females who mate non-randomly (e.g., choose the male with the highest merit or highest display A) will pay another 50 on top of that (i.e., 1 + 50 [for being female] + 50 [for being choosy] = 101).</p>
<p><br />-------------------------------------------</p>
<h2>Memory considerations:</h2>
<p>Normally, Avida keeps a record of every genotype (i.e., genome sequence) that has ever existed in the population. This is very useful when you want to trace an organism's lineage back through its ancestors. However, when sexual reproduction is turned on (and especially when separate mating types are turned on), all that recombination generates much more diversity. This means that Avida can take up quite a bit of memory (and will probably crash if you're doing a long run), and will get slower and slower as the run progresses and there are more and more genotypes to keep track of.</p>
<p>If you don't need detailed information on organismal ancestry, you can turn off lineage tracking with the following config setting:</p>
<p><code>DISABLE_GENOTYPE_CLASSIFICATION 1</code></p>
<p><br />-------------------------------------------</p>
<h2>Collecting data on mating types:</h2>
<p>There are a variety of print actions that can be used to gather data on mating types and mate preferences as your population evolves:</p>
<p><code>PrintMatingTypeHistogram</code>: This action will print the number of juvenile, female, and male organisms in the population at the current update</p>
<p><code>PrintMatingDisplayData</code>: This action prints the average display values (display-A and display-B) of all of the juveniles, females, and males in the population at the current update</p>
<p><code>PrintFemaleMatePreferenceData</code>: This action prints the number of females with each different type of mating preference at the current update</p>
<p><code>PrintBirthChamberMatingTypeHistogram</code>: This action prints the number of "sperm" and "eggs" in the birth chamber at the current update</p>
<p><code>PrintSuccessfulMates</code>: This action prints the genotypes and some useful phenotypic information (e.g., merit, mating types, displays, preferences) of the parents of all the successful matings that occurred during the current update</p>
<p><code>PrintBirthChamber</code>: This action prints the genotypes and some useful phenotypic information (e.g., merit, mating types, displays, preferences) of the parents of all the "sperm" or "eggs" waiting in the birth chamber at the current update</p>
<p>(Note: by using PrintBirthChamber to print all the organisms currently waiting to be chosen as mates, and PrintSuccessfulMates to print all the organisms that were successfully chosen as mates, you can estimate sexual selection differentials.)</p>
<p>-------------------------------------------</p>
<h2>Analyze Mode</h2>
<p>There are a few phenotypic attributes that can be used with the DETAIL statement in analyze mode (pretty self-explanatory):</p>
<p><code>mating_type</code><br /><code>mate_preference</code><br /><code>mating_display_a</code><br /><code>mating_display_b</code></p>
<p>Using these, you can take a genotype and run it through analyze mode to find out what mating type it develops as (useful if there are multiple set-mating-type-XXX instructions in a genome and it's hard to tell which one will be executed first), what kind of mate preference it will exhibit, how large its sexual displays are, etc.</p>
<p>&nbsp;</p>
