⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gfdlg.cpp

📁 用vc++编写的遗传算法程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{
  unsigned nbytes;
 
  int j;
  /* 分配给当前代和新一代种群内存空间 */
  nbytes = popsize*sizeof(struct individual);
  oldpop=new (struct individual [nbytes]); 
  newpop=new (struct individual [nbytes]);
  
  nbytes = chromsize*sizeof(unsigned);
  for(j = 0; j < popsize; j++)
    {
      oldpop[j].chrom=new (unsigned [nbytes]);
      newpop[j].chrom=new (unsigned [nbytes]);
    }
  bestfit.chrom=new (unsigned [nbytes]);
  bestfit_min.chrom=new(unsigned[nbytes]);

}
void CGFDlg::randomize()  /* 设定随机数种子并初始化随机数发生器 */
{
    float randomseed=0.5;
    int j1;
    for(j1=0; j1<=54; j1++)
      oldrand[j1] = 0.0;
    jrand=0;
    warmup_random(randomseed);
}
void CGFDlg::warmup_random(float random_seed)  /* 初始化随机数发生器*/
{
    int j1, ii;
    double new_random, prev_random;

    oldrand[54] = random_seed;
    new_random = 0.000000001;
    prev_random = random_seed;
    for(j1 = 1 ; j1 <= 54; j1++)
    {
        ii = (21*j1)%54;
        oldrand[ii] = new_random;
        new_random = prev_random-new_random;
        if(new_random<0.0) new_random = new_random + 1.0;
        prev_random = oldrand[ii];
    }
    advance_random();
    advance_random();
    advance_random();
    jrand = 0;
}

void CGFDlg::advance_random()          /* 产生55个随机数 */
{
    int j1;
    double new_random;
    for(j1 = 0; j1 < 24; j1++)
    {
        new_random = oldrand[j1] - oldrand[j1+31];
        if(new_random < 0.0) new_random = new_random + 1.0;
        oldrand[j1] = new_random;
    }
    for(j1 = 24; j1 < 55; j1++)
    {
        new_random = oldrand [j1] - oldrand [j1-24];
        if(new_random < 0.0) new_random = new_random + 1.0;
        oldrand[j1] = new_random;
    }
}
float CGFDlg::randomperc()            /*与库函数random()作用相同, 产生[0,1]之间一个随机数 */
{
 
	jrand++;
 
	if(jrand >= 55)
    {
        jrand = 1;
        advance_random();
    }
    return((float) oldrand[jrand]);
}
int CGFDlg::flip(float prob)          /* 以一定概率产生0或1 */
{
    //float 
	randomperc();
    if(randomperc() <= prob)
        return(1);
    else
        return(0);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
CString  CGFDlg::writechrom(unsigned *chrom) /* 输出染色体编码 */
{
    int s,j, k, stop;
	int save[100];
    unsigned mask = 1, tmp;
	
	CString h;
	  for(int l=0;l<100;l++)
	   save[l]=0;
    for(k = 0; k < chromsize; k++)
    {
        tmp = chrom[k];
        if(k == (chromsize-1))
		{
			stop = lchrom - (k*(8*sizeof(unsigned)));
            s=stop;
		}
       else
		   stop =8*sizeof(unsigned);
		 
        for(j = 0; j < stop; j++)
        {
            if(tmp&mask)
			{
			    
				h=h+'1';
			  
			}
			   
            else
			{
			
				h=h+'0';
			  
			
            }
				tmp = tmp>>1;
			
        }
    }
	 CString stor="";
	 char b;
	 for(int i=0;i<h.GetLength();i++)
	    {
		 b=h.GetAt(h.GetLength()-i-1);
	     stor=stor+b;
	 }    
	  return stor;
  

}
double p(double x,double y)
{
	return pow(x,y);
}
void CGFDlg::objfunc(struct individual *critter)  /* 计算适应度函数值 */

{
    unsigned mask=1;
    unsigned bitpos;
    unsigned tp;
    double pow(), bitpow ,x,y,z,e,f,g,h;
    int j, k,tag=0, stop;
	for(int m=0;m<8;m++)
	{
       critter->varible[m] = 0.0;
	   }
    for(k = 0; k < chromsize; k++)
    {
        if(k == (chromsize-1))
		  
			stop = lchrom-(k*(8*sizeof(unsigned)));
            
		
		else
            stop =8*sizeof(unsigned);
		  
        tp = critter->chrom[k];
        for(j = 0; j < stop; j++)
        {
            bitpos = j + (8*sizeof(unsigned))*k;
            if((tp&mask) == 1)
            {
                bitpow =p(2.0,(double) bitpos);
                critter->varible[7] = critter->varible[7] + bitpow;
				
            }
            tp = tp>>1;
		    tag=tag+1;
			if(tag==chanshu[0].l)x=critter->varible[7];
		    if(tag==chanshu[0].l+chanshu[1].l)y=critter->varible[7];
            if(tag==chanshu[0].l+chanshu[1].l+chanshu[2].l)z=critter->varible[7];
			if(tag==chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l)e=critter->varible[7];
			if(tag==chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l)f=critter->varible[7];
			if(tag==chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l+chanshu[5].l)g=critter->varible[7];
			//if(tag==chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l+chanshu[5].l+chanshu[6].l)h=critter->varible[7];





			//if(tag==
          
        }

    }
	h=(critter->varible[7]-g)/p(2.0,(double)(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l+chanshu[5].l));
	g=(g-f)/p(2.0,(double)(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l));
	f=(f-e)/p(2.0,(double)(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l));
	e=(e-z)/p(2.0,(double)(chanshu[0].l+chanshu[1].l+chanshu[2].l));
	z=(z-y)/p(2.0,(double)(chanshu[0].l+chanshu[1].l));
	y=(y-x)/p(2.0,(double)chanshu[0].l);
	
  
	     x=chanshu[0].m_min+x*(chanshu[0].m_max-chanshu[0].m_min)/(p(2.0,(double)chanshu[0].l)-1);
	if(chanshu[1].l==0)y=0;
	else y=chanshu[1].m_min+y*(chanshu[1].m_max-chanshu[1].m_min)/(p(2.0,(double)chanshu[1].l)-1);
	if(chanshu[2].l==0)z=0;
	else z=chanshu[2].m_min+z*(chanshu[2].m_max-chanshu[2].m_min)/(p(2.0,(double)chanshu[2].l)-1);
	if(chanshu[3].l==0)e=0;
	else e=chanshu[3].m_min+e*(chanshu[3].m_max-chanshu[3].m_min)/(p(2.0,(double)chanshu[3].l)-1);
	if(chanshu[4].l==0)f=0;
	else f=chanshu[4].m_min+f*(chanshu[4].m_max-chanshu[4].m_min)/(p(2.0,(double)chanshu[4].l)-1);
	if(chanshu[5].l==0)g=0;
	else g=chanshu[5].m_min+g*(chanshu[5].m_max-chanshu[5].m_min)/(p(2.0,(double)chanshu[5].l)-1);
	if(chanshu[6].l==0)h=0;
	else h=chanshu[6].m_min+h*(chanshu[6].m_max-chanshu[6].m_min)/(p(2.0,(double)chanshu[6].l)-1);


	
	 critter->varible[0]=x;
     critter->varible[1]=y;
	 critter->varible[2]=z;
     critter->varible[3]=e;
     critter->varible[4]=f;
     critter->varible[5]=g;
     critter->varible[6]=h;


	 //critter->varible=x+y+z;
     //CGenes_sDlg aa;
	 //double b=aa.transfer(x,y);
	 
	 //CString b=CGenes_sDlg::transfer(x,y);
	 
	 //critter->fitness=b;
	//critter->fitness=sin(x+y);
     CString s=Change(m_function_s);
	 critter->fitness=Compute(s,x,y,z,e,f,g,h);
	 //critter->fitness=0.5-(sin(sqrt(x*x+y*y))*(sin(sqrt(x*x+y*y)))-0.5)/((1+0.001*(x*x+y*y))*(1+0.001*(x*x+y*y)));
	//cout<<critter->varible<<endl;
    //critter->varible =-1+critter->varible*3/(p(2.0,(double)lchrom)-1);
    //cout<<critter->varible<<endl;
 
	//critter->fitness =critter->varible*sin(critter->varible*10*atan(1)*4)+2.0;
}

void  CGFDlg::initpop()           /* 随机初始化种群 */
{
    //initthedata();
	int j, j1, k, stop;
    unsigned mask = 1;
    for(j = 0; j < popsize; j++)
    {
       
       for(k = 0; k < chromsize; k++)
        {
            oldpop[j].chrom[k] = 0;
            if(k == (chromsize-1))
                stop = lchrom - (k*(8*sizeof(unsigned)));
            else
                stop =8*sizeof(unsigned);
            for(j1 = 1; j1 <= stop; j1++)
            {
               oldpop[j].chrom[k] = oldpop[j].chrom[k]<<1;
               if(flip(0.5))
                  oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask;         
			}
		
        }
        oldpop[j].parent[0] = 0;     /* 初始父个体信息 */
        oldpop[j].parent[1] = 0;
        oldpop[j].xsite = 0;
        objfunc(&(oldpop[j]));      /* 计算初始适应度*/
		
		//writechrom((&oldpop[j])->chrom);
    }

 }

int CGFDlg::rnd(int low, int high) /*在整数low和high之间产生一个随机整数*/
{
    int i;
    //float 
	randomperc();
    if(low >= high)
        i = low;
    else
    {
        i = (randomperc() * (high - low + 1)) + low;
        if(i > high) i = high;
    }
    return(i);
}
void  CGFDlg::mutation(unsigned *child)   /*变异操作*/
{
    int j, k, stop;
    unsigned mask, temp = 1;
    for(k = 0; k < chromsize; k++)
    {
        mask = 0;
        if(k == (chromsize-1))
            stop = lchrom - (k*(8*sizeof(unsigned)));
        else
            stop = 8*sizeof(unsigned);
        for(j = 0; j < stop; j++)
        {
            if(flip(pmutation))
            {
                mask = mask|(temp<<j);
                nmutation++;
            }
        }
        child[k] = child[k]^mask;
    }
}

int CGFDlg::crossover (unsigned *parent1, unsigned *parent2, unsigned *child1, unsigned *child2)
/* 由两个父个体交叉产生两个子个体 */
{
    int j, jcross, k;
    unsigned mask, temp;
    if(flip(pcross))
    {
        jcross = rnd(1 ,(lchrom - 1));/* Cross between 1 and l-1 */
        ncross++;
        for(k = 1; k <= chromsize; k++)
        {
            if(jcross >= (k*(8*sizeof(unsigned))))
            {
                child1[k-1] = parent1[k-1];
                child2[k-1] = parent2[k-1];
            }
            else if((jcross < (k*(8*sizeof(unsigned)))) && (jcross > ((k-1)*(8*sizeof(unsigned)))))
            {
                mask = 1;
                for(j = 1; j <= (jcross-1-((k-1)*(8*sizeof(unsigned)))); j++)
                {
                    temp = 1;
                    mask = mask<<1;
                    mask = mask|temp;
                }
                child1[k-1] = (parent1[k-1]&mask)|(parent2[k-1]&(~mask));
                child2[k-1] = (parent1[k-1]&(~mask))|(parent2[k-1]&mask);
            }
            else
            {
                child1[k-1] = parent2[k-1];
                child2[k-1] = parent1[k-1];
            }
        }
    }
    else
    {
        for(k = 0; k < chromsize; k++)
        {
            child1[k] = parent1[k];
            child2[k] = parent2[k];
        }
        jcross = 0;
    }
    return(jcross);
}

void CGFDlg::preselect()
{
    int j;
    sumfitness = 0;
    for(j = 0; j < popsize; j++) sumfitness += oldpop[j].fitness;
}

int CGFDlg::select()  /* 轮盘赌选择*/
{
    //extern float 
	randomperc();
    float sum, pick;
    int i;
    pick = randomperc();
    sum = 0;
    if(sumfitness != 0)
    {
        for(i = 0; (sum < pick) && (i < popsize); i++)
            sum += oldpop[i].fitness/sumfitness;
    }
    else
        i = rnd(1,popsize);
    return(i-1);
}

void CGFDlg::generation()
{
  int mate1, mate2, jcross, j = 0;
  /*  每代运算前进行预选 */
  preselect();
  /* 选择, 交叉, 变异 */
  do
    {
      /* 挑选交叉配对 */
      mate1 = select();
      mate2 = select();
      /* 交叉和变异 */
      jcross = crossover(oldpop[mate1].chrom, oldpop[mate2].chrom, newpop[j].chrom, newpop[j+1].chrom);
      mutation(newpop[j].chrom);
      mutation(newpop[j+1].chrom);
      /* 解码, 计算适应度 */
      objfunc(&(newpop[j]));
      /*记录亲子关系和交叉位置 */
      newpop[j].parent[0] = mate1+1;
      newpop[j].xsite = jcross;
      newpop[j].parent[1] = mate2+1;
      objfunc(&(newpop[j+1]));
      newpop[j+1].parent[0] = mate1+1;
      newpop[j+1].xsite = jcross;
      newpop[j+1].parent[1] = mate2+1;
      j = j + 2;
    }
  while(j < (popsize-1));

}

void CGFDlg::statistics(struct individual *pop)  /* 计算种群统计数据 */

{
    int i, j;
	CString s,s_max,s_min,x_max,y_max,z_max,x_min,y_min,z_min;
    sumfitness = 0.0;
    min = pop[0].fitness;
    max = pop[0].fitness;
    /* 计算最大、最小和累计适应度 */
    for(j = 0; j < popsize; j++)
    {
        sumfitness = sumfitness + pop[j].fitness;            
        if(pop[j].fitness > max) max = pop[j].fitness;        
        if(pop[j].fitness < min) min = pop[j].fitness;         
        /* new global best-fit individual */
      if(pop[j].fitness > bestfit.fitness)
	  {
	   for(i = 0; i < chromsize; i++)
	    bestfit.chrom[i]      = pop[j].chrom[i];
            bestfit.fitness    = pop[j].fitness;
            bestfit.varible[0]   = pop[j].varible[0];
			bestfit.varible[1]   = pop[j].varible[1];
			bestfit.varible[2]   = pop[j].varible[2];
			bestfit.varible[3]   = pop[j].varible[3];
			bestfit.varible[4]   = pop[j].varible[4];
			bestfit.varible[5]   = pop[j].varible[5];
			bestfit.varible[6]   = pop[j].varible[6];

            bestfit.generation = gen;
	  }
	   if(pop[j].fitness < bestfit_min.fitness)
	  {
	   for(i = 0; i < chromsize; i++)
	    bestfit_min.chrom[i]      = pop[j].chrom[i];
            bestfit_min.fitness    = pop[j].fitness;
            bestfit_min.varible[0]   = pop[j].varible[0];
			bestfit_min.varible[1]   = pop[j].varible[1];
			bestfit_min.varible[2]   = pop[j].varible[2];
			bestfit_min.varible[3]   = pop[j].varible[3];
			bestfit_min.varible[4]   = pop[j].varible[4];
			bestfit_min.varible[5]   = pop[j].varible[5];
			bestfit_min.varible[6]   = pop[j].varible[6];

            bestfit_min.generation = gen;
	  }
	}
   //if(min<bestfit_min.fitness) bestfit_min.fitness=min;
   /* 计算平均适应度 */
   avg = sumfitness/popsize;
   
   s=s+"                       最大值:";
   s_max.Format("%s%f",s_max,bestfit.fitness);
   s=s+s_max;
   //x_max.Format("%s%f",s_max,bestfit.varible[0]);
   //y_max.Format("%s%f",y_max,bestfit.varible[1]);
   //z_max.Format("%s%f",z_max,bestfit.varible[2]);
   //s=s+"   "+"对应变量:x="+x_max+"  y="+y_max+"    z="+z_max;
   s_min.Format("%s%f",s_min,bestfit_min.fitness);
   s=s+"          最小值:";
   s=s+s_min;
   SetDlgItemText(IDC_EDIT18,s);
   
}


void CGFDlg::initdata()
{
	
	//////////////////////获取变量值/////////
	//save_x=m_xmin;
	//save_y=m_ymin;
	//save_z=m_zmin;
	//save_x_sum=m_xmax-m_xmin;
	//save_y_sum=m_ymax-m_ymin;
	//save_z_sum=m_zmax-m_zmin;
	popsize=m_popsize;
    pcross=m_pc;
	pmutation=m_pm;
	maxgen=m_maxgen;
	m_function_s=m_function;
	
	//CString s="";
	//m_change.GetLBText(0,s);
	//chanshu[0].c=s.GetAt(0);
	//chanshu[0].m_min=m_min;
//	chanshu[0].m_max=m_max;
	
	////////////////////////////////////////////////求精度/////////
	//m_xls=(int)(log10(save_x_sum*pow(10,m_xl))/log10(2));
	//m_yls=(int)(log10(save_y_sum*pow(10,m_yl))/log10(2));
	//if(save_z_sum)m_zls=(int)(log10(save_z_sum*pow(10,m_zl))/log10(2));
    //lchrom_x=m_xls;
	//lchrom_y=m_yls;
	//lchrom_z=m_zls;

	//lchrom_z=m_zls;
	lchrom=chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l+chanshu[5].l+chanshu[6].l;
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -