蚁群算法程序

上传人:大米 文档编号:484682874 上传时间:2023-11-17 格式:DOCX 页数:20 大小:22.27KB
返回 下载 相关 举报
蚁群算法程序_第1页
第1页 / 共20页
蚁群算法程序_第2页
第2页 / 共20页
蚁群算法程序_第3页
第3页 / 共20页
蚁群算法程序_第4页
第4页 / 共20页
蚁群算法程序_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《蚁群算法程序》由会员分享,可在线阅读,更多相关《蚁群算法程序(20页珍藏版)》请在金锄头文库上搜索。

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

3、_FOOD 0 #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(vo

4、id);void AntMove(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

5、xxx,int yyy,int 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 HomeStructint xxx,yyy;int amount;int

6、TargetFood;home;struct FoodStructint xxx,yyy;int amount;food;struct AntStructint 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 Smell2MAX

7、X+1MAXY+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) got

8、oxy(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 = N

9、ULL; 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-oldhund

10、; 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_sec; minnow =

11、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);void DispPlayTime

12、(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(void)int ph,pm,ps;gettim

13、e(&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; regs0.h.ch=0x30; regs0.h.cl=0x31; int

14、86(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(); ;BlockInitial();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 =

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号