﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-twzheng's cppblog-文章分类-算法和数据结构</title><link>http://www.cppblog.com/twzheng/category/3969.html</link><description>『站在风口浪尖紧握住鼠标旋转！』  人在台北心在汉</description><language>zh-cn</language><lastBuildDate>Mon, 15 Mar 2010 04:03:33 GMT</lastBuildDate><pubDate>Mon, 15 Mar 2010 04:03:33 GMT</pubDate><ttl>60</ttl><item><title>求windows xp 繁体版</title><link>http://www.cppblog.com/twzheng/articles/109710.html</link><dc:creator>谭文政</dc:creator><author>谭文政</author><pubDate>Sun, 14 Mar 2010 16:05:00 GMT</pubDate><guid>http://www.cppblog.com/twzheng/articles/109710.html</guid><wfw:comment>http://www.cppblog.com/twzheng/comments/109710.html</wfw:comment><comments>http://www.cppblog.com/twzheng/articles/109710.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/twzheng/comments/commentRss/109710.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/twzheng/services/trackbacks/109710.html</trackback:ping><description><![CDATA[求windows xp 繁体版，最好是台湾版的，要能一步步的手动安装(想装双系统)<br><br>找了好几天了，很多资源都下不动<br><br>今天好不容易下了一个，但是香港版的，且安装到选择盘符时，键盘所有键都不能使用，所以还是无法安装<br><br>谢谢各位大哥大姐帮帮忙，小弟感激不尽！<img src ="http://www.cppblog.com/twzheng/aggbug/109710.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/twzheng/" target="_blank">谭文政</a> 2010-03-15 00:05 <a href="http://www.cppblog.com/twzheng/articles/109710.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>“百元百鸡”问题</title><link>http://www.cppblog.com/twzheng/articles/22054.html</link><dc:creator>谭文政</dc:creator><author>谭文政</author><pubDate>Mon, 16 Apr 2007 13:46:00 GMT</pubDate><guid>http://www.cppblog.com/twzheng/articles/22054.html</guid><wfw:comment>http://www.cppblog.com/twzheng/comments/22054.html</wfw:comment><comments>http://www.cppblog.com/twzheng/articles/22054.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/twzheng/comments/commentRss/22054.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/twzheng/services/trackbacks/22054.html</trackback:ping><description><![CDATA[<p>&#8220;百元百鸡&#8221;问题：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;一百元钱买一百只鸡，公鸡5元/只，母鸡3元/只，小鸡1元钱3只。问：公鸡、母鸡和小鸡各买多少只能满足百元百鸡要求？<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本是一道心算题目，这里用程序来实现就显得特别的简单。</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;bybj.cpp&nbsp;:&nbsp;定义控制台应用程序的入口点。<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #008000">//<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">stdafx.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_tmain(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;argc,&nbsp;_TCHAR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;argv[])<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img id=Codehighlighter1_111_293_Open_Image style="DISPLAY: inline" onclick="this.style.display='none'; Codehighlighter1_111_293_Open_Text.style.display='none'; Codehighlighter1_111_293_Closed_Image.style.display='inline'; Codehighlighter1_111_293_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_111_293_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_111_293_Closed_Text.style.display='none'; Codehighlighter1_111_293_Open_Image.style.display='inline'; Codehighlighter1_111_293_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_111_293_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_111_293_Open_Text style="DISPLAY: inline"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x,y,z,j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;x</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">;x</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_150_280_Open_Image style="DISPLAY: inline" onclick="this.style.display='none'; Codehighlighter1_150_280_Open_Text.style.display='none'; Codehighlighter1_150_280_Closed_Image.style.display='inline'; Codehighlighter1_150_280_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_150_280_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_150_280_Closed_Text.style.display='none'; Codehighlighter1_150_280_Open_Image.style.display='inline'; Codehighlighter1_150_280_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_150_280_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_150_280_Open_Text style="DISPLAY: inline"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">x)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">y;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(z</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">y</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">z</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%2d:&nbsp;cock=%2d&nbsp;hen=%2d&nbsp;chicken=%2d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j,x,y,z);<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<img src ="http://www.cppblog.com/twzheng/aggbug/22054.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/twzheng/" target="_blank">谭文政</a> 2007-04-16 21:46 <a href="http://www.cppblog.com/twzheng/articles/22054.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>遗传算法入门</title><link>http://www.cppblog.com/twzheng/articles/21339.html</link><dc:creator>谭文政</dc:creator><author>谭文政</author><pubDate>Thu, 05 Apr 2007 10:39:00 GMT</pubDate><guid>http://www.cppblog.com/twzheng/articles/21339.html</guid><wfw:comment>http://www.cppblog.com/twzheng/comments/21339.html</wfw:comment><comments>http://www.cppblog.com/twzheng/articles/21339.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/twzheng/comments/commentRss/21339.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/twzheng/services/trackbacks/21339.html</trackback:ping><description><![CDATA[<div class=cnt>
<h2 class=diaryTitle style="COLOR: rgb(51,102,255)">遗传算法入门</h2>
<span style="COLOR: rgb(51,102,255)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
<p style="COLOR: rgb(51,102,255)"><a name=N10043><span class=atitle><font color=#3366ff size=4>遗传算法</font></span></a></p>
<p style="COLOR: rgb(51,102,255)">遗传算法（Genetic Algorithm, GA）是近几年发展起来的一种崭新的全局优化算法。1962年霍兰德(Holland)教授首次提出了GA算法的思想，它借用了仿真生物遗传学和自然选择机理，通过自然选择、遗传、变异等作用机制，实现各个个体的适应性的提高。从某种程度上说遗传算法是对生物进化过程进行的数学方式仿真。</p>
<p style="COLOR: rgb(51,102,255)">这一点体现了自然界中"物竞天择、适者生存"进化过程。与自然界相似，遗传算法对求解问题的本身一无所知，它所需要的仅是对算法所产生的每个染色体进行评价,把问题的解表示成染色体，并基于适应值来选择染色体，使适应性好的染色体有更多的繁殖机会。在算法中也即是以二进制编码的串。并且，在执行遗传算法之前，给出一群染色体，也即是假设解。然后，把这些假设解置于问题的&#8220;环境&#8221;中，也即一个适应度函数中来评价。并按适者生存的原则，从中选择出较适应环境的染色体进行复制, 淘汰低适应度的个体，再通过交叉，变异过程产生更适应环境的新一代染色体群。对这个新种群进行下一轮进化，至到最适合环境的值。</p>
<p style="COLOR: rgb(51,102,255)">遗传算法已用于求解带有应用前景的一些问题，例如遗传程序设计、函数优化、排序问题、人工神经网络、分类系统、计算机图像处理和机器人运动规划等。</p>
<p style="COLOR: rgb(51,102,255)"><a name=N10052><span class=smalltitle><strong><font color=#3366ff size=3>术语说明</font></strong></span></a></p>
<p style="COLOR: rgb(51,102,255)">由于遗传算法是由进化论和遗传学机理而产生的搜索算法，所以在这个算法中会用到很多生物遗传学知识，下面是我们将会用来的一些术语说明：</p>
<p style="COLOR: rgb(51,102,255)"><strong>一、染色体(Chronmosome)</strong></p>
<p style="COLOR: rgb(51,102,255)">染色体又可以叫做基因型个体(individuals),一定数量的个体组成了群体(population),群体中个体的数量叫做群体大小。</p>
<p style="COLOR: rgb(51,102,255)"><strong>二、基因(Gene)</strong></p>
<p style="COLOR: rgb(51,102,255)">基因是串中的元素，基因用于表示个体的特征。例如有一个串S＝1011，则其中的1，0，1，1这4个元素分别称为基因。它们的值称为等位基因(Alletes)。</p>
<p style="COLOR: rgb(51,102,255)"><strong>三、基因地点(Locus)</strong></p>
<p style="COLOR: rgb(51,102,255)">基因地点在算法中表示一个基因在串中的位置称为基因位置(Gene Position)，有时也简称基因位。基因位置由串的左向右计算，例如在串 S＝1101 中，0的基因位置是3。</p>
<p style="COLOR: rgb(51,102,255)"><strong>四、基因特征值(Gene Feature)</strong></p>
<p style="COLOR: rgb(51,102,255)">在用串表示整数时，基因的特征值与二进制数的权一致；例如在串 S=1011 中，基因位置3中的1，它的基因特征值为2；基因位置1中的1，它的基因特征值为8。</p>
<p style="COLOR: rgb(51,102,255)"><strong>五、适应度(Fitness)</strong></p>
<p style="COLOR: rgb(51,102,255)">各个个体对环境的适应程度叫做适应度(fitness)。为了体现染色体的适应能力，引入了对问题中的每一个染色体都能进行度量的函数，叫适应度函数. 这个函数是计算个体在群体中被使用的概率。</p>
<p style="COLOR: rgb(51,102,255)"><a name=N1007E><span class=smalltitle><strong><font color=#3366ff size=3>操作算法</font></strong></span></a></p>
<p style="COLOR: rgb(51,102,255)">霍兰德(Holland)教授最初提出的算法也叫简单遗传算法，简单遗传算法的遗传操作主要有三种：选择(selection)、交叉(crossover)、变异(mutation)这也是遗传算法中最常用的三种算法：</p>
<p style="COLOR: rgb(51,102,255)"><strong>1．选择(selection)</strong></p>
<p style="COLOR: rgb(51,102,255)">选择操作也叫复制操作，从群体中按个体的适应度函数值选择出较适应环境的个体。一般地说，选择将使适应度高的个体繁殖下一代的数目较多，而适应度较小的个体，繁殖下一代的数目较少,甚至被淘汰。最通常的实现方法是轮盘赌(roulette wheel)模型。令&#931;fi表示群体的适应度值之总和，fi表示种群中第i个染色体的适应度值，它被选择的概率正好为其适应度值所占份额fi／&#931;fi。如下图表中的数据适应值总和&#931;fi=6650,适应度为2200变选择的可能为fi／&#931;fi=2200/6650=0.394.</p>
<br style="COLOR: rgb(51,102,255)"><a style="COLOR: rgb(51,102,255)" name=N10090><strong><font color=#3366ff>图1. 轮盘赌模型</font></strong></a><br style="COLOR: rgb(51,102,255)"><span style="COLOR: rgb(51,102,255)">&nbsp;</span><img style="COLOR: rgb(51,102,255)" src="http://robindy.bokee.com/inc/image001.jpg" align=bottom><br style="COLOR: rgb(51,102,255)">
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 border=1>
    <tbody>
        <tr>
            <td>Fitness 值：</td>
            <td>2200</td>
            <td>1800</td>
            <td>1200</td>
            <td>950</td>
            <td>400</td>
            <td>100</td>
        </tr>
        <tr>
            <td>选择概率：</td>
            <td>3331</td>
            <td>0.271</td>
            <td>0.18</td>
            <td>0.143</td>
            <td>0.06</td>
            <td>0.015</td>
        </tr>
    </tbody>
</table>
<span style="COLOR: rgb(51,102,255)">　 </span>
<p style="COLOR: rgb(51,102,255)"><strong>2．交叉(Crossover)</strong></p>
<p style="COLOR: rgb(51,102,255)">交叉算子将被选中的两个个体的基因链按一定概率pc进行交叉，从而生成两个新的个体，交叉位置pc是随机的。其中Pc是一个系统参数。根据问题的不同，交叉又为了单点交叉算子（Single Point Crossover）、双点交叉算子（Two Point Crossover）、均匀交叉算子 (Uniform Crossover)，在此我们只讨论单点交叉的情况。</p>
<p style="COLOR: rgb(51,102,255)">单点交叉操作的简单方式是将被选择出的两个个体S1和S2作为父母个体，将两者的部分基因码值进行交换。假设如下两个8位的个体：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>S1 1000  1111 S2 1110  1100</pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">产生一个在1到7之间的随机数c，假如现在产生的是2，将S1和S2的低二位交换：S1的高六位与S2的低六位组成数串10001100，这就是S1和S2 的一个后代P1个体；S2的高六位与S1的低二位组成数串11101111，这就是S1和S2的一个后代P2个体。其交换过程如下图所示：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 border=1>
    <tbody>
        <tr>
            <th>Crossover</th>
            <th>11110000</th>
            <th>Crossover</th>
            <th>11110000</th>
        </tr>
        <tr>
            <td>S1</td>
            <td>1000 1111</td>
            <td>S2</td>
            <td>1110 1100</td>
        </tr>
        <tr>
            <td>P1</td>
            <td>1000 1100</td>
            <td>P2</td>
            <td>1110 1111</td>
        </tr>
    </tbody>
</table>
<p style="COLOR: rgb(51,102,255)"><strong>3．变异(Mutation)</strong></p>
<p style="COLOR: rgb(51,102,255)">这是在选中的个体中，将新个体的基因链的各位按概率pm进行异向转化，最简单方式是改变串上某个位置数值。对二进制编码来说将0与1互换：0变异为1，1变异为0。</p>
<p style="COLOR: rgb(51,102,255)">如下8位二进制编码：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>1 1 1 0 1 1 0 0</pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">随机产生一个1至8之间的数i，假如现在k=6，对从右往左的第6位进行变异操作，将原来的1变为0，得到如下串：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>1 1 0 0 1 1 0 0</pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">整个交叉变异过程如下图：</p>
<br style="COLOR: rgb(51,102,255)"><a style="COLOR: rgb(51,102,255)" name=N10132><strong><font color=#3366ff>图2. 交叉变异过程</font></strong></a><br style="COLOR: rgb(51,102,255)"><font color=#3366ff><img style="COLOR: rgb(51,102,255)" src="http://robindy.bokee.com/inc/image002.jpg" align=bottom><br style="COLOR: rgb(51,102,255)"><span style="COLOR: rgb(51,102,255)">&nbsp;</span></font><img style="COLOR: rgb(51,102,255)" src="http://robindy.bokee.com/inc/image002-2.jpg" align=bottom><span style="COLOR: rgb(51,102,255)"> </span><br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)"><strong>4．精英主义 （Elitism）</strong></p>
<p style="COLOR: rgb(51,102,255)">仅仅从产生的子代中选择基因去构造新的种群可能会丢失掉上一代种群中的很多信息。也就是说当利用交叉和变异产生新的一代时，我们有很大的可能把在某个中间步骤中得到的最优解丢失。在此我们使用精英主义（Elitism）方法，在每一次产生新的一代时，我们首先把当前最优解原封不动的复制到新的一代中，其他步骤不变。这样任何时刻产生的一个最优解都可以存活到遗传算法结束。</p>
<p style="COLOR: rgb(51,102,255)">上述各种算子的实现是多种多样的，而且许多新的算子正在不断地提出，以改进GA某些性能。比如选择算法还有分级均衡选择等等。</p>
<p style="COLOR: rgb(51,102,255)"><a name=N1014F><span class=smalltitle><strong><font color=#3366ff size=3>遗传算法的所需参数</font></strong></span></a></p>
<p style="COLOR: rgb(51,102,255)">说简单点遗传算法就是遍历搜索空间或连接池，从中找出最优的解。搜索空间中全部都是个体，而群体为搜索空间的一个子集。并不是所有被选择了的染色体都要进行交叉操作和变异操作，而是以一定的概率进行，一般在程序设计中交叉发生的概率要比变异发生的概率选取得大若干个数量级。大部分遗传算法的步骤都很类似，常使用如下参数：</p>
<p style="COLOR: rgb(51,102,255)">Fitness函数：见上文介绍。</p>
<p style="COLOR: rgb(51,102,255)">Fitnessthreshold（适应度阀值）：适合度中的设定的阀值，当最优个体的适应度达到给定的阀值，或者最优个体的适应度和群体适应度不再上升时(变化率为零)，则算法的迭代过程收敛、算法结束。否则，用经过选择、交叉、变异所得到的新一代群体取代上一代群体，并返回到选择操作处继续循环执行。</p>
<p style="COLOR: rgb(51,102,255)">P：种群的染色体总数叫种群规模，它对算法的效率有明显的影响，其长度等于它包含的个体数量。太小时难以求出最优解，太大则增长收敛时间导致程序运行时间长。对不同的问题可能有各自适合的种群规模，通常种群规模为 30 至 160。</p>
<p style="COLOR: rgb(51,102,255)">pc：在循环中进行交叉操作所用到的概率。交叉概率（Pc）一般取0.6至0.95之间的值，Pc太小时难以向前搜索，太大则容易破坏高适应值的结构。</p>
<p style="COLOR: rgb(51,102,255)">Pm：变异概率，从个体群中产生变异的概率，变异概率一般取0.01至0.03之间的值变异概率Pm太小时难以产生新的基因结构，太大使遗传算法成了单纯的随机搜索。</p>
<p style="COLOR: rgb(51,102,255)">另一个系统参数是个体的长度，有定长和变长两种。它对算法的性能也有影响。由于GA是一个概率过程，所以每次迭代的情况是不一样的,系统参数不同，迭代情况也不同。</p>
<p style="COLOR: rgb(51,102,255)"><a name=N1016A><span class=smalltitle><strong><font color=#3366ff size=3>遗传步骤</font></strong></span></a></p>
<p style="COLOR: rgb(51,102,255)">了解了上面的基本参数，下面我们来看看遗传算法的基本步骤。</p>
<p style="COLOR: rgb(51,102,255)">基本过程为： </p>
<ol style="COLOR: rgb(51,102,255)">
    <li>对待解决问题进行编码,我们将问题结构变换为位串形式编码表示的过程叫编码；而相反将位串形式编码表示变换为原问题结构的过程叫译码。
    <li>随机初始化群体P(0)：=(p1, p2, &#8230; pn)；
    <li>计算群体上每个个体的适应度值(Fitness)
    <li>评估适应度,对当前群体P(t)中每个个体Pi计算其适应度F(Pi)，适应度表示了该个体的性能好坏
    <li>按由个体适应度值所决定的某个规则应用选择算子产生中间代Pr(t)
    <li>依照Pc选择个体进行交叉操作
    <li>仿照Pm对繁殖个体进行变异操作
    <li>没有满足某种停止条件，则转第3步，否则进入9
    <li>输出种群中适应度值最优的个体 </li>
</ol>
<p style="COLOR: rgb(51,102,255)">程序的停止条件最简单的有如下二种：完成了预先给定的进化代数则停止；种群中的最优个体在连续若干代没有改进或平均适应度在连续若干代基本没有改进时停止。</p>
<p style="COLOR: rgb(51,102,255)">根据遗传算法思想可以画出如右图所示的简单遗传算法框图： </p>
<br style="COLOR: rgb(51,102,255)"><a style="COLOR: rgb(51,102,255)" name=N1019C><strong><font color=#3366ff>图3. 简单遗传算法框图</font></strong></a><br style="COLOR: rgb(51,102,255)"><span style="COLOR: rgb(51,102,255)">&nbsp;</span><img style="COLOR: rgb(51,102,255)" src="http://robindy.bokee.com/inc/image003.gif" align=bottom><br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">下面伪代码简单说明了遗传算法操作过程：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>choose an intial population<br>For each h in population,compute Fitness(h)<br>While(max Fitness(h) &lt; Fitnessthreshold)<br>do selection<br>    do crossover<br>do mutation  <br> update population<br>For each h in population,compute Fitness(h)<br>Return best Fitness<br></pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)"><a name=N101B1><span class=atitle><font color=#3366ff size=4>Robocode 说明</font></span></a></p>
<p style="COLOR: rgb(51,102,255)">能有效实现遗传算法的应用例子有很多，像西洋双陆棋、国际名模等等都是遗传程序设计学习的工具，但是 Robocode 有着其他几个无可比拟的优势：</p>
<ol style="COLOR: rgb(51,102,255)">
    <li>它是基于面向对象语言 Java 开发，而遗传算法本身的思想也是存在继承等面向对象概念；
    <li>Robocode 是一种基于游戏与编程语言之间的平台，有一个充满竞技与乐趣的坦克战斗平台，你能很快的通过与其他坦克机器比赛而测试自己的遗传算法；
    <li>Robocode 社群有 4000 个左右各种策略的例子机器人可供你选择，这些机器人足以让我们模拟真实的遗传环境。而且很多代码可直接开放源代码供我们借鉴 ；
    <li>Robocode 是一个开源软件，你可直接上Robocode控制器上加入自己的遗传特点，而加快遗传过程的收敛时间；
    <li>Robocoe 是一个很容易使用的机器人战斗仿真器，您在此平台上创建自己的坦克机器人，并与其它开发者开发的机器人竞技。以得分排名的方式判定优胜者。每个 Robocode 参加者都要利用 Java 语言元素创建他或她的机器人，这样就使从初学者到高级黑客的广大开发者都可以参与这一娱乐活动。如果您对Robocode不是很了解，请参考 developerWorks 网站 Java 技术专区文章：&#8220;<a href="http://www-128.ibm.com/developerworks/cn/java/j-Robocode/"><u><font color=#0000ff>重锤痛击 Robocode</font></u></a>&#8221;； </li>
</ol>
<p style="COLOR: rgb(51,102,255)">在 Robocode 中其实有很多种遗传算法方法来实现进化机器人，从全世界的 Robocode 流派中也发展几种比较成熟的方法，比如预设策略遗传、自开发解释语言遗传、遗传移动我们就这几种方法分别加以介绍。由于遗传算法操作过程都类似，所以前面二部分都是一些方法的介绍和部分例子讲解，后面部分会给出使用了遗传算法的移动机器人人例子。在附录中，也提供了机器人仓库中有关遗传算法机器人的下载，大家可参考。</p>
<br style="COLOR: rgb(51,102,255)">
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 src="vid:leeshaoqun%E7%9A%84%E4%B8%93%E6%A0%8F.files/blue_rule.gif" width=547><br><img height=6 src="vid:leeshaoqun%E7%9A%84%E4%B8%93%E6%A0%8F.files/c.gif" width=8 border=0></td>
        </tr>
    </tbody>
</table>
<table class=no-print style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td><img height=4 src="vid:leeshaoqun%E7%9A%84%E4%B8%93%E6%A0%8F.files/c.gif" width=69><br>
            <table cellSpacing=0 cellPadding=0 border=0>
                <tbody>
                    <tr>
                        <td vAlign=center><br></td>
                        <td vAlign=top align=right><a class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-lo-robocode1/index.html#main"><strong></strong></a><br></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)"><br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)"><a name=N101D3><span class=atitle><font color=#3366ff size=4>预设策略进化机器人</font></span></a></p>
<p style="COLOR: rgb(51,102,255)">Robocode 坦克机器人所有行为都离不开如移动、射击、扫描等基本操作。所以在此把这些基本操作所用到的策略分别进化如下编码：移动策略move-strategy (MS), 子弹能量bullet-power-strategy (BPS), 雷达扫描radar-strategy (RS), 和瞄准选择策略target- strategy (TS)。由于Robocode爱好者社群的发展，每一种基本操作都发展了很多比较成熟的策略，所有在此我们直接在下面预先定义的这些策略如下表：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 border=1>
    <tbody>
        <tr>
            <th>MS</th>
            <th>BPS</th>
            <th>RS</th>
            <th>TS</th>
        </tr>
        <tr>
            <td>random</td>
            <td>distance-based</td>
            <td>always-turn</td>
            <td>HeadOn</td>
        </tr>
        <tr>
            <td>Linear</td>
            <td>light-fast</td>
            <td>target-focus</td>
            <td>Linear</td>
        </tr>
        <tr>
            <td>circular</td>
            <td>Powerful-slow</td>
            <td>target-scope-focus</td>
            <td>Circular</td>
        </tr>
        <tr>
            <td>Perpendicular</td>
            <td>Medium</td>
            <td>&nbsp;</td>
            <td>nearest robot</td>
        </tr>
        <tr>
            <td>arbitary</td>
            <td>hit-rate based</td>
            <td>&nbsp;</td>
            <td>Log</td>
        </tr>
        <tr>
            <td>anti gravity</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>Statistic</td>
        </tr>
        <tr>
            <td>Stop</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>Angular</td>
        </tr>
        <tr>
            <td>Bullet avoid</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>wave</td>
        </tr>
        <tr>
            <td>wall avoid</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>track</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>Oscillators</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>
<p style="COLOR: rgb(51,102,255)">下面是基本移动策略的说明：</p>
<ul style="COLOR: rgb(51,102,255)">
    <li>Random：随机移动主要用来混乱敌人的预测，其最大的一个缺点是有可能撞击到其他机器人
    <li>Linear：直线移动,机器人做单一的直线行走
    <li>circular：圆周移动，这种移动是以某一点为圆心，不停的绕圈
    <li>Perpendicular：正对敌人移动，这是很多人采用的一种移动方式，这在敌人右边， 以随时调整与敌人的相对角
    <li>Arbitrary：任意移动
    <li>AntiGravity：假设场地有很多力点的反重力移动，本方法是模拟在重力场作用下，物体总是远离重力势高的点，滑向重力势低的点，开始战场是一个平面然后生成一些势点重力势大的势点的作用就像是一个山(起排斥作用），其衰减系数与山的坡度对应。重力势小的势点的作用就像是一个低谷（起吸引作用），其衰减系数与谷的坡度对应。这样使本来的平面变得不平了，从来物体沿着最陡的方向向下滑动
    <li>Track：跟踪敌人，敌人移动到哪，机器人也移动到哪，但是总与敌人保持一定最佳躲避子弹距离和角度
    <li>Oscillators：重复做一振荡移动
    <li>Bullet avoid：每当雷达觉察到敌人时有所动作。机器人保持与敌人成30度倾向角。自身成 90 度角静止并逐渐接近目标。如果机器人觉察到能量下降介于 0.1 和 3.0 之间（火力范围），那么机器人就立即切换方向，向左或向右移动。
    <li>wall avoid：这是一种使自己的机器人不会被困在角落里或者不会撞墙的移动方式 </li>
</ul>
<p style="COLOR: rgb(51,102,255)">瞄准策略说明如下：</p>
<ul style="COLOR: rgb(51,102,255)">
    <li>Headon：正对瞄准
    <li>Linear：直线瞄准
    <li>circular：圆周瞄准
    <li>nearest robot：接近机器人瞄准
    <li>Log：保存每次开火记录
    <li>Statistic：统计学瞄准，分析所有打中及未打中的次数，以其中找出最高打中敌人的概率为准则
    <li>Angular：找到最佳角度瞄准
    <li>Wave：波形瞄准，子弹以波的方式进行探测 </li>
</ul>
<p style="COLOR: rgb(51,102,255)"><a name=N102C7><span class=smalltitle><strong><font color=#3366ff size=3>Robocode 行为事件</font></strong></span></a></p>
<p style="COLOR: rgb(51,102,255)">坦克的主要都定义在一个主循环中，我们在程序中定义为上面四个策略定义四种战略如Move,Radar,Power,Target，当某一事件发生，基于这个事件而定的行为就会触发。而每个战略中都有不同的行为处理方式。这些行为通过遗传算法触发，遗传算法将调用这些基本动作并搜索这些策略的最佳组合。基于这些基本动作将有4224 (=4*11*4*3*8)种可能发生。在Robocode AdvancedRobot 类下有如下的移动函数：</p>
<ul style="COLOR: rgb(51,102,255)">
    <li>setAhead和ahead：让机器人向前移动一定距离.
    <li>setBack和back：让机器人向后移动一定距离
    <li>setMaxTurnRate：设置机器人最大的旋转速度
    <li>setMaxVelocity：设置机器人最大的运动速度
    <li>setStop和stop：停止移动或暂停机器人，并记住停止的位置
    <li>setResume和resume：重新开始移动停止的机器人
    <li>setTurnLeft和turnLeft：向左旋转机器人
    <li>setTurnRight和turnRight：向右旋转机器人 </li>
</ul>
<p style="COLOR: rgb(51,102,255)">下面是 doMove 移动方法中使用部分程序代码：</p>
<p style="COLOR: rgb(51,102,255)">Random：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>switch(Math.random()*2) {<br> case 0： setTurnRight(Math.random()*90);<br>break;<br>case 1： setTurnLeft(Math.random()*90);<br> break; }<br> execute();<br></pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">Linear：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>ahead(200);<br>setBack(200);<br></pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">Circular：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>setTurnRight(1000);<br>setMaxVelocity(4);<br>ahead(1000);<br></pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">anti gravity：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section> double forceX = 0;<br>  double forceY = 0;<br>  for (int i=0; i   </pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">这里我们用遗传算法来控制机器人移动位置。这些策略是基于下面几点：机器人人自己的位置、速度和方位;对手的位置（x,y坐标）、速度、方位以及相对角;所有机器人和子弹位置，方位及速度;场地大小等参数。</p>
<p style="COLOR: rgb(51,102,255)">当上面的信息在下一回移动中使用时，出输出一对坐标值，根据这对坐标在Robocode就能得到距离和角度。要想让移动实现遗传必须要让它实现在线学习：所以我们的代码必须做下面几件事：要有一个函数收集适应度值，在Robocode运行过程中要运用到遗传操作，遗传后代要在Robocode运行中产生，而不是事后由手写入代码。</p>
<p style="COLOR: rgb(51,102,255)"><a name=N10310><span class=smalltitle><strong><font color=#3366ff size=3>遗传操作</font></strong></span></a></p>
<p style="COLOR: rgb(51,102,255)">本例中遗传算法为实现移动用到两个类GA和MovePattern。此处的GA比较简单主要完成数据和群体的定义，以及这些定义的读写文件操作。基中包括如下参数：群体大小、交叉概率、变异概率、精英概率（既告诉从当前群体到下一代中有多少移动不需要改变）、方程式中使用的加权系数大小，它通过一个主循环完成MovePattern的封装。MovePattern类中实现交叉、变异方法等方法，完成移动模式操作。而所有的输出保存在一个vector函数当中。Vector函数拥有一对实数数组，一个用于计算x坐标，另一个用于计算y坐标。通过对x,y坐标的计算，从而得到距离、角度等值，并产生相就在移动策略。如下，MovePattern包含三个参数，grad表示vector函数排列顺序，input即表示算法给出的输入编号，rang是加权的范围。</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>public class MovePatteren implements Comparable {<br> private int grad, input;<br> private double range;<br> protected double fitness=0;<br> protected double[] weightsX, weightsY;    <br>&#8230; }<br></pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">交叉操作：每一个交叉操作执行如下步骤，先在交叉操作中产生一个特征码。这个特征码是个0到1之间的变量数组。有关交叉的基本原理可参考上面部分。最后通过遍历vector函数，把相应的加权值进行交叉操作。</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>protected MovePatteren crossOver(MovePatteren mate, boolean[] maskx, boolean[] masky) {<br>  double[] wx= new double[weightsX.length];<br>  double[] wy= new double[weightsX.length];<br>  for(int mask=0; mask &lt;="" pre="" mask++)="" for(int="" g="0;" g  </pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">这里的变异操作比较简单。把加权范围内的随机数值去代替0到数组长之间的随机数并保存到移动模式中。则完成整个数组的变异过程：</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee border=1>
    <tbody>
        <tr>
            <td><code>
            <pre class=section>protected void mutate() {<br>weightsX[(int)(Math.random()*weightsX.length)]=Math.random()*range*2-range;<br>weightsY[(int)(Math.random()*weightsX.length)]=Math.random()*range*2-range;<br>}<br></pre>
            </code></td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)">从上面的例子我们知道了遗传算法的大概实现，但并没有告诉我们这些组件是如何一起工作的。当Robocode开始时，如果文件中没有数据，所以系统会依照输入的策略随机生成一个移动模式，如果文件中有数据，则加载这些数据。每一个移动模式在开始都会给出了一个适应度值。当所有的移动模式都接收到适应度值，并完成各自的编号后，下面的操作将开始执行：</p>
<ol style="COLOR: rgb(51,102,255)">
    <li>对所有的移动模式依据它们的适应度值进行分级处理
    <li>执行精英操作
    <li>执行交叉操作
    <li>应用变异操作
    <li>保存加权
    <li>算法重新开始 </li>
</ol>
<p style="COLOR: rgb(51,102,255)">适应度值在进行运算过程中由机器人程序不断调整，以找到最优适应度。</p>
<p style="COLOR: rgb(51,102,255)">限于篇副其他的一些策略本文不与详细说明，上面所有提到的策略和行为程序都可在网上或IBM的开发杂志上找到成熟的讲解和例子机器人。有兴趣的朋友可以把这些策略都加入到自己的遗传算法中来。我们取群体大小为50，选择概率为0.7，交叉概率为0.6，变异概率为0.3，与Robocode部分例子机器人测试，经过150代后你会发现系统产生了很多有趣的策略。比如撞击策略，这些策略都不在我们定义的策略之中。</p>
<br style="COLOR: rgb(51,102,255)">
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 src="vid:leeshaoqun%E7%9A%84%E4%B8%93%E6%A0%8F.files/blue_rule.gif" width=547><br><img height=6 src="vid:leeshaoqun%E7%9A%84%E4%B8%93%E6%A0%8F.files/c.gif" width=8 border=0></td>
        </tr>
    </tbody>
</table>
<table class=no-print style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td><img height=4 src="vid:leeshaoqun%E7%9A%84%E4%B8%93%E6%A0%8F.files/c.gif" width=69><br>
            <table cellSpacing=0 cellPadding=0 border=0>
                <tbody>
                    <tr>
                        <td vAlign=center><br></td>
                        <td vAlign=top align=right><a class=fbox href="http://www-128.ibm.com/developerworks/cn/java/j-lo-robocode1/index.html#main"><strong></strong></a><br></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br style="COLOR: rgb(51,102,255)"><br style="COLOR: rgb(51,102,255)">
<p style="COLOR: rgb(51,102,255)"><a name=N10349><span class=atitle><font color=#3366ff size=4>中间解释程序进化机器人</font></span></a></p>
<p style="COLOR: rgb(51,102,255)">遗传算法可被看做任意基因组字符串。但是你必须决定这些字符所代表的意义，也就是说如何解释每一个基因组。最简单的方法是把每一个基因组视为java代码，编译并运行它们。但是这些程序编译都很困难，所以也就有可能不能工作。Jacob Eisenstein设计了一种机器翻译语言TableRex来解决这个问题。在java中，TableRex被用于进化和解释动行时的Robocode 机器人。通过测试，只要我把TableRex解释程序作为文件放入Robocode控制器目录中，这些控制器就会读取文件并开始战斗。TableRex是一些最适合遗传算法的二进制编程。只要符合TableRex程序文法，每个程序都能被解释。</p>
<p style="COLOR: rgb(51,102,255)"><a name=N10352><span class=smalltitle><strong><font color=#3366ff size=3>编码</font></strong></span></a></p>
<p style="COLOR: rgb(51,102,255)">下表中显示了TableRex编码结构，它由一个行动作函数，二个输入和一个输出组成。如行6的值 ，这是个布尔型的表达式&#8220;值 line4 小于 90&#8221;，这个结果会在最后一行输出布尔为1的值。</p>
<table style="COLOR: rgb(51,102,255)" cellSpacing=0 cellPadding=0 border=1>
    <tbody>
        <tr>
            <th>Function</th>
            <th>Input 1</th>
            <th>Input 2</th>
            <th>Output</th>
        </tr>
        <tr>
            <td>1. Random</td>
            <td>ignore</td>
            <td>ignore</td>
            <td>0,87</td>
        </tr>
        <tr>
            <td>2. Divide</td>
            <td>Const_1</td>
            <td>Const_2</td>
            <td>0,5</td>
        </tr>
        <tr>
            <td>3. Greater Than</td>
            <td>Line 1</td>
            <td>Line 2</td>
            <td>1</td>
        </tr>
        <tr>
            <td>4. Normalize Angle</td>
            <td>Enemy bearing</td>
            <td>ignore</td>
            <td>-50</td>
        </tr>
        <tr>
            <td>5. Absolute Value</td>
            <td>Line 4</td>
            <td>ignore</td>
            <td>50</td>
        </tr>
        <tr>
            <td>6. Less Than</td>
            <td>Line 4</td>
            <td>Const_90</td>
            <td>1</td>
        </tr>
        <tr>
            <td>7. And</td>
            <td>Line 6</td>
            <td>Line 3</td>
            <td>1</td>
        </tr>
        <tr>
            <td>8. Multiply</td>
            <td>Const_10</td>
            <td>Const_10</td>
            <td>100</td>
        </tr>
        <tr>
            <td>9. Less Than</td>
            <td>Enemy distance</td>
            <td>Line 8</td>
            <td>0</td>
        </tr>
        <tr>
            <td>10. And</td>
            <td>Line 9</td>
            <td>Line 7</td>
            <td>0</td>
        </tr>
        <tr>
            <td>11. Multiply</td>
            <td>Line 10</td>
            <td>Line 4</td>
            <td>0</td>
        </tr>
        <tr>
            <td>12 Output</td>
            <td>Turn gun left</td>
            <td>Line 11</td>
            <td>0</td>
        </tr>
    </tbody>
</table>
</div>
<br>
<img src ="http://www.cppblog.com/twzheng/aggbug/21339.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/twzheng/" target="_blank">谭文政</a> 2007-04-05 18:39 <a href="http://www.cppblog.com/twzheng/articles/21339.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>