- 在线时间
- 3 小时
- 最后登录
- 2017-2-14
- 注册时间
- 2006-1-11
- 听众数
- 2
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 32
- 相册
- 0
- 日志
- 0
- 记录
- 1
- 帖子
- 62
- 主题
- 2
- 精华
- 0
- 分享
- 0
- 好友
- 9
升级   28.42% 该用户从未签到
 |
天才开始了解遗传算法,看了一天的书,昨晚做了一个基本的遗传算法的仿真(最简单的那种),希望大家多多指教。<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 <= x1 , x2 <= 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 >= 0 find the maximum of the function<BR>% FunctionMode < 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 < 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 <= x1 , x2 <= 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 > 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 > 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 < 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 < 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>% >= 0 find the maximum of the function<BR>% < 0 find the minimum of the function<BR>% Fitness is big than 0 <BR>if FunctionMode >= 0<BR>if (Value + Vmin) > 0<BR>Fitness = Value + Vmin ;<BR>else<BR>Fitness = 0;<BR>end<BR>else <BR>if Value < 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 <= x1 , x2 <= 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
|