蚁群算法的原理以及实现源代码文档格式.docx

上传人:b****4 文档编号:8043058 上传时间:2023-05-09 格式:DOCX 页数:42 大小:25.45KB
下载 相关 举报
蚁群算法的原理以及实现源代码文档格式.docx_第1页
第1页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第2页
第2页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第3页
第3页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第4页
第4页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第5页
第5页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第6页
第6页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第7页
第7页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第8页
第8页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第9页
第9页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第10页
第10页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第11页
第11页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第12页
第12页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第13页
第13页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第14页
第14页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第15页
第15页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第16页
第16页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第17页
第17页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第18页
第18页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第19页
第19页 / 共42页
蚁群算法的原理以及实现源代码文档格式.docx_第20页
第20页 / 共42页
亲,该文档总共42页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

蚁群算法的原理以及实现源代码文档格式.docx

《蚁群算法的原理以及实现源代码文档格式.docx》由会员分享,可在线阅读,更多相关《蚁群算法的原理以及实现源代码文档格式.docx(42页珍藏版)》请在冰点文库上搜索。

蚁群算法的原理以及实现源代码文档格式.docx

最大信息素:

蚂蚁在一开始拥有的信息素总量,越大表示程序在较长一段时间能够存在信息素。

信息素消减的速度:

随着时间的流逝,已经存在于世界上的信息素会消减,这个数值越大,那么消减的越快。

错误概率表示这个蚂蚁不往信息素最大的区域走的概率,越大则表示这个蚂蚁越有创新性。

速度半径表示蚂蚁一次能走的最大长度,也表示这个蚂蚁的感知范围。

记忆能力表示蚂蚁能记住多少个刚刚走过点的坐标,这个值避免了蚂蚁在本地打转,停滞不前。

而这个值越大那么整个系统运行速度就慢,越小则蚂蚁越容易原地转圈。

源代码如下(不同编译器可能需做一定修改):

/*ant.c*/

#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"

conio.h"

dos.h"

stdlib.h"

process.h"

ctype.h"

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

}food;

structAntStruct

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

}

=180&

home.amount<

100&

HardtoFindPath)

God!

itissodifficulttofindapath."

if(WaitForKey(10)==0x0d)WorldInitial();

else

HardtoFindPath=1;

"

if(home.amount>

=home.TargetFood)

gettime(&

endtime);

KeyPress=WaitForKey(60);

DispPlayTime();

elseif(kbhit())

KeyPress=getch();

DealKey(KeyPress);

elseKeyPress=NULL;

while(KeyPress!

=ESC);

ResetCur();

}

/*------generalsubprocess-----------*/

intMainTimer(void)

/*output:

howmuch10mshavepassfromlasttimecallthisprocess*/

staticintoldhund,oldsec;

structtimet;

inttimeuse;

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;

secin=t.ti_sec;

minin=t.ti_min;

hourin=t.ti_hour;

if(kbhit())return(getch());

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)

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;

Timeuse:

%dhour-%dmin-%dsec"

ph,pm,ps);

intTimeUse(void)

return(ps+(60*(pm+60*ph)));

voidHideCur(void)

unionREGSregs0;

regs0.h.ah=1;

regs0.h.ch=0x30;

regs0.h.cl=0x31;

int86(0x10,&

regs0,&

regs0);

voidResetCur(void)

regs0.h.ch=0x06;

regs0.h.cl=0x07;

/*------------mainANTprograme-------------*/

voidWorldInitial(void)

intk,i,j;

randomize();

HomeFoodInitial();

for(AntNow=0;

AntNow<

MAX_ANT;

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;

starttime);

SmellDispFlag=0;

CanFindFood=0;

HardtoFindPath=0;

voidBlockInitial(void)

inti,j;

intbn;

block[i][j]=0;

bn=1+MAX_BLOCK/2+random(MAX_BLOCK/2);

=bn;

i++)CreatBlock();

voidCreatBlock(void)

intx1,y1,x2,y2;

intdx,dy;

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>

home.xxx<

home.yyy>

home.yyy<

for(i=x1;

=x2;

for(j=y1;

=y2;

block[i][j]=1;

gotoxy(i,j);

putch(BLOCK_CHAR);

voidSaveBlock(void)

FILE*fp_block;

charFileNameBlock[20];

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

Creatfile%sfail..."

exit

(2);

fputc(home.xxx,fp_block);

fputc(home.yyy,fp_block);

fputc(food.xxx,fp_block);

fputc(food.yyy,fp_block);

fputc(block[i][j],fp_block);

fclose(fp_block);

voidLoadBlock(void)

inti,j,k;

Loadfile..."

rb"

Openfile%sfail..."

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.amount<

TARGET_FOOD)?

food.amount:

TARGET_FOOD;

block[i][j]=fgetc(fp_block);

if(block[i][j])

putch(BLOCK

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

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2