/// <inheritdoc/> public void Resample(AbstractParticleController parCon) { parCon.NormalizeWeights(); int[] indexes = Multinomial(parCon); float[] newParticleValues = new float[indexes.Length]; for (int i = 0; i < newParticleValues.Length; i++) { newParticleValues[i] = parCon.GetValueAt(indexes[i]); } parCon.Values = newParticleValues; }
public void Setup() { this.rngsource = new Mock <IContinuousDistribution>(); this.rngsource.SetupGet(foo => foo.Maximum).Returns(1); this.rngsource.SetupGet(foo => foo.Minimum).Returns(0); this.rngsource.Setup(foo => foo.Sample()).Returns(this.addedNoise); this.particleGenerator = new Mock <IParticleGenerator>(); this.particleGenerator.Setup(foo => foo.Generate(It.IsAny <int>(), It.IsAny <float>(), It.IsAny <float>())) .Returns <int, float, float>((par, min, max) => Enumerable.Repeat(min, par).ToArray()); this.particles = new LinearParticleController(this.particleGenerator.Object, this.particleCount, 0, 1); this.rng = new RandomNoiseGenerator(this.rngsource.Object); }
public void Setup() { this.particleGenerator = new Mock <IParticleGenerator>(); this.particleGenerator.Setup(foo => foo.Generate(It.IsAny <int>(), It.IsAny <float>(), It.IsAny <float>())).Returns <int, float, float>((par, min, max) => { float[] res = new float[par]; for (int i = 0; i < par; i++) { res[i] = (((float)i / par) * (max - min)) + min; } return(res); }); this.controller = new LinearParticleController(this.particleGenerator.Object, this.particleAmount, 0, 200); this.mr = new MultinomialResampler(); }
/// <summary> /// Selects a list of indexes of the Particles that survived the resampling. /// </summary> /// <param name="particles">The Particles in a certain dimension</param> /// <returns>The list with indexes of the Particles that are chosen by the resample algorithm</returns> private static int[] Multinomial(AbstractParticleController particles) { int[] listout = new int[particles.Count]; IList <float> weights = CumSum(particles.Weights); weights[weights.Count - 1] = 1; int i = 0; while (i < weights.Count) { double rand = random.NextDouble(); int j = 0; while (weights[j] < rand) { j++; } listout[i] = j; i++; } return(listout); }
/// <inheritdoc/> public void GenerateNoise(float percentage, AbstractParticleController particles) { float noisesize = percentage * (particles.MaxValue - particles.MinValue); this.GenerateNoise(-noisesize, noisesize, particles); }
/// <inheritdoc/> public void GenerateNoise(float min, float max, AbstractParticleController particles) { float[] noisearray = Enumerable.Repeat(0, particles.Count).Select(i => (float)(min + (this.rng.Sample() * (max - min)))).ToArray(); particles.AddToValues(noisearray); }