ITPub博客

首页 > IT职业 > IT生活 > 陈国良版SGA

陈国良版SGA

原创 IT生活 作者:chenjinglzz 时间:2007-10-07 15:08:19 0 删除 编辑

该程序摘自陈国良等编《遗传算法及其应用》

由于原程序有错误,本人在turbo c2.0下修改并调试通过

[@more@]/*----------------*/
/*-------Simple Genetic Algorithm------*/
/*---该程序摘自陈国良等编《遗传算法及其应用》--*/
/*-------------*/
#include
#include
#include
#include
#include
/*-------------*/
#include
#include
#include
#include
/*------------*/
#define maxpop 100
#define maxstring 64
/*----------*/
struct pp
{
unsigned char chrom[maxstring];
float x,fitness;
unsigned int parent1,parent2,xsite;
};
/*------------*/
struct pp *oldpop,*newpop,*p1;
unsigned int popsize,lchrom,gen,maxgen;
unsigned int nmutation,ncross,jcross,maxpp,minpp,jrand;
float pcross,pmutation,sumfitness,avg,max,min;
float seed,rj[maxpop],oldrand[maxpop];
double coef;
float objfun(float);
void statistics();
int select();
int flip(float);
int crossover();
int mutation();
void generation();
void initialize();
void report();
void initpop();
void initdata();
void report();
float decode();
float fandom1();
void randomize1();
/*-----------*/
/*SGA program*/
main()
{
long int gen,k,j;
float oldmax;
int oldmaxpp;
if(!(oldpop=(struct pp*)malloc(maxpop*sizeof(struct pp))))
{
printf("memory request failed!n");
exit(0);
}
if(!(newpop=(struct pp *)malloc(maxpop*sizeof(struct pp))))
{
printf("memory request failed!n");
exit(0);
}
if(!(p1=(struct pp *)malloc(maxpop*sizeof(struct pp))))
{
printf("memory request failed!n");
exit(0);
}
for(k=0;k oldpop[k].chrom[0]=';
for(k=0;k newpop[k].chrom[0]=';
gen=0;
initialize();
clrscr();
p1=newpop;
newpop=oldpop;
statistics(newpop);
report(gen);
newpop=p1;
getchar();
do{
gen=gen+1;
randomize();
oldmax=max;
oldmaxpp=maxpp;
generation();
statistics(newpop);
if(max {
for(j=0;j newpop[minpp].chrom[j]=oldpop[oldmaxpp].chrom[j];
newpop[minpp].x=oldpop[oldmaxpp].x;
newpop[minpp].fitness=oldpop[oldmaxpp].fitness;
statistics(newpop);
}
report(gen);
p1=oldpop;
oldpop=newpop;
newpop=p1;
getchar();
}while(gen free(p1);
free(oldpop);
free(oldpop);
exit(0);
}
/*//////////*/
/*个体适应度计算*/
float objfun(float x1)
{
float y;
y=3.1415926*x1;
y=sin(2.0*y);
return y*y;
}
/*//////////*/
/*群体适应度计算*/
void statistics(pop)
struct pp *pop;
{
int j;
sumfitness=pop[0].fitness;
min=pop[0].fitness;
max=pop[0].fitness;
maxpp=0;
minpp=0;
for(j=1;j {
sumfitness=sumfitness+pop[j].fitness;
if(pop[j].fitness>max)
{
max=pop[j].fitness;
maxpp=j;
}
if(pop[j].fitness {
min=pop[j].fitness;
minpp=j;
}
}
avg=sumfitness/(float)popsize;
}
/*///////////*/
/*群体更新*/
void generation()
{
unsigned int j,mate1,mate2;
j=0;
do{
mate1=select();
mate2=select();
crossover(oldpop[mate1].chrom,oldpop[mate2].chrom,j);
newpop[j].x=(float)decode(newpop[j].chrom);
newpop[j].fitness=objfun(newpop[j].x);
newpop[j].parent1=mate1;
newpop[j].parent2=mate2;
newpop[j].xsite=jcross;
newpop[j+1].x=(float)decode(newpop[j+1].chrom);
newpop[j+1].fitness=objfun(newpop[j+1].x);
newpop[j+1].parent1=mate1;
newpop[j+1].parent2=mate2;
newpop[j+1].xsite=jcross;
j=j+2;
}while(j}
/*//////////*/
/*控制参数输入*/
void initdata()
{
unsigned int ch,j;
clrscr();
printf("----------------n");
printf("A Simple Genetic Algorithms---SGA");
printf("----------------n");
pause();clrscr();
printf("********SGA DATA ENTRY AND INITIALIZATION********n");
printf("n");
printf("Enter population size------>");scanf("%d",&popsize);
printf("Enter chromosome length---->");scanf("%d",&lchrom);
printf("Enter max. generation------>");scanf("%d",&maxgen);
printf("Enter crossover probability---->");scanf("%f",&pcross);
printf("Enter mutatoion probability---->");scanf("%f",&pmutation);
clrscr();
randomize();
randomize1();
nmutation=0;
ncross=0;
}
/*//////////////*/
/*初始化信息输出*/
void initreport()
{
int j,k;
printf("------------------n");
printf("A Simple Genetic Algorithm--SGAn");
printf("SGA Parametersn");
printf("------------------n");
printf("n");
printf("Population size (popsize) = %dn",popsize);
printf("Chromosome length (lchrom) = %dn",lchrom);
printf("Maximum # of generation(maxgen) = %dn",maxgen);
printf("Crossover probability (pcross) = %fn",pcross);
printf("------------------n");
printf("n");
printf("Initial Population Maximum Fitness = %fn",max);
printf("Initial Population Average Fitness = %fn",avg);
printf("Initial Population Minimum Fitness = %fn",min);
printf("Initial Population Sum of Fitness = %fn",sumfitness);
pause();
}
/*////////////////*/
/*生成初始种群*/
void initpop()
{
int j,j1;
randomize();
for(j=0;j {
for(j1=0;j1 oldpop[j].chrom[j1]=random(2);
oldpop[j].x=(float)decode(oldpop[j].chrom);
oldpop[j].fitness=objfun(oldpop[j].x);
oldpop[j].parent1=0;
oldpop[j].parent2=0;
oldpop[j].xsite=0;
}
}
/*//////////////*/
/*初始化*/
void initialize()
{
initdata();
coef=pow(2.00,lchrom)-1.0;
initpop();
statistics(oldpop);
initreport();
}
/*///////////////*/
/*数据输出*/
void report(int gen)
{
int k,j;
for(j=0;j<79;j++)
printf("*");
printf("n");
printf("Population Reportn");
printf("Generation %3dn",gen);
printf(" # parents xsite string x fitnessn");
for(j=0;j<79;j++)
printf("+");
printf("n");
for(j=0;j {
printf("%2d ) ( %2d,",j,newpop[j].parent1);
printf("%2d ) %2d",newpop[j].parent2,newpop[j].xsite);
for(k=0;k printf(" %d ",newpop[j].chrom[k]);
printf(" %12.1f ",newpop[j].x);
printf(" %6.4f --New.n",newpop[j].fitness);
}
for(k=0;k<79;k++)
printf("*");
printf("n");
printf("RESULT: GEN: %3d",gen);
printf("AVG = %8.4f MIN = %8.4f MAX = %8.4fn",avg,min,max);
printf("ncross = %4d nmutation = %4d n",ncross,nmutation);
}
/*////////////////*/
/*译码*/
float decode(unsigned char *pp)
{
int j;float tt,tt1;
tt1=1.0;
tt=0.0;
for(j=lchrom-1;j>-1;j--)
{
if(pp[j])tt=tt+tt1;
tt1=2.0*tt1;
}
tt=tt/coef;
return tt;
}
/*//////////////*/
/*延时*/
int pause()
{
int j,j1;
int x1;
x1=0;
for(j=1;j<=25;j++)
{
for(j1=1;j1<2;j1++)
x1=x1+1;
}
return x1;
}
/*////////////////*/
/*选择操作*/
int select()
{
double rand1,partsum;
int j;
partsum=0.0;
j=0;
rand1=random1()*sumfitness;
do{
partsum=partsum+oldpop[j].fitness;
j=j+1;
}while((partsum return j-1;
}
/*/////////////////*/
/*变异操作*/
int mutation(ch)
unsigned char ch;
{
int mutate,j;
mutate=flip(pmutation);
if(mutate)
{
nmutation=nmutation+1;
if(ch)
ch=0;
else
ch=1;
}
if(ch)
return 1;
else
return 0;
}
/*/////////////////*/
/*交叉操作*/
int crossover(unsigned char *parent1,unsigned char *parent2,int k5)
{
int i,j,j1;
if(flip(pcross))
{
jcross=random(lchrom-1);
ncross=ncross+1;
}
else
jcross=lchrom;
if(jcross!=lchrom)
{
for(j=0;j {
newpop[k5].chrom[j]=mutation(parent1[j]);
newpop[k5+1].chrom[j]=mutation(parent2[j]);
}
for(j=jcross;j {
newpop[k5].chrom[j]=mutation(parent2[j]);
newpop[k5+1].chrom[j]=mutation(parent1[j]);
}
}
else
{
for(j=0;j {
newpop[k5].chrom[j]=mutation(parent1[j]);
newpop[k5+1].chrom[j]=mutation(parent2[j]);
}
}
return 1;
}
/*///////////////////*/
/*重启动随机数发生器*/
void randomize1()
{
int i;
randomize();
for(i=0;i oldrand[i]=random(30001)/30000.0;
jrand=0;
}
/*//////////////////*/
int random1()
{
jrand=jrand+1;
if(jrand>=lchrom)
{
jrand=0;
randomize1();
}
return oldrand[jrand];
}
/*///////////////////*/
/*Bernulli test */
int flip(float probability)
{
float ppp;
ppp=random(20001)/20000.0;
if(ppp<=probability)
return 1;
return 0;
}
/*///////////////////
/////end of the program
///////////////////*/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11355887/viewspace-974941/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 二分法(c++版)
请登录后发表评论 登录
全部评论

注册时间:2007-12-10

  • 博文量
    17
  • 访问量
    334612