⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 as.txt

📁 蚁群算法小程序(C/C++语言实现) 蚁群算法小程序(C/C++语言实现)
💻 TXT
📖 第 1 页 / 共 3 页
字号:
    
    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; 
    }
    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))
                   {
                                ms = MAX_SMELL;
                             break;
                   }
                            if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break; 
        case DOWN:  for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
                        for(j=yyy+1;j<=yyy+ANT_EYESHOT;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))
                   {
                                ms = MAX_SMELL;
                             break;
                   }
                            if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break; 
        case LEFT:  for(i=xxx-ANT_EYESHOT;i<xxx;i++)
                        for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;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))
                   {
                                ms = MAX_SMELL;
                             break;
                   }
                            if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break; 
        case RIGHT: for(i=xxx+1;i<=xxx+ANT_EYESHOT;i++)
                        for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;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))
                   {
                                ms = MAX_SMELL;
                             break;
                   }
                            if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break; 
        default:    break;
    }
    return(ms);
}

int IsTrace(int xxx,int yyy)
{
    int i;
    
    for(i=0;i<TRACE_REMEMBER;i++)
        if(ant[AntNow].tracex[i]==xxx&&ant[AntNow].tracey[i]==yyy) return(1);
    return(0);  
}

int MaxLocation(int num1,int num2,int num3)
{
    int maxnum;
    
    if(num1==0&&num2==0&&num3==0) return(0);
    
    maxnum = num1;
    if(num2>maxnum) maxnum = num2;
    if(num3>maxnum) maxnum = num3;
    
    if(maxnum==num1) return(1);
    if(maxnum==num2) return(2);
    if(maxnum==num3) return(3); 
}

int CanGo(int xxx,int yyy,int ddir)
/* input: xxx,yyy - location of ant
          ddir - now dir
   output: 0 - forward and left and right can go
           1 - forward can not go
           2 - left can not go
           3 - right can not go
           4 - forward and left can not go
           5 - forward and right can not go
           6 - left and right can not go
           7 - forward and left and right all can not go
*/
{
    int tx,ty,tdir;
    int okf,okl,okr;
    
    /* forward can go ? */
    tdir = ddir;
    tx = xxx;
    ty = yyy;
    switch(tdir)
    {
        case UP:    ty--;
                    break; 
        case DOWN:  ty++;
                    break; 
        case LEFT:  tx--;
                    break; 
        case RIGHT: tx++;
                    break; 
        default:    break;
    } /* of switch dir */
    if(JudgeCanGo(tx,ty)) okf = 1;
    else okf = 0;
    
    /* turn left can go ? */
    tdir = TurnLeft(ddir);
    tx = xxx;
    ty = yyy;
    switch(tdir)
    {
        case UP:    ty--;
                    break; 
        case DOWN:  ty++;
                    break; 
        case LEFT:  tx--;
                    break; 
        case RIGHT: tx++;
                    break; 
        default:    break;
    } /* of switch dir */
    if(JudgeCanGo(tx,ty)) okl = 1;
    else okl = 0;
    
    /* turn right can go ? */
    tdir = TurnRight(ddir);
    tx = xxx;
    ty = yyy;
    switch(tdir)
    {
        case UP:    ty--;
                    break; 
        case DOWN:  ty++;
                    break; 
        case LEFT:  tx--;
                    break; 
        case RIGHT: tx++;
                    break; 
        default:    break;
    } /* of switch dir */
    if(JudgeCanGo(tx,ty)) okr = 1;
    else okr = 0;
    
    if(okf&&okl&&okr) return(0);
    if(!okf&&okl&&okr) return(1);
    if(okf&&!okl&&okr) return(2);
    if(okf&&okl&&!okr) return(3);
    if(!okf&&!okl&&okr) return(4);
    if(!okf&&okl&&!okr) return(5);
    if(okf&&!okl&&!okr) return(6);
    if(!okf&&!okl&&!okr) return(7);
    return(7);
}

int JudgeCanGo(int xxx,int yyy)
/* input: location to judeg
   output: 0 -- can not go
           1 -- can go
*/
{
    int i,j;
    
    if(xxx<=0||xxx>MAXX) return(0);
    if(yyy<=0||yyy>MAXY) return(0);
    if(block[xxx][yyy]) return(0);
    return(1);
}

int TurnLeft(int ddir)
{
    switch(ddir)
    {
        case UP:    return(LEFT);
        case DOWN:  return(RIGHT);
        case LEFT:  return(DOWN);
        case RIGHT: return(UP);
        default:    break;
    } /* of switch dir */
}

int TurnRight(int ddir)
{
    switch(ddir)
    {
        case UP:    return(RIGHT);
        case DOWN:  return(LEFT);
        case LEFT:  return(UP);
        case RIGHT: return(DOWN);
        default:    break;
    } /* of switch dir */
}

int TurnBack(int ddir)
{
    switch(ddir)
    {
        case UP:    return(DOWN);
        case DOWN:  return(UP);
        case LEFT:  return(RIGHT);
        case RIGHT: return(LEFT);
        default:    break;
    } /* of switch dir */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -