遗传算法实例---------来个例子,大家好理解------------基于遗传算法的人工生命模拟#includestdio.h#includestdlib.h#includegraphics.h#includemath.h#includetime.h#includestring.h#includegraph.c/*宏定义*/#defineTL120/*植物性食物限制时间*/#defineTL25/*动物性食物限制时间*/#defineNEWFOODS3/*植物性食物每代生成数目*/#defineMUTATION0.05/*变异概率*/#defineG_LENGTH32/*个体染色体长度*/#defineMAX_POP100/*个体总数的最大值*/#defineMAX_FOOD100/*食物总数的最大值*/#defineMAX_WX60/*虚拟环境的长度最大值*/#defineMAX_WY32/*虚拟环境的宽度最大值*/#defineSX1330/*虚拟环境图左上角点x坐标*/#defineSY140/*虚拟环境图左上角点y坐标*/#defineGX360/*个体数进化图形窗口的左上角点X坐标*/#defineGY257/*个体数进化图形窗口的左上角点Y坐标*/#defineGXR250/*个体数进化图形窗口的长度*/#defineGYR100/*个体数进化图形窗口的宽度*/#defineGSTEP2/*个体数进化图形窗口的X方向步长*/#defineR_LIFE0.05/*初期产生生物数的环境比率*/#defineR_FOOD0.02/*初期产生食物数的环境比率*/#defineSL_MIN10/*个体寿命最小值*//*全局变量*/unsignedchargene[MAX_POP][G_LENGTH];/*遗传基因*/unsignedchariflg[MAX_POP];/*个体死活状态标志变量*/unsignedcharfflg[MAX_FOOD];/*食物有无状态标志变量*/unsignedcharworld[MAX_WX][MAX_WY];/*虚拟环境的数据*/unsignedchar/*各中行为模式数据*/life1[5][5]={{0,0,1,0,0},{0,1,0,1,0},{1,0,0,0,1},{0,1,0,1,0},{0,0,1,0,0}};unsignedcharlife2[5][5]={{1,1,1,1,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,1}};unsignedcharfood1[5][5]={{0,0,0,1,0},{0,0,1,1,0},{0,1,0,1,0},{0,0,1,1,0},{0,0,0,1,0}};unsignedcharfood2[5][5]={{0,0,0,1,0},{0,0,1,1,0},{0,1,1,1,0},{0,0,1,1,0},{0,0,0,1,0}};intpop_size;/*个体总数*/intiatr[MAX_POP][4];/*个体属性*//*iatr[][0]个体当前位置x坐标*//*iatr[][1]个体当前位置y坐标*//*iatr[][2]内部能量*//*iatr[][3]年龄属性*/intfood_size;/*食物总数*/intfatr[MAX_FOOD][4];/*食物属性*//*fatr[][0]食物当前位置x坐标*//*fatr[][1]食物当前位置y坐标*//*fatr[][2]=0:植物性=1:动物性*//*fatr[][3]新鲜程度*/intwx,wy;/*虚拟环境的长宽度*/voiduni_crossover(gene,g1,g2,g3,ratio1,g_length)/*均匀交叉*/unsignedchar*gene;/*遗传基因*/intg1,g2,g3;/*g1g2父个体编号g3子个体编号*/doubleratio1;/*父个体g1被选中的概率*/intg_length;/*个体遗传基因的位长*/{unsignedchar*gene1;/*父1遗传基因的指针*/unsignedchar*gene2;/*父2遗传基因的指针*/unsignedchar*gene3;/*子遗传基因的指针*/doublernd,r1;inti;gene1=gene+g_length*g1;gene2=gene+g_length*g2;gene3=gene+g_length*g3;r1=(int)(10000.0*ratio1);for(i=0;ig_length;i++){rnd=random(10000);if(rnd=r1)*(gene3+i)=*(gene1+i);else*(gene3+i)=*(gene2+i);}}voidg_disp_unit(x,y,n)/*绘制虚拟环境的一个单元*/intx,y;/*x=0,1,2....,wx-1;y=0,1,2,....,wy-1*/intn;/*n=0:=1:生物1=2:生物2=3:植物性食物=4:障碍物=5:动物性食物*/{intgx,gy,i,j;unsignedcharcol;gx=SX1+5*x;gy=SY1+5*y;for(i=0;i5;i++)for(j=0;j5;j++){switch(n){case0:col=0;break;case1:col=life1[j][i]*2;break;case2:col=life2[j][i]*4;break;case3:col=food1[j][i]*6;break;case4:col=7;break;case5:col=food2[j][i]*11;}g_pset(gx+j,gy+i,col);}}voidg_draw_world()/*显示虚拟环境画面*/{inti,j;for(i=0;iwy;i++)for(j=0;jwx;j++)g_disp_unit(j,i,world[j][i]);}voidg_draw_frame(x1,y1,x2,y2,c1,c2,c3,c4,text)intx1,y1,x2,y2,c1,c2,c3,c4;char*text;{intn,x3;g_rectangle(x1,y1,x2,y2,c1,1);g_rectangle(x1,y1,x2,y2,c2,0);g_rectangle(x1,y1,x2,y1+16,c3,1);g_rectangle(x1,y1,x2,y1+16,c2,0);n=strlen(text);x3=x1+((x2-x1-n*8)/2);disp_hz16(text,x3,y1,c4);}voidg_init_frames()/*初始化画面*/{inti,j,cx,cy,x,y;chartext[17];g_draw_frame(0,0,639,399,15,0,4,15,基于遗传算法的人工生命模拟);g_draw_frame(0,16,320,170,7,0,8,15,设定参数);y=48;setcolor(9);disp_hz16(植物食物限制时间,16,y,15);sprintf(text,%d,TL1);g_text(200,y+8,4,text);y=y+24;setcolor(9);disp_hz16(动物食物限制时间,16,y,15);sprintf(text,%d,TL2);g_text(200,y+8,4,text);y=y+24;setcolor(9);disp_hz16(植物食物每代生成个数,16,y,15);sprintf(text,%d,NEWFOODS);g_text(200,y+8,4,text);y=y+24;setcolor(9);disp_hz16(变异概率,16,y,15);i=(int)(MUTATION*100.0);sprintf(text,%d,i);g_text(152,y+8,4,text);g_draw_frame(0,170,320,399,7,0,8,15,最佳基因型);x=16;y=194;setcolor(9);disp_hz16(SP:物种号........,x,y,15);y=y+16;disp_hz16(SL:寿命..........,x,y,15);y=y+16;disp_hz16(VF:视野..........,x,y,15);y=y+16;disp_hz16(TM:基本移动模式..,x,y,15);y=y+16;disp_hz16(CM:移动特点......,x,y,15);y=y+16;disp_hz16(LM:移动能耗......,x,y,15);y=y+16;disp_hz16(CA:行动特点......,x,y,15);y=y+16;disp_hz16(CR:善变性........,x,y,15);y=y+16;disp_hz16(SA:攻击速度......,x,y,15);y=y+16;disp_hz16(DA:防御能力......,x,y,15);y=y+16;disp_hz16(LA:攻击能耗......,x,y,15);y=y+16;disp_hz16(EF:食物吸取效率..,x,y,15);y=y+16;g_draw_frame(320,16,639,207,7,0,8,15,虚拟世界);g_draw_frame(320,207,639,399,7,0,8,15,世代个体数目变化);}voidg_init_graph()/*个体数进化图初始化*/{g_rectangle(GX,GY,GX+GXR,GY+GYR,0,1);g_rectangle(GX,GY,GX+GXR,GY+GYR,6,0);setcolor(1);disp_hz16(生物1,GX+5,GY-18,15);g_line(GX+90,GY-10,GX+110,GY-10,1);setcolor(4);disp_hz16(生物2,GX+120,GY-18,15);g_line(GX+205,GY-10,GX+225,GY-10,4);setcolor(0);disp_hz16(世代数,GX+168,GY+GYR+10,15);g_text(GX-25,GY,0,100);g_text(GX-14,GY+GYR,0,0);}voidg_plot_population(gen_num,n1,n2,n1old,n2old)intgen_num,n1,n2,n1old,n2old;{intx,y,gx,gy,x_old,y_old;chartext[8];if(gen_num%10==0){x=GX+(gen_num-1)*GSTEP;g_line(x,GY+1,x,GY+GYR-1,1);sprintf(text,%d,gen_num);if(gen_num100||gen_num%20==0)g_text(x-8,GY+GYR+5,15,text);}x_old=GX+(gen_num-1)*GSTEP;x=x_old+GSTEP;y_old=GY+GYR-n1old;y=GY+GYR-n1;g_line(x_old,y_old,x,y,1);y_old=GY+GYR-n2old;y=GY+GYR-n2;g_line(x_old,y_old,x,y,4);}voidg_disp_genotype()/*显示最佳个体的遗传基因型*/{inti,j,n0,n1,x,y;unsignedcharg[G_LENGTH];unsignedcharbits[12][2]={{0,0},{1,4},{5,6},{7,8},{9,11},{12,12},{13,15},{16,18},{19,21},{22,24},{25,27},{28,31}};/*画面消除*/g_rectangle(200,187,319,398,7,1);if(pop_s