程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 蟻群算法小程序(C/C++語言實現)(三)

蟻群算法小程序(C/C++語言實現)(三)

編輯:關於C++

} /* 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--;
                    break;
        case DOWN:  ttty++;
                    break;
        case LEFT:  tttx--;
                    break;
        case RIGHT: tttx++;
                    break;
        default:    break;
    } /* of switch dir */

    ant[AntNow].dir = ddir;
    ant[AntNow].xxx = tttx;
    ant[AntNow].yyy = ttty;

    if(ant[AntNow].food)
    /* this ant carry with food, search for home */
    {
        if(tttx==home.xxx&&ttty==home.yyy)
        {
            home.amount++;
            AntInitial();
        }
        if(tttx==food.xxx&&ttty==food.yyy)
            ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = MAX_SMELL;
    } /* of search for home */
    else
    /* this ant is empty, search for food */
    {
        if(tttx==food.xxx&&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&&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);
                    break;
        case 't':   if(SmellDispFlag)[Page]
              {
                        SmellDispFlag=0;
                        ClearSmellDisp();
              }
                    else SmellDispFlag = 1;
                    break;
        case '1':   DispSmell(SMELL_TYPE_FOOD);
                    getch();
                    ClearSmellDisp();
                    break;
        case '2':   DispSmell(SMELL_TYPE_HOME);
                    getch();
                    ClearSmellDisp();
                    break;
        case '3':   DispSmell(2);
                    getch();
                    ClearSmellDisp();
                    break;

  case 's':   SaveBlock();
           break;
        case 'l':   LoadBlock();
           break;
        default:    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 smell
          1 -- Only display home smell
          2 -- Display both food and home smell
*/
{
    int k,i,j;
    int fromk,tok;
    int smelldisp;

    switch(type)
    {
        case 0: fromk = 0;
                tok = 0;
                break;
        case 1: fromk = 1;
                tok = 1;
                break;
        case 2: fromk = 0;
                tok = 1;
                break;
        default: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&&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 MAX
               2 - msl is MAX
               3 - msr is MAX
               0 - all 3 number is 0 */

    testdir = NULL;
    switch(maxms)
    {
        case 0: /* all is 0, keep testdir = NULL, random select dir */
                break;
        case 1: if(cangof)
                    testdir = ddir;
                else
                    if(msl>msr) if(cangol) testdir = TurnLeft(ddir);
                    else if(cangor) testdir = TurnRight(ddir);
                break;
        case 2: if(cangol)
                    testdir = TurnLeft(ddir);
                else
                    if(msf>msr) if(cangof) testdir = ddir;
                    else if(cangor) testdir = TurnRight(ddir);
                break;
        case 3: if(cangor)
                    testdir = TurnRight(ddir);
                else
                    if(msf>msl) if(cangof) testdir =ddir;
                    else if(cangol) testdir = TurnLeft(ddir);
                break;
 default: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 can not go
       then random select dir
       2. 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);
                    break;
            case 1: if(randnum<50) testdir = TurnLeft(ddir);
                    else testdir = TurnRight(ddir);
                    break;
            case 2: if(randnum<90) testdir = ddir;
                    else testdir = TurnRight(ddir);
                    break;
            case 3: if(randnum<90) testdir = ddir;
                    else testdir = TurnLeft(ddir);
                    break;
            case 4: testdir = TurnRight(ddir);
                    break;
            case 5: testdir = TurnLeft(ddir);
                    break;
            case 6: testdir = ddir;
                    break;
            case 7: testdir = TurnBack(ddir);
                    break;
            default: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))
                   {

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved