粒子群演算法 C 語言的實現

2021-08-28 03:44:55 字數 1363 閱讀 2321

最近開始重新學習c語言,在研究生期間,坐在dspa實驗室編寫了基本的粒子群演算法,當時是matlab語言實現的,相對而言比較簡單,c語言是比較原始的基礎性的語言,所以執行速度是非常快的,因此貼上c實現基本粒子群演算法的程式,有不對的地方請指教。

/***************particle swarm algorithm*************************/

/** @author: gong xu**/

# include# include# include#include #define nvar 30  // number of variables

#define npop 50   // number of population

#define iter 1000  // maximum number of iterations

struct particle

;struct pso

;// sum(x^2)

double function_fitness(double *var)

}// end of initialization 

for (int it = 1; it <= iter; it++)

if (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)

if (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)

if (pso.ptcle[i].personal_best_fitness > pso.ptcle[i].fitness)}}

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++)

}void main(void)

粒子群演算法及C語言實現

粒子群演算法是由鳥類覓食啟發而來,鳥類會朝著食物豐盛的地方移動,鳥類移動的指導思想有兩點。1是每只鳥自身的移動過程中記下了自己曾遇到過的最好的食物所在的位置。2是整個鳥群中的鳥類在覓食過程中會交流自己所遇到的最好食物所在位置,會通知其他鳥過來。這兩點就是鳥類的移動規則。有了基本思想,那鳥類覓食過程中...

C語言 粒子群演算法(PSO)

for int i 0 ifor int j 0 j0,1 population i his best j population i location j c2 randvar 0,1 global best j population i location j population i locati...

粒子群優化演算法,C 實現

粒子群演算法是一門新興演算法,此演算法與遺傳演算法有很多相似之處,其收斂於全域性最優解的概率很大。相較於傳統演算法計算速度非常快,全域性搜尋能力也很強 pso對於種群大小不十分敏感,所以初始種群設為500 1000,速度影響也不大 粒子群演算法適用於連續函式極值問題,對於非線性 多峰問題均有較強的全...