蚁群算法.docx
《蚁群算法.docx》由会员分享,可在线阅读,更多相关《蚁群算法.docx(40页珍藏版)》请在冰点文库上搜索。
蚁群算法
/*ant.c*/
#defineSPACE0×20
#defineESC0×1b
#defineANT_CHAR_EMPTY‘+’
#defineANT_CHAR_FOOD153
#defineHOME_CHAR‘H’
#defineFOOD_CHAR‘F’
#defineFOOD_CHAR2‘f’
#defineFOOD_HOME_COLOR12
#defineBLOCK_CHAR177
#defineMAX_ANT50
#defineINI_SPEED3
#defineMAXX80
#defineMAXY23
#defineMAX_FOOD10000
#defineTARGET_FOOD200
#defineMAX_SMELL5000
#defineSMELL_DROP_RATE0.05
#defineANT_ERROR_RATE0.02
#defineANT_EYESHOT3
#defineSMELL_GONE_SPEED50
#defineSMELL_GONE_RATE0.05
#defineTRACE_REMEMBER50
#defineMAX_BLOCK100
#defineNULL0
#defineUP1
#defineDOWN2
#defineLEFT3
#defineRIGHT4
#defineSMELL_TYPE_FOOD0
#defineSMELL_TYPE_HOME1
#include“stdio.h”
#include“conio.h”
#include“dos.h”
#include“stdlib.h”
#include“dos.h”
#include“process.h”
#include“ctype.h”
#include“math.h”
voidWorldInitial(void);
voidBlockInitial(void);
voidCreatBlock(void);
voidSaveBlock(void);
voidLoadBlock(void);
voidHomeFoodInitial(void);
voidAntInitial(void);
voidWorldChange(void);
voidAntMove(void);
voidAntOneStep(void);
voidDealKey(charkey);
voidClearSmellDisp(void);
voidDispSmell(inttype);
intAntNextDir(intxxx,intyyy,intddir);
intGetMaxSmell(inttype,intxxx,intyyy,intddir);
intIsTrace(intxxx,intyyy);
intMaxLocation(intnum1,intnum2,intnum3);
intCanGo(intxxx,intyyy,intddir);
intJudgeCanGo(intxxx,intyyy);
intTurnLeft(intddir);
intTurnRight(intddir);
intTurnBack(intddir);
intMainTimer(void);
charWaitForKey(intsecnum);
voidDispPlayTime(void);
intTimeUse(void);
voidHideCur(void);
voidResetCur(void);
/*—————*/
structHomeStruct
{
intxxx,yyy;
intamount;
intTargetFood;
}home;
structFoodStruct
{
intxxx,yyy;
intamount;
}food;
structAntStruct
{
intxxx,yyy;
intdir;
intspeed;
intSpeedTimer;
intfood;
intSmellAmount[2];
inttracex[TRACE_REMEMBER];
inttracey[TRACE_REMEMBER];
intTracePtr;
intIQ;
}ant[MAX_ANT];
intAntNow;
inttimer10ms;
structtimestarttime,endtime;
intSmell[2][MAXX+1][MAXY+1];
intblock[MAXX+1][MAXY+1];
intSmellGoneTimer;
intSmellDispFlag;
intCanFindFood;
intHardtoFindPath;
/*—–Main——–*/
voidmain(void)
{
charKeyPress;
inttu;
clrscr();
HideCur();
WorldInitial();
do
{
timer10ms=MainTimer();
if(timer10ms)AntMove();
if(timer10ms)WorldChange();
tu=TimeUse();
if(tu>=60&&!
CanFindFood)
{
gotoxy(1,MAXY+1);
printf(“Cannotfindfood,maybeablockworld.”);
WaitForKey(10);
WorldInitial();
}
if(tu>=180&&home.amount<100&&!
HardtoFindPath)
{
gotoxy(1,MAXY+1);
printf(“God!
itissodifficulttofindapath.”);
if(WaitForKey(10)==0×0d)WorldInitial();
else
{
HardtoFindPath=1;
gotoxy(1,MAXY+1);
printf(”“);
}
}
if(home.amount>=home.TargetFood)
{
gettime(&endtime);
KeyPress=WaitForKey(60);
DispPlayTime();
WaitForKey(10);
WorldInitial();
}
elseif(kbhit())
{
KeyPress=getch();
DealKey(KeyPress);
}
elseKeyPress=NULL;
}
while(KeyPress!
=ESC);
gettime(&endtime);
DispPlayTime();
WaitForKey(10);
clrscr();
ResetCur();
}
/*——generalsubprocess———–*/
intMainTimer(void)
/*output:
howmuch10mshavepassfromlasttimecallthisprocess*/
{
staticintoldhund,oldsec;
structtimet;
inttimeuse;
gettime(&t);
timeuse=0;
if(t.ti_hund!
=oldhund)
{
if(t.ti_sec!
=oldsec)
{
timeuse+=100;
oldsec=t.ti_sec;
}
timeuse+=t.ti_hund-oldhund;
oldhund=t.ti_hund;
}
elsetimeuse=0;
return(timeuse);
}
charWaitForKey(intsecnum)
/*funtion:
ifhavekeyin,exitimmediately,elsewait’secnum’sencondsthenexit
input:
secnum—waitthissenconds,must<3600(1hour)
output:
keychar,ifnokeyin(exitwhentimeout),returnNULL*/
{
intsecin,secnow;
intminin,minnow;
inthourin,hournow;
intsecuse;
structtimet;
gettime(&t);
secin=t.ti_sec;
minin=t.ti_min;
hourin=t.ti_hour;
do
{
if(kbhit())return(getch());
gettime(&t);
secnow=t.ti_sec;
minnow=t.ti_min;
hournow=t.ti_hour;
if(hournow!
=hourin)minnow+=60;
if(minnow>minin)secuse=(minnow-1-minin)+(secnow+60-secin);
elsesecuse=secnow-secin;
/*countingerrorcheck*/
if(secuse<0)
{
gotoxy(1,MAXY+1);
printf(“Timeconutingerror,anykeytoexit…”);
getch();
exit(3);
}
}
while(secuse<=secnum);
return(NULL);
}
voidDispPlayTime(void)
{
intph,pm,ps;
ph=endtime.ti_hour-starttime.ti_hour;
pm=endtime.ti_min-starttime.ti_min;
ps=endtime.ti_sec-starttime.ti_sec;
if(ph<0)ph+=24;
if(pm<0){ph–;pm+=60;}
if(ps<0){pm–;ps+=60;}
gotoxy(1,MAXY+1);
printf(“Timeuse:
%dhour-%dmin-%dsec“,ph,pm,ps);
}
intTimeUse(void)
{
intph,pm,ps;
gettime(&endtime);
ph=endtime.ti_hour-starttime.ti_hour;
pm=endtime.ti_min-starttime.ti_min;
ps=endtime.ti_sec-starttime.ti_sec;
if(ph<0)ph+=24;
if(pm<0){ph–;pm+=60;}
if(ps<0){pm–;ps+=60;}
return(ps+(60*(pm+60*ph)));
}
voidHideCur(void)
{
unionREGSregs0;
regs0.h.ah=1;
regs0.h.ch=0×30;
regs0.h.cl=0×31;
int86(0×10,®s0,®s0);
}
voidResetCur(void)
{
unionREGSregs0;
regs0.h.ah=1;
regs0.h.ch=0×06;
regs0.h.cl=0×07;
int86(0×10,®s0,®s0);
}
/*————mainANTprograme————-*/
voidWorldInitial(void)
{
intk,i,j;
randomize();
clrscr();
HomeFoodInitial();
for(AntNow=0;AntNow{
AntInitial();
}/*offorAntNow*/;
BlockInitial();
for(k=0;k<=1;k++)
/*SMELLTYPEFOODandHOME*/
for(i=0;i<=MAXX;i++)
for(j=0;j<=MAXY;j++)
Smell[k][i][j]=0;
SmellGoneTimer=0;
gettime(&starttime);
SmellDispFlag=0;
CanFindFood=0;
HardtoFindPath=0;
}
voidBlockInitial(void)
{
inti,j;
intbn;
for(i=0;i<=MAXX;i++)
for(j=0;j<=MAXY;j++)
block[i][j]=0;
bn=1+MAX_BLOCK/2+random(MAX_BLOCK/2);
for(i=0;i<=bn;i++)CreatBlock();
}
voidCreatBlock(void)
{
intx1,y1,x2,y2;
intdx,dy;
inti,j;
x1=random(MAXX)+1;
y1=random(MAXY)+1;
dx=random(MAXX/10)+1;
dy=random(MAXY/10)+1;
x2=x1+dx;
y2=y1+dy;
if(x2>MAXX)x2=MAXX;
if(y2>MAXY)y2=MAXY;
if(food.xxx>=x1&&food.xxx<=x2&&food.yyy>=y1&&food.yyy<=y2)return;
if(home.xxx>=x1&&home.xxx<=x2&&home.yyy>=y1&&home.yyy<=y2)return;
for(i=x1;i<=x2;i++)
for(j=y1;j<=y2;j++)
{
block[i][j]=1;
gotoxy(i,j);
putch(BLOCK_CHAR);
}
}
voidSaveBlock(void)
{
FILE*fp_block;
charFileNameBlock[20];
inti,j;
gotoxy(1,MAXY+1);
printf(”“);
gotoxy(1,MAXY+1);
printf(“Savetofile…”,FileNameBlock);
gets(FileNameBlock);
if(FileNameBlock[0]==0)strcpy(FileNameBlock,“Ant.ant”);
elsestrcat(FileNameBlock,“.ant”);
if((fp_block=fopen(FileNameBlock,“wb”))==NULL)
{gotoxy(1,MAXY+1);
printf(“Creatfile%sfail…”,FileNameBlock);
getch();
exit
(2);
}
gotoxy(1,MAXY+1);
printf(”“);
fputc(home.xxx,fp_block);
fputc(home.yyy,fp_block);
fputc(food.xxx,fp_block);
fputc(food.yyy,fp_block);
for(i=0;i<=MAXX;i++)
for(j=0;j<=MAXY;j++)
fputc(block[i][j],fp_block);
fclose(fp_block);
}
voidLoadBlock(void)
{
FILE*fp_block;
charFileNameBlock[20];
inti,j,k;
gotoxy(1,MAXY+1);
printf(”“);
gotoxy(1,MAXY+1);
printf(“Loadfile…”,FileNameBlock);
gets(FileNameBlock);
if(FileNameBlock[0]==0)strcpy(FileNameBlock,“Ant.ant”);
elsestrcat(FileNameBlock,“.ant”);
if((fp_block=fopen(FileNameBlock,“rb”))==NULL)
{gotoxy(1,MAXY+1);
printf(“Openfile%sfail…”,FileNameBlock);
getch();
exit
(2);
}
clrscr();
home.xxx=fgetc(fp_block);
home.yyy=fgetc(fp_block);
food.xxx=fgetc(fp_block);
food.yyy=fgetc(fp_block);
gotoxy(home.xxx,home.yyy);putch(HOME_CHAR);
gotoxy(food.xxx,food.yyy);putch(FOOD_CHAR);
food.amount=random(MAX_FOOD/3)+2*MAX_FOOD/3+1;
/*food.amount=MAX_FOOD;*/
home.amount=0;
home.TargetFood=
(food.amountfood.amount:
TARGET_FOOD;
for(AntNow=0;AntNow{
AntInitial();
}/*offorAntNow*/;
for(i=0;i<=MAXX;i++)
for(j=0;j<=MAXY;j++)
{
block[i][j]=fgetc(fp_block);
if(block[i][j])
{
gotoxy(i,j);
putch(BLOCK_CHAR);
}
}
for(k=0;k<=1;k++)
/*SMELLTYPEFOODandHOME*/
for(i=0;i<=MAXX;i++)
for(j=0;j<=MAXY;j++)
Smell[k][i][j]=0;
SmellGoneTimer=0;
gettime(&starttime);
SmellDispFlag=0;
CanFindFood=0;
HardtoFindPath=0;
fclose(fp_block);
}
voidHomeFoodInitial(void)
{
intrandnum;
inthomeplace;
/*1—homeatleft-up,foodatright-down
2—homeatleft-down,foodatright-up
3—homeatright-up,foodatleft-down
4—homeatright-down,foodatleft-up*/
randnum=random(100);
if(randnum<25)homeplace=1;
elseif(randnum>=25&&randnum<50)homeplace=2;
elseif(randnum>=50&&randnum<75)homeplace=3;
elsehomeplace=4;
switch(homeplace)
{
case1:
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;
case2:
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;
case3:
home.xxx=random(MAXX/3)+2*MAXX/3+1;
home.yyy=random(MAXY/3)+1;
food.xxx=random(MAXX/3)+1;
food.yyy=random(MAXY/3)+2*MAXY/3+1;
break;
case4:
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.amountfood.amount:
TARGET_FOOD;
/*datacorrectnesscheck*/
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(“Worldinitialfail,anykeytoexit…”