【问题标题】:My Particle Swarm Optimization code generates different answers in C++ and MATLAB我的粒子群优化代码在 C++ 和 MATLAB 中生成不同的答案
【发布时间】:2012-08-04 20:35:51
【问题描述】:

我用 C++ 编写了一个全局版本的粒子群优化算法。 我尝试将其编写为与我之前编写的 MATLAB PSO 代码完全相同,但是此代码会生成不同且最糟糕的答案。 MATLAB代码是:

clear all;

numofdims = 30;
numofparticles = 50;
c1 = 2;
c2 = 2;
numofiterations = 1000;
V = zeros(50, 30);
initialpop = V;
Vmin = zeros(30, 1);
Vmax = Vmin;
Xmax = ones(30, 1) * 100;
Xmin = -Xmax;
pbestfits = zeros(50, 1);
worsts = zeros(50, 1);
bests = zeros(50, 1);
meanfits = zeros(50, 1);
pbests = zeros(50, 30);

initialpop = Xmin + (Xmax - Xmin) .* rand(numofparticles, numofdims);

X = initialpop;
fitnesses = testfunc1(X);
[minfit, minfitidx] = min(fitnesses);
gbestfit = minfit;
gbest = X(minfitidx, :);

for i = 1:numofdims
    Vmax(i) = 0.2 * (Xmax(i) - Xmin(i));
    Vmin(i) = -Vmax(i);
end

for t = 1:1000
    w = 0.9 - 0.7 * (t / numofiterations);

    for i = 1:numofparticles
        if(fitnesses(i) < pbestfits(i))
            pbestfits(i) = fitnesses(i);
            pbests(i, :) =  X(i, :);
        end
    end
    for i = 1:numofparticles
        for j = 1:numofdims
            V(i, j) = min(max((w * V(i, j) + rand * c1 * (pbests(i, j) - X(i, j))...
                + rand * c2 * (gbest(j) - X(i, j))), Vmin(j)), Vmax(j));
            X(i, j) = min(max((X(i, j) + V(i, j)), Xmin(j)), Xmax(j));
        end
    end

    fitnesses = testfunc1(X);
    [minfit, minfitidx] = min(fitnesses);
    if(minfit < gbestfit)
        gbestfit = minfit;
        gbest = X(minfitidx, :);
    end

    worsts(t) = max(fitnesses);
    bests(t) = gbestfit;
    meanfits(t) = mean(fitnesses);
end

其中,testfunc1为:

function [out] = testfunc1(R)
    out = sum(R .^ 2, 2);
end

C++ 代码是:

#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <ctime>

#define rand_01 ((float)rand() / (float)RAND_MAX)

const int numofdims = 30;
const int numofparticles = 50;

using namespace std;

void fitnessfunc(float X[numofparticles][numofdims], float fitnesses[numofparticles])
{
    memset(fitnesses, 0, sizeof (float) * numofparticles);
    for(int i = 0; i < numofparticles; i++)
    {
        for(int j = 0; j < numofdims; j++)
        {
            fitnesses[i] += (pow(X[i][j], 2));
        }
    }
}

float mean(float inputval[], int vallength)
{
    int addvalue = 0;
    for(int i = 0; i < vallength; i++)
    {
        addvalue += inputval[i];
    }
    return (float)(addvalue / vallength);
}

void PSO(int numofiterations, float c1, float c2,
              float Xmin[numofdims], float Xmax[numofdims], float initialpop[numofparticles][numofdims],
              float worsts[], float meanfits[], float bests[], float *gbestfit, float gbest[numofdims])
{
    float V[numofparticles][numofdims] = {0};
    float X[numofparticles][numofdims];
    float Vmax[numofdims];
    float Vmin[numofdims];
    float pbests[numofparticles][numofdims];
    float pbestfits[numofparticles];
    float fitnesses[numofparticles];
    float w;
    float minfit;
    int   minfitidx;

    memcpy(X, initialpop, sizeof(float) * numofparticles * numofdims);
    fitnessfunc(X, fitnesses);
    minfit = *min_element(fitnesses, fitnesses + numofparticles);
    minfitidx = min_element(fitnesses, fitnesses + numofparticles) - fitnesses;
    *gbestfit = minfit;
    memcpy(gbest, X[minfitidx], sizeof(float) * numofdims);

    for(int i = 0; i < numofdims; i++)
    {
        Vmax[i] = 0.2 * (Xmax[i] - Xmin[i]);
        Vmin[i] = -Vmax[i];
    }

    for(int t = 0; t < 1000; t++)
    {
        w = 0.9 - 0.7 * (float) (t / numofiterations);

        for(int i = 0; i < numofparticles; i++)
        {
            if(fitnesses[i] < pbestfits[i])
            {
                pbestfits[i] = fitnesses[i];
                memcpy(pbests[i], X[i], sizeof(float) * numofdims);
            }
        }
        for(int i = 0; i < numofparticles; i++)
        {
            for(int j = 0; j < numofdims; j++)
            {
                V[i][j] = min(max((w * V[i][j] + rand_01 * c1 * (pbests[i][j] - X[i][j])
                                   + rand_01 * c2 * (gbest[j] - X[i][j])), Vmin[j]), Vmax[j]);
                X[i][j] = min(max((X[i][j] + V[i][j]), Xmin[j]), Xmax[j]);
            }
        }

        fitnessfunc(X, fitnesses);
        minfit = *min_element(fitnesses, fitnesses + numofparticles);
        minfitidx = min_element(fitnesses, fitnesses + numofparticles) - fitnesses;
        if(minfit < *gbestfit)
        {
            *gbestfit = minfit;
            memcpy(gbest, X[minfitidx], sizeof(float) * numofdims);
        }

        worsts[t] = *max_element(fitnesses, fitnesses + numofparticles);
        bests[t] = *gbestfit;
        meanfits[t] = mean(fitnesses, numofparticles);
    }


}

int main()
{
    time_t t;
    srand((unsigned) time(&t));

    float xmin[30], xmax[30];
    float initpop[50][30];
    float worsts[1000], bests[1000];
    float meanfits[1000];
    float gbestfit;
    float gbest[30];
    for(int i = 0; i < 30; i++)
    {
        xmax[i] = 100;
        xmin[i] = -100;
    }
    for(int i = 0; i < 50; i++)
        for(int j = 0; j < 30; j++)
        {
            initpop[i][j] = rand() % (100 + 100 + 1) - 100;
        }

    PSO(1000, 2, 2, xmin, xmax, initpop, worsts, meanfits, bests, &gbestfit, gbest);

    cout<<"fitness: "<<gbestfit<<endl;
    return 0;
}

我已经多次调试了两个代码,但找不到导致答案不同的差异。 它让我发疯! 你能帮帮我吗?

更新:

请注意,mean函数仅用于报告一些信息,并未用于优化过程。

【问题讨论】:

  • 而那些不同的答案将是......?
  • 在 MATLAB 中:大约 10^-80,在 C++ 中:对于这个函数,大约 2000
  • 两者中哪一个接近您认为正确的答案?
  • MATLAB 如此接近。对于 30 维变量,函数是 sum(X(dimensionnum)^2)。此函数的最小值应为 0,MATLAB 代码可以大致找到它。
  • IIRC,MATLAB 默认为双精度浮点值,因此在 C++ 中等效类型为 'double'。

标签: c++ matlab porting mathematical-optimization particle-swarm


【解决方案1】:

您在以下行中有整数除法 w = 0.9 - 0.7 * (float) (t / numofiterations); 每次迭代 w 将为 0.2,将其更改为 w = 0.9 - 0.7 * t / numofiterations;

第一次乘法将自动将 t 提升为双精度,然后除法应将 numof 迭代提升为双精度。

括号表示它将首先完成,因此不会被提升,因为除法中涉及两个整数。

【讨论】:

  • 非常感谢。您的解决方案可以完成工作。现在,适应度达到 0 左右。这是完美的。而你的回答对我帮助很大。我希望你在生活的各个方面都能取得成功。
【解决方案2】:

这可能是函数mean中的错误:

return (float)(addvalue / vallength);

这是整数除法,所以结果被截断,然后转换为浮点数。这不太可能是您想要的。

【讨论】:

  • 还要记住,在 Matlab 中浮点精度是双倍的,而在 C++ 中是浮点精度。
  • 但是函数mean不是必须的,只是用来上报一些信息。它不用于优化过程。
  • @Hamed 好的,所以你预计会被截断吗?
  • @juanchopanza:对于这个函数的结果,是的。这对我来说并不重要。重要的结果是 gbestfit,它应该是 0,但在 C++ 中约为 2000,在 MATLAB 中约为 10^-80。 (预计它不会正好为 0,但 c++ 的结果是如此糟糕。)
  • @Hamed 这就是我想要得到的。如果您在一个地方做了一些奇怪的事情,那么您很可能在其他地方也犯了同样的错误。但我不打算复习所有的代码:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多