code_environment.html

来自「有趣的模拟进化的程序 由国外一生物学家开发 十分有趣」· HTML 代码 · 共 300 行 · 第 1/2 页

HTML
300
字号
<h3>Building a Reaction</h3><p>The reaction class keeps track of all of the information associated witha single possible environmental reaction.  The class appears as follows:<pre>  class <font color="#880000">cReaction</font> {  private:    <font color="#880000">cString</font> <font color="#000088">name</font>;    <font color="#880000">int</font> <font color="#000088">id</font>;    <font color="#880000">cTaskEntry</font> * <font color="#000088">task</font>;    <font color="#880000">tList</font><<font color="#880000">cReactionRequisite</font>> <font color="#000088">requisite_list</font>;    <font color="#880000">tList</font><<font color="#880000">cReactionProcess</font>> <font color="#000088">process_list</font>;  public:    <font color="#008800">cReaction</font>(const <font color="#880000">cString</font> & <font color="#000088">_name</font>, <font color="#880000">int</font> <font color="#000088">_id</font>);    ~<font color="#008800">cReaction</font>();    const <font color="#880000">cString</font> & <font color="#008800">GetName</font>() const { return <font color="#000088">name</font>; }    <font color="#880000">int</font> <font color="#008800">GetID</font>() const { return <font color="#000088">id</font>; }    <font color="#880000">cTaskEntry</font> * <font color="#008800">GetTask</font>() { return <font color="#000088">task</font>; }    const <font color="#880000">tList</font><<font color="#880000">cReactionRequisite</font>> & <font color="#008800">GetRequisites</font>()      { return <font color="#000088">requisite_list</font>; }    const <font color="#880000">tList</font><<font color="#880000">cReactionProcess</font>> & <font color="#008800">GetProcesses</font>() { return <font color="#000088">process_list</font>; }    void <font color="#008800">SetTask</font>(<font color="#880000">cTaskEntry</font> * <font color="#000088">_task</font>) { <font color="#000088">task</font> = <font color="#000088">_task</font>; }    <font color="#880000">cReactionProcess</font> * <font color="#008800">AddProcess</font>();    <font color="#880000">cReactionRequisite</font> * <font color="#008800">AddRequisite</font>();  };</pre><p>Each reaction must have a unique name and a unique numerical ID associatedwith them.  In addition to those data, a reaction object also has a task thatacts as its trigger, a list of other requisites that must be met for thetrigger to work, and a list of processes that will occur if the reaction goesoff.  The cReaction object acts a a single place to store all of thisinformation.<h3>Resources</h3><p>Resources are a little more complicated than task entries to manageand understand.  An objectof type <font color="#880000">cResource</font> contains 18 pieces ofdata, and the associated accessors.  Like all of the other individual units we have discussed, resources have a unique <font color="#000088">name</font> and numerical<font color="#000088">id</font>.  For all resource we store the quantitiesassociated with their <font color="#000088">inflow</font>,<font color="#000088">outflow</font>, and<font color="#000088">initial</font> count (each stored as a<font color="#880000">double</font>) as well as the <font color="#000088">geometry</font> of that resource.<p>For spatial resources we need to be able to describe how a resourceexists in space so we store data for:<ul>  <li>    <font color="#000088">inflowX1</font>, <font    color="#000088">inflowX2</font>, <font    color="#000088">inflowY1</font>, and  <font    color="#000088">inflowY2</font> to describe a rectangle where    resources flow in  </li>  <li>    <font color="#000088">outflowX1</font>, <font    color="#000088">outflowX2</font>, <font    color="#000088">outflowY1</font>, and  <font    color="#000088">outfowY2</font> for a rectangle where resources flow    out of  </li>  <li>    <font color="#000088">xdiffuse</font> and <font    color="#000088">ydiffuse</font> describe how fast resources will    flow from cells of higher amounts of that resource to cells with    lower amounts of that resource  </li>  <li>    <font color="#000088">xgravity</font> and <font    color="#000088">ygravity</font> describe the preferential flow of    resource in a given direction  </li></ul>                                   <p>This class describes the dynamics of a resource, not its current count (since, for example, we might want local resources where each cell would have its own count).  However, every time aresource is needed, any changes in its quantity from the last time it wasused can be calculated using these numbers.<h3>Tying it all together: The Environment</h3><p>The cEnvironment class is used to maintain the details of how the environmentswork using the classes described above and a few others.  Below is anabbreviated version of this class:<pre>class <font color="#880000">cEnvironment</font> {private:  <font color="#886600">// Keep libraries of resources, reactions, and tasks.</font>  <font color="#880000">cResourceLib</font> <font color="#000088">resource_lib</font>;  <font color="#880000">cReactionLib</font> <font color="#000088">reaction_lib</font>;  <font color="#880000">cTaskLib</font> <font color="#000088">task_lib</font>;  <font color="#880000">cInstLib</font> <font color="#000088">inst_lib</font>;  <font color="#880000">cMutationRates</font> <font color="#000088">mut_rates</font>;public:  <font color="#008800">cEnvironment</font>();  <font color="#008800">cEnvironment</font>(const <font color="#880000">cString</font> & <font color="#000088">filename</font>);  ~<font color="#008800">cEnvironment</font>() { ; }  <font color="#880000">bool</font> <font color="#008800">Load</font>(const <font color="#880000">cString</font> & <font color="#000088">filename</font>);  <font color="#886600">// Interaction with the organisms</font>  <font color="#880000">bool</font> <font color="#008800">TestOutput</font>(  <font color="#880000">cReactionResult</font> & <font color="#000088">result</font>,                    const <font color="#880000">tBuffer</font><<font color="#880000">int</font>> & <font color="#000088">inputs</font>,                    const <font color="#880000">tBuffer</font><<font color="#880000">int</font>> & <font color="#000088">outputs</font>,                    const <font color="#880000">tArray</font><<font color="#880000">int</font>> & <font color="#000088">task_count</font>,                    const <font color="#880000">tArray</font><<font color="#880000">int</font>> & <font color="#000088">reaction_count</font>,                    const <font color="#880000">tArray</font><<font color="#880000">double</font>> & <font color="#000088">resource_count</font> ) const;};</pre><p>The private data members include all of the libraries needed to specifythe environment, plus its mutation rates.  The<font color="#008800">Load</font>() method takes a filename (environment.cfgby default) and will fill out all of the libraries in this environment.  Themost important feature of this class is the<font color="#008800">TestOutput</font>() method, which takes in all sortsof information about the current state of the organism that has just donean output and fills out an object of type<font color="#880000">cReactionResult</font> with information about whathappened.  It also directly returns a bool that will indicate if there havebeen any changes at all.  The specific information it uses to determinethe results are the <font color="#000088">inputs</font> the organism hastaken in and the <font color="#000088">outputs</font> it has produced --both needed to determine what tasks have been done, and therefore whatreactions may have been triggered.  That organism's previous<font color="#000088">task_count</font> and<font color="#000088">resource_count</font> are also needed to determineif the reactions requisites have been met.  And finally the<font color="#000088">resource_count</font> available to the organisms isneeded to determine how much of each resource can be used in the reactions.

⌨️ 快捷键说明

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