蚁群算法程序.docx

上传人:b****6 文档编号:5042419 上传时间:2022-12-12 格式:DOCX 页数:29 大小:19.54KB
下载 相关 举报
蚁群算法程序.docx_第1页
第1页 / 共29页
蚁群算法程序.docx_第2页
第2页 / 共29页
蚁群算法程序.docx_第3页
第3页 / 共29页
蚁群算法程序.docx_第4页
第4页 / 共29页
蚁群算法程序.docx_第5页
第5页 / 共29页
点击查看更多>>
下载资源
资源描述

蚁群算法程序.docx

《蚁群算法程序.docx》由会员分享,可在线阅读,更多相关《蚁群算法程序.docx(29页珍藏版)》请在冰豆网上搜索。

蚁群算法程序.docx

蚁群算法程序

#defineSPACE0x20

#defineESC0x1b

#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);

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;

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)==0x0d)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();

}

intMainTimer(void)

{

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)

{

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;

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=0x30;

regs0.h.cl=0x31;

int86(0x10,®s0,®s0);

}

voidResetCur(void)

{

unionREGSregs0;

regs0.h.ah=1;

regs0.h.ch=0x06;

regs0.h.cl=0x07;

int86(0x10,®s0,®s0);

}

voidWorldInitial(void)

{

intk,i,j;

randomize();

clrscr();

HomeFoodInitial();

for(AntNow=0;AntNow

{

AntInitial();

};

BlockInitial();

for(k=0;k<=1;k++)

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);

}

}

voidHomeFoodInitial(void)

{

intrandnum;

inthomeplace;

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;

home.amount=0;

home.TargetFood=(food.amount

food.amount:

TARGET_FOOD;

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...");

getch();

exit

(2);

}

gotoxy(home.xxx,home.yyy);putch(HOME_CHAR);

gotoxy(food.xxx,food.yyy);putch(FOOD_CHAR);

}

voidAntInitial(void)

{

intrandnum;

inti;

ant[AntNow].xxx=home.xxx;

ant[AntNow].yyy=home.yyy;

randnum=random(100);

if(randnum<25)ant[AntNow].dir=UP;

elseif(randnum>=25&&randnum<50)ant[AntNow].dir=DOWN;

elseif(randnum>=50&&randnum<75)ant[AntNow].dir=LEFT;

elseant[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

{

ant[AntNow].tracex[i]=0;

ant[AntNow].tracey[i]=0;

}

ant[AntNow].TracePtr=0;

}

voidWorldChange(void)

{

intk,i,j;

intsmelldisp;

SmellGoneTimer+=timer10ms;

if(SmellGoneTimer>=SMELL_GONE_SPEED)

{

SmellGoneTimer=0;

for(k=0;k<=1;k++)

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)

{

gotoxy(i,j);

if((i==food.xxx&&j==food.yyy)||(i==home.xxx&&j==home.yyy));

else

{

if(smelldisp>9)putch('#');

elseputch(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);

}

}

}

}

}

}

voidAntMove(void)

{

intantx,anty;

intsmelltodrop,smellnow;

for(AntNow=0;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);

if(ant[AntNow].food)putch(ANT_CHAR_FOOD);

elseputch(ANT_CHAR_EMPTY);

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;

antx=ant[AntNow].xxx;

anty=ant[AntNow].yyy;

if(ant[AntNow].food)

{

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;

ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]-=smelltodrop;

if(ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]<0)ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]=0;

}

}

else

{

if(ant[AntNow].SmellAmount[SMELL_TYPE_HOME

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1