ImageVerifierCode 换一换
格式:DOCX , 页数:29 ,大小:19.54KB ,
资源ID:5042419      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5042419.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(蚁群算法程序.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

蚁群算法程序.docx

1、蚁群算法程序#define SPACE 0x20#define ESC 0x1b#define ANT_CHAR_EMPTY +#define ANT_CHAR_FOOD 153#define HOME_CHAR H#define FOOD_CHAR F#define FOOD_CHAR2 f#define FOOD_HOME_COLOR 12#define BLOCK_CHAR 177#define MAX_ANT 50#define INI_SPEED 3#define MAXX 80#define MAXY 23#define MAX_FOOD 10000#define TARGET_F

2、OOD 200#define MAX_SMELL 5000#define SMELL_DROP_RATE 0.05#define ANT_ERROR_RATE 0.02#define ANT_EYESHOT 3#define SMELL_GONE_SPEED 50#define SMELL_GONE_RATE 0.05#define TRACE_REMEMBER 50#define MAX_BLOCK 100#define NULL 0#define UP 1#define DOWN 2#define LEFT 3#define RIGHT 4#define SMELL_TYPE_FOOD 0

3、#define SMELL_TYPE_HOME 1#include stdio.h#include conio.h#include dos.h#include stdlib.h#include dos.h#include process.h#include ctype.h#include math.hvoid WorldInitial(void);void BlockInitial(void);void CreatBlock(void);void HomeFoodInitial(void);void AntInitial(void);void WorldChange(void);void An

4、tMove(void);void AntOneStep(void);void DealKey(char key);void ClearSmellDisp(void);void DispSmell(int type);int AntNextDir(int xxx,int yyy,int ddir);int GetMaxSmell(int type,int xxx,int yyy,int ddir);int IsTrace(int xxx,int yyy);int MaxLocation(int num1,int num2,int num3);int CanGo(int xxx,int yyy,i

5、nt ddir);int JudgeCanGo(int xxx,int yyy);int TurnLeft(int ddir);int TurnRight(int ddir);int TurnBack(int ddir);int MainTimer(void);char WaitForKey(int secnum);void DispPlayTime(void);int TimeUse(void);void HideCur(void);void ResetCur(void);struct HomeStruct int xxx,yyy; int amount; int TargetFood;ho

6、me;struct FoodStruct int xxx,yyy; int amount; food;struct AntStruct int xxx,yyy; int dir; int speed; int SpeedTimer; int food; int SmellAmount2; int tracexTRACE_REMEMBER; int traceyTRACE_REMEMBER; int TracePtr; int IQ;antMAX_ANT;int AntNow;int timer10ms;struct time starttime,endtime;int Smell2MAXX+1

7、MAXY+1;int blockMAXX+1MAXY+1;int SmellGoneTimer;int SmellDispFlag;int CanFindFood;int HardtoFindPath;void main(void) char KeyPress; int tu; clrscr(); HideCur(); WorldInitial(); do timer10ms = MainTimer(); if(timer10ms) AntMove(); if(timer10ms) WorldChange(); tu = TimeUse(); if(tu=60&!CanFindFood) go

8、toxy(1,MAXY+1); printf(Can not find food, maybe a block world.); WaitForKey(10); WorldInitial(); if(tu=180&home.amount=home.TargetFood) gettime(&endtime); KeyPress = WaitForKey(60); DispPlayTime(); WaitForKey(10); WorldInitial(); else if(kbhit() KeyPress = getch(); DealKey(KeyPress); else KeyPress =

9、 NULL; while(KeyPress!=ESC); gettime(&endtime); DispPlayTime(); WaitForKey(10); clrscr(); ResetCur(); int MainTimer(void) static int oldhund,oldsec; struct time t; int timeuse; gettime(&t); timeuse = 0; if(t.ti_hund!=oldhund) if(t.ti_sec!=oldsec) timeuse+=100; oldsec = t.ti_sec; timeuse+=t.ti_hund-o

10、ldhund; oldhund = t.ti_hund; else timeuse = 0; return (timeuse);char WaitForKey(int secnum) int secin,secnow; int minin,minnow; int hourin,hournow; int secuse; struct time t; gettime(&t); secin = t.ti_sec; minin = t.ti_min; hourin = t.ti_hour; do if(kbhit() return(getch(); gettime(&t); secnow = t.ti

11、_sec; minnow = t.ti_min; hournow = t.ti_hour; if(hournow!=hourin) minnow+=60; if(minnowminin) secuse = (minnow-1-minin) + (secnow+60-secin); else secuse = secnow - secin; if(secuse0) gotoxy(1,MAXY+1); printf(Time conuting error, any keyto exit.); getch(); exit(3); while(secuse=secnum); return (NULL)

12、;void DispPlayTime(void) int ph,pm,ps; ph = endtime.ti_hour - starttime.ti_hour; pm = endtime.ti_min - starttime.ti_min; ps = endtime.ti_sec - starttime.ti_sec; if(ph0) ph+=24; if(pm0) ph-; pm+=60; if(ps0) pm-; ps+=60; gotoxy(1,MAXY+1); printf(Time use: %d hour- %d min- %d sec ,ph,pm,ps);int TimeUse

13、(void) int ph,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(ph0) ph+=24; if(pm0) ph-; pm+=60; if(ps0) pm-; ps+=60; return(ps+(60*(pm+60*ph);void HideCur(void) union REGS regs0; regs0.h.ah=1; regs

14、0.h.ch=0x30; regs0.h.cl=0x31; int86(0x10,®s0,®s0);void ResetCur(void) union REGS regs0; regs0.h.ah=1; regs0.h.ch=0x06; regs0.h.cl=0x07; int86(0x10,®s0,®s0);void WorldInitial(void) int k,i,j; randomize(); clrscr(); HomeFoodInitial(); for(AntNow=0;AntNowMAX_ANT;AntNow+) AntInitial(); ; Blo

15、ckInitial(); for(k=0;k=1;k+) for(i=0;i=MAXX;i+) for(j=0;j=MAXY;j+) Smellkij = 0; SmellGoneTimer = 0; gettime(&starttime); SmellDispFlag = 0; CanFindFood = 0; HardtoFindPath = 0;void BlockInitial(void) int i,j; int bn; for(i=0;i=MAXX;i+) for(j=0;j=MAXY;j+) blockij = 0; bn = 1+ MAX_BLOCK/2 + random(MA

16、X_BLOCK/2); for(i=0;iMAXX) x2 = MAXX; if(y2MAXY) y2 = MAXY; if(food.xxx=x1&food.xxx=y1&food.yyy=x1&home.xxx=y1&home.yyy=y2) return; for(i=x1;i=x2;i+) for(j=y1;j=y2;j+) blockij = 1; gotoxy(i,j); putch(BLOCK_CHAR); void HomeFoodInitial(void) int randnum; int homeplace; randnum = random(100); if(randnu

17、m=25&randnum=50&randnum75) 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

18、+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; 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

19、*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.amountTARGET_FOOD)?food.amount:TARGET_FOOD; if(home.xxxMAXX|home.yyyMAXY| food.xxxMAXX|food.yyyMAXY| food.amount=0) gotoxy(1,MAXY+1);

20、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) int randnum; int i; antAntNow.xxx = home.xxx; antAntNow.yyy = home.yyy; randnum = random(100); if(randnum=25&randnum=50&randn

21、um75) antAntNow.dir = LEFT; else antAntNow.dir = RIGHT; antAntNow.speed = 2*(random(INI_SPEED/2)+1); antAntNow.SpeedTimer = 0; antAntNow.food = 0; antAntNow.SmellAmountSMELL_TYPE_FOOD = 0; antAntNow.SmellAmountSMELL_TYPE_HOME = MAX_SMELL; antAntNow.IQ = 1; for(i=0;i=SMELL_GONE_SPEED) SmellGoneTimer

22、= 0; for(k=0;k=1;k+) for(i=1;i=MAXX;i+) for(j=1;j=30000|smelldisp9) putch(#); else putch(smelldisp+0); Smellkij-= 1+(Smellkij*SMELL_GONE_RATE); if(Smellkij0) Smellkij = 0; if(SmellDispFlag) if(Smellkij=2) gotoxy(i,j); putch(SPACE); void AntMove(void) int antx,anty; int smelltodrop,smellnow; for(AntN

23、ow=0;AntNow=antAntNow.speed) antAntNow.SpeedTimer = 0; gotoxy(antAntNow.xxx,antAntNow.yyy); putch(SPACE); AntOneStep(); gotoxy(antAntNow.xxx,antAntNow.yyy); if(antAntNow.food) putch(ANT_CHAR_FOOD); else putch(ANT_CHAR_EMPTY); antAntNow.tracexantAntNow.TracePtr = antAntNow.xxx; antAntNow.traceyantAnt

24、Now.TracePtr = antAntNow.yyy; if(+(antAntNow.TracePtr)=TRACE_REMEMBER) antAntNow.TracePtr= 0; antx = antAntNow.xxx; anty = antAntNow.yyy; if(antAntNow.food) if(antAntNow.SmellAmountSMELL_TYPE_FOOD) smellnow = SmellSMELL_TYPE_FOODantxanty;smelltodrop=antAntNow.SmellAmountSMELL_TYPE_FOOD*SMELL_DROP_RATE; if(smelltodropsmellnow) SmellSMELL_TYPE_FOODantxanty=smelltodrop; antAntNow.SmellAmountSMELL_TYPE_FOOD-= smelltodrop; if(antAntNow.SmellAmountSMELL_TYPE_FOOD0) antAntNow.SmellAmountSMELL_TYPE_FOOD = 0; else if(antAntNow.SmellAmountSMELL_TYPE_HOME

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

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