帮助将数学函数转换为代码:线性卡尔曼粒子群优化

发布于 2024-11-04 16:10:16 字数 1182 浏览 4 评论 0原文

我正在尝试将本文中提出的数学转换为:

http://www.bouncingchairs.net/pskalman-lategecco.pdf

第 3 页左右转发为工作代码。算法本身给出 大约第 6 页,但我不会说希腊语或数学;所以暂时我被困住了。

如果我理解代码,它应该像这样运行:

    vt = particle velocity, 1d array
    vbest = best particle velocity 1d array
    v_prime = 1d storage array
    v_hat = 1d storage array

    alpha = 0.45
    sigma = 0.60

    denom = float
    denom_best = float

准备:

for(int i = 0; i < vt.length; i++)
{
     denom += vt[i] ^ 2
     denom_best += vbest[i] ^ 2
}
denom = denom ^ (1/2)
denom_best = denom_best ^ (1/2)

方程 7:

for(int i = 0; i < vt.length; i++)
{
v_prime[i] = alpha * (vt[i]/denom) + (1 - alpha) * (vbest[i]/denom_best)
}

方程 8:

for(int i = 0; i < vt.length; i++)
{ 
 v_hat[i] = Rand_Gauss(v_prime[i], sigma) //gaussian random number with 
                                          //v_prime[i] average, and sigma StDev
}

方程 9:

for(int i = 0; i < vt.length; i++)
{
vt[i] = (alpha * denom + (1 - alpha) * denom_best) * v_hat[i]
}

这是否接近数学所说的?

提前致谢, -JW

I'm trying to convert the math presented on this paper:

http://www.bouncingchairs.net/pskalman-lategecco.pdf

Around page 3 forward into working code. The algorithm itself is given
around page 6, but I don't speak greek or math; so for the time being I'm stuck.

If I understand the code, it should run down like this:

    vt = particle velocity, 1d array
    vbest = best particle velocity 1d array
    v_prime = 1d storage array
    v_hat = 1d storage array

    alpha = 0.45
    sigma = 0.60

    denom = float
    denom_best = float

Prep:

for(int i = 0; i < vt.length; i++)
{
     denom += vt[i] ^ 2
     denom_best += vbest[i] ^ 2
}
denom = denom ^ (1/2)
denom_best = denom_best ^ (1/2)

Equation 7:

for(int i = 0; i < vt.length; i++)
{
v_prime[i] = alpha * (vt[i]/denom) + (1 - alpha) * (vbest[i]/denom_best)
}

Equation 8:

for(int i = 0; i < vt.length; i++)
{ 
 v_hat[i] = Rand_Gauss(v_prime[i], sigma) //gaussian random number with 
                                          //v_prime[i] average, and sigma StDev
}

Equation 9:

for(int i = 0; i < vt.length; i++)
{
vt[i] = (alpha * denom + (1 - alpha) * denom_best) * v_hat[i]
}

Is this even close to what the math is saying?

Thanks in advance,
-JW

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短暂陪伴 2024-11-11 16:10:16

我认为您可能错过了对“norm(...)”的调用。归一化向量只是将向量的每个分量除以长度。在公式 9 中,他们计算了 vt 和 vbest 长度的加权和,并将平均长度乘以 norm(vbar)。你只需将它直接乘以 vbar 即可。

该方程背后的意图似乎是创建一个新的向量 v_{t+1} ,其长度是 vt 和 vbest 的平均长度。然而,vhat 可以是任意长度,因此方程 9 中的乘法大多数时候不会给出正确的答案,除非您强制 vhat 向量的长度恰好为 1。这就是向量范数的作用。

范数只是向量的分量除以长度。因此,将方程 9 的代码替换为如下所示:

vtlen=0
for(int i=0; i<vt.length; i++)
{
    vtlen+=vt[i]*vt[i];
}
vtlen=sqrt(vtlen);
for(int i=0; i<vt.length; i++)
{
    vt[i] = (alpha * denom + (1 - alpha) * denom_best) * (v_hat[i] / vtlen);
}

您还忽略了方程 7 中的范数运算。我还没有读过这篇论文,但这里可能并不严格需要,因为权重总和为 1,向量为已经正常化了。我必须花更多的时间来说服自己,但继续对计算出的 v' 向量进行归一化肯定不会有什么坏处。

I think you might be missing the calls to "norm(...)". Normalizing a vector is just dividing each component of the vector by the length. In Equation 9, they calculate the weighted sum of the lengths of vt and vbest and multiply that average length by norm(vbar). You're just multiplying it by vbar directly.

The intent behind that equation seems to be to create a new vector v_{t+1} whose length is the average length of vt and vbest. However, vhat could be any length at all, so the multiplication in Equation 9 most of the time won't give you the correct answer unless you force the length of the vhat vector to be exactly one. That's what the vector norm does.

The norm is just the components of the vector divided by the length. So replace your code for equation 9 with something like this:

vtlen=0
for(int i=0; i<vt.length; i++)
{
    vtlen+=vt[i]*vt[i];
}
vtlen=sqrt(vtlen);
for(int i=0; i<vt.length; i++)
{
    vt[i] = (alpha * denom + (1 - alpha) * denom_best) * (v_hat[i] / vtlen);
}

You've also ignored the norm operation in Equation 7. I haven't read the paper, but it may not be strictly necessary here as the weights sum to one and the vectors are already normalized. I'd have to spend a bit more time to convince myself one way or the other, but it certainly wouldn't hurt to go ahead and normalize that calculated v' vector too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文