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

📄 sgalib.cpp

📁 在软件测试的单元测试中
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	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 SGAlib::statistics(individual* pop) //计算种群统计数据
{
	int i,j;
	sumfitness=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.generation=gen+1;  
			bestfit.varible[0]=pop[j].varible[0];
			bestfit.varible[1]=pop[j].varible[1];
			bestfit.varible[2]=pop[j].varible[2];

		}
	}
	//计算平均适应度
	avg=sumfitness/popsize;
}

void SGAlib::repchar(FILE *outfp,char * ch,int repcount)
{
	for(int j=1;j<=repcount;j++)fprintf(outfp,"%s",ch);
}

void SGAlib::calcfitfunc(individual* critter) //解码计算适应度函数值
{
	unsigned mask=1;
	unsigned bitpos=0;
	unsigned tp=0;
	double bitpow=0.0;
	double val=0;
	int j,k,stop,L=0;
	char a[48];
	char pSideA[16],pSideB[16],pSideC[16]; //3个字符串(三条边长)
	char tmp[2]={' ',' '};
	critter->varible[0]=0;
	critter->varible[1]=0;
	critter->varible[2]=0;
	memset(a,' ',sizeof(char)*48);
	memset(pSideA,' ',sizeof(char)*16);
	memset(pSideB,' ',sizeof(char)*16);
	memset(pSideC,' ',sizeof(char)*16);
	
	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=pow(2,bitpos);
			    val=val+bitpow;
				a[lchrom-1-L]='1';   //末位放在最后
			}
			else
				a[lchrom-1-L]='0';
			tp=tp>>1;
			L++;
		}
	}
    for(k=0;k<(lchrom/3);k++)
	{
		int tmpA=0;
		int tmpB=0;
		int tmpC=0;
		strncpy(tmp,a+k,1);
		tmpA=atoi(tmp);
		critter->varible[0]+=(unsigned)pow(2,(L/3-1-k))*tmpA;
		strncpy(tmp,a+k+L/3,1);
		tmpB=atoi(tmp);
		critter->varible[1]+=(unsigned)pow(2,(L/3-1-k))*tmpB;
		strncpy(tmp,a+(L/3)*2+k,1);
		tmpC=atoi(tmp);
		critter->varible[2]+=(unsigned)pow(2,(L/3-1-k))*tmpC;

	}

	//把三个参数(范围限制在100之内)传入函数求的适应值	
	critter->varible[0]=critter->varible[0]-(critter->varible[0]/100)*100;
	critter->varible[1]=critter->varible[1]-(critter->varible[1]/100)*100;
	critter->varible[2]=critter->varible[2]-(critter->varible[2]/100)*100;
	
	critter->fitness=programtested.Triangle(critter->varible[0],critter->varible[1],critter->varible[2]);
	for(k=0;k<6;k++)
	{
		critter->PathValues[k]=programtested.PathValues[k];
	}
	

}

void SGAlib::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)) //以不大于pmutation的概率产生0或1
			{
				mask=mask|(temp<<j);
				nmutation++; //当前代变异产生次数   (一个个体中)
			}
		}
		child[k]=child[k]^mask;   //按位异或。
	}
}

int SGAlib::crossover(unsigned* parent1,unsigned* parent2,unsigned* child1,unsigned* child2 )
{
	unsigned j,jcross;
	int 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 SGAlib::randomize() //设定随机数种子并初始化随机数发生器
{
	float randomseed;
	int j1;
	for(j1=0;j1<=54;j1++)
	{
		oldrand[j1]=0.0;
	}
	jrand=0;
	do {
		cout<<"\n随机数种子[0-1]:";
		cin>>randomseed;
	} while((randomseed<0.0)||(randomseed>1.0));
	warmup_random(randomseed);
}

void SGAlib::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 SGAlib::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;
	}
}

int SGAlib::flip(float prob) //以一定概率产生0或1
{	
	if (randomperc()<=prob) return 1;
	else  return 0;
}

double SGAlib::randomnormaldeviate() //产生随机标准差
{
	double t,rndx1;
	if (rndcalcflag)
	{
		rndx1=sqrt(-2.0*log((double)randomperc()));
		t=6.2831853072*(double)randomperc();
		rndx2=rndx1*sin(t);
		rndcalcflag=0;
		return (rndx1*cos(t));
	}
	else
	{
		rndcalcflag=1;
		return rndx2;
	}
}

float SGAlib::randomperc()  //与库函数random()作用相同,产生[0,1]之间一个随机数
{
	jrand++;
	if (jrand>=55)
	{
		jrand=1;
		advance_random();
	}
	return (float)oldrand[jrand];	
}

int SGAlib::rnd(int low,int hight)
{
	int i;
	if (low>=hight)  i=low;
	else
	{
		i=(randomperc()*(hight-low+1))+low;
		if (i>hight) i=hight;
	}
	return i;
}

bool SGAlib::evaluate(individual* critter) //评价函数 分支覆盖100%?
{
	int flag[6]={0};
  //6个分支
	
	int intRaw=0;
	
	for(int j=0;j<6;j++) //6个分支
	{
		for(int i=0;i<popsize;i++)
		{
			if(critter[i].PathValues[j]==1)
			{
				flag[j]=1;  //该种群中只要有一个个体覆盖分支j,就标记为1。
				break;
			}
			else flag[j]=0;
		}
		intRaw+=flag[j];
	}
	fprintf(outfp,"覆盖分支数为:%d\n",intRaw);
	if(intRaw==6)  return true;  //判断是否全面覆盖分支
	else return false;
}



void SGAlib::printpathvalue()
{
	for(int i=0;i<popsize;i++)
	{
		for(int j=0;j<6;j++)
		{   
			fprintf(outfp," %d",oldpop[i].PathValues[j]);
		}
		fprintf(outfp,"\n");
	}
}

⌨️ 快捷键说明

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