本篇是 PSO 的程式落地篇,概念部分可參考同系列的〈Particle Swarm Optimization 介紹〉。這裡用最簡單的測試函式,把整個流程跑通。
main 幾乎就是照流程圖直譯,每一步都對應一個函式,閱讀起來最直觀:
int main(int argc, const char *argv[])
{
int n = 0;
RandInitofSwarm(); // 隨機初始化族群
ComputFitofSwarm(); // 計算每個粒子的適應度
FirstComputPandGbest(); // 首次決定 pbest 與 gbest
while (n++ != N) {
printf("The %dth time to calculate .\n", n);
UpdateofVandX(); // 更新速度與位置
ComputFitofSwarm(); // 重算適應度
UpdatePandGbest(); // 更新個體最佳與群體最佳
}
getchar();
return 0;
}
那些被註解掉的 printf 是除錯用的,需要觀察中間值時打開即可。
測試問題是搜尋二維空間 y = x² + y² + 3 的最大值,x, y 範圍 (-100, 100),理論最大值 20003。粒子數 20、迭代 40 次、最大速度 2、慣性權重 W = 1.4、學習因子 c1 = c2 = 2。
#ifndef _PSO_H_
#define _PSO_H_
#define Dim 2
#define PNum 20
#define N 40
typedef struct PARTICLE {
double X[Dim]; // 當前位置
double P[Dim]; // 個體最佳位置 pbest
double V[Dim]; // 速度
double Fitness; // 適應度
} particle;
typedef struct SWARM {
particle Particle[PNum];
int GBestIndex;
double GBest[Dim]; // 群體最佳位置 gbest
double W; // 慣性權重
double C1, C2; // 加速度因子
double Xup[Dim], Xdown[Dim]; // 位置上下界
double Vmax[Dim]; // 速度上限
} swarm;
void RandInitofSwarm(void);
void ComputFitofSwarm(void);
void FirstComputPandGbest(void);
void UpdateofVandX(void);
void UpdatePandGbest(void);
double InertWeight(void);
int CriteriaofStop(void);
#endif
swarm *s = (swarm *)malloc(sizeof(swarm));
particle *p = (particle *)malloc(sizeof(particle));
void RandInitofSwarm(void)
{
int i, j;
s->W = 1.4; s->C1 = 2.0; s->C2 = 2.0;
for (j = 0; j < Dim; j++) {
s->Xdown[j] = -100;
s->Xup[j] = 100;
s->Vmax[j] = 2;
}
srand((unsigned)time(NULL));
for (i = 0; i < PNum; i++) {
for (j = 0; j < Dim; j++) {
s->Particle[i].X[j] = rand() / (double)RAND_MAX
* (s->Xup[j] - s->Xdown[j]) + s->Xdown[j];
s->Particle[i].V[j] = rand() / (double)RAND_MAX
* s->Vmax[j] * 2 - s->Vmax[j];
}
}
}
void ComputFitofSwarm(void)
{
int i;
for (i = 0; i < PNum; i++) {
s->Particle[i].Fitness = s->Particle[i].X[0] * s->Particle[i].X[0]
+ s->Particle[i].X[1] * s->Particle[i].X[1] + 3;
}
}
static double ComputAFitness(double X[])
{
return X[0] * X[0] + X[1] * X[1] + 3;
}
void FirstComputPandGbest(void)
{
int i, j;
for (i = 0; i < PNum; i++) // pbest 初始等於当前位置
for (j = 0; j < Dim; j++)
s->Particle[i].P[j] = s->Particle[i].X[j];
s->GBestIndex = 0; // 找初始 gbest
for (i = 0; i < PNum; i++)
if (s->Particle[i].Fitness >= s->Particle[s->GBestIndex].Fitness)
s->GBestIndex = i;
for (j = 0; j < Dim; j++)
s->GBest[j] = s->Particle[s->GBestIndex].P[j];
}
void UpdateofVandX(void)
{
int i, j;
for (i = 0; i < PNum; i++) {
for (j = 0; j < Dim; j++) {
s->Particle[i].V[j] = s->W * s->Particle[i].V[j]
+ rand() / (double)RAND_MAX * s->C1 * (s->Particle[i].P[j] - s->Particle[i].X[j])
+ rand() / (double)RAND_MAX * s->C2 * (s->GBest[j] - s->Particle[i].X[j]);
}
for (j = 0; j < Dim; j++) { // 速度夾限
if (s->Particle[i].V[j] > s->Vmax[j]) s->Particle[i].V[j] = s->Vmax[j];
if (s->Particle[i].V[j] < -s->Vmax[j]) s->Particle[i].V[j] = -s->Vmax[j];
}
for (j = 0; j < Dim; j++) { // 位置更新並夾限
s->Particle[i].X[j] += s->Particle[i].V[j];
if (s->Particle[i].X[j] > s->Xup[j]) s->Particle[i].X[j] = s->Xup[j];
if (s->Particle[i].X[j] < s->Xdown[j]) s->Particle[i].X[j] = s->Xdown[j];
}
}
}
void UpdatePandGbest(void)
{
int i, j;
for (i = 0; i < PNum; i++) // 比現在好就更新 pbest
if (s->Particle[i].Fitness > ComputAFitness(s->Particle[i].P))
for (j = 0; j < Dim; j++)
s->Particle[i].P[j] = s->Particle[i].X[j];
for (i = 0; i < PNum; i++) // 再更新 gbest
if (ComputAFitness(s->Particle[i].P) >= s->Particle[s->GBestIndex].Fitness)
s->GBestIndex = i;
for (j = 0; j < Dim; j++)
s->GBest[j] = s->Particle[s->GBestIndex].P[j];
printf("GBestIndex , GBest , Fitness of GBest:%d ,%.2f ,%.2f ,%.2f \n",
s->GBestIndex, s->GBest[0], s->GBest[1], ComputAFitness(s->GBest));
}
第一,ComputFitofSwarm 與 UpdateofVandX 裡都重新呼叫了 srand((unsigned)time(NULL))。在同一秒內連續呼叫會得到完全相同的種子,導致「隨機」因子失去意義,實務上只在 main 開頭播種一次就好。
第二,particle *p 配置了卻從未使用,s 與 p 也都沒有 free,記得補上 free(s); free(p);。
第三,收斂條件寫成固定迭代 N,而 CriteriaofStop() 已備好卻沒被呼叫;若要提早停止,把它接進 while 條件即可。由於這個測試函式是單峰且向上開口,粒子很快就會全部貼到 (100, 100),收斂到 20003。