code_environment.html

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

HTML
300
字号
<html><title>The Environment Code</title><body bgcolor="#FFFFFF" text="#000000" link="#0000AA" alink="#0000FF" vlink="#000044"><h2 align=center>The Environment Code</h2>The environment source code consists of several main components: resources,reactions, and task triggers, plus the libraries that maintain each of these.<h3>Task Entries</h3>A task library is composed of a collection of entries, each of which fullydescribes a single task that can be used to trigger reactions.<pre>  typedef <font color="#880000">double</font> (<font color="#880000">cTaskLib</font>::*<font color="#000088">tTaskTest</font>)() const;  class <font color="#880000">cTaskEntry</font> {  private:    <font color="#880000">cString</font> <font color="#000088">name</font>;  <font color="#886600">// Short keyword for task</font>    <font color="#880000">cString</font> <font color="#000088">desc</font>;  <font color="#886600">// For more human-understandable output...</font>    <font color="#880000">int</font> <font color="#000088">id</font>;    <font color="#880000">tTaskTest</font> <font color="#000088">test_fun</font>;  public:    <font color="#008800">cTaskEntry</font>(const <font color="#880000">cString</font> & <font color="#000088">_name</font>, const <font color="#880000">cString</font> & <font color="#000088">_desc</font>, <font color="#880000">int</font> <font color="#000088">_id</font>,               <font color="#880000">tTaskTest</font> <font color="#000088">_test_fun</font>);    ~<font color="#008800">cTaskEntry</font>();      const <font color="#880000">cString</font> & <font color="#008800">GetName</font>()    const { return <font color="#000088">name</font>; }    const <font color="#880000">cString</font> & <font color="#008800">GetDesc</font>()    const { return <font color="#000088">desc</font>; }    const <font color="#880000">int</font>       <font color="#008800">GetID</font>()      const { return <font color="#000088">id</font>; }    const <font color="#880000">tTaskTest</font> <font color="#008800">GetTestFun</font>() const { return <font color="#000088">test_fun</font>; }  };</pre><p>Task entries are very straight-forward.  They consist of a name, a description,a unique ID number, and a method from the task library (cTaskLib) that theyare associated with.  This method looks at the inputs the organism has takenin, the values it has output, and returns a number between 0.0 and 1.0representing how well the task was performed.  Currently, all task tests willreturn an exact zero or one, but fractions are possible if thereis a quality component associated with the task.<h3>Task Libraries</h3><p>Here is the task library that manages all of the individual entries:<pre>  class <font color="#880000">cTaskLib</font> {  private:    <font color="#880000">tArray</font><<font color="#880000">cTaskEntry</font> *> <font color="#000088">task_array</font>;      <font color="#886600">// Active task information...</font>    <font color="#880000">tBuffer</font><<font color="#880000">int</font>> <font color="#000088">input_buffer</font>;    <font color="#880000">tBuffer</font><<font color="#880000">int</font>> <font color="#000088">output_buffer</font>;    <font color="#880000">int</font> <font color="#000088">logic_id</font>;    void <font color="#008800">SetupLogicTests</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">double</font> <font color="#008800">Task_Echo</font>() const;    <font color="#880000">double</font> <font color="#008800">Task_Add</font>()  const;    <font color="#880000">double</font> <font color="#008800">Task_Sub</font>()  const;      <font color="#880000">double</font> <font color="#008800">Task_Not</font>()    const;    <font color="#880000">double</font> <font color="#008800">Task_Nand</font>()   const;    <font color="#880000">double</font> <font color="#008800">Task_And</font>()    const;    <font color="#886600">// ... And a whole bunch more ...</font>  public:    <font color="#008800">cTaskLib</font>();    ~<font color="#008800">cTaskLib</font>();    <font color="#880000">int</font> <font color="#008800">GetSize</font>() const { return <font color="#000088">task_array</font>.<font color="#008800">GetSize</font>(); }    <font color="#880000">cTaskEntry</font> * <font color="#008800">AddTask</font>(const <font color="#880000">cString</font> & <font color="#000088">name</font>);    const <font color="#880000">cTaskEntry</font> & <font color="#008800">GetTask</font>(<font color="#880000">int</font> <font color="#000088">id</font>) const;      void <font color="#008800">SetupTests</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">double</font> <font color="#008800">TestOutput</font>(const <font color="#880000">cTaskEntry</font> & <font color="#000088">task</font>) const;  };</pre><p>The task library contains an array of task entries that define all of therewarded (or otherwise acted upon) tasks in an environment.  This array is theonly "long term" variable that is stored here.  Whenever an organism outputsa new number that needs to be tested, the<font color="#008800">SetupTasks</font>() method is called with theappropriate inputs and output for that organism, which are temporarily savedin the data variables <font color="#000088">input_buffer</font> and<font color="#000088">output_buffer</font>.  Additionally, if there areany logic-based tasks rewarded, SetupTasks() will, in turn, call the privatemethod <font color="#008800">SetupLogicTasks</font>() to calculate alogic_id.  This is a number generated by looking at all bitwise inputs andtheir associated bit output.  It can be later used so that work doesn't needto be repeated for each and every logic task tested.<p>The <font color="#008800">TestOutput</font>() method can only be run afterthe setup has finished.  It will test the specific task passed in and returnthe 0.0 - 1.0 quality measure of how well that task was done with the mostrecent output.<p>Below is a sample task-tester implementation:<pre>  <font color="#880000">double</font> <font color="#880000">cTaskLib</font>::<font color="#008800">Task_Add</font>() const  {    const <font color="#880000">int</font> <font color="#000088">test_output</font> = <font color="#000088">output_buffer</font>[0];    for (<font color="#880000">int</font> <font color="#000088">i</font> = 0; <font color="#000088">i</font> < <font color="#000088">input_buffer</font>.<font color="#008800">GetNumStored</font>(); <font color="#000088">i</font>++) {      for (<font color="#880000">int</font> <font color="#000088">j</font> = 0; <font color="#000088">j</font> < <font color="#000088">i</font>; <font color="#000088">j</font>++) {        if (<font color="#000088">test_output</font> == <font color="#000088">input_buffer</font>[<font color="#000088">i</font>] + <font color="#000088">input_buffer</font>[<font color="#000088">j</font>]) return 1.0;      }    }    return 0.0;  }</pre><p>This case tests to see if the organism has performed an addition operation.It compares all pairs of inputs summed together against the most recentoutput of the organism.  If there is a match a full reward (1.0) is given.If no match is found, no reward is given (0.0).  The logic_id has 256possible values, each of which can only be associated with a single logictask.  These tests look more like:<pre>  <font color="#880000">double</font> <font color="#880000">cTaskLib</font>::<font color="#008800">Task_AndNot</font>() const  {    if (<font color="#000088">logic_id</font> == 10 || <font color="#000088">logic_id</font> == 12 || <font color="#000088">logic_id</font> == 34 ||        <font color="#000088">logic_id</font> == 48 || <font color="#000088">logic_id</font> == 68 || <font color="#000088">logic_id</font> == 80) return 1.0;      return 0.0;  }</pre><p>If the logic ID is on the list, the task has been done, otherwise it hasn't.In each case, the outside world needs to request a test of which tasks havebeen performed, and the library just replied with a numerical answer.

⌨️ 快捷键说明

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