Ant Colony Algorithm Applet (c + + language Implementation) (II.)

Source: Internet
Author: User

Fclose (Fp_block);
}

void homefoodinitial (void)
{
int randnum;
int homeplace;
/* 1--Home at left-up, food at Right-down
2--Home at Left-down, food at right-up
3--Home at right-up, food at Left-down
4--Home at Right-down, food at left-up/

Randnum = random (100);
if (randnum<25) homeplace = 1;
else if (randnum>=25&&randnum<50) homeplace = 2;
else if (randnum>=50&&randnum<75) 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+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; [Page]
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*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;
/* Food.amount = Max_food; */
Home.amount = 0;
Home. Targetfood = (food.amount<target_food)? Food.amount:TARGET_FOOD;

/* Data correctness Check * *
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 ("World initial fail, no 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)
/* Initial Ant[antnow] * *
{
int randnum;
int i;

Ant[antnow].xxx = home.xxx;
ant[antnow].yyy = home.yyy;

Randnum = random (100);
if (randnum<25) Ant[antnow].dir = up;
else if (randnum>=25&&randnum<50) Ant[antnow].dir = down;
else if (randnum>=50&&randnum<75) Ant[antnow].dir = left; [Page]
else Ant[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<trace_remember;i++)
{
Ant[antnow].tracex[i] = 0;
Ant[antnow].tracey[i] = 0;
}
Ant[antnow]. traceptr = 0;

* a sepecail ant * *
if (antnow==0) ant[antnow].speed = ini_speed;
}

void Worldchange (void)
{
int k,i,j;
int smelldisp;

smellgonetimer+=timer10ms;
if (smellgonetimer>=smell_gone_speed)
{
Smellgonetimer = 0;
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])
{
Smelldisp = 1+ ((10*smell[k][i][j))/(max_smell*smell_drop_rate));
if (smelldisp>=30000| | smelldisp<0) Smelldisp = 30000;
if (Smelldispflag)
{[Page]
Gotoxy (I,J);
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 ');
}
}
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);
}
}
}
}/* of one location * *
}/* of time to change the world * *
}/* The World Change * *

void Antmove (void)
{
int antx,anty;
int Smelltodrop,smellnow;

for (antnow=0; antnow<max_ant; 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);
/* Ant0 is a sepecail ant, use different color * *
if (antnow==0) TextColor (0xd);
if (Ant[antnow].food) Putch (Ant_char_food);
else Putch (ant_char_empty);
if (antnow==0) TextColor (0x7);

/* Remember Trace * *
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;

/* Drop Smell * *
Antx = ant[antnow].xxx;
Anty = ant[antnow].yyy;

if (Ant[antnow].food)
/* have food, looking for home * *
{
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;
/* Else smell[...] = Smellnow *
Ant[antnow]. smellamount[smell_type_food]-= Smelltodrop;
if (Ant[antnow). smellamount[smell_type_food]<0) Ant[antnow]. Smellamount[smell_type_food] = 0;

Contact Us

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.