⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code_events.html

📁 有趣的模拟进化的程序 由国外一生物学家开发 十分有趣
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  * merit (double) default: -1  *   The initial merit of the organism. If set to -1, this is ignored.  * lineage label (integer) default: 0  *   An integer that marks all descendants of this organism.  * neutral metric (double) default: 0  *   A double value that randomly drifts over time.  **/</font>  <b>:args:</b>  <font color="#880000">cString</font> <font color="#000088">fname</font>          "START_CREATURE"  <font color="#880000">int</font>     <font color="#000088">cell_id</font>        0  <font color="#880000">double</font>  <font color="#000088">merit</font>          -1  <font color="#880000">int</font>     <font color="#000088">lineage_label</font>  0  <font color="#880000">double</font>  <font color="#000088">neutral_metric</font> 0  <b>:body:</b>  if (<font color="#000088">fname</font> == "START_CREATURE") <font color="#000088">fname</font> = <font color="#880000">cConfig</font>::<font color="#008800">GetStartCreature()</font>;  <font color="#880000">cGenome</font> <font color="#000088">genome</font> =     <font color="#880000">cInstUtil</font>::<font color="#008800">LoadGenome</font>(<font color="#000088">fname</font>, <font color="#000088">population</font>-><font color="#008800">GetEnvironment()</font>.<font color="#008800">GetInstLib()</font>);  <font color="#000088">population</font>-><font color="#008800">Inject</font>(<font color="#000088">genome</font>, <font color="#000088">cell_id</font>, <font color="#000088">merit</font>, <font color="#000088">lineage_label</font>, <font color="#000088">neutral_metric</font>);</pre><p>As indicated by its description, the "<tt>inject</tt>" command will inject asingle organism into the population.  The user can specify a filename thatcontains the organism's genome (or START_CREATURE to use this value from thegenesis file), the cell in which the organism should beplaced, the merit to start the organism with (or -1 to auto-calculate it onloading), a lineage label to tag all of this organism's offspring,and a neutral value that will drift over each generation.  Each ofthese configuration values is supplied with a default so that the useris not required to enter any additional information.<p>But what actually happens when this event is run?<p>First, the <tt>if</tt>-statement checks if the user has entered(or left as default) the value <tt>"START_CREATURE"</tt> as the filename.If so, it looks up the proper filename (from the cConfig class that holdsthe current state of all the genesis variables -- we'll talk more about itsoon) and sets this variable such that it is now indicating a real file.<p>The next line builds an object of class <font color="#880000">cGenome</font>using a function called <font color="#008800">LoadGenome</font>in the utility class <font color="#880000">cInstUtil</font>.  This functionneeds the filename to load from, and an instruction set to convert thecontents of that file into a genome.  The instruction set is stored withinthe population's environment, and therefore requires a series of Methods tobe called to retrieve it.<p>Finally, this event takes that newly created genome, and runs the method<font color="#008800">Inject</font> on the population object.  This methodtakes in all of the information about the organism to be injected and thenactually builds the cOrganism object and places into the population.<h3>Example: The <tt>inject_all</tt> event</h3><p>Below is a slight variation on the <tt>inject</tt> command.  Rather thaninjecting a single organism into a specific cell, it will inject identicalorganisms into every cell in the population.<p><pre>  <b>inject_all</b>  <b>:descr:</b>  <font color="#886600">/**  * Injects identical organisms into all cells of the population.  *  * Parameters:  * filename (string)  *   The filename of the genotype to load. If this is left empty, or the keyword  *   "START_CREATURE" is given, than the genotype specified in the genesis  *   file under "START_CREATURE" is used.  * merit (double) default: -1  *   The initial merit of the organism. If set to -1, this is ignored.  * lineage label (integer) default: 0  *   An integer that marks all descendants of this organism.  * neutral metric (double) default: 0  *   A double value that randomly drifts over time.  **/</font>  <b>:args:</b>  <font color="#880000">cString</font> <font color="#000088">fname</font>          "START_CREATURE"  <font color="#880000">double</font>  <font color="#000088">merit</font>          -1  <font color="#880000">int</font>     <font color="#000088">lineage_label</font>  0  <font color="#880000">double</font>  <font color="#000088">neutral_metric</font> 0  <b>:body:</b>  if (<font color="#000088">fname</font> == "START_CREATURE") <font color="#000088">fname</font> = <font color="#880000">cConfig</font>::<font color="#008800">GetStartCreature()</font>;  <font color="#880000">cGenome</font> <font color="#000088">genome</font> =     <font color="#880000">cInstUtil</font>::<font color="#008800">LoadGenome</font>(<font color="#000088">fname</font>, <font color="#000088">population</font>-><font color="#008800">GetEnvironment()</font>.<font color="#008800">GetInstLib()</font>);  for (<font color="#880000">int</font> <font color="#000088">i</font> = 0; <font color="#000088">i</font> < <font color="#000088">population</font>-><font color="#008800">GetSize()</font>; <font color="#000088">i</font>++) {    <font color="#000088">population</font>-><font color="#008800">Inject</font>(<font color="#000088">genome</font>, <font color="#000088">i</font>, <font color="#000088">merit</font>, <font color="#000088">lineage_label</font>, <font color="#000088">neutral_metric</font>);  }  <font color="#000088">population</font>-><font color="#008800">SetSyncEvents</font>(true);</pre><p>The first difference that you should notice is the lack of the<font color="#000088"><tt>cell_id</tt></font> variable.  Since we are nolonger injecting into just a single cell, no cell ID needs to beincluded.  The remaining differences are all in the body of the event.<p>The first two commands in the body remain the same; that is, the filenameis finalized, and the genome used to create the organisms is itselfconstructed.  The next thing we need to do is actually inject the organismsinto each cell of the population.  To run the Inject command once for eachposition we employ a <tt>for</tt> command.  This command has a strangesyntax:<pre>  for ( <font color="#886600">setup</font> ; <font color="#886600">continue_test</font> ; <font color="#886600">step_method</font> ) {    <font color="#886600">loop body</font>  }</pre><p>In our case, we want to set a variable to every number that representsa cell in the population.  These numbers go from zero to the population sizeminus one.  To build this loop, we <font color="#886600">setup</font> bycreating a variable called <font color="#000088">i</font> and initialize itto 0 -- the ID of the first cell we plan to inject into.  We want tokeep injecting into all cells with ID less than the full population size, soour <font color="#886600">continue_test</font> tells us to keep going aslong as i is less than that number.  Finally our<font color="#886600">step_method</font> tells the program that aftereach execution of the contents of the loop, it should increment the value ofi by one.  The Inject method itself works the same as in theprevious event.<p>Finally, we run the method <font color="#008800">SetSyncEvents</font>(true)on the population object.  This is not necessary, but it helps keep the event triggers in proper sync.  Since we just injected organisms into everysite in the population, we have effectively reset the generation of thepopulation to zero.  This could screw up some events that are triggered basedon generation if we're not careful, so we warn the population that suchtrouble may occur, and it should re-sync the events at its earliestconvenience.<p>So that isn't so hard is it?  Good!  Now we get to some...<h3>Try it Out...</h3><p>Here are a couple of example events that you can try implementing.  Thefirst one of them is the "<b><tt>inject_range</tt></b>" event.  This eventwill be used when you want to inject organisms into more than one cell in thepopulation, but not all of them.  Where the normal <tt>inject</tt> eventtook a single cell_id value, this new event should take two of them -- astarting cell and a stopping cell.  You should then use a <tt>for</tt> loop(similar to the one in "<tt>inject_all</tt>") to inject the genome intoall cells in range.  Extra credit if you test the order of the two numbersand swap them if the start_cell is larger than the end_cell.<p>The second event you are going to implement is "<b><tt>kill_cell</tt></b>"This is again somewhat similar to <tt>inject</tt> in code structure, butyou're goal is to kill the organism (if one exists) in the specified cell.For help with how to do this, take a look at the <tt>apocalypse</tt> event,which causes a larger scale extinction.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -