蚁群算法

一、 算法步骤
1 初始化城市的距离矩阵
2 用贪心算法,随机选择起点,根据信息素初始化公式初始化信息素矩阵
3 蚁群的每一只蚂蚁随机选择出发点,通过计算公式算出转移到其他城市的概率,再根据轮盘赌算法选择下一个城市,直到城市全部走完
4 根据蚂蚁所走路线的长度,使用参数公式更新信息素矩阵,并得到局部最优解
5 迭代3和4直至结果满足特定条件或设定迭代次数,得到全局最优解。
此处简化了第一步,初始化的城市的距离矩阵如下:

    city[0][1] = city[1][0] = 3;
    city[0][2] = city[2][0] = 1;
    city[0][3] = city[3][0] = 2;
    city[1][2] = city[2][1] = 5;
    city[1][3] = city[3][1] = 4;
    city[2][3] = city[3][2] = 2;

二、源代码

/************************************************************
//蚁群算法,可用于解决旅行商等TSP问题
//代码中注释掉很多输出流语句,原用于检查函数算法是否出现问题
//
************************************************************/
#include 
#include 
#include 
#include 
using namespace std;
int city[4][4];
float imf_record[4][4];
int num_city=4;
int num_ant=3;
const int alpha=1;
const int blta=2;
const float run=0.5;
int visited[4]={0,0,0,0};
int ant_road[3][5];
int ant_length[3]={0};
int xth_ant=0;
int yth_city=1;
bool IsIn(int i,int j,int a,int b,int c,int d,int e )//to judge whether (i,j) belongs to Ck
{
    if((i==a)&&(j==b))return true;
    if((i==b&&j==c))return true;
    if((i==c&&j==d))return true;
    if((i==d&&j==e))return true;
    return false;
}

void InitCity()//initial the distances between the cities
{
    city[0][1] = city[1][0] = 3;
    city[0][2] = city[2][0] = 1;
    city[0][3] = city[3][0] = 2;
    city[1][2] = city[2][1] = 5;
    city[1][3] = city[3][1] = 4;
    city[2][3] = city[3][2] = 2;
    for (int i = 0; i < 4; i++)
        city[i][i] =-1;
        cout<<"The initial distances between the cities are:"<for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
            cout<" ";
        cout<int FindNext(int curr_city)//help the greedy method find next shortest city
{
        int curr_min=1000;
        int next_city=0;
        for(int n=0;n<4;n++)
        {
            if(visited[n]==0&&(city[curr_city][n]1)
            {
                curr_min=city[curr_city][n];
                next_city=n;
            }
        }
        visited[next_city]=1;
        return next_city;
}
void InitImf()//initial the informations along each side, using greedy alog.
{
    srand((unsigned)time(NULL));
    int gre_road[5];
    srand((unsigned)time(NULL));
    gre_road[0]=rand() % 4;     //choose a city randomly
    visited[gre_road[0]]=1;

    for(int k=1;k<4;k++)
        gre_road[k]=FindNext(gre_road[k-1]);
    gre_road[4]=gre_road[0];
    int gre_length=0;
    for(int n=0;n<3;n++)
    {
        int row,col;
        row=gre_road[n];
        col=gre_road[n+1];
        gre_length=gre_length+city[row][col];

    }
    gre_length+=city[gre_road[3]][gre_road[4]];
    cout<<"The greedy length is :"<float init_imf;
          init_imf=float(num_ant)/float(gre_length);
          cout<<"The initial imformation is:"<for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
            if(j!=i)
                imf_record[i][j]=init_imf;
            else imf_record[i][j]=0;
    }
        cout<<"The greedy road is:"<0]<<" "<1]<<" "<2]<<" "<3]<<" "<4]<cout<int WheelChoose()//the wheel choose alo to choose the next city to go
{
    float pro_to_[4];
    for(int p=0;p<4;p++)
    {
        pro_to_[p]=0;
    }
    float sum_of_pro=0;
    for (int j = 0; j < 4; j++) //calculate the probality to next city
    {
        if(visited[j]==1)//passed by and i=j
            pro_to_[j]=0;
        else    pro_to_[j]=imf_record[(ant_road[xth_ant][yth_city-1])][j]*(1.0/float(city[ant_road[xth_ant][yth_city-1]][j]))*(1.0/float(city[ant_road[xth_ant][yth_city-1]][j]));//alpha&bela
        sum_of_pro+=pro_to_[j];
    }
    //cout<<"1.xth_ant="<// cout<<"the pro_to_[0,1,2,3"<<"] is "<
    float each_pro[4]={0};//to use wheel selection
    for(int k=0;k<4;k++)
    {
        each_pro[k]=float(pro_to_[k])/float(sum_of_pro);
        //cout<
    }
    //srand((unsigned)time(NULL));
    float ran_num=rand()/float(RAND_MAX);
    //cout<<"the random number between 0~1 is: "<
    float com=0;
    int next_city=0;
    for(int s=0;s<4;s++)                    //0.3 0.5 0.8 1.0
    {
        com+=each_pro[s];
        if(ran_num<=com)
        {
            next_city=s;
            visited[next_city]=1;
            //cout<<"this time visit "<
            break;
        }
    }
    return next_city;
}
void AntMove()//the three ants choose a way to go around the cities
{
    srand((unsigned)time(NULL));
    ant_road[0][0]= rand() % 4;
    ant_road[1][0]= rand() % 4;
    ant_road[2][0]= rand() % 4; //every ant chooses a begin city randomly
    for( xth_ant=0;xth_ant<3;xth_ant++)
    {
        //cout<<"the ant"<
        for(int b=0;b<4;b++)
        {
                visited[b]=0;
        }
        //cout<<"the ant"<
        visited[ant_road[xth_ant][0]]=1;            //set the first city as visited
        //cout<<"the 0 city is:"<

        for( yth_city=1;yth_city<4;yth_city++)
        {
            ant_road[xth_ant][yth_city]=WheelChoose();
            //cout<<"the ant"<
        }
        ant_road[xth_ant][4]=ant_road[xth_ant][0];
        ant_length[xth_ant]=city[ant_road[xth_ant][0]][ant_road[xth_ant][1]]+city[ant_road[xth_ant][1]][ant_road[xth_ant][2]]
                          +city[ant_road[xth_ant][2]][ant_road[xth_ant][3]]+city[ant_road[xth_ant][3]][ant_road[xth_ant][4]];
        /*cout<<"the visited cities are:";
        for(int a=0;a<4;a++)
        {
                cout<
        //cout<<"the ant"<
        //cout<<"the ant_length["<
        //cout<

    }



}

void UpdateImf()//according the ways, update the informations for each road
{
    //cout<<"new imf_record is:"<
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            imf_record[i][j]=(1-run)*imf_record[i][j]+(1.0/float(ant_length[0]))*(IsIn(i,j,ant_road[0][0],ant_road[0][1],ant_road[0][2],ant_road[0][3],ant_road[0][4]))
                                +(1.0/float(ant_length[1]))*(IsIn(i,j,ant_road[1][0],ant_road[1][1],ant_road[1][2],ant_road[1][3],ant_road[1][4]))
                                +(1.0/float(ant_length[2]))*(IsIn(i,j,ant_road[2][0],ant_road[2][1],ant_road[2][2],ant_road[2][3],ant_road[2][4]));

            //cout<
        }
        //cout<
    }
}

/*void Display()//show the best results when the times end
{
    for (int i = 0; i < 3; i++)
    {
        if(ant_length[0]<=ant_length[1]&&ant_length[0]<=ant_length[2])
            {cout<<"The local best solution is "<
void Display()//show the best results when the times end
{
    for (int i = 0; i < 3; i++)
    {
        if((ant_length[0]<=ant_length[1])&&(ant_length[0]<=ant_length[2]))
            {cout<<"The best solution is "<0][0]<<" "<0][1]<<" "<0][2]<<" "<0][3]<<" "<0][4]<<", and its length is:"<0]<break;}
        if((ant_length[1]<=ant_length[2])&&(ant_length[1]<=ant_length[0]))
            {cout<<"The best solution is "<1][0]<<" "<1][1]<<" "<1][2]<<" "<1][3]<<" "<1][4]<<", and its length is:"<1]<break;}
        if((ant_length[2]<=ant_length[0])&&(ant_length[2]<=ant_length[1]))
            {cout<<"The best solution is "<2][0]<<" "<2][1]<<" "<2][2]<<" "<2][3]<<" "<2][4]<<", and its length is:"<2]<break;}
    }
}
int main()
{
    cout<<"Please tell me how many times do you want the ants explore:";
    int t;
    cin>>t;
    InitCity();
    InitImf();
    while(t--)
    {
        AntMove();
        UpdateImf();
        Display();
    }
    cout<"pause");
    return 0;
}

你可能感兴趣的:(算法)