Ant Colony Algorithm Applet (c + + language Implementation) (III)

Source: Internet
Author: User

`}/* of have smell to drop * *}/* of have food * *Else/* No food, looking for food * *{if (Ant[antnow). Smellamount[smell_type_home]){Smellnow = Smell[smell_type_home][antx][anty];Smelltodrop = Ant[antnow]. Smellamount[smell_type_home]*smell_drop_rate;if (smelltodrop>smellnow) smell[smell_type_home][antx][anty] = Smelltodrop;/* Else smell[...] = Smellnow *Ant[antnow]. smellamount[smell_type_home]-= Smelltodrop;if (Ant[antnow). smellamount[smell_type_home]<0) Ant[antnow]. Smellamount[smell_type_home] = 0;}/* of have smell to drop * *}}/* of time to go * */Else not go * *}/* of for Antnow * * TextColor (Food_home_color);Gotoxy (HOME.XXX,HOME.YYY); Putch (Home_char);Gotoxy (FOOD.XXX,FOOD.YYY);if (food.amount>0) Putch (Food_char);else Putch (FOOD_CHAR2);TextColor (7); Gotoxy (1,maxy+1);printf ("Food%d, home%d", food.amount,home.amount);} void Antonestep (void){int ddir,tttx,ttty;int i; Ddir = Ant[antnow].dir;TTTX = ant[antnow].xxx; [Page]Ttty = ant[antnow].yyy; Ddir = Antnextdir (Tttx,ttty,ddir); Switch (DDIR){Case up:ttty--;BreakCase down:ttty++;BreakCase left:tttx--;BreakCase right:tttx++;BreakDefault:break;}/* of Switch dir * * Ant[antnow].dir = Ddir;Ant[antnow].xxx = TTTX;ant[antnow].yyy = Ttty; if (Ant[antnow].food)/* This ant carry and food, search for home * *{if (TTTX==HOME.XXX&AMP;&AMP;TTTY==HOME.YYY){home.amount++;Antinitial ();}if (TTTX==FOOD.XXX&AMP;&AMP;TTTY==FOOD.YYY)Ant[antnow]. Smellamount[smell_type_food] = Max_smell;}/* of search for Home * *Else/* This ant are empty, search for food * *{if (TTTX==FOOD.XXX&AMP;&AMP;TTTY==FOOD.YYY){if (food.amount>0){Ant[antnow].food = 1;food.amount--; [Page]Ant[antnow]. Smellamount[smell_type_food] = Max_smell;Ant[antnow]. Smellamount[smell_type_home] = 0;Ant[antnow].dir = turnback (Ant[antnow].dir);for (i=0;i<trace_remember;i++){Ant[antnow].tracex[i] = 0;Ant[antnow].tracey[i] = 0;}Ant[antnow]. traceptr = 0;Canfindfood = 1;}/* of still have food * *}if (TTTX==HOME.XXX&AMP;&AMP;TTTY==HOME.YYY)Ant[antnow]. Smellamount[smell_type_home] = Max_smell;}/* of search for food * *} void Dealkey (char key){int i;Switch (key){Case ' P ': gettime (&endtime);Dispplaytime ();Getch ();Gotoxy (1,maxy+1);for (i=1;i<=maxx-1;i++) putch (space);BreakCase ' t ': if (smelldispflag) [Page]{smelldispflag=0;Clearsmelldisp ();}else Smelldispflag = 1;BreakCase ' 1 ': Dispsmell (Smell_type_food);Getch ();Clearsmelldisp ();BreakCase ' 2 ': Dispsmell (Smell_type_home);Getch ();Clearsmelldisp ();BreakCase ' 3 ': Dispsmell (2);Getch ();Clearsmelldisp ();Break Case ' s ': Saveblock ();BreakCase ' L ': loadblock ();BreakDefault:gotoxy (1,maxy+1);for (i=1;i<=maxx-1;i++) putch (space);}/* of Switch * *} void Clearsmelldisp (void){int k,i,j; for (k=0;k<=1;k++)/* Smell TYPE FOOD and Home * *for (i=1;i<=maxx;i++)for (j=1;j<=maxy;j++){if (Smell[k][i][j]){Gotoxy (I,J);Putch (space);}}/* of one location * *} void Dispsmell (int type)/* input:0-only display food smell1--Only display home smell2--Display both food and home smell*/{int k,i,j;int Fromk,tok;int smelldisp; Switch (type){Case 0:fromk = 0;Tok = 0;BreakCase 1:fromk = 1;Tok = 1;BreakCase 2:fromk = 0;Tok = 1;BreakDefault:fromk = 0;Tok = 1;Break [Page]}Smellgonetimer = 0;for (k=fromk;k<=tok;k++)/* Smell TYPE FOOD and Home * *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;Gotoxy (I,J);if (i!=food.xxx| | J!=FOOD.YYY){if ((i==food.xxx&&j==food.yyy) | | (I==HOME.XXX&AMP;&AMP;J==HOME.YYY))/* Don t over write Food and home * *;`

`Else{if (smelldisp>9) putch (' # ');else Putch (smelldisp+ ' 0 ');}}}}/* of one location * *} int antnextdir (int xxx,int yyy,int ddir){int randnum;int TestDir;int cangostate;int Cangof,cangol,cangor;int msf,msl,msr,maxms;int type; Cangostate = Cango (Xxx,yyy,ddir);if (cangostate==0| | cangostate==2| | cangostate==3| | cangostate==6) Cangof = 1;else Cangof = 0;if (cangostate==0| | cangostate==1| | cangostate==3| | cangostate==5) Cangol = 1;else Cangol = 0;if (cangostate==0| | cangostate==1| | cangostate==2| | cangostate==4) Cangor = 1;else Cangor = 0; if (ant[antnow].food) type = Smell_type_home;else type = Smell_type_food; MSF = Getmaxsmell (Type,xxx,yyy,ddir);MSL = Getmaxsmell (Type,xxx,yyy,turnleft (Ddir));Msr= Getmaxsmell (Type,xxx,yyy,turnright (Ddir));MAXMS = Maxlocation (MSF,MSL,MSR);/* Maxms-1-MSF is MAX2-MSL is MAX3-MSR is MAX0-all 3 # is 0 * * TestDir = NULL;Switch (MAXMS){Case 0:/* All is 0, keep TestDir = NULL, Random select dir * *BreakCase 1:if (CANGOF)TestDir = Ddir;Elseif (MSL&GT;MSR) if (cangol) TestDir = TurnLeft (Ddir);else if (cangor) TestDir = TurnRight (Ddir);BreakCase 2:if (Cangol)TestDir = TurnLeft (Ddir);Elseif (MSF&GT;MSR) if (cangof) TestDir = Ddir;else if (cangor) TestDir = TurnRight (Ddir);BreakCase 3:if (Cangor)TestDir = TurnRight (Ddir);Elseif (MSF&GT;MSL) if (cangof) TestDir =ddir;else if (cangol) TestDir = TurnLeft (Ddir);BreakDefault:break;}/* of MAXMS * * Randnum = random (1000);if (randnum<smell_drop_rate*1000| | Testdir==null)/* 1. If TestDir = NULL, means can not find the max smell or the dir to max smell canThen random select dir2. If ant error, don ' t follow the smell, random select dir*/{Randnum = random (100);Switch (cangostate){Case 0:if (randnum<90) TestDir = Ddir;else if (randnum>=90&&randnum<95) TestDir = TurnLeft (Ddir);else TestDir = TurnRight (Ddir);BreakCase 1:if (randnum<50) TestDir = TurnLeft (Ddir);else TestDir = TurnRight (Ddir);BreakCase 2:if (randnum<90) TestDir = Ddir;else TestDir = TurnRight (Ddir);BreakCase 3:if (randnum<90) TestDir = Ddir;else TestDir = TurnLeft (Ddir);BreakCase 4:testdir = TurnRight (Ddir);BreakCase 5:testdir = TurnLeft (Ddir);BreakCase 6:testdir = Ddir;BreakCase 7:testdir = turnback (Ddir);BreakDefault:testdir = turnback (Ddir);}/* of can go state */}return (TESTDIR);} int Getmaxsmell (int type,int xxx,int yyy,int ddir){int i,j;int MS; * MAX Smell * * ms = 0;Switch (DDIR){Case Up:for (i=xxx-ant_eyeshot;i<=xxx+ant_eyeshot;i++)for (j=yyy-ant_eyeshot;j<yyy;j++){if (! Judgecango (I,J)) continue;if ((I==food.xxx&&j==food.yyy&&type==smell_type_food) | |(I==home.xxx&&j==home.yyy&&type==smell_type_home)){`

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.