蚁群优化算法 C语言实现

仿生学 模仿生物群体迁移的动作 抽象为数学公式

// 蚁群优化算法
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define  NOA 10
#define  ILIMIT 50
#define  Q 3
#define  RHO 0.8
#define  STEP 10
#define  EPSILON 0.15
#define  SEED 32768


void update(int cost[2][STEP], double pheromone[2][STEP], int mstep[NOA][STEP])
{
    int m;
    int lm;
    int i, j;
    double sum_lm = 0;
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < STEP; ++j)
            pheromone[i][j] *= RHO;
    for(m = 0 ; m < NOA; ++m)
        lm += cost[mstep[m][i]][i];
    for(i = 0; i < STEP; ++i)
        pheromone[mstep[m][i]][i] += Q * (1.0 / lm) ;
    sum_lm += lm;
   }
    printf("%lf\n",sum_lm / NOA);
}


void printp(double pheromone[2][STEP]);
void printmstep(int msstep[NOA][STEP]);
void walk(int cost[NOA][STEP], double pheromone[2][STEP], int mstep[NOA][STEP]);
double rand1();
int rand01();
int main()
{
    int cost[2][STEP] = {{1,1,1,1,1,1,1,1,1,1},{5,5,5,5,5,5,5,5,5,5}};
    double pheromone[2][STEP] = {0};
    int mstep[NOA][STEP];
    int i;
    srand(SEED);
    for(i = 0; i < ILIMIT; ++i)
    {
        printf("%d:\n",i);
        printp(pheromone);
        walk(cost, pheromone, mstep);
        update(cost, pheromone, mstep);
    }
    
    printf("%d:\n",i);
    printp(pheromone);
    return  0;
    
}

    
    void printmstep(int mstep[NOA][STEP])
    {
        int i, j;
        printf("*mstep\n");
        for(i = 0; i < NOA; ++i)
        {
            for(j = 0; j < STEP; ++j)
                printf("%d ",mstep[i][j]);
            printf("\n");
        }
    }
    
void printp(double pheromone[2][STEP])
    {
        int i, j;
        for(i = 0; i < 2; ++i)
        {
            for(j = 0; j < STEP; ++j)
                printf("%4.2lf ",pheromone[i][j]);
               printf("\n");
        }
}
    
    
double rand1()
{
    return (double)rand() / RAND_MAX;
}
    
    
int rand01()
{
    int rnd ;
    while((rnd = rand()) == RAND_MAX);
    return (int)((double)rnd / RAND_MAX * 2);
}

    

    
    void walk(int cost[2][STEP], double pheromone[2][STEP], int mstep[NOA][STEP])
    {
        int m;
        int s;
        for(m = 0; m < NOA ; ++m)
        {
            for(s = 0; s < STEP; ++s)
            {
                if((rand1() < EPSILON) || (abs(pheromone[0][s] - pheromone[1][s]) < 1e-9))
                {
                    mstep[m][s] = rand01();
                }
        else
        {
            if(pheromone[0][s] > pheromone[1][s])
            {
                mstep[m][s] = 0;
            }
            else
            {
                mstep[m][s] = 1;
            }
        }
                
            }
    }
        printmstep(mstep);
    }



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值