快速业务通道

蚁群算法小程序(C/C++语言实现)(二)

作者 佚名技术 来源 程序设计 浏览 发布时间 2012-06-29

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(

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号