程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 蟻群算法小程序(C/C++語言實現)(二)

蟻群算法小程序(C/C++語言實現)(二)

編輯:關於C++

fclose(fp_block);
}

void HomeFoodInitial(void)
{
int randnum;
int homeplace;
/* 1 -- home at left-up, food at right-down
2 -- home at left-down, food at right-up
3 -- home at right-up, food at left-down
4 -- home at right-down, food at left-up */

randnum = random(100);
if(randnum<25) homeplace = 1;
else if (randnum>=25&&randnum<50) homeplace = 2;
else if (randnum>=50&&randnum<75) homeplace = 3;
else homeplace = 4;

switch(homeplace)
{
case 1: home.xxx = random(MAXX/3)+1;
home.yyy = random(MAXY/3)+1;
food.xxx = random(MAXX/3)+2*MAXX/3+1;
food.yyy = random(MAXY/3)+2*MAXY/3+1;
break;
case 2: home.xxx = random(MAXX/3)+1;
home.yyy = random(MAXY/3)+2*MAXY/3+1;
food.xxx = random(MAXX/3)+2*MAXX/3+1;
food.yyy = random(MAXY/3)+1;
break;
case 3: home.xxx = random(MAXX/3)+2*MAXX/3+1;[Page]
home.yyy = random(MAXY/3)+1;
food.xxx = random(MAXX/3)+1;
food.yyy = random(MAXY/3)+2*MAXY/3+1;
break;
case 4: home.xxx = random(MAXX/3)+2*MAXX/3+1;
home.yyy = random(MAXY/3)+2*MAXY/3+1;
food.xxx = random(MAXX/3)+1;
food.yyy = random(MAXY/3)+1;
break;
}

food.amount = random(MAX_FOOD/3)+2*MAX_FOOD/3+1;
/* food.amount = MAX_FOOD; */
home.amount = 0;
home.TargetFood = (food.amount<TARGET_FOOD)?food.amount:TARGET_FOOD;

/* data correctness check */
if(home.xxx<=0||home.xxx>MAXX||home.yyy<=0||home.yyy>MAXY||
food.xxx<=0||food.xxx>MAXX||food.yyy<=0||food.yyy>MAXY||
food.amount<=0)
{
gotoxy(1,MAXY+1);
printf("World initial fail, any key to exit...");
getch();
exit(2);
}

gotoxy(home.xxx,home.yyy); putch(HOME_CHAR);
gotoxy(food.xxx,food.yyy); putch(FOOD_CHAR);
}

void AntInitial(void)
/* initial ant[AntNow] */
{
int randnum;
int i;

ant[AntNow].xxx = home.xxx;
ant[AntNow].yyy = home.yyy;

randnum = random(100);
if(randnum<25) ant[AntNow].dir = UP;
else if (randnum>=25&&randnum<50) ant[AntNow].dir = DOWN;
else if (randnum>=50&&randnum<75) ant[AntNow].dir = LEFT;[Page]
else ant[AntNow].dir = RIGHT;

ant[AntNow].speed = 2*(random(INI_SPEED/2)+1);
ant[AntNow].SpeedTimer = 0;
ant[AntNow].food = 0;


ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = 0;
ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = MAX_SMELL;
ant[AntNow].IQ = 1;

for(i=0;i<TRACE_REMEMBER;i++)
{
ant[AntNow].tracex[i] = 0;
ant[AntNow].tracey[i] = 0;
}
ant[AntNow].TracePtr = 0;

/* a sepecail ant */
if(AntNow==0) ant[AntNow].speed = INI_SPEED;
}

void WorldChange(void)
{
int k,i,j;
int smelldisp;

SmellGoneTimer+=timer10ms;
if(SmellGoneTimer>=SMELL_GONE_SPEED)
{
SmellGoneTimer = 0;
for(k=0;k<=1;k++)
/* SMELL TYPE FOOD and HOME */
for(i=1;i<=MAXX;i++)
for(j=1;j<=MAXY;j++)
{
if(Smell[k][i][j])
{
smelldisp = 1+((10*Smell[k][i][j])/(MAX_SMELL*SMELL_DROP_RATE));
if(smelldisp>=30000||smelldisp<0) smelldisp = 30000;
if(SmellDispFlag)
{[Page]
gotoxy(i,j);
if((i==food.xxx&&j==food.yyy)||(i==home.xxx&&j==home.yyy))
/* don't over write Food and Home */;
else
{
if(smelldisp>9) putch('#');
else putch(smelldisp+'0');
}
}
Smell[k][i][j]-= 1+(Smell[k][i][j]*SMELL_GONE_RATE);
if(Smell[k][i][j]<0) Smell[k][i][j] = 0;
if(SmellDispFlag)
{
if(Smell[k][i][j]<=2)


{
gotoxy(i,j);
putch(SPACE);
}
}
}
} /* of one location */
} /* of time to change the world */
} /* of world change */

void AntMove(void)
{
int antx,anty;
int smelltodrop,smellnow;

for(AntNow=0;AntNow<MAX_ANT;AntNow++)
{
ant[AntNow].SpeedTimer+=timer10ms;
if(ant[AntNow].SpeedTimer>=ant[AntNow].speed)
{
ant[AntNow].SpeedTimer = 0;
gotoxy(ant[AntNow].xxx,ant[AntNow].yyy);
putch(SPACE);
AntOneStep();
gotoxy(ant[AntNow].xxx,ant[AntNow].yyy);
/* ant0 is a sepecail ant, use different color */
if(AntNow==0) textcolor(0xd);
if(ant[AntNow].food) putch(ANT_CHAR_FOOD);
else putch(ANT_CHAR_EMPTY);
if(AntNow==0) textcolor(0x7);

/* remember trace */
ant[AntNow].tracex[ant[AntNow].TracePtr] = ant[AntNow].xxx;
ant[AntNow].tracey[ant[AntNow].TracePtr] = ant[AntNow].yyy;
if(++(ant[AntNow].TracePtr)>=TRACE_REMEMBER) ant[AntNow].TracePtr = 0;

/* drop smell */
antx = ant[AntNow].xxx;
anty = ant[AntNow].yyy;

if(ant[AntNow].food)
/* have food, looking for home */
{
if(ant[AntNow].SmellAmount[SMELL_TYPE_FOOD])
{
smellnow = Smell[SMELL_TYPE_FOOD][antx][anty];
smelltodrop = ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]*SMELL_DROP_RATE;
if(smelltodrop>smellnow) Smell[SMELL_TYPE_FOOD][antx][anty] = smelltodrop;
/* else Smell[...] = smellnow */
ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]-= smelltodrop;
if(ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]<0) ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = 0;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved