?? ga.c
字號:
pat=decode_gene(n,7,2);
/* pat(0,1,2,3): 表示基本移動模式 */
x1=iatr[n][0]; /* 當前x坐標 */
y1=iatr[n][1]; /* 當前y坐標 */
if(pat<=1) /* 基本移動模式1 */
{
rndnum=random(8);
x2=x1+pat1[rndnum][0]; /* 移動目的點x坐標 */
y2=y1+pat1[rndnum][1]; /* 移動目的點y坐標 */
}
else /* 基本移動模式2與3 */
{
rndnum=random(4);
x2=x1+pat2_3[pat-2][rndnum][0];
y2=y1+pat2_3[pat-2][rndnum][1];
}
if(x2>=0 && x2<wx && y2>=0 && y2<wy)
if(get_world(x2,y2)==0)
move_pos(n,x1,y1,x2,y2);
/* 非法目的點的場合不作移動 */
void move_individual(n) /* 個體n移動 */
int n;
{
int cx,cy,dx,dy,sp,vf,sumx,sumy;
int i,j,a,sgn[3],num;
double vect[8][2]={{1,0},{1,1},{0,1},{-1,1},
{-1,0},{-1,-1},{0,-1},{1,-1}};
double vx,vy,d1,d2;
double _cos,cos_max;
cx=iatr[n][0]; /* 當前x坐標 */
cy=iatr[n][1]; /* 當前y坐標 */
sp=decode_gene(n,0,1)+1; /* 生物種1和2 */
for(i=0;i<3;i++) /* 移動特點CM */
{
sgn [ i]=decode_gene(n,9+i,1);
if(sgn [ i]==0) sgn [ i]=-1;
}
sumx=0;sumy=0;num=0;
vf=decode_gene(n,5,2)+1; /* 視野 */
for(i=-vf;i<=vf;i++)
for(j=-vf;j<=vf;j++)
{
if(i!=0||j!=0)
{
a=get_world(cx+j,cy+i);
if(a==1||a==2) /* 生物 1和2 */
{ num++;
if(a==sp) /* 同種生物 */
{
sumx=sumx+sgn[0]*j;
sumy=sumy+sgn[0]*i;
}
else /* 異種生物 */
{
sumx=sumx+sgn[1]*j;
sumy=sumy+sgn[1]*i;
}
} else
if(a==3||a==5) /* 食物 */
{
num++;
sumx=sumx+sgn[2]*j;
sumy=sumy+sgn[2]*i;
}
}
}
if(num!=0) /* 視野內有其他生物和食物時 */
{
vx=(double)sumx/(double)num;
vy=(double)sumy/(double)num;
if(vx!=0||vy!=0)
{
cos_max=-1.0;
j=0;
for(i=0;i<8;i++)
{
d1=sqrt(vx*vx+vy*vy);
d2=sqrt(vect [ i][0]*vect [ i][0]+vect [ i][1]*vect [ i][1]);
_cos=(vx*vect [ i][0]+vy*vect [ i][1])/d1/d2;
if(_cos>cos_max)
{
cos_max=_cos;j=i;
}
}
dx=cx+(int)vect[j][0];
dy=cy+(int)vect[j][1];
if(dx>=0 && dx<wx && dy>=0 && dy<wy)
if(world[dx][dy]==0)
move_pos(n,cx,cy,dx,dy);
}
else move_randomly(n);
}
else move_randomly(n);
/* 視野內有其他生物和食物時 */
}
void act1_attack(n) /* 個體 n攻擊行動范圍內的其他生物個體 */
int n;
{
int sft[8][2]={{1,0},{1,1},{0,1},{-1,1},
{-1,0},{-1,-1},{0,-1},{1,-1}};
int x1,y1,x2,y2,n2;
int found,rndnum;
double attack1,attack2,sa1,sa2,da1,da2,rnd1,rnd2,La1,La2;
x1=iatr[n][0];y1=iatr[n][1];
/* 獲得攻擊對象的坐標(x2,y2) */
found=0;
while(found==0)
{
rndnum=random(8);
x2=x1+sft[rndnum][0];
y2=y1+sft[rndnum][1];
if(get_world(x2,y2)==1||get_world(x2,y2)==2)
found=1;
}
/* 檢查攻擊對象個體號n2 */
found=0;n2=0;
while(found==0)
{
if(iatr[n2][0]==x2 && iatr[n2][1]==y2 && iflg[n2]==1)
found=1; else n2++;
}
/* 計算雙方的 Attack量 */
sa1=(double)decode_gene(n,19,3);
da1=(double)decode_gene(n,22,3);
sa2=(double)decode_gene(n2,19,3);
da2=(double)decode_gene(n2,22,3);
rnd1=(double)random(1001)/1000.0;
rnd2=(double)random(1001)/1000.0;
attack1=(double)iatr[n][2]+sa1*20.0/7.0*rnd1+da1*20.0/7.0*rnd2;
rnd1=(double)random(1001)/1000.0;
rnd2=(double)random(1001)/1000.0;
attack2=(double)iatr[n2][2]+sa2*20.0/7.0*rnd1+da2*20.0/7.0*rnd2;
/* 減少內部能量 */
La1=decode_gene(n,25,3);
La2=decode_gene(n2,25,3);
rnd1=(double)random(1001)/1000.0;
iatr[n][2]=iatr[n][2]-(int)((double)La1*rnd1);
rnd2=(double)random(1001)/1000.0;
iatr[n2][2]=iatr[n2][2]-(int)((double)La2*rnd2);
if(attack1>=attack2) /* 勝者: n 敗者:n2 */
iatr[n2][2]=iatr[n2][2]-40;
else /* 勝者: n2 敗者:n */
iatr[n][2]=iatr[n][2]-40;
if(iatr[n][2]<=0) remove_life(n);
if(iatr[n2][2]<=0) remove_life(n2);
}
void act2_eat(n) /* 個體n獲取行動范圍內的食物 */
int n;
{
int sft[8][2]={{1,0},{1,1},{0,1},{-1,1},
{-1,0},{-1,-1},{0,-1},{1,-1}};
int x1,y1,x2,y2,n2,ef;
int found,rndnum;
x1=iatr[n][0];y1=iatr[n][1];
/* 獲取食物位置(x2,y2) */
found=0;
while(found==0)
{
rndnum=random(8);
x2=x1+sft[rndnum][0];
y2=y1+sft[rndnum][1];
if(get_world(x2,y2)==3||get_world(x2,y2)==5)
found=1;
}
/* 增加內部能量 */
ef=decode_gene(n,28,4); /* 食物吸取效率 */
iatr[n][2]=iatr[n][2]+(int)(40.0*(50.0+(double)ef*50.0/15.0)/100.0);
if(iatr[n][2]>100) iatr[n][2]=100;
/* 檢查食物號n2 */
found=0;n2=0;
while(found==0)
{
if(fatr[n2][0]==x2 && fatr[n2][1]==y2 && fflg[n2]==1)
found=1; else n2++;
}
remove_food(n2);
}
void act3_makechild(n) /* 個體n與行動范圍內的其他生物個體交配產生子個體 */
int n;
{
int i,j,k,x,y,x2,y2,found,n2,trial;
int x3,y3;
double rnd;
if(pop_size+1<=MAX_POP)
{
x=iatr[n][0];y=iatr[n][1];
found=0;
while(found==0)
{
x2=x+random(3)-1;
y2=y+random(3)-1;
if(x2!=x||y2!=y)
if(get_world(x2,y2)==gene[n][0]+1);
found=1;
}
/* 檢查交配對象個體號n2 */
found=0; n2=0;
while(found==0)
{
if((iatr[n2][0]==x2 || iatr[n2][1]==y2) && iflg[n2]==1)
found=1; else n2++;
if(n2>=pop_size-1) return;
}
/* 確定產生個體位置 */
found=0;trial=0;
while(found==0 && trial<50)
{
i=random(3)-1;j=random(3)-1;
k=random(2);
if(k==0) { x3=x+i;y3=y+j;}
else { x3=x2+i;y3=y2+j;}
if(get_world(x3,y3)==0) found=1;
trial++;
}
if(found==1)
{
/* 個體 n與個體 n2產生子個體 */
pop_size++;
/* 均勻交叉 */
uni_crossover(gene,n,n2,pop_size-1,0.5,G_LENGTH);
/* 變異 */
for(i=1;i<G_LENGTH;i++)
{
rnd=random(10001)/10000.0;
if(rnd<=MUTATION)
if(gene[pop_size-1] [ i]==1)
gene[pop_size-1] [ i]=0;
else gene[pop_size-1] [ i]=1;
}
/* 交配后父個體能量減少 */
iatr[n][2]=iatr[n][2]-45;
if(iatr[n][2]<=0) remove_life(n);
iatr[n2][2]=iatr[n2][2]-45;
if(iatr[n2][2]<=0) remove_life(n2);
/* 子個體屬性輸入 */
iatr[pop_size-1][0]=x3;
iatr[pop_size-1][1]=y3;
iatr[pop_size-1][2]=100;
iatr[pop_size-1][3]=0;
iflg[pop_size-1]=1;
/* 子個體畫面表示 */
world[x3][y3]=gene[pop_size-1][0]+1;
g_disp_unit(x3,y3,gene[pop_size-1][0]+1);
}
}
}
void act_individual(n) /* 為行動范圍內的其他生物和食物決定行動 */
int n;
{
int i,j,k,pattern,action,cr,ca;
int act[3]; /* act[0]:攻擊 act[1]:獲取食物 act[2]:交配 */
int pat[6][3]={{1,2,3},{1,3,2},{2,1,3},
{3,1,2},{2,3,1},{3,2,1}};
/* pat:行動優先順序 {攻擊,獲取食物,交配} */
int sp;
double rnd;
sp=decode_gene(n,0,1)+1;
for(i=0;i<3;i++) act [ i]=0;
for(i=-1;i<=1;i++)
for(j=-1;j<=1;j++)
{
if(i!=0||j!=0)
{
k=get_world(iatr[n][0]+j,iatr[n][1]+i);
if(k==1||k==2) act[0]=1;
if(k==3||k==5) act[1]=1;
if(k==sp) act[2]=1;
}
}
cr=decode_gene(n,16,3);
rnd=(double)random(10001)/10000.0;
if(rnd<=(double)cr/7.0)
{
action=random(3);
while(act[action]==0)
action=random(3);
}
else
{
ca=decode_gene(n,13,3); /* ca行動特點 */
if(ca<3) pattern=0;else pattern=ca-2;
/* 基本行動模式pattern 0-5 */
i=0;
action=pat[pattern] [ i]-1;
while( act[action]==0)
{
i++;
action=pat[pattern] [ i]-1;
}
}
switch(action+1)
{
case 1: act1_attack(n);break;
case 2: act2_eat(n);break;
case 3: act3_makechild(n);
}
}
void init_flags() /* 狀態標志初始化 */
{
int i;
for(i=0;i<pop_size;i++) iflg [ i]=1;
for(i=0;i<food_size;i++) fflg [ i]=1;
}
void act_lives() /* 改變狀態(移動或行動) */
{
int i,j,k,x,y,move,a;
for(i=0;i<pop_size;i++)
{
if(iflg [ i]==1)
{
move=1;
for(j=-1;j<=1;j++)
for(k=-1;k<=1;k++)
{
if(j!=0||k!=0)
{
a=get_world(iatr [ i][0]+k,iatr [ i][1]+j);
if(a==1||a==2||a==3||a==5)
move=0;
}
}
if(move==1)
move_individual(i);
else
act_individual(i);
}
}
}
void increase_age() /* 個體年齡增1 */
{
int i,j,s;
for(i=0;i<pop_size;i++)
{
if(iflg [ i]==1)
{
j=decode_gene(i,1,4);
s=SL_MIN+j;
iatr [ i][3]++;
if(iatr [ i][3]>s)
remove_life(i);
}
}
}
void increase_frsh() /* 食物新鮮度增1 */
{
int i;
for(i=0;i<food_size;i++)
if(fflg [ i]==1)
{
fatr [ i][3]++;
if((fatr [ i][2]==0 && fatr [ i][3]>TL1)||
(fatr [ i][2]==1 && fatr [ i][3]>TL2))
remove_food(i);
}
}
void gabage_col() /* 死去個體及消減食物清除*/
{
int i,j;
int new_pop,new_food;
/* 檢查食物 */
new_food=0;
for(i=0;i<food_size;i++)
if(fflg [ i]==1)
{
new_food++;
for(j=0;j<4;j++)
fatr[new_food-1][j]=fatr [ i][j];
}
food_size=new_food;
/* 檢查個體 */
new_pop=0;
for(i=0;i<pop_size;i++)
if(iflg [ i]==1)
{
new_pop++;
/* 遺傳基因復制 */
for(j=0;j<G_LENGTH;j++)
gene[new_pop-1][j]=gene [ i][j];
/* 屬性復制 */
for(j=0;j<4;j++)
iatr[new_pop-1][j]=iatr [ i][j];
}
pop_size=new_pop;
}
void make_foods() /* 產生一代中植物性食物 */
{
int i,x,y;
for(i=0;i<NEWFOODS;i++)
{
if(food_size+1<=MAX_FOOD)
{
food_size++;
find_empty(&x,&y);
fatr[food_size-1][0]=x;
fatr[food_size-1][1]=y;
fatr[food_size-1][2]=0; /* 植物性 */
fatr[food_size-1][3]=0;
fflg[food_size-1]=1;
world[x][y]=3;
g_disp_unit(x,y,3);
}
}
}
void calc_population(n1,n2) /* 計算生物1和2 的個體數 */
int *n1,*n2;
{
int i,p1,p2;
p1=0;p2=0;
if(pop_size>0)
for(i=0;i<pop_size;i++)
if(gene [ i][0]==0) p1++; else p2++;
*n1=p1;
*n2=p2;
}
main() /* 主程序 */
{
int i,work;
int n1,n2,n1old,n2old;
char choice[2];
randomize();
/* 圖形界面初始化 */
g_init();
settextstyle(0,0,4);
gprintf(220,20,4,0,"ALIFE");
setcolor(9);
disp_hz24("基于遺傳算法的人工生命模擬",150,60,25);
setcolor(15);
disp_hz16("人工環境及生物分布",10,160,20);
disp_hz16("1:隨機產生 2: 讀文件產生 ==>",10,190,20);
gscanf(300,190,15,1,4,"%s",choice);
work=atoi(choice);
if(work==2) load_world_file();else make_world();
make_lives_and_foods();
/*狀態初始化 */
init_flags();
/*計算個體數 */
calc_population(&n1old,&n2old);
/*生成初始畫面*/
g_init_frames();
/*虛擬世界畫面*/
g_draw_world();
/* 顯示初始圖形 */
g_init_graph();
for(i=1;i<121;i++)
{
/*狀態初始化 */
init_flags();
/* 改變狀態(移動或行動) */
act_lives();
/* 個體年齡增加 */
increase_age();
/* 食物新鮮度增加 */
increase_frsh();
/*死去個體及消減食物清除*/
gabage_col();
/*產生新的食物 */
make_foods();
/* 求生物1和2 的個體數 */
calc_population(&n1,&n2);
/* 個體數變化的圖形更新 */
g_plot_population(i,n1,n2,n1old,n2old);
n1old=n1;n2old=n2;
/* 顯示最佳遺傳基因 */
g_disp_genotype();
}
setcolor(9);
disp_hz16("回車鍵結束",10,430,20);
getch();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -