📄 main.c
字号:
{
unsigned j;
sumfitness = 0;
for(j = 0; j < popsize; j++) sumfitness += oldpop[j].fitness*oldpop[j].fitness;
}
/********************************************************************
轮盘赌选择
*********************************************************************/
int select()
{
extern float randomperc();
double sum, pick;
unsigned i;
pick = randomperc();
sum = 0;
if(sumfitness != 0)
{
for(i = 0; (sum < pick) && (i < popsize); i++)
sum += oldpop[i].fitness*oldpop[i].fitness/sumfitness;
}
else
i = rnd(1,popsize);
return(i-1);
}
/********************************************************************
对适应度高的个体编码进行简化
*********************************************************************/
void selectbest1(struct bestever *critter)
{
unsigned temp,i;
char mask;
temp=0x01;
for(i=chromsize-1;i>0;i--)
{
mask=0x55;
if(((mask&critter->chrom[i-1])<<1)&(critter->chrom[i-1]))
critter->chrom[chromsize-1]=critter->chrom[chromsize-1]&(~temp);
if((critter->chrom[chromsize-1]&(temp))==0)
critter->chrom[i-1]=0;
temp=temp<<1;
}
}
/********************************************************************
对适应度相同的个体编码进行比较,存取最简的
*********************************************************************/
void selectbest2()
{
unsigned k,flag1=0,flag2=0;
char i,stop,temp,mask;
for(k=0;k<chromsize;k++)
{
if(k == (chromsize-1))
{
stop = lchrom - (k*(8*sizeof(char)));
mask = mask >>((8*sizeof(char))-stop);
}
else
stop = 8*sizeof(char);
temp=0x01;
for(i=0;i<stop;i++)
{
if(bestfit[run].chrom[k]&(temp))
flag1++;
if(bestfit[maxruns].chrom[k]&(temp))
flag2++;
temp=temp<<1;
}
}
if(flag2<flag1)
{
for(k = 0; k < chromsize; k++)
bestfit[run].chrom[k] = bestfit[maxruns].chrom[k];
bestfit[run].fitness = bestfit[maxruns].fitness;
bestfit[run].generation = bestfit[maxruns].generation;
}
}
/********************************************************************
计算种群统计数据
*********************************************************************/
void statistics(struct individual *pop)
{
unsigned i, j;
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[run].fitness)
{
for(i = 0; i < chromsize; i++)
bestfit[run].chrom[i] = pop[j].chrom[i];
bestfit[run].fitness = pop[j].fitness;
bestfit[run].generation = gen;
selectbest1(&(bestfit[run]));
}
if(pop[j].fitness == bestfit[run].fitness)
{
for(i = 0; i < chromsize; i++)
bestfit[maxruns].chrom[i] = pop[j].chrom[i];
bestfit[maxruns].fitness = pop[j].fitness;
bestfit[maxruns].generation = gen+1;
selectbest1(&(bestfit[maxruns]));
selectbest2();
}
}
/* 计算平均适应度 */
avg = sumfitness/popsize;
}
/********************************************************************
在文件中输出int个字符
*********************************************************************/
void repchar (FILE *outfp,char *ch,int repcount)
{
int j;
for (j = 1; j <= repcount; j++) fprintf(outfp,"%s", ch);
}
/********************************************************************
在输出文件中换行
*********************************************************************/
void skip(FILE *outfp,int skipcount)
{
int j;
for (j = 1; j <= skipcount; j++) fprintf(outfp,"\n");
}
/********************************************************************
计算个体对应的真值表
*********************************************************************/
void calculateZZB(struct individual *critter)
{
unsigned X1,X0,Y1,Y0,SUM;
unsigned F[10]={0};
unsigned i,j,k;
for(j=0;j<16;j++)
{
SUM = 0;
X1 = SHURU[j][0];
X0 = SHURU[j][1];
Y1 = SHURU[j][2];
Y0 = SHURU[j][3];
for(i = 0; i < chromsize-1; i++)
{
F[i] = 1;
k=0x01<<i;
if(critter->chrom[chromsize-1]&k)
{
if(critter->chrom[i]&0x80)
F[i] = F[i]&X1;
if(critter->chrom[i]&0x40)
F[i] = F[i]&(~X1);
if(critter->chrom[i]&0x20)
F[i] = F[i]&(X0);
if(critter->chrom[i]&0x10)
F[i] = F[i]&(~X0);
if(critter->chrom[i]&0x08)
F[i] = F[i]&(Y1);
if(critter->chrom[i]&0x04)
F[i] = F[i]&(~Y1);
if(critter->chrom[i]&0x02)
F[i] = F[i]&(Y0);
if(critter->chrom[i]&0x01)
F[i] = F[i]&(~Y0);
}
SUM=SUM|F[i];
}
critter->ZZ[j] = SUM;
}
}
/********************************************************************
计算适应度函数值
*********************************************************************/
void objfunc(struct individual *critter)
{
unsigned i;
double cout=0.0;
for(i=0;i<16;i++)
{
if(critter->ZZ[i]==ZZB[i][run])
cout=cout+1.0;
}
critter->fitness=cout/16.0;
}
/********************************************************************
变异操作
*********************************************************************/
void mutation(char *child)
{
unsigned j, k, stop;
unsigned mask=0x80, temp;
for(k = 0; k < chromsize; k++)
{
temp = 0;
if(k == (chromsize-1))
{
stop = lchrom - (k*(8*sizeof(char)));
mask = mask >>((8*sizeof(char))-stop);
}
else
stop = 8*sizeof(char);
for(j = 0; j < stop; j++)
{
if(flip(pmutation))
{
temp = temp|(mask>>j);
nmutation++;
}
}
child[k] = child[k]^temp;
}
}
/********************************************************************
由两个父个体交叉产生两个子个体
*********************************************************************/
int crossover (char *parent1, char *parent2, char *child1, char *child2)
{
unsigned 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(char))))
{
child1[k-1] = parent1[k-1];
child2[k-1] = parent2[k-1];
}
else if((jcross < (k*(8*sizeof(char)))) && (jcross > ((k-1)*(8*sizeof(char)))))
{
mask = 1;
for(j = 1; j <= (jcross-1-((k-1)*(8*sizeof(char)))); 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);
}
/********************************************************************
以一定概率产生0或1
*********************************************************************/
int flip(float prob)
{
float randomperc();
if(randomperc() <= prob)
return(1);
else
return(0);
}
/********************************************************************
产生[0,1]之间一个随机数
*********************************************************************/
float randomperc()
{
float a;
a=(float)rand()/0x7fff;
return(a);
}
/********************************************************************
在整数low和high之间产生一个随机整数
*********************************************************************/
int rnd(low, high)
int low,high;
{
int i;
float randomperc();
if(low >= high)
i = low;
else
{
i = (int)(randomperc() * (high - low + 1)) + low;
if(i > high) i = high;
}
return(i);
}
/********************************************************************
主程序
*********************************************************************/
main()
{
char outfile1[20],infile[20],outfile2[20];
unsigned i,j;
struct individual *temp;
FILE *fopen();
printf("Enter the file name which comtain the Zhenzhibiao: ");
scanf("%s",infile);
if((infp = fopen(infile,"r")) == NULL)
{
fprintf(stderr,"Cannot open input file %s\n",outfile1);
exit(-1);
}
printf("Enter a file name for the detailed information: ");
scanf("%s",outfile1);
if((outfp = fopen(outfile1,"w")) == NULL)
{
fprintf(stderr,"Cannot open output file %s\n",outfile1);
exit(-1);
}
printf("Enter a file name for the final information: ");
scanf("%s",outfile2);
if((outfp2 = fopen(outfile2,"w")) == NULL)
{
fprintf(stderr,"Cannot open output file %s\n",outfile1);
exit(-1);
}
printf("How many outputs does the genetic algorithm have ? ");
scanf("%d",&maxruns);
for(i=0;i<16;i++)
{
for(j=0;j<maxruns;j++)
{
fscanf(infp,"%d",&ZZB[i][j]);
}
}
initdata();
fprintf(outfp2,"遗传算法最终得到的染色体编码为:\n");
for(run=0; run<maxruns; run++)
{
initialize();
for(gen=0; gen<maxgen; gen++)
{
fprintf(outfp,"\n第 %d / %d 次运行: 当前代为 %d, 共 %d 代\n", run+1,maxruns,gen+1,maxgen);
fflush(outfp);
/* 产生新一代 */
generation();
/* 计算新一代种群的适应度统计数据 */
statistics(newpop);
/* 输出新一代统计数据 */
report();
temp = oldpop;
oldpop = newpop;
newpop = temp;
/*跑一次,扔掉一个随机数*/
randomperc();
}
writechrom(outfp2,(&bestfit[run])->chrom);
fprintf(outfp2," 适应度:%f\n", bestfit[run].fitness);
freeall();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -