最近开始重新学习C语言,在研究生期间,坐在DSPA实验室编写了基本的粒子群算法,当时是MATLAB语言实现的,相对而言比较简单,C语言是比较原始的基础性的语言,所以执行速度是非常快的,因此贴上C实现基本粒子群算法的程序,有不对的地方请指教。
/***************Particle Swarm Algorithm*************************/
/*
* @Author: Gong Xu
* (c) Copyright 2018 ,DSP Laboratory of Lanzhou University of Technology, All rights reserved.
*
*/
# include<stdio.h>
# include<time.h>
# include<stdlib.h>
#include <string.h>
#define nVar 30 // Number of variables
#define nPop 50 // Number of Population
#define Iter 1000 // Maximum number of iterations
struct particle
{
double position[nVar];
double velecity[nVar];
double personal_best_position[nVar];
double personal_best_fitness;
double fitness;
};
struct PSO
{
struct particle ptcle [nPop];
double c1;
double c2;
double w;
double (*function)(double *); // Function pointer definition
double global_fitness;
double global_solution[nVar];
double x_low_bound;
double x_up_bound;
double v_low_bound;
double v_up_bound;
};
// sum(X^2)
double function_fitness(double *var)
{
double result=0;
for(int i=0;i<nVar;i++)
{
result += *(var + i)* *(var + i);// method one : pointer
//result += var[i] * var[i]; // method two : array
}
return result;
}
void runpso(double w,double c1,double c2,double x_low_bound,double x_up_bound,double (*function_fitness)())
{
struct PSO pso;
pso.w = w;
pso.c1 = c1;
pso.c2 = c2;
pso.x_low_bound = x_low_bound;
pso.x_up_bound = x_up_bound;
pso.v_low_bound=0.5*pso.x_low_bound;
pso.v_up_bound=0.5*pso.x_up_bound;
pso.function = function_fitness;
srand((unsigned)time(NULL));
// inital pso for iteration program
for(int i=0;i<nPop;i++)
{
for(int j=0;j<nVar;j++)
{
double randx = (double)rand() / RAND_MAX;
pso.ptcle[i].position[j] = pso.x_low_bound + (pso.x_up_bound - pso.x_low_bound)*randx;
pso.ptcle[i].velecity[j] = 0.1*pso.ptcle[i].position[j];
pso.ptcle[i].personal_best_position[j]=pso.ptcle[i].position[j];
}
pso.ptcle[i].fitness = pso.function(&pso.ptcle[i].position);// Transmission of structural members
pso.ptcle[i].personal_best_fitness = pso.function(&pso.ptcle[i].personal_best_position);
memcpy(&pso.global_solution,&pso.ptcle[i].position,sizeof(double)*nVar);
pso.global_fitness=pso.ptcle[i].fitness;
}
for(int i=1;i<nPop;i++)
{
if(pso.global_fitness>pso.ptcle[i].fitness)
{
pso.global_fitness=pso.ptcle[i].fitness;
memcpy(&pso.global_solution,&pso.ptcle[i].position,sizeof(double)*nVar);
}
}
// End of initialization
for (int it = 1; it <= Iter; it++)
{
for (int i = 0; i < nPop; i++)
{
for (int j = 0; j<nVar; j++)
{
srand((unsigned)time(NULL));
double rand1 = (double)rand() / RAND_MAX;
srand((unsigned)time(NULL));
double rand2 = (double)rand() / RAND_MAX;
pso.ptcle[i].velecity[j] =pso.w*pso.ptcle[i].velecity[j] + pso.c1*rand1*(pso.global_solution[j] - pso.ptcle[i].position[j])
+ pso.c2*rand2*(pso.global_solution[j] - pso.ptcle[i].position[j]);
if (pso.ptcle[i].velecity[j]> pso.v_up_bound)
{
pso.ptcle[i].velecity[j] = pso.v_up_bound;
}
if (pso.ptcle[i].velecity[j] < pso.v_low_bound)
{
pso.ptcle[i].velecity[j] = pso.v_low_bound;
}
pso.ptcle[i].position[j] = pso.ptcle[i].position[j] + pso.ptcle[i].velecity[j];
if (pso.ptcle[i].position[j] > pso.x_up_bound)
{
pso.ptcle[i].position[j] = pso.x_up_bound;
}
if (pso.ptcle[i].position[j] < pso.x_low_bound)
{
pso.ptcle[i].position[j] = pso.x_low_bound;
}
pso.ptcle[i].fitness = pso.function(&pso.ptcle[i].position);
if (pso.global_fitness > pso.ptcle[i].fitness)
{
pso.global_fitness = pso.ptcle[i].fitness;
memcpy(&pso.global_solution, &pso.ptcle[i].position, sizeof(double)*nVar);
}
if (pso.ptcle[i].personal_best_fitness > pso.ptcle[i].fitness)
{
pso.ptcle[i].personal_best_fitness = pso.ptcle[i].fitness;
memcpy(&pso.ptcle[i].personal_best_position, &pso.ptcle[i].position, sizeof(double)*nVar);
}
}
}
printf("This is %d iteration, globalfitness is %f\n ", it, pso.global_fitness);
}
printf("The iteration is end, show the soultion and fitness!\n");
printf("global_fitness :%f\n", pso.global_fitness);
for (int i = 0; i < nVar; i++)
{
printf("%f,", pso.global_solution[i]);
}
}
void main(void)
{ // Test the running time of the program . including time.h
clock_t pro_start, pro_finished;
pro_start = clock();
runpso(0.96,2.0,2.0, -100.0,100.0,function_fitness);
pro_finished = clock();
double Total_time = (double)(pro_finished - pro_start) / CLOCKS_PER_SEC;
printf("the program ran for : %3f second", Total_time);
system("pause");
}