遗传算法的c程序

本文介绍了一个使用C语言实现的遗传算法示例,通过定义种群规模、染色体长度及最大迭代次数等参数来优化目标函数。该算法包含了选择、交叉与变异等基本遗传操作,并记录了每一代的最佳解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




程序代码:(代码标记[code]...[/code])

#include
<iostream.h>
#include
<math.h>
#include
<time.h>
//#include<graphics.h>
#include<stdlib.h>
#include
<string.h>

#definemaxpop100
#definemaxstring32

typedef
struct...{
charchrom[maxstring];
floatx,fitness;
intparent1,parent2,xsite;
}
pp;

pp
*oldpop,*newpop,*p1;
intpopsize,lchrom,gen,maxgen,nmutation,ncross,jcross,maxpp,minpp,jrand;
floatpcross,pmutation,sumfitness,avg,max,min,seed,rj[maxpop],oldrand[maxpop];
doublecoef;

floatobjfunc(float);
intselect();
intflip(float);
intcrossover(char*,char*,int);
charmutation();
voidgeneration();
voidinitialize();
voidreport();
voidinitpop();
voidinitdata();
voidinitreport();
doubledecode(char*);
floatrandom1();
voidrandomize1();
voidpause();

voidtest(charx)
...{cout<<x<<' ';}

floatobjfunc(floatx1)
//computeobjectfitness;
...{
floaty;
y
=3.14*x1;
y
=sin(2.0*y);
returny*y;
}


voidstatistic(pp*pop)
//statisticthefitnessofpopulation
...{
intj;
sumfitness
=pop[0].fitness;
max
=pop[0].fitness;
min
=pop[0].fitness;
maxpp
=0;
minpp
=0;
for(j=1;j<popsize;j++)...{
sumfitness
=sumfitness+pop[j].fitness;
if(pop[j].fitness>max)...{
max
=pop[j].fitness;
maxpp
=j;
}

if(pop[j].fitness<min)...{
min
=pop[j].fitness;
minpp
=j;
}

}
//endfor
avg=sumfitness/(float)popsize;
}


voidgeneration()
//updateageneration;
...{
intj,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
=objfunc(newpop[j].x);
newpop[j].parent1
=mate1;
newpop[j].parent2
=mate2;
newpop[j].xsite
=jcross;//recodethecrosspoint;
newpop[j+1].x=(float)decode(newpop[j+1].chrom);
newpop[j
+1].fitness=objfunc(newpop[j+1].x);
newpop[j
+1].parent1=mate1;
newpop[j
+1].parent2=mate2;
newpop[j
+1].xsite=jcross;
j
=j+2;
}
while(j<popsize);
}


voidinitdata()
//inputcontrolparameters
...{
intch,j;
cout
<<"*********SGADATAENTRYANDINITIALIZATION******* ";
cout
<<"Enterpopulationsize:";
cin
>>popsize;
//cout<<"Enterchromosomelength:";
//cin>>lchrom;
lchrom=32;
cout
<<"Entermaxgenerations";
cin
>>maxgen;
cout
<<"Entercrossoverprobability:";
cin
>>pcross;
cout
<<"Entermutationprobability:";
cin
>>pmutation;

//randomize1();
nmutation=0;
ncross
=0;
}


voidinitreport()
...{
cout
<<"Populationsize:"<<popsize<<' ';
//cout<<"Chromosomelength:"<<lchrom<<' ';
cout<<"Maximum#ofgeneration:"<<maxgen<<' ';
cout
<<"Crossoverprobability:"<<pcross<<' ';
cout
<<"Mutationprobability:"<<pmutation<<' ';
cout
<<"----------------------------------------------------- ";
cout
<<"InitialPopulationMaximumFitness:"<<max<<' ';
cout
<<"InitialPopulationAverageFitness:"<<avg<<' ';
cout
<<"InitialPopulationMinimunFitness:"<<min<<' ';
cout
<<"InitialPopulationSumofFitness:"<<sumfitness<<' ';
pause();
}


voidinitpop()
...{
srand((unsigned)time(NULL));
intbit;
oldpop
=newpp[popsize];
newpop
=newpp[popsize];
intj,j1;
for(j=0;j<popsize;j++)...{
for(j1=0;j1<lchrom;j1++)...{
if(rand()%2==1)
oldpop[j].chrom[j1]
='1';
elseoldpop[j].chrom[j1]='0';
}

oldpop[j].x
=(float)decode(oldpop[j].chrom);
oldpop[j].fitness
=objfunc(oldpop[j].x);
oldpop[j].parent1
=0;
oldpop[j].parent2
=0;
oldpop[j].xsite
=0;
}
//endfor
}


voidinitialize()
...{
initdata();
//test('a');
coef=pow(2.00,lchrom)-0.1;
initpop();
statistic(oldpop);
initreport();
}


voidreport(intgen)
...{
intk,j;
cout
<<"****************************************************** ";
cout
<<"Generation:"<<gen<<' ';
cout
<<"#parentsxsitestringxfitness ";
for(j=0;j<popsize;j++)
...{
cout
<<j<<" "<<newpop[j].parent1<<' '<<newpop[j].parent2<<' '<<newpop[j].xsite<<' ';
for(k=0;k<lchrom;k++)cout<<newpop[j].chrom[k];
cout
<<' '<<newpop[j].x<<' '<<newpop[j].fitness<<' ';
}

cout
<<"******************************************************** ";
cout
<<"ResultGen:"<<gen<<' ';
cout
<<"AVG="<<avg<<' '<<"MIN="<<min<<' '<<"MAX="<<max<<' ';
cout
<<"ncross="<<ncross<<' '<<"nmutation="<<nmutation<<' ';
}


intselect()
...{
doublerand1,partsum;
intj;
partsum
=0.0;
j
=0;
rand1
=random1()*sumfitness;
do...{
partsum
=partsum+oldpop[j].fitness;
j
=j+1;
}
while((partsum<rand1)&&(j<popsize));
returnj-1;
}


doubledecode(char*pp)
//decodechromtotherealnumber
ContractedBlock.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值