马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
这是用实验函数--Ackley函数来检验遗传算法的优越性,我自己编写的,其中有点小毛病,就是种群数在20个左右效果最好,一旦增加种群,结果就不大对劲了。忙了好多天,也没调出来,还请各位大侠指点喽
- #include"iostream.h"
- #include<stdlib.h>
- #include <stdio.h>
- #include<math.h>
- #define pi 3.1415926
- class GA
- {
- private:
- double N_random;
- int N_gene[501][5][6];
- double N_integer[501][5];
- double N_integer1[501][5];
- double t_index[501][5];
- double t_index1[501][5];
- double t_down[5],t_up[5];
- double Energy[501];
- double Energy1[501];
- int m_i_th,m_i_th1; /* ?üá?×?′ó?μ?ù?úDD */
- double Sum_energy;
- /*float max;*/
- double Fitness[501];
- /*float Sum_fitness;*/
- double q_fitness[501];
- double q_random[501];
- int i_qmax;
- int N_parent[501][21];
-
- int N_newgene[501][21];
- int N_supergene[501][21]; /* 3???è?é?ì?*/
- int N_addgene[501][21]; /*??í¨?ùòò+213??ùòò*/
- int Bottle;
- int t_bottle; /*??Dòê±?D??±?á? */
- int i_ch,j_ch; /* */
- int ic,is,nlose;
- int nochose[501];
- double P_cr;
- int N_select[501][21];
- int N_crosspoint;
- int N_offspringc[501][21];
-
- int jm,im,N_mutate;
- double P_mp[501][21];
- int judge;
- int N_renewgene[501][5][6];
- int i_length;
- public:
- GA();
-
- void N_changetointeger(void);
- void N_changetointeger1(void);
- void t_changetoindex(void);
- void t_changetoindex1(void);
- N_3to2();
- E_nergy();
- E_nergy1();
- Makeorder();
- Addgene();
- Makeorder1();
- Chose_super();
- F_itness();
-
- crossover();
- mutate();
- N_2to3();
- N_add2to3();
- F_irst();
- C_ircle();
- };
- GA::GA()
- {
- int a;
- int i,j,k;
-
- cout<<"ê?è???èoêy i_length=";
- cin>>i_length;
- /* cout<<"N_gene[][][]"<<endl;*/
- for(i=1;i<=i_length;i++)
- for(j=1;j<=4;j++)
- for(k=1;k<=5;k++)
- {
- a=rand()%10000/1000;
- N_gene[i][j][k]=a; /* 2úéú?-ê????úêy×÷?a?ùòò */
-
- /* cout<<N_gene[i][j][k]<<" " ; /* ê?3??ùòò */
- /* if(j==4&&k==5)
- cout<<endl;
- */
- }
-
- t_down[1]=-3.0,t_down[2]=4.1,t_down[3]=0,t_down[4]=0;
- t_up[1]=12.1,t_up[2]=5.8,t_up[3]=0,t_up[4]=0;
-
- }
- void GA::N_changetointeger(void)
- { int i,j,k;
- /* cout<<"N_changetointeger"<<endl; */
- /* judge=0;
- for(i=1;i<i_length;i++)
- for(j=1;j<=4;j++)
- for(k=1;k<=5;k++)
- {
- if(N_gene[i][j][k]!=N_gene[i+1][j][k])
- judge=1;
-
- }
- */
- for(i=1;i<=i_length;i++)
- for(j=1;j<=4;j++)
- {
- N_integer[i][j]=0;
- for(k=1;k<=5;k++) /* ???-ê??ùòò×a?ˉ?a??êy */
- {
- N_integer[i][j]=N_integer[i][j]+N_gene[i][j][k]*pow(10,(5-k));
- }
- /* cout<<N_integer[i][j]<<" "; /* ????êyê?3? */
- /**/ /* if(j==4)
- cout<<endl;
- */
- }
- }
- void GA::N_changetointeger1(void)
- { int i,j,k;
- for(i=1;i<=1.2*i_length;i++)
- for(j=1;j<=4;j++)
- {
- N_integer1[i][j]=0;
- for(k=1;k<=5;k++) /* ???-ê??ùòò×a?ˉ?a??êy */
- {
- N_integer1[i][j]=N_integer1[i][j]+N_renewgene[i][j][k]*pow(10,(5-k));
- }
-
- }
- }
- void GA::t_changetoindex(void)
- { int i,j;
- /* cout<<"t_changetoindex"<<endl;
- */ for(i=1;i<=i_length;i++)
- for(j=1;j<=4;j++) /* ????êy×a?ˉ?aóDìa??o?ò?μ?êy */
- {
- t_index[i][j]=t_down[j]+N_integer[i][j]*
- ((t_up[j]-t_down[j])/(pow(10,5)-1));
-
- /* cout<<t_index[i][j]<<" ";
- if(j==4)
- cout<<endl;
- */
- }
- }
- void GA::t_changetoindex1(void)
- { int i,j;
- for(i=1;i<=1.2*i_length;i++)
- for(j=1;j<=4;j++) /* ????êy×a?ˉ?aóDìa??o?ò?μ?êy */
- {
- t_index1[i][j]=t_down[j]+N_integer1[i][j]*
- ((t_up[j]-t_down[j])/(pow(10,5)-1));
- }
- }
- GA::N_3to2() /* èy??êy×é×a?ˉ?a?t??êy×é*/
- {
- /* cout<<endl<<"3to2(èy??êy×é×a?ˉ?a?t??êy×é)"<<endl; */
- int i,j,k;
- int m=1;
-
- for(i=1;i<=i_length;i++)
- for(j=1;j<=4;j++)
- for(k=1;k<=5;k++)
- {
- if(m<=20)
- {
- N_newgene[i][m]=N_gene[i][j][k]; /* èy??êy×é×a?ˉ?a?t??êy×é*/
- m++;
- if(m>20)
- m=1;
- }
-
- }
- /* cout<<endl<<"N_newgene[][]"<<endl; /*ê?3?D?μ??t??êy×é*/
- /* for(i=1;i<=i_length;i++)
- for(j=1;j<=20;j++)
- {
- cout<<N_newgene[i][j]<<" ";
- if(j==20)
- cout<<endl;
-
- }
- */
- }
- GA::E_nergy() /* ?üá?oˉêy */
- {
- cout<<endl<<"E_nergy()"<<endl;
- int i,j=1;
- int m;
- int max,Fo;
- max=0;
- for(i=1;i<=i_length;i++)
-
- { Fo=21.5+t_index[i][j]*sin(4*pi*t_index[i][j])
- +t_index[i][j+1]*sin(20*pi*t_index[i][j+1]);
-
- /* Energy[i]=Fo;
-
- cout<<"Energy="<<Energy[i]<<" ";
- if(i%5==0)
- cout<<endl;
- /* ê?3?×?′ó?üá?oˉêy?μ */
- if(Fo>=max)
- {
- max=Fo;
- m_i_th=i;
- }
-
- }
- cout<<"±?′?μü′ú×?′ó?üá??ù?úμ?DD£o"<<m_i_th<<endl;
- cout<<"Max Energy="<<max<<" ";
- /* cout<<"Fitness[i]="<<Fitness[m_i_th]<<" ";*/
- }
- GA::Makeorder()
- { cout<<endl;
-
- int i,j;
-
-
- for(j=1;j<=i_length-1;j++) /* ???á1???DD??Dò*/
- for(i=1;i<=i_length-j;i++)
- { /* ò?±?ìáè?3?à′3???è?é?ì?*/
- if(Energy[i]<Energy[i+1])
- {
- t_bottle=Energy[i+1],Energy[i+1]=Energy[i],
- Energy[i]=t_bottle;
-
- /* for(i_ch=1;i_ch<i_length;i_ch++)*/
- for(j_ch=1;j_ch<=20;j_ch++)
- {
- Bottle=N_newgene[i+1][j_ch],
- N_newgene[i+1][j_ch]=N_newgene[i][j_ch],
- N_newgene[i][j_ch]=Bottle;
- }
- }
- }
- /* cout<<"??Dòoóμ?Energy:"<<endl;
- for(i=1;i<=i_length;i++)
- {
- cout<<"Energy="<<Energy[i]<<" ";
- if(i%5==0)
- cout<<endl;
- }
- cout<<"??Dòoóμ?N_newgene:"<<endl;
- for(i=1;i<=i_length;i++)
- for(j=1;j<=20;j++)
- {
- cout<<N_newgene[i][j]<<" ";
- if(j==20)
- cout<<endl;
-
- }
- /* for(i_ch=1;i_ch<=i_length;i_ch++) /* ê?3???Dòoóμ??ùòò*//*
-
- { cout<<"μú"<<i_ch<<"DD";
- for(j_ch=1;j_ch<=20;j_ch++)
- {
- cout<<N_newgene[i_ch][j_ch]<<" ";
- if(j_ch==20)
- cout<<endl;
- }
- }
- */
- }
- GA::Addgene()
- {
- int i,j;
- int i_super=1;
-
- for(i=1;i<=i_length;i++)
- for(j=1;j<=20;j++)
- {
- N_addgene[i][j]=N_newgene[i][j]; /*??D?μ?N_newgene */
- } /*·??úN_addgeneà? */
- for(i=i_length+1;i<=1.2*i_length;i++)
- {
- for(j=1;j<=20;j++)
- /* ??N_supergene */
- {
- N_addgene[i][j]=N_supergene[i_super][j]; /* 21·??úN_addgeneà? */
- }
- i_super++;
- }
-
-
- }
- GA::E_nergy1() /* 213?oóμ??üá?oˉêy */
- { cout<<endl;
- int i,j=1;
-
- int F1;
- int max=0;
- for(i=1;i<=1.2*i_length;i++)
-
- { F1=21.5+t_index1[i][j]*sin(4*pi*t_index1[i][j])
- +t_index1[i][j+1]*sin(20*pi*t_index1[i][j+1]);
-
- Energy1[i]=F1;
- /* cout<<"Energy1="<<Energy1[i]<<" ";
- if(i%5==0)
- cout<<endl;
- */
- if(F1>=max)
- {
- max=F1;
- m_i_th1=i;
- }
-
- }
- /* cout<<"Energy1="<<max<<" ";*/
-
- }
- GA::Makeorder1()
- {
- cout<<endl;
-
- int i,j;
-
- for(j=1;j<=1.2*i_length-1;j++) /* ???á1???DD??Dò*/
- for(i=1;i<=1.2*i_length-j;i++)
- { /* ò?±?ìáè?3?à′3???è?é?ì?*/
- if(Energy1[i]<Energy1[i+1])
- {
- t_bottle=Energy1[i+1],Energy1[i+1]=Energy1[i],
- Energy1[i]=t_bottle;
-
- /*for(i_ch=1;i_ch<1.2*i_length;i_ch++)*/
- for(j_ch=1;j_ch<=20;j_ch++)
- {
- Bottle=N_addgene[i+1][j_ch],
- N_addgene[i+1][j_ch]=N_addgene[i][j_ch],
- N_addgene[i][j_ch]=Bottle;
- }
- }
- }
-
- /* cout<<endl;
- cout<<"??Dòoóμ?N_addgene:"<<endl;
- for(i=1;i<=1.2*i_length;i++)
- for(j=1;j<=20;j++)
- {
- cout<<N_addgene[i][j]<<" ";
- if(j==20)
- cout<<endl;
- }
- */
- for(i_ch=1;i_ch<i_length;i_ch++) /*è??°??1--i_lengthμ??ùòò */
- for(j_ch=1;j_ch<=20;j_ch++) /*·??úN_newgeneà? */
- {
- N_newgene[i_ch][j_ch]=N_addgene[i_ch][j_ch];
- }
-
- /* cout<<"??Dòoóμ?N_newgene:"<<endl;
- for(i=1;i<=i_length;i++)
- for(j=1;j<=20;j++)
- {
- cout<<N_newgene[i][j]<<" ";
- if(j==20)
- cout<<endl;
- }
- */
- }
- GA::Chose_super()
- { cout<<"N_supergene"<<endl;
- int i,j;
- for(i=1;i<=0.2*i_length;i++)
- for(j=1;j<=20;j++)
- {
- N_supergene[i][j]=N_newgene[i][j]; /* 3???è?é?ì?*/
- /* cout<<N_supergene[i][j]<<" ";
- if(j==20)
- cout<<endl;*/
- }
-
- for(i=1;i<=0.2*i_length;i++)
- for(j=1;j<=20;j++)
- {
- N_newgene[i][j]=rand()%1000/100; /*213??ùòò*/
- }
- cout<<"N_newgene&&N_offspringc:"<<endl;
- for(i=1;i<=i_length;i++)
- for(j=1;j<=20;j++)
- {
- N_offspringc[i][j]=N_newgene[i][j]; /* ?aá???DDN_2to3×a?ˉ */
- /* cout<<N_offspringc[i][j]<<" ";
- if(j==20)
- cout<<endl;*/
- }
-
- }
- GA::F_itness() /* êêó|?èoˉêy */
- { int i,j;
-
- int i_parent;
- cout<<endl<<"F_itness()"<<endl;
- Sum_energy=0;
- for(i=1;i<=i_length;i++)
- { /*cout<<"Energy="<<Energy[i]<<" ";
- if(i%5==0)
- cout<<endl;
- */
- Sum_energy=Sum_energy+fabs(Energy[i]);
- }
- /* cout<<endl;*/
- /* cout<<endl<<"Sum_energy="<<Sum_energy<<" "; */
- for(i=1;i<=i_length;i++)
- { Fitness[i]=(Energy[i])/(Sum_energy);
-
- /* cout<<"Fitness="<<Fitness[i]<<" ";*/
- }
- /* Sum_fitness=0;
- /* for(i=1;i<=i_length;i++)
- {
- Sum_fitness=Sum_fitness+fabs(Fitness[i]);
- }
- cout<<"Sum_fitness="<<Sum_fitness<<" ";
- */
- /* cout<<endl;*/
- q_fitness[0]=0;
- for(i=1;i<=i_length;i++)
- {
- q_fitness[i]=q_fitness[i-1]+Fitness[i]; /* ????à??y???ê*/
- /* cout<<"q_fitness="<<q_fitness[i]<<endl;*/
- }
-
- for(i=1;i<=i_length;i++)
- {
- q_random[i]=rand()%100/100.0;
-
- }
- for(i=1;i<=i_length;i++)
- {
- /* cout<<"q_random="<<q_random[i]<<endl;*/
- }
- i_parent=1;
- for(i=1;i<=i_length;i++)
- for(i_qmax=1;i_qmax<=i_length;i_qmax++)
- { /* ??×a????*/
- if(q_random[i]<=q_fitness[i_qmax])
- { /*cout<<"q_random="<<q_random[i]<<endl;*/
- for(j=1;j<=20;j++)
- {
- N_parent[i_parent][j]=N_newgene[i][j];
- }
- i_parent++;
-
- }
- }
- }
- GA::crossover() /*??2?*/
- { /* cout<<endl;*/
-
- /* cout<<endl<<"crossover()"<<endl;*/
- int a;
-
- int i=1,j,m;
- ic=1,nlose=1;
- is=1;
- while(i<=i_length) /* ?ú4????èo?D???????? */
- {
- N_random=rand()%10001/10000.0; /* ?ú[0,1]????2úéú???úêy */
- P_cr=N_random;
- if(P_cr<0.8) /* ò?0.7μ????ê???????? */
- { /* cout<<"μú"<<i<<"DD±?????"<<endl;
- cout<<"P_cr="<<P_cr<<endl;*/
- for(j=1;j<=20;j++)
- { /* ??·?o?ì??tμ?????·??úD?μ?êy×é */
- N_select[ic][j]=N_parent[i][j];
-
- /* cout<<N_select[ic][j]<<" ";
- if(j==20) /* ??·?o?ì??tμ?????ê?3? */
- /* cout<<endl; */
- }
- ic++;
- }
- else
- {
- nochose[nlose]=i; /* ??2?·?o?ì??tμ?????μ?DDo?·??úD?μ?êy×é */
- nlose++;
- /* cout<<"μú"<<i<<"DD??±?????"<<endl; */
- }
-
- i++;
- }
- /* cout<<"i="<<i<<endl; */
- /* cout<<"12??2?μ?DDêy ic="<<ic-1<<endl;*/
-
- a=rand()%1900/100; /* ?ú[0,19]????2úéú???úêy */
- N_crosspoint=a; /* ??μ?????£¨D?óú20£? */
- cout<<"??2?μ? N_crosspoint="<<N_crosspoint<<endl;
- for(i=1;i<ic-1;i++)
- for(j=N_crosspoint;j<=20;j++)
- {
- N_offspringc[i+1][j]= N_select[i][j];
-
- N_offspringc[i][j]= N_select[i+1][j];
- /*?ú??2?μ?óò±?μ?è?é?ì???2?*/
- }
- for(i=1;i<ic-1;i++)
- for(j=1;j<N_crosspoint;j++)
- {
- N_offspringc[i][j]= N_select[i][j]; /*?ú??2?μ?×ó±?μ?è?é?ì?±£á?*/
-
- N_offspringc[i+1][j]= N_select[i+1][j];
-
- }
- /* cout<<"??2?oóμ?è?é?ì?"<<endl;
- for(i=1;i<=ic-1;i++)
- for(j=1;j<=20;j++)
- {
- cout<<N_offspringc[i][j]<<" "; /*ê?3?????oóμ?è?é?ì?*/
- /* if(j==20)
- cout<<endl;
- }
- */
- j=1;
- for(i=ic;i<=i_length;i++)
- {
- for(m=1;m<=20;m++) /*??????2?μ?????μ?21·??úD?μ?êy×é */
- {
- N_offspringc[i][m]=N_parent[nochose[j]][m];
- }
- j++;
- }
- /* cout<<endl<<"N_offspringc[][]="<<endl;
-
- for(i=1;i<=i_length;i++)
- for(j=1;j<=20;j++)
- {
- cout<<N_offspringc[i][j]<<" ";
- if(j==20)
- cout<<endl;
- }
- */
- }
- GA::mutate()
- {
- /* cout<<endl;*/
- /* cout<<"mutate()"<<endl;*/
- int a,m;
- m=0;
- /* cout<<"±?òìμ??ùòòêy??£o"<<endl; */
- for(im=1;im<=i_length;im++)
- for(jm=1;jm<=20;jm++)
- {
- N_random=rand()%10001/10000.0;
- P_mp[im][jm]=N_random; /*2úéú[0,1]?????ù?è·?2?μ????úêyDòáD*/
-
- if(P_mp[im][jm]<0.15) /*ò?20%μ????ê·¢éú±?òì*/
- { m++;
- /* cout<<"μú"<<im<<"DD ";
- cout<<"μú"<<jm<<"áD?ùòòòa±?ì??? ";
- */
- /**/ a=rand()%800/100;
- N_mutate=a; /*2úéú[0,8]????·?2?μ????úêy×÷?aì???òò×ó*/
- /* cout<<"ì????ùòò£o"<<a<<endl;
- /**/
- if(N_mutate==N_offspringc[im][jm])
- {
- N_mutate++; /*è?1?ì???òò×óoí?-à′?àí?£??ò±??ˉ*/
- }
-
- N_offspringc[im][jm]=N_mutate; /*ì???òò×óì???μ??-à′òò×ó*/
-
- }
-
-
- }
- /* cout<<m<<" ";*/
- cout<<endl;
- /* cout<<"±?òìoóμ?è?é?ì?£o"<<endl;
- for(im=1;im<=i_length;im++)
- for(jm=1;jm<=20;jm++)
- {
- cout<<N_offspringc[im][jm]<<" ";
- if(jm==20)
- cout<<endl;
- }
- */
- }
- GA::N_2to3()
- {
- /* cout<<endl<<"2to3(?t??êy×é×a?ˉ?aèy??êy×é)"<<endl; */
- int i,j,k;
- int m=1;
-
- for(i=1;i<=i_length;i++)
- for(j=1;j<=4;j++)
- for(k=1;k<=5;k++)
- {
- if(m<=20)
- {
- /* ?t??êy×é×a?ˉ?aèy??êy×é*/
- N_renewgene[i][j][k]=N_offspringc[i][m];
- N_gene[i][j][k]=N_renewgene[i][j][k];
- m++;
- if(m>20)
- m=1;
- }
-
- }
-
- }
- GA::N_add2to3()
- {
- int i,j,k;
- int m=1;
-
- for(i=1;i<=1.2*i_length;i++)
- for(j=1;j<=4;j++)
- for(k=1;k<=5;k++)
- {
- if(m<=20)
- {
- /* ?t??êy×é×a?ˉ?aèy??êy×é*/
- N_renewgene[i][j][k]=N_addgene[i][m];
- N_gene[i][j][k]=N_renewgene[i][j][k];
- m++;
- if(m>20)
- m=1;
- }
-
- }
- }
- GA::F_irst()
- {
- N_changetointeger();
- t_changetoindex();
- N_3to2();
- E_nergy();
- Makeorder();
- Chose_super();
- N_2to3();
- N_changetointeger();
- t_changetoindex();
- E_nergy();
- F_itness();
- crossover();
- mutate();
- Addgene();
- N_2to3();
- }
- GA::C_ircle()
- {
- F_irst();
- int count;
- int total;
-
- cout<<"ê?è?μü′ú′?êy£o";
- cin>>total;
- for(count=2;count<=total;count++)
- {
- cout<<endl<<endl<<"μú"<<count<<"′?μü′ú:";
- cout<<endl<<"****************";
-
- N_add2to3();
- N_changetointeger1();
- t_changetoindex1();
-
- E_nergy1();
- Makeorder1();
- Chose_super();
- N_2to3();
- N_changetointeger();
- t_changetoindex();
- E_nergy();
- F_itness();
-
- crossover();
- mutate();
- Addgene();
- N_2to3();
-
- cout<<endl;
- cout<<"*********************************"<<endl;
- }
- }
- main()
- {
- GA gene1;
- /* gene1.F_irst();*/
- gene1.C_ircle();
-
- cout<<endl;
- return(0);
- }
复制代码
[ 本帖最后由 风花雪月 于 2006-11-12 06:28 编辑 ] |