我想标准化粒子列表中的权重。这些权重属于粒子对象。我尝试通过将它们除以权重之和来标准化它们。所有的重量都以双打形式声明。当程序从列表的开头开始除法时,值是正确的,但是在第二次或第三次除法之后不久,我得到了错误的结果..这导致运算后的权重总和不是 1,它应该。谁能帮我解决这个问题?也许与线程有关?提前谢谢..
// normalizing weights
double weightsum = 0;
double check = 0;
List<ParticleRobot> temporalparticleSet = new List<ParticleRobot>();
for (int i = 0; i < particleSet.Count; i++)
{
weightsum = weightsum + this.particleSet[i].Weight;
}
Program.Weightsum = weightsum;
Console.WriteLine("Sum of unnormalized particleweights is " + weightsum);
foreach (ParticleRobot p in this.particleSet)
{
Program.Weight = p.Weight;
p.Weight = Program.Weight / Program.Weightsum;
Console.WriteLine("Updated Particleweight is now : " + p.Weight);
}
// checking that they sum up to 1
for (int i = 0; i < particleSet.Count; i++)
{
check = check + this.particleSet[i].Weight;
}
Console.WriteLine("Check: Sum of particles-weights is = " + check);