| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 417 人关注过本帖
标题:"蚁群算法"求注释2
只看楼主 加入收藏
xiazhi123456
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-6-30
收藏
 问题点数:0 回复次数:0 
"蚁群算法"求注释2

void ClearSmellDisp(void)
{
int k,i,j;

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])
{
gotoxy(i,j);
putch(SPACE);
}
} /* of one location */
}

void DispSmell(int type)
/* input: 0 -- Only display food smell
1 -- Only display home smell
2 -- Display both food and home smell
*/
{
int k,i,j;
int fromk,tok;
int smelldisp;

switch(type)
{
case 0: fromk = 0;
tok = 0;
break;
case 1: fromk = 1;
tok = 1;
break;
case 2: fromk = 0;
tok = 1;
break;
default:fromk = 0;
tok = 1;
break;
}
SmellGoneTimer = 0;
for(k=fromk;k<=tok;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;
gotoxy(i,j);
if(i!=food.xxx||j!=food.yyy)
{
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');
}
}
}
} /* of one location */
}

int AntNextDir(int xxx,int yyy,int ddir)
{
int randnum;
int testdir;
int CanGoState;
int cangof,cangol,cangor;
int msf,msl,msr,maxms;
int type;

CanGoState = CanGo(xxx,yyy,ddir);
if(CanGoState==0||CanGoState==2||CanGoState==3||CanGoState==6) cangof = 1;
else cangof = 0;
if(CanGoState==0||CanGoState==1||CanGoState==3||CanGoState==5) cangol = 1;
else cangol = 0;
if(CanGoState==0||CanGoState==1||CanGoState==2||CanGoState==4) cangor = 1;
else cangor = 0;

if(ant[AntNow].food) type = SMELL_TYPE_HOME;
else type = SMELL_TYPE_FOOD;

msf = GetMaxSmell(type,xxx,yyy,ddir);
msl = GetMaxSmell(type,xxx,yyy,TurnLeft(ddir));
msr= GetMaxSmell(type,xxx,yyy,TurnRight(ddir));
maxms = MaxLocation(msf,msl,msr);
/* maxms - 1 - msf is MAX
2 - msl is MAX
3 - msr is MAX
0 - all 3 number is 0 */

testdir = NULL;
switch(maxms)
{
case 0: /* all is 0, keep testdir = NULL, random select dir */
break;
case 1: if(cangof)
testdir = ddir;
else
if(msl>msr) if(cangol) testdir = TurnLeft(ddir);
else if(cangor) testdir = TurnRight(ddir);
break;
case 2: if(cangol)
testdir = TurnLeft(ddir);
else
if(msf>msr) if(cangof) testdir = ddir;
else if(cangor) testdir = TurnRight(ddir);
break;
case 3: if(cangor)
testdir = TurnRight(ddir);
else
if(msf>msl) if(cangof) testdir =ddir;
else if(cangol) testdir = TurnLeft(ddir);
break;
default:break;
} /* of maxms */

randnum = random(1000);
if(randnum<SMELL_DROP_RATE*1000||testdir==NULL)
/* 1. if testdir = NULL, means can not find the max smell or the dir to max smell can not go
then random select dir
2. if ant error, don't follow the smell, random select dir
*/
{
randnum = random(100);
switch(CanGoState)
{
case 0: if(randnum<90) testdir = ddir;
else if (randnum>=90&&randnum<95) testdir = TurnLeft(ddir);
else testdir = TurnRight(ddir);
break;
case 1: if(randnum<50) testdir = TurnLeft(ddir);
else testdir = TurnRight(ddir);
break;
case 2: if(randnum<90) testdir = ddir;
else testdir = TurnRight(ddir);
break;
case 3: if(randnum<90) testdir = ddir;
else testdir = TurnLeft(ddir);
break;
case 4: testdir = TurnRight(ddir);
break;
case 5: testdir = TurnLeft(ddir);
break;
case 6: testdir = ddir;
break;
case 7: testdir = TurnBack(ddir);
break;
default:testdir = TurnBack(ddir);
} /* of can go state */
}
return(testdir);
}

int GetMaxSmell(int type,int xxx,int yyy,int ddir)
{
int i,j;
int ms; /* MAX smell */

ms = 0;
switch(ddir)
{
case UP: for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
for(j=yyy-ANT_EYESHOT;j<yyy;j++)
{
if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
(i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
ms = MAX_SMELL;
break;
}
if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break;
case DOWN: for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
for(j=yyy+1;j<=yyy+ANT_EYESHOT;j++)
{
if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
(i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
ms = MAX_SMELL;
break;
}
if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break;
case LEFT: for(i=xxx-ANT_EYESHOT;i<xxx;i++)
for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
{
if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
(i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
ms = MAX_SMELL;
break;
}
if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break;
case RIGHT: for(i=xxx+1;i<=xxx+ANT_EYESHOT;i++)
for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
{
if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
(i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
ms = MAX_SMELL;
break;
}
if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break;
default: break;
}
return(ms);
}

int IsTrace(int xxx,int yyy)
{
int i;

for(i=0;i<TRACE_REMEMBER;i++)
if(ant[AntNow].tracex[i]==xxx&&ant[AntNow].tracey[i]==yyy) return(1);
return(0);
}

int MaxLocation(int num1,int num2,int num3)
{
int maxnum;

if(num1==0&&num2==0&&num3==0) return(0);

maxnum = num1;
if(num2>maxnum) maxnum = num2;
if(num3>maxnum) maxnum = num3;

if(maxnum==num1) return(1);
if(maxnum==num2) return(2);
if(maxnum==num3) return(3);
}

搜索更多相关主题的帖子: 注释 算法 void int FOOD 
2007-06-30 16:02
快速回复:"蚁群算法"求注释2
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023789 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved