人工智能(遗传算法)
人工智能
2
人工智能
3
人工智能
4
程序代码
#include<>
#include<>
#include<>
typedef struct
{
int code; //染色体
int degree;//适应度
人工智能
5
}Indi;
Indi group[40];//种群规模为40
void Judge(Indi &x)
{
=*;
}
int happened(double p)//发生一个p=0~1间概率的事件
{
return rand()<(int)(p*RAND_MAX);
}
void Cross(Indi &x,Indi &y)//交叉操作
{
Indi z,z1;
int temp,temp1;
temp=&0x3;
temp1=&0x3;
=-temp+temp1;
=-temp1+temp;
Judge(z);
Judge(z1);
if(<)
{
if(>=) //如果新个体不如双亲,淘汰之
x=z;
}
else
{
if(>=)
y=z;
}
if(<)
{
if(>=) //如果新个体不如双亲,淘汰之
x=z1;
}
else
{
if(>=)
人工智能
6
y=z1;
}
}
int main(void)
{
Indi indidest;
int i,j,best,x,y,c;
int sum,strick,SUM=0;
static int n=0;
srand(time(NULL));
for(i=0;i<40;++i)//随机得到初始种群
{
group[i].code=rand()%32;
Judge(group[i]);
}
for(i=1;i<=10;++i)//固定进化10代
{
for(sum=0,best=0,j=0;j<40;++j)
{
sum+=group[j].degree;//求总的适应度sum
if(group[j].degree>group[best].degree)
{
best=j;//求当前最优个体
}
}
printf("第%2d代中 最优个体为 %d (%d) 平均适应度为 %10f\n",
i,group[best].code,group[best].degree,sum/);
for(c=40;c;--c)
{
strick=(int)((float)ran
人工智能(遗传算法) 来自淘豆网m.daumloan.com转载请标明出处.