[原创]用VC++编的遗传算法
这是用实验函数--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;
double N_integer;
double N_integer1;
double t_index;
double t_index1;
double t_down,t_up;
double Energy;
double Energy1;
int m_i_th,m_i_th1; /* ?üá?×?′ó?μ?ù?úDD*/
double Sum_energy;
/*float max;*/
double Fitness;
/*float Sum_fitness;*/
double q_fitness;
double q_random;
int i_qmax;
int N_parent;
int N_newgene;
int N_supergene; /* 3???è?é?ì?*/
int N_addgene; /*??í¨?ùòò+213??ùòò*/
int Bottle;
int t_bottle; /*??Dòê±?D??±?á? */
int i_ch,j_ch; /* */
int ic,is,nlose;
int nochose;
double P_cr;
int N_select;
int N_crosspoint;
int N_offspringc;
int jm,im,N_mutate;
double P_mp;
int judge;
int N_renewgene;
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=a; /* 2úéú?-ê????úêy×÷?a?ùòò*/
/* cout<<N_gene<<" " ; /* ê?3??ùòò*/
/* if(j==4&&k==5)
cout<<endl;
*/
}
t_down=-3.0,t_down=4.1,t_down=0,t_down=0;
t_up=12.1,t_up=5.8,t_up=0,t_up=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!=N_gene)
judge=1;
}
*/
for(i=1;i<=i_length;i++)
for(j=1;j<=4;j++)
{
N_integer=0;
for(k=1;k<=5;k++) /* ???-ê??ùòò×a?ˉ?a??êy*/
{
N_integer=N_integer+N_gene*pow(10,(5-k));
}
/* cout<<N_integer<<" "; /* ????ê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=0;
for(k=1;k<=5;k++) /* ???-ê??ùòò×a?ˉ?a??êy*/
{
N_integer1=N_integer1+N_renewgene*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=t_down+N_integer*
((t_up-t_down)/(pow(10,5)-1));
/* cout<<t_index<<" ";
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=t_down+N_integer1*
((t_up-t_down)/(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;
intm=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=N_gene; /* è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<<" ";
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*sin(4*pi*t_index)
+t_index*sin(20*pi*t_index);
/* Energy=Fo;
cout<<"Energy="<<Energy<<"";
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="<<Fitness<<"";*/
}
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<Energy)
{
t_bottle=Energy,Energy=Energy,
Energy=t_bottle;
/* for(i_ch=1;i_ch<i_length;i_ch++)*/
for(j_ch=1;j_ch<=20;j_ch++)
{
Bottle=N_newgene,
N_newgene=N_newgene,
N_newgene=Bottle;
}
}
}
/*cout<<"??Dòoóμ?Energy:"<<endl;
for(i=1;i<=i_length;i++)
{
cout<<"Energy="<<Energy<<"";
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<<" ";
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<<"";
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=N_newgene; /*??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=N_supergene; /* 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*sin(4*pi*t_index1)
+t_index1*sin(20*pi*t_index1);
Energy1=F1;
/* cout<<"Energy1="<<Energy1<<"";
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<Energy1)
{
t_bottle=Energy1,Energy1=Energy1,
Energy1=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,
N_addgene=N_addgene,
N_addgene=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<<" ";
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=N_addgene;
}
/* cout<<"??Dòoóμ?N_newgene:"<<endl;
for(i=1;i<=i_length;i++)
for(j=1;j<=20;j++)
{
cout<<N_newgene<<" ";
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=N_newgene; /* 3???è?é?ì?*/
/* cout<<N_supergene<<" ";
if(j==20)
cout<<endl;*/
}
for(i=1;i<=0.2*i_length;i++)
for(j=1;j<=20;j++)
{
N_newgene=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=N_newgene; /* ?aá???DDN_2to3×a?ˉ */
/* cout<<N_offspringc<<" ";
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<<"";
if(i%5==0)
cout<<endl;
*/
Sum_energy=Sum_energy+fabs(Energy);
}
/* cout<<endl;*/
/* cout<<endl<<"Sum_energy="<<Sum_energy<<"";*/
for(i=1;i<=i_length;i++)
{ Fitness=(Energy)/(Sum_energy);
/* cout<<"Fitness="<<Fitness<<"";*/
}
/* Sum_fitness=0;
/* for(i=1;i<=i_length;i++)
{
Sum_fitness=Sum_fitness+fabs(Fitness);
}
cout<<"Sum_fitness="<<Sum_fitness<<"";
*/
/* cout<<endl;*/
q_fitness=0;
for(i=1;i<=i_length;i++)
{
q_fitness=q_fitness+Fitness; /* ????à??y???ê*/
/* cout<<"q_fitness="<<q_fitness<<endl;*/
}
for(i=1;i<=i_length;i++)
{
q_random=rand()%100/100.0;
}
for(i=1;i<=i_length;i++)
{
/* cout<<"q_random="<<q_random<<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<=q_fitness)
{ /*cout<<"q_random="<<q_random<<endl;*/
for(j=1;j<=20;j++)
{
N_parent=N_newgene;
}
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; /* ?ú????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=N_parent;
/* cout<<N_select<<" ";
if(j==20) /* ??·?o?ì??tμ?????ê?3? */
/* cout<<endl; */
}
ic++;
}
else
{
nochose=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; /* ?ú????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= N_select;
N_offspringc= N_select;
/*?ú??2?μ?óò±?μ?è?é?ì???2?*/
}
for(i=1;i<ic-1;i++)
for(j=1;j<N_crosspoint;j++)
{
N_offspringc= N_select; /*?ú??2?μ?×ó±?μ?è?é?ì?±£á?*/
N_offspringc= N_select;
}
/*cout<<"??2?oóμ?è?é?ì?"<<endl;
for(i=1;i<=ic-1;i++)
for(j=1;j<=20;j++)
{
cout<<N_offspringc<<" "; /*ê?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=N_parent];
}
j++;
}
/* cout<<endl<<"N_offspringc[][]="<<endl;
for(i=1;i<=i_length;i++)
for(j=1;j<=20;j++)
{
cout<<N_offspringc<<" ";
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=N_random; /*2úéú?????ù?è·?2?μ????úêyDòáD*/
if(P_mp<0.15) /*ò?20%μ????ê·¢éú±?òì*/
{ m++;
/* cout<<"μú"<<im<<"DD ";
cout<<"μú"<<jm<<"áD?ùòòòa±?ì??? ";
*/
/**/ a=rand()%800/100;
N_mutate=a; /*2úéú????·?2?μ????úêy×÷?aì???òò×ó*/
/* cout<<"ì????ùòò£o"<<a<<endl;
/**/
if(N_mutate==N_offspringc)
{
N_mutate++; /*è?1?ì???òò×óoí?-à′?àí?£??ò±??ˉ*/
}
N_offspringc=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<<" ";
if(jm==20)
cout<<endl;
}
*/
}
GA::N_2to3()
{
/* cout<<endl<<"2to3(?t??êy×é×a?ˉ?aèy??êy×é)"<<endl;*/
int i,j,k;
intm=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=N_offspringc;
N_gene=N_renewgene;
m++;
if(m>20)
m=1;
}
}
}
GA::N_add2to3()
{
int i,j,k;
intm=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=N_addgene;
N_gene=N_renewgene;
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 编辑 ]
帮个忙
<P>哥们我现在要做关于遗传算法的毕业设计,能不帮个忙呢QQ261211250,谢谢</P>回复:(Hamlet)[原创]用VC++编的遗传算法
有什么具体问题请说明 你的算法问题很大,效果也不理想,多多改善 有没有遗传算法在Qos路由选择中的应用程序啊 原帖由 superimi 于 2006-7-24 18:31 发表 http://forum.vibunion.com/forum/images/common/back.gif你的算法问题很大,效果也不理想,多多改善
希望你提出能提出更多宝贵的意见,以便搂主改进 还没有我的效果好,参考
http://forum.vibunion.com/forum/viewthread.php?tid=41877&pid=222193&page=1&extra=page%3D1#pid222193
希望能讨论讨论。 原帖由 assist 于 2007-5-8 10:14 发表 http://forum.vibunion.com/forum/images/common/back.gif
还没有我的效果好,参考
http://forum.vibunion.com/forum/viewthread.php?tid=41877&pid=222193&page=1&extra=page%3D1#pid222193
希望能讨论讨论。
做个比较发出来看看
页:
[1]