code_genome.html
来自「有趣的模拟进化的程序 由国外一生物学家开发 十分有趣」· HTML 代码 · 共 375 行 · 第 1/2 页
HTML
375 行
<html><title>The building blocks of the virtual CPU</title><body bgcolor="#FFFFFF" text="#000000" link="#0000AA" alink="#0000FF" vlink="#000044"><h2 align=center>The building blocks of the virtual CPU</h2><p>This document discusses the implementation of the cInstruction, cGenome,and cInstLib (instruction library) classes.<p><h3>The cInstruction class</h3><p>This class is used to represent a single instruction within a genome. Theprivate portion of this class consists of a single number that uniquelyidentifies the type of instruction, and the public sectionhas a number of helper methods that allow us to work with that number.<pre> class <font color="#880000">cInstruction</font> { private: <font color="#880000">UCHAR</font> <font color="#000088">operand</font>; public: <font color="#886600">// Accessors...</font> <font color="#880000">int</font> <font color="#008800">GetOp()</font> const { return (<font color="#880000">int</font>) <font color="#000088">operand</font>; } void <font color="#008800">SetOp</font>(<font color="#880000">int</font> <font color="#000088">in_op</font>) { <font color="#008800">assert</font>(<font color="#000088">in_op</font> < 256); <font color="#000088">operand</font> = <font color="#000088">in_op</font>; } <font color="#886600">// Operators...</font> void <font color="#008800">operator=</font>(const <font color="#880000">cInstruction</font> & <font color="#000088">inst</font>) { <font color="#000088">operand</font> = <font color="#000088">inst</font>.<font color="#000088">operand</font>; } <font color="#880000">bool</font> <font color="#008800">operator==</font>(const <font color="#880000">cInstruction</font> & <font color="#000088">inst</font>) const { return (<font color="#000088">operand</font> == <font color="#000088">inst</font>.<font color="#000088">operand</font>); } <font color="#880000">bool</font> <font color="#008800">operator!=</font>(const <font color="#880000">cInstruction</font> & <font color="#000088">inst</font>) const { return !(<font color="#008800">operator==</font>(<font color="#000088">inst</font>)); } <font color="#886600">// Constructors and Destructor...</font> <font color="#008800">cInstruction()</font> { <font color="#000088">operand</font> = 0; } <font color="#008800">cInstruction</font>(const <font color="#880000">cInstruction</font> & <font color="#000088">_inst</font>) { *<font color="#000088">this</font> = <font color="#000088">_inst</font>; } explicit <font color="#008800">cInstruction</font>(<font color="#880000">int</font> <font color="#000088">in_op</font>) { <font color="#008800">SetOp</font>(<font color="#000088">in_op</font>); } <font color="#008800">~cInstruction</font>() { ; } <font color="#886600">// Some extra methods to convert too and from alpha-numeric symbols...</font> <font color="#880000">char</font> <font color="#008800">GetSymbol()</font> const; void <font color="#008800">SetSymbol</font>(<font color="#880000">char</font> <font color="#000088">symbol</font>); };</pre><p>As I stated above, the only private datum is a numerical value thatidentifies this instruction. The name "<font color="#000088">operand</font>"is the term that is used for a command name in an assembly language. Mostnormal assembly languages have both an operand and arguments associated witheach full command. In avida, the commands have no arguments, and hence theyjust consist of a single operand. The data type used for this is <font color="#880000">UCHAR</font>, which is short for"<font color="#880000">unsigned char</font>". A char is an 8 bit number,so when one is unsigned, it represents a number from 0 to 255. As avidais currently implemented, we are limited to 256 distinct instructions. Tothe outside world, we treat the instruction operand like an integer, so itwould be easy to modify this class were we ever to need more than 256 instructions in theset. The only reason to limit it to 8 bits internally (rather than the 32of an int) is to save memory when we have a very large number of instructionsthroughout a population. Instructions already make up over half of allthe memory resources used by avida.<p>The public methods begin with the <font color="#008800">GetOp()</font> and<font color="#008800">SetOp()</font> methods, which are standard accessors.Next, we have a collection of methods that begin with the word "operator".These are used to define how the corresponding symbols should be treatedwhen applied to objects of this class. For example, the method <font color="#008800">operator==()</font> is called when we try to compareon object of type cInstruction to another. We have full control over thedefinition of this method, just like any other.<p>The next batch of methods we come to are the constructors and destructor.Notice that there are three different constructors, so there are multipleways an instruction object can be created.<p>Finally, we have a pair of methods that convert instructions to and fromalphanumeric characters (symbols). These methods are used to print instructions out in a maximally compressed format, and to load them backin. The order of symbols used are the letters 'a' through 'z' in lowercase,followed by an uppercase 'A' through 'Z' and finally the numbers '0' through'9'. If there are morethan 62 possible instructions, all the rest are assigned a '?' when printedout, and cannot be read back in properly from this format.<h3>The cGenome class</h3><p>A genome is a sequence of instructions. The followingclass maintains this sequence as an array, and provides a collection ofmethods to manipulate the total construct.<pre> class <font color="#880000">cGenome</font> { protected: <font color="#880000">tArray</font><<font color="#880000">cInstruction</font>> <font color="#000088">genome</font>; <font color="#880000">int</font> <font color="#000088">active_size</font>; public: explicit <font color="#008800">cGenome</font>(<font color="#880000">int</font> <font color="#000088">_size</font>); <font color="#008800">cGenome</font>(const <font color="#880000">cGenome</font> & <font color="#000088">in_genome</font>); <font color="#008800">cGenome</font>(const <font color="#880000">cString</font> & <font color="#000088">in_string</font>); virtual <font color="#008800">~cGenome</font>(); virtual void <font color="#008800">operator=</font>(const <font color="#880000">cGenome</font> & <font color="#000088">other_genome</font>); virtual <font color="#880000">bool</font> <font color="#008800">operator==</font>(const <font color="#880000">cGenome</font> & <font color="#000088">other_genome</font>) const; <font color="#880000">cInstruction</font> & <font color="#008800">operator[]</font>(<font color="#880000">int</font> <font color="#000088">index</font>) { <font color="#008800">assert</font>(<font color="#000088">index</font> >= 0 && <font color="#000088">index</font> < <font color="#000088">active_size</font>); return <font color="#000088">genome</font>[<font color="#000088">index</font>]; } const <font color="#880000">cInstruction</font> & <font color="#008800">operator[]</font>(<font color="#880000">int</font> <font color="#000088">index</font>) const { <font color="#008800">assert</font>(<font color="#000088">index</font> >= 0 && <font color="#000088">index</font> < <font color="#000088">active_size</font>); return <font color="#000088">genome</font>[<font color="#000088">index</font>]; } virtual void <font color="#008800">Copy</font>(<font color="#880000">int</font> <font color="#000088">to</font>, <font color="#880000">int</font> <font color="#000088">from</font>); <font color="#880000">int</font> <font color="#008800">GetSize()</font> const { return <font color="#000088">active_size</font>; } <font color="#880000">cString</font> <font color="#008800">AsString()</font> const; };</pre><p>The protected variable <font color="#000088">genome</font> is an array containing an object of type cInstruction at each position. The secondvariable, <font color="#000088">active_size</font> denotes the number ofinstructions in this array that are currently being used. The fact thatthese variables are "protected" instead of "private" means that anyclass derived from cGenome will also have access to the variables. Inparticular, we will discuss the class <font color="#880000">cCPUMemory</font>in a future lecture, which extends cGenome, adding methods to alter thearray length and new variables to keep track of information about eachinstruction.<p>Three constructors allow for a new cGenome object to be specified by either agenome length, a previously created genome, or else a string -- a sequenceof symbols representing each instruction in order.<p>The operators created for manipulating genomes include both assignment (setting one genome equal to another) and comparison (testing to see iftwo genomes are identical.) Additionally, there are two<font color="#008800">operator[]</font> methods. These means that if youhave an object of type cGenome, you can index into it to retrieve a singleinstruction. Thus, if the object was called "initial_genome", the statement "initial_genome[15]" would return the instruction at positionfifteen in the genome. This occurs by calling one of these methods withthe appropriate integer. The difference between these two operator methodsis that one of them is for mutable genomes (i.e. those that can be modified)-- a referenceto the instruction in question is returned allowing it to be altered.The other index operator (operator[] method) is for const genomes, whichcan never be changed so only the value of the instruction is returned.<p>The <font color="#008800">Copy()</font> method is a shortcut to copy memoryfrom one position in the genome to another. This method will later be<b>overloaded</b> (that is, replaced with a newer version) by cCPUMemory suchthat the proper flags will be copied, withthe instruction and others will be set to indicate the copied instructionfor future tests. <font color="#008800">GetSize</font> returns the lengthof the genome, and <font color="#008800">AsString</font> returns a stringwho has symbols in each position that correspond to the the instruction inthe same position in the genome.<h3>The cInstLib class</h3><p>This class keeps track of all of the possible, legal instructions, and relates each of them to the hardware method that should be triggered whenthat instruction is executed.<p>The beginning of the <tt>inst_lib.hh</tt> file has a couple of commandsrequired for the class definition. They are:<pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?