编辑代码

#include <stdio.h>
#include "math.h"
#include <stdlib.h> 
#include <string.h>


#define ACC_1   1.49445     //加速度因子
#define ACC_2   1.49445
#define MAXCNT  300         //迭代次数
#define popNum  20          //种群规模

#define Vmax    0.5         //速度最大值
#define Vmin    -0.5        //速度最小值
#define VIM     2           //粒子的维数
#define PI      3.1415926   //圆周率

//个体取值
#define popMax  2
#define popMin -2
/************************************************************************************************************************/

//种群数
typedef struct{
    double popu[VIM];
    double itrV[VIM];

    double pbest[VIM];
    double fitnesspbest;
    double fitness;
}TPSOITRDATA;

//迭代数
typedef struct 
{
    double itrRlt;
    double gblRlt[VIM];
}TPSOResult;

double psoFitCalc(double *arr);
void   popInit(void);

/************************************************************************************************************************/
//PSO 公用数据结构

TPSOITRDATA psoData[popNum];
TPSOResult  psoResult[MAXCNT];
double psoBestLocat[VIM];
double psoBestValue;
/************************************************************************************************************************/
/**
 * @fun     popInit
 * @brief   种群初始化
 * 
 * @para    popu:优化变量范围
 *          itrV:变化速度
 * @details
 * */ 
void popInit(void){
    for(int i=0;i<popNum;i++)
    {
        for(int j=0;j<VIM;j++)
        {
            psoData[i].popu[j] = (((double)rand())/RAND_MAX-0.5)*4; //-2到2之间的随机数
            psoData[i].itrV[j] = ((double)rand())/RAND_MAX-0.5; //-0.5到0.5之间
        }
        psoData[i].fitness = psoFitCalc(&psoData[i].popu[0]); //计算适应度函数值
    }
}

/**
 * @fun      psoFitCalc
 * @brief    种群适应度计算/优化目标计算
 * @author
 * @para     fitness:优化函数
 * @details
*/
double psoFitCalc(double *arr){
    double x = *arr; //x 的值
    double y = *(arr+1); //y的值
    double fitness = sin(sqrt(x*x+y*y))/(sqrt(x*x+y*y)) + exp((cos(2*PI*x)+cos(2*PI*y))/2) - 2.71289;
    return fitness;
}

/**
 * @fun      Max
 * @brief    数组最大值
 * @author
 * @para     
 * @details
*/
double * psoRltMax(double *fitness,int length){
    int index = 0; 
    double max = *fitness; 
    static double best_fit_index[2];
    for(int i=1;i<length;i++)
    {
        if(*(fitness+i) > max)
        max = *(fitness+i);
        index = i;
    }
    best_fit_index[0] = index;
    best_fit_index[1] = max;
    return best_fit_index;
}

/**
 * @fun     findPsoOptValue
 * @brief   寻种群最优极值/位置
 * 
*/
double findPsoOptValue(){


}



/**
 * @fun      PSOItrCore
 * @brief    PSO优化核函数
 * @author
 * @details
*/
void PSOItrCore(){

    int Index;
    double * psoItrIndex;
    double * best_fit_index; 

//  初始化
    popInit();
    best_fit_index = psoRltMax(&psoData[0].fitness, popNum);
    Index = (int)(*best_fit_index);

    memcpy(psoBestLocat,psoData[Index].popu,VIM);
    for(int i=0; i<popNum; i++){
        memcpy(psoData[i].pbest,psoData[i].popu,VIM);
        psoData[i].fitnesspbest = psoData[i].fitness;
    }
    psoBestValue = *(best_fit_index+1);

//  PSO迭代计算
    for(int i=0;i<MAXCNT;i++){
        
        //速度更新及粒子更新
        for(int j=0;j<popNum;j++){
            for(int k=0;k<VIM;k++){

                // 速度更新
                double rand1 = (double)rand()/RAND_MAX; //0到1之间的随机数
                double rand2 = (double)rand()/RAND_MAX; 
                psoData[j].itrV[k] = psoData[j].itrV[k] + \
                    ACC_1*rand1*(psoData[j].pbest[k]-psoData[j].popu[k]) + ACC_2*rand2*(psoBestLocat[k]-psoData[j].popu[k]);
                if(psoData[j].itrV[k] > Vmax) psoData[j].itrV[k] = Vmax;
                if(psoData[j].itrV[k] < Vmin) psoData[j].itrV[k] = Vmin;  

                psoData[j].popu[k] = psoData[j].popu[k] + psoData[j].itrV[k]; 
                if(psoData[j].popu[k] > popMax)  psoData[j].popu[k]  = popMax;      
                if(psoData[j].popu[k] < popMin)  psoData[j].popu[k]  = popMin;           
            }
            psoData[j].fitness = psoFitCalc(psoData[j].popu);
        }

        //极值更新
        for(int j=0;j<popNum;j++){
            if(psoData[j].fitness > psoData[j].fitnesspbest ){
                memcpy(psoData[i].pbest,psoData[j].popu,VIM);
                psoData[i].fitnesspbest = psoData[j].fitness;
            }
            if(psoData[j].fitness > psoBestValue ){
                memcpy(psoBestLocat,psoData[j].popu,VIM);
                psoBestValue = psoData[j].fitness;
            }

        }

        memcpy(psoResult[i].gblRlt,psoBestLocat,VIM);
        psoResult[i].itrRlt = psoBestValue;

    }
}

void main(){
    
        PSOItrCore();
        double * best_arr;
        best_arr = psoRltMax(&psoData[0].fitness,popNum);
        int best_gen_number = *best_arr; // 最优值所处的代数
        double best = *(best_arr+1);       //最优值
        printf("迭代了%d次,在第%d次取到最优值,最优值为:%lf.\n",MAXCNT,best_gen_number+1,best);
    //    printf("取到最优值的位置为(%lf,%lf).\n",genbest[best_gen_number][0],genbest[best_gen_number][1]);

}