📄 history.htm
字号:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<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 IGraphicHistory 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">
<div align="center">
<p style="margin-top: 0; margin-bottom: 0"> </p>
<p style="margin-top: 0; margin-bottom: 0" align="left">
<font face="Arial">The IGraphicHistory <i>Base Class</i> extends the
IHistory <i>Base Class</i>. The IHistory class represents an
<b>IIndividual</b> in an <b>IEnvironment</b>. The IIndividual's actions are stored
in the IGraphicHistory and fitness is then calculated from this. The
IGraphicHistory class creates a graphical representation of the
IIndividual's performance in an IEnvrionment. Each history class
will be unique to the individual problem. That is, each history
class will have different sets of methods depending on the functions of
the individual. For example, the Ant measures fitness as the
amount of food it gathered and has four moves that it can make,
specifically up, down, left, and right on the grid.</font></p>
<p align="left"><font face="Arial">The first method is the
IGraphicHistory get and set methods. The parameters vary on the needs of
the individual problem. In the Artificial Ant's case, the
IEnvironment and the Ant grid are needed for the history. First, AntHistory ensures that the
IEnvironment name and the Ant grid are not
null. If they are valid, an AntHistory is created. </font></p>
<table border="0" width="90%" id="table1">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0">
<font face="Courier New" size="2">public AntHistory( string
environmentName, Grid grid ) {<br>
//check for null args<br>
if( environmentName == null ) {<br>
throw new ArgumentNullException( "environmentName",
"The environment name passed</font><p style="margin-top: 0; margin-bottom: 0">
<font face="Courier New" size="2">
to AntHistory.constructor was null." );<br>
}<br>
if( grid == null ) {<br>
throw new ArgumentNullException( "grid", "The grid passed to
AntHistory.constructor</font><p style="margin-top: 0; margin-bottom: 0">
<font face="Courier New" size="2"> was null."
);<br>
}<br>
<br>
m_EnvironmentName = environmentName;<br>
m_Grid = grid;<br>
}<br>
</font></td>
</tr>
</table>
</div>
<h4 class="dtH4"><font face="Arial"><span style="font-weight: 400">The raw
fitness method returns the fitness of the specified individual in the
specified environment. Fitness measures vary for each problem and, for the Artificial Ant, is the amount of food collected.</span></font></h4>
<div align="center">
<table border="0" width="90%" id="table2">
<tr>
<td><font face="Courier New" size="2">public decimal RawFitness
{<br>
get {<br>
return m_FoodFound;<br>
}<br>
}</font></td>
</tr>
</table>
</div>
<h4 class="dtH4"><font face="Arial"><span style="font-weight: 400">The
environment method retrieves the name of the IEnvironment the IIndividual is
being tested in. </span></font></h4>
<div align="center">
<table border="0" width="90%" id="table3">
<tr>
<td><font face="Courier New" size="2">public string
EnvironmentName {<br>
get {<br>
return m_EnvironmentName;<br>
}<br>
}<br>
private string m_EnvironmentName;</font></td>
</tr>
</table>
</div>
<p class="dtH4"><font face="Arial">Next, a property should be defined to get
and set the fitness measure of the <span style="font-weight: 400">
IIndividual</span>. In the Ant's case it is
the amount of food collected.</font></p>
<div align="center">
<table border="0" width="90%" id="table4">
<tr>
<td><font face="Courier New" size="2">public int FoodFound {<br>
get {<br>
return m_FoodFound;<br>
}<br>
set {<br>
m_FoodFound = value;<br>
}<br>
}<br>
private int m_FoodFound;</font></td>
</tr>
</table>
<p align="left"><font face="Arial">The history class should also
maintain a list of actions performed by the <span style="font-weight: 400">
IIndividual </span>in the
IEnvironment. For the Artificial Ant, this would be a collection of
the moves the Ant made in the IEnvironment.</font></p>
<div align="center">
<table border="0" width="90%" id="table5">
<tr>
<td><font face="Courier New" size="2">public ArrayList Moves
{<br>
get {<br>
return m_Moves;<br>
}<br>
}<br>
private ArrayList m_Moves = new ArrayList();<br>
private Direction[] m_FinishedMoves;</font></td>
</tr>
</table>
<p align="left"><font face="Arial">Any problem-specific variables
should also be defined. This would include the environment
grid and a bitmap image to draw on for the Artificial Ant.</font></p>
<div align="center">
<table border="0" width="90%" id="table6">
<tr>
<td><font face="Courier New" size="2">private int
m_MovesIndex = -1;<br>
private Grid m_Grid;<br>
private Bitmap m_bmp;<br>
private SmartPoint m_Position;<br>
private int m_CellDim;</font></td>
</tr>
</table>
<p align="left"><font face="Arial">The DrawFrame method handles
the graphical portion of the history. This method draws a
graphical representation of the <span style="font-weight: 400">
IIndividual's </span>actions on the
Graphics object one frame at a time.</font></p>
<div align="center">
<table border="0" width="90%" id="table7">
<tr>
<td>
<p style="margin-top: 0; margin-bottom: 0">public
bool DrawFrame( Graphics g ) {<br>
//if just starting, set up graphics stuff<br>
if( m_MovesIndex == -1 ) {<br>
//initialize state<br>
m_Position = new SmartPoint(
m_Grid.Width, m_Grid.Height,
m_Grid.InitialAntPosition.X,
</p>
<p style="margin-top: 0; margin-bottom: 0">
m_Grid.InitialAntPosition.Y );<br>
<br>
//create square bitmap<br>
int maxGridSize = (int) Math.Max(
m_Grid.Width, m_Grid.Height );<br>
int dim = (int) Math.Min(
g.ClipBounds.Width, g.ClipBounds.Height );<br>
m_CellDim = (dim - 2)/maxGridSize;<br>
m_bmp = new Bitmap( dim, dim );<br>
<br>
//draw grid<br>
using( Graphics gBmp =
Graphics.FromImage( m_bmp ) ) {<br>
gBmp.FillRectangle( Brushes.White, 0, 0, dim, dim );<br>
gBmp.DrawRectangle( Pens.Black, 0, 0, dim - 1, dim -
1 );<br>
for( int x =
0; x < m_Grid.Width; ++x ) {<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -