📄 ga_simple.cpp
字号:
#include <iostream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::endl;
const int maxruns=150;
const int popsize=50;
const int lchrom=2;
unsigned oldpop[popsize][lchrom];
unsigned newpop[popsize][lchrom];
double fitarray[popsize];
double sumfit;
unsigned mate1[lchrom];
unsigned mate2[lchrom];
unsigned child1[lchrom];
unsigned child2[lchrom];
double best=0.0;
void initialize()
{
unsigned mask=1;
for(int j=0;j<popsize;++j)
for (int k=0;k<lchrom;++k)
{
oldpop[j][k]=0;
for (int s=0;s<(8*sizeof(unsigned));++s)
{
int a=rand();
double sj=static_cast<double>(a)/RAND_MAX;
if (sj<=0.5)
{
oldpop[j][k]=oldpop[j][k]|(mask<<s);
}
}
}
////////////////
cout<<"initialization"<<endl;
for(int k=0;k<popsize;++k)
{
for (int j=0;j<lchrom;++j)
{
cout<<oldpop[k][j]<<" ";
if (j==lchrom-1)
cout<<endl;
}
}
///////////
}
double objfun(int p)
{
unsigned mask=1;
int bitpos;
unsigned tp;
double sum=0;
double v;
double dvalue;
double fitness;
for(int k=0;k<lchrom;++k)
{
tp=oldpop[p][k];
for(int j=1;j<=8*sizeof(unsigned);++j)
{
bitpos=8*sizeof(unsigned)*k+j;
if ((tp&mask)==1)
{
v=pow(2,(bitpos-1));
sum=sum+v;
}
tp=tp>>1;
}
}
dvalue=-1+3*sum/(pow(2.0,lchrom*8*sizeof(unsigned))-1);
fitness=dvalue*sin(40*atan(1)*dvalue)+2.0;
return fitness;
}
void preselect()
{
sumfit=0;
for(int i=0;i<popsize;++i)
{
fitarray[i]=objfun(i);
sumfit+=fitarray[i];
if(best<fitarray[i]) best=fitarray[i];
}
}
int select()
{
int f;
int a=rand();
double sj=static_cast<double>(a)/RAND_MAX;
double sjfit=sj*sumfit;
double sum=fitarray[0];
for(int i=0;i<popsize;++i)
{
if (sjfit<=sum)
{
f=i;
break;
}
else
{
sum+=fitarray[i+1];
}
}
return f;
}
void crossover()
{
unsigned mask;
unsigned temp=1;
int a=rand();
double pc=static_cast<double>(a)/RAND_MAX;
if (pc<=0.25)
{
int crosspos=1+rand()%(8*lchrom*sizeof(unsigned)-1);
for(int k=1;k<=lchrom;++k)
{
if (crosspos>=(k*8*sizeof(unsigned)))
{
child1[k-1]=mate1[k-1];
child2[k-2]=mate2[k-1];
}
else if ((crosspos<(k*8*sizeof(unsigned)))&&(crosspos>((k-1)*8*sizeof(unsigned))))
{
mask=0;
for (int j=1;j<=(crosspos-((k-1)*8*sizeof(unsigned)));++j)
{
mask=mask<<1;
mask=mask|temp;
}
child1[k-1]=((mate1[k-1]&mask)|(mate2[k-1]&(~mask)));
child2[k-1]=((mate1[k-1]&(~mask))|(mate2[k-1]&mask));
}
else
{
child1[k-1]=mate2[k-1];
child2[k-1]=mate1[k-1];
}
}
}
else
{
for (int k=0;k<lchrom;++k)
{
child1[k]=mate1[k];
child2[k]=mate2[k];
}
}
}
void mutation()
{
int a;
double pm;
unsigned mask;
unsigned temp=1;
for (int k=0;k<lchrom;++k)
{
for (int j=0;j<8*sizeof(unsigned);++j)
{
a=rand();
pm=static_cast<double>(a)/RAND_MAX;
if (pm<=0.01)
{
mask=(temp<<j);
child1[k]=child1[k]^mask;
}
}
}
for (int n=0;n<lchrom;++n)
{
for (int j=0;j<8*sizeof(unsigned);++j)
{
a=rand();
pm=static_cast<double>(a)/RAND_MAX;
if (pm<=0.01)
{
mask=(temp<<j);
child2[n]=child2[n]^mask;
}
}
}
}
int main()
{
initialize();
for(int d=0;d<maxruns;++d)
{
preselect();
int par;
for(int b=0;b<popsize;b+=2)
{
par=select();
for (int v=0;v<lchrom;++v)
mate1[v]=oldpop[par][v];
par=select();
for (int u=0;u<lchrom;++u)
mate2[u]=oldpop[par][u];
crossover();
mutation();
for (int l=0;l<lchrom;++l)
{
newpop[b][l]=child1[l];
newpop[b+1][l]=child2[l];
}
}
for(int h=0;h<popsize;++h)
for(int j=0;j<lchrom;++j)
oldpop[h][j]=newpop[h][j];
for (int g=0;g<popsize;++g)
cout<<fitarray[g]<<" ";
cout<<endl;
}
cout<<endl<<endl;
cout<<best;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -