QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2187|回复: 0
打印 上一主题 下一主题

[转帖]111

[复制链接]
字体大小: 正常 放大
dahai2080        

2

主题

2

听众

32

积分

升级  28.42%

该用户从未签到

跳转到指定楼层
1#
发表于 2006-1-11 19:44 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
天才开始了解遗传算法,看了一天的书,昨晚做了一个基本的遗传算法的仿真(最简单的那种),希望大家多多指教。<BR><BR>程序如下<BR><BR>% Simple Genetic Algorithm<BR>% Revised according to the program which is the first appendix of the book<BR>% "Genetic Algorithms theory and Application"<BR>% Programmed by Yang Wuyi 2005.6.15 <BR><BR>% To calculate the maximal value of the function<BR>% In this program the function is<BR>% F( x1 , x2 ) = 100 * ( x1^2 - x2 )^2 + ( 1 - x1 )^2<BR>% s.t - XRang &lt;= x1 , x2 &lt;= XRang <BR><BR>clear all<BR>close all<BR><BR>% Some parameters of the Simple Genetic Algorithm<BR>opulationSize = 80 ; %The population of the colony<BR>MaxGeneration = 100 ; %The maximal generation <BR>roCro = 0.6 ; %The probability of crossover<BR>roMut = 0.001 ; %The probability of mutation <BR><BR>XRang = 2.048 ;<BR><BR>Vmax = 100 ; %<BR>Vmin = 0 ; %<BR>% FunctionMode &gt;= 0 find the maximum of the function<BR>% FunctionMode &lt; 0 find the minimum of the function<BR>FunctionMode = 1 ; % <BR><BR>ChromosomeLen1 = 10 ; % The lenght of the first chromosome<BR>ChromosomeLen2 = 10 ; % The lenght of the second chromosome<BR>ChromosomeLen = 10 ;<BR><BR>ChromosomeLength = ChromosomeLen1 + ChromosomeLen2 ; % The total lenght of chromosome<BR><BR>X1 = zeros ( 1 , PopulationSize );<BR>X2 = zeros ( 1 , PopulationSize );<BR><BR>% Generate the first generation of the population<BR>rand('state',sum(100*clock)); % Resets the generator to a different state each time<BR><BR>for j = 1 : PopulationSize<BR>for k = 1 : ChromosomeLength<BR>Temp = rand(1);<BR>if Temp &lt; 0.5<BR>opulation( j ) . Chromosome( k ) = 0;<BR>else<BR>opulation( j ) . Chromosome( k ) = 1;<BR>end % End of if<BR>end % End of for - <BR><BR>X1( j ) = DecodeChromosome(Population( j ) . Chromosome, 1 , ChromosomeLen );<BR>X2( j ) = DecodeChromosome(Population( j ) . Chromosome, ChromosomeLen+1 , ChromosomeLen ); <BR><BR>opulation( j ).Value = 0 ;<BR>opulation( j ).Fitness = 0 ;<BR>end<BR><BR>X1 = 2 * XRang * X1/1023 - XRang;<BR>X2 = 2 * XRang * X2/1023 - XRang;<BR><BR>figure<BR>plot( X1 , X2 , '*') , grid;<BR><BR>BestValue = zeros( 1 , MaxGeneration ) ;<BR>AverageValue = zeros( 1 , MaxGeneration ) ;<BR><BR>for Gen = 1 : MaxGeneration<BR>% Calculate the value according to the function which we want to find its maximum <BR>% In this program the function is<BR>% F( x1 , x2 ) = 100 * ( x1^2 - x2 )^2 + ( 1 - x1 )^2<BR>% s.t -XRang &lt;= x1 , x2 &lt;= XRang <BR>for j = 1 : PopulationSize<BR>TempValue = CalculateObjectValue( Population( j ).Chromosome , ChromosomeLen);<BR>opulation( j ).Value = TempValue;<BR>end % End of for - j<BR><BR>%Calculate fitness value<BR>for j = 1 : PopulationSize<BR>TempValue = CalculateFitnessValue( Population( j ).Value , Vmax , Vmin , FunctionMode);<BR>opulation( j ).Fitness = TempValue;<BR>end % End of for - j <BR><BR>% Find out the best and worst individual of this generation<BR>BestValue( Gen ) = Population( 1 ).Value ;<BR>for j = 1 : PopulationSize<BR>if Population( j ).Value &gt; BestValue( Gen ) <BR>BestValue( Gen ) = Population( j ).Value;<BR>end<BR>end<BR><BR>Sum = 0 ;<BR>for j = 1 : PopulationSize<BR>Sum = Sum + Population( j ).Value;<BR>end <BR>AverageValue( Gen ) = Sum / PopulationSize ;<BR><BR>% Reproduce a chromosome by proportional selection<BR>Sum = 0 ;<BR>CFitness = zeros ( 1 , PopulationSize ); % The cumulative fitness value<BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>% Calculate cumulative fitness<BR>for j = 1 : PopulationSize<BR>Sum = Sum + Population( j ).Fitness ;<BR>end % End of for - j <BR><BR>for j = 1 : PopulationSize<BR>CFitness( j ) = Population( j ).Fitness / Sum ;<BR>end % End of for - j <BR>for j = 2 : PopulationSize<BR>CFitness( j ) = CFitness( j - 1 ) + CFitness( j ) ; <BR>end % End of for - j <BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>% Select operation<BR>for j = 1 : PopulationSize<BR>RandP = rand( 1 ) ;<BR>Index = 1;<BR>while RandP &gt; CFitness( Index )<BR>Index = Index + 1 ;<BR>end % End of while <BR>NewPopulation( j ) = Population( Index ) ; <BR>end % End of for - j <BR><BR>for j = 1 : PopulationSize<BR>opulation( j ) = NewPopulation( j ) ; <BR>end % End of for - j <BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>% Crossover two chromosome<BR>IndexPair = zeros( 1, PopulationSize);<BR>for j = 1 : PopulationSize<BR>IndexPair( j ) = j;<BR>end % End of for - j <BR><BR>for j = 1 : PopulationSize<BR>oint = floor ( (PopulationSize - j - 1) * rand( 1 ) );<BR>Temp = IndexPair( j ) ;<BR>IndexPair( j ) = IndexPair( j + Point );<BR>IndexPair( j + Point ) = Temp;<BR>end % End of for - j <BR><BR>for j = 1 : 2 : ( PopulationSize - 1 )<BR>RandP = rand( 1 ) ;<BR>if RandP &lt; ProCro<BR><BR>oint = floor ( rand( 1 ) * ChromosomeLen ) ;<BR>if Point == 0<BR>oint = 1;<BR>end<BR><BR>for k = Point : (ChromosomeLen + Point - 1)<BR>Temp = Population ( IndexPair( j ) ) . Chromosome( k ) ;<BR>opulation ( IndexPair( j ) ) . Chromosome( k ) = Population ( IndexPair( j + 1) ) . Chromosome( k ) ;<BR>opulation ( IndexPair( j + 1) ) . Chromosome( k ) = Temp ;<BR>end % End of for - k<BR><BR>end % End of if<BR>end % End of for - j<BR>% Crossover two chromosome <BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>% Mutation of a chromosome <BR>for j = 1 : PopulationSize<BR>for k = 1 : 2*ChromosomeLen<BR>RandP = rand( 1 ) ; <BR>if RandP &lt; ProMut<BR><BR>if Population( j ) . Chromosome( k ) == 1<BR>opulation( j ) . Chromosome( k ) = 0 ; <BR>else<BR>opulation( j ) . Chromosome( k ) = 1 ;<BR>end% End of if <BR><BR>end% End of if <BR>end % End of for - k <BR>end % End of for - j <BR><BR>end % End of for - Gen<BR><BR>for j = 1 : PopulationSize<BR>X1( j ) = DecodeChromosome(Population( j ) . Chromosome, 1 , ChromosomeLen );<BR>X2( j ) = DecodeChromosome(Population( j ) . Chromosome, ChromosomeLen+1 , ChromosomeLen ); <BR>end<BR><BR>Xmin = -2.5 ;<BR>Xmax = 2.5 ;<BR>Ymin = -2.5;<BR>Ymax = 2.5; <BR><BR>X1 = 2 * XRang * X1/1023 - XRang;<BR>X2 = 2 * XRang * X2/1023 - XRang;<BR><BR>figure<BR>plot( X1 , X2 , '*') , grid , axis([Xmin Xmax Ymin Ymax]); <BR><BR>figure<BR>[X , Y] = meshgrid(-XRang : 0.1: XRang);<BR>Z = 100 * ( X.^2 - Y ).^2 + ( 1 - X ).^2;<BR>mesh(X , Y , Z);<BR><BR>AimValue = ones(1 , MaxGeneration) * (100 * ( XRang^2 + XRang )^2 + ( 1 + XRang )^2) ;<BR><BR>Xmin = 0 ;<BR>Xmax = MaxGeneration ;<BR>Ymin = 0;<BR>Ymax = 5000; <BR><BR>figure<BR>hold on <BR>plot( BestValue , 'r' ) ; <BR>plot( AverageValue , 'b' ) ; <BR>plot( AimValue , 'g' ) ; <BR>hold off<BR>grid<BR>axis([Xmin Xmax Ymin Ymax]);<BR><BR>下面是四个子函数<BR><BR>function [ Fitness ] = CalculateFitnessValue( Value , Vmax , Vmin , FunctionMode)<BR>% To calculate fitness value<BR>% FunctionMode : <BR>% &gt;= 0 find the maximum of the function<BR>% &lt; 0 find the minimum of the function<BR>% Fitness is big than 0 <BR>if FunctionMode &gt;= 0<BR>if (Value + Vmin) &gt; 0<BR>Fitness = Value + Vmin ;<BR>else<BR>Fitness = 0;<BR>end<BR>else <BR>if Value &lt; Vmax<BR>Fitness = Vmax - Value ;<BR>else<BR>Fitness = 0;<BR>end <BR>end<BR><BR>function [Value] = CalculateObjectValue( Chromosome , Len)<BR>% To calculate value according to the function which we want to find its<BR>% maximum or minimum<BR>% In this program the function is<BR>% F( x1 , x2 ) = 100 * ( x1^2 - x2 )^2 + ( 1 - x1 )^2<BR>% s.t -2.048 &lt;= x1 , x2 &lt;= 2.048<BR><BR>Value1 = DecodeChromosome(Chromosome, 1 , Len );<BR>Value2 = DecodeChromosome(Chromosome, Len+1 , Len );<BR><BR>Value1 = 4.096 * Value1/1023 - 2.048;<BR>Value2 = 4.096 * Value2/1023 - 2.048;<BR><BR>Value = 100 * ( Value1^2 - Value2 )^2 + ( 1 - Value1 )^2;<BR><BR>function [Value] = DecodeChromosome( ChromosomeCode , CodePos , Len )<BR>% Function : To decode a binary chromosome code into a decimal integer<BR>Value = 0 ; <BR>k = Len - 1;<BR>Ed = CodePos + Len -1;<BR>for j = CodePos : Ed<BR>Value = Value + ChromosomeCode( j ) * ( 2 ^ ( k ) ) ;<BR>k = k - 1;<BR>end <BR>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-20 04:35 , Processed in 0.432343 second(s), 50 queries .

回顶部