📄 individual.htm
字号:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Genetic Programming FAQ</title>
</head>
<body>
<div id="nsbanner">
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td>
<div id="bannerrow1">
<table class="bannerparthead" cellSpacing="0">
<tr id="hdr">
<td class="runninghead" style="font-size: 70%">
<font face="Arial"><a class="show" id="design_toggle" title="Show or hide design notes" href="../../index.html"><img src="../../Images/Icons/G.png" width="14" height="14" border="0" align="middle"/></a> Genetic Programming Engine FAQ</font></td>
</tr>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" style="font-size: 85%; left: 5px; position: relative">
<font face="Arial">How to Write the Individual Class</font></h1>
</div>
</td>
<td vAlign="top" align="right">
<font face="Arial">
<img src="../../Images/Icons/GP_blue.png" width="191" height="39"></font></td>
</tr>
</table>
</div>
<div id="nstext">
<h4 class="dtH4"><font face="Arial"><span style="font-weight: 400">The </span></font>
<span style="font-family:Arial">GPEngine</span><span lang="EN-GB" style="font-family:Arial"> </span><font face="Arial"><span style="font-weight: 400">derives potential solutions to problems from the </span>IIndividual<span style="font-weight: 400"> <i>Base Class</i>. The
IIndividual class represents an individual as used by the </span></font>
<span style="font-family:Arial; font-weight:400">GPEngine</span><font face="Arial"><span style="font-weight: 400">. In the
Artificial Ant problem, an Ant is an example of an individual. The
IIndividual class defines all of the actions an IIndividual can take to
interact with its </span>IEnvironment<span style="font-weight: 400">. Each
IIndividual has an IEnvironment to interact with. An IIndividual has methods
common to all problems as well as problem-specific methods. Common
methods include getting and setting the IEnvironment ; methods to initialize
the state of the IIndividual and, if necessary, reset the state of the
IIndividual after testing; and a method to determine if testing is complete.
Problem-specific methods are unique to each problem. In the case of the Artificial Ant, problem-specific
methods include moving forward, turning left and right, and determining if
food is in front of the Ant.</span></font></h4>
<h4 class="dtH4"><font face="Arial"><span style="font-weight: 400">The first
methods to be defined are the IEnvironment get and set methods. The
example below is for the Artificial Ant.</span></font></h4>
<div align="center">
<table border="0" width="90%" id="table1">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0">
<font face="Courier" size="2">public IEnvironment Environment {<br>
get {<br>
return m_Environment;<br>
}<br>
set {<br>
if( value == null ) {<br>
throw new
ArgumentNullException( "value", "The value passed to</font></p>
<p style="margin-top: 0; margin-bottom: 0">
<font face="Courier" size="2">
AntIndividual.Environment_set property was null." );<br>
}<br>
m_Environment = value as
AntEnvironment;<br>
if( m_Environment == null ) {<br>
throw new
ArgumentException( "The value passed to
AndIndividual.Environment_set</font></p>
<p style="margin-top: 0; margin-bottom: 0">
<font face="Courier" size="2">
property was not of type AntEnvironment.", "value" );<br>
}<br>
}<br>
}<br>
private AntEnvironment m_Environment;<br>
</font></td>
</tr>
</table>
</div>
<h4><span style="font-family:Arial;font-weight:normal">The Environment
method sets the IEnvironment in which the IIndividual will be tested. The
IEnvironment that is passed to this method should be verified that it is not
null and that it is of a compatible type for this IIndividual. If the
IEnvironment is of the proper type, the
</span><span style="font-family:Arial;font-weight:normal">IEnvironment </span><span style="font-family:Arial;font-weight:normal">of this </span><span style="font-family:Arial;font-weight:normal">
IIndividual </span><span style="font-family:Arial;font-weight:normal">will be set. This method also returns the
</span><span style="font-family:Arial;font-weight:normal">IEnvironment </span><span style="font-family:Arial;font-weight:normal">to callers of this method.</span></h4>
<h4><span style="font-family:Arial;font-weight:normal">Next, any
problem-specific variables should be defined. </span>
<span style="font-family: Arial; font-weight: normal">These variables
include any IIndividual states that need to be tracked. In the case of the
Artificial Ant, this includes variables to count how many steps were taken
and the current direction the Ant is facing.</span></h4>
<div align="center">
<table border="0" width="90%" id="table2">
<tr>
<td>
<h4 style="margin-top: 0; margin-bottom: 0">
<span style="font-family: Courier New; font-weight: normal">
<font size="2">private int m_StepCount;</font></span></h4>
<h4 style="margin-top: 0; margin-bottom: 0">
<span style="font-family: Courier New; font-weight: normal">
<font size="2">private Direction m_Facing;</font></span></h4>
</td>
</tr>
</table>
</div>
<h4><span style="font-family: Arial; font-weight: normal">m_StepCount is
incremented for each step the Ant takes or if it turns left or right. This
is one condition used to test if the IIndividual is done being tested. When
the Ant has taken a certain number of steps (as defined by the IEnvironment),
testing stops. This prevents the program from entering an endless loop. m_Facing is used to track which way the Ant is facing. This is used to
determine which directions the Ant can move.</span></h4>
<h4 style="margin:0in;margin-bottom:.0001pt">
<span style="font-family: Arial; font-weight: normal">The next method to
define is the TestSetup in which any initial states of the individual are
set. For the Artificial Ant, the individual Ant starts out with a step
count of 0 (it hasn抰 yet moved) and facing to the right.</span></h4>
<h4 style="margin:0in;margin-bottom:.0001pt">
<span style="font-family: Arial; font-weight: normal"> </span></h4>
<div align="center">
<table border="0" width="90%" id="table3">
<tr>
<td>
<h4 style="margin:0in;margin-bottom:.0001pt">
<span style="font-family: Courier New; font-weight: normal">
<font size="2">public void TestSetup() {</font></span></h4>
<h4 style="margin:0in;margin-bottom:.0001pt">
<font face="Courier" size="2"> </font>
<span style="font-family: Courier New; font-weight: normal">
<font size="2">//reset state</font></span></h4>
<h4 style="margin:0in;margin-bottom:.0001pt">
<font face="Courier" size="2"> </font>
<span style="font-family: Courier New; font-weight: normal">
<font size="2">m_StepCount = 0;</font></span></h4>
<h4 style="margin:0in;margin-bottom:.0001pt">
<font face="Courier" size="2"> </font>
<span style="font-family: Courier New; font-weight: normal">
<font size="2">m_Facing = Direction.Right;</font></span></h4>
<h4 style="margin:0in;margin-bottom:.0001pt">
<span style="font-family: Courier New; font-weight: normal">
<font size="2">}</font></span></h4>
</td>
</tr>
</table>
</div>
<h4 style="margin:0in;margin-bottom:.0001pt">
<span style="font-family: Arial; font-weight: normal"> </span></h4>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -