public static double SampleGaussian(SafeRandom random, double mean, double stddev) { // The method requires sampling from a uniform random of (0,1] // but Random.NextDouble() returns a sample of [0,1). double x1 = 1 - random.NextDouble(); double x2 = 1 - random.NextDouble(); double y1 = Math.Sqrt(-2.0 * Math.Log(x1)) * Math.Cos(2.0 * Math.PI * x2); return(y1 * stddev + mean); }
public static TimeSpan NextTimeSpan(this SafeRandom random, TimeSpan timeSpan) { if (timeSpan <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(timeSpan), timeSpan, "SafeRandom.NextTimeSpan timeSpan must be a positive number."); } double ticksD = timeSpan.Ticks * random.NextDouble(); long ticks = checked ((long)ticksD); return(TimeSpan.FromTicks(ticks)); }
public void UpdateVelocity(Particle bestSwarmParticle, Particle globallyBestParticle) { for (var index = 0; index < Parameters.Count; index++) { var parameter = Parameters[index]; var bestParameter = BestParameters[index]; var bestSwarmParameter = bestSwarmParticle.BestParameters[index]; var globallyBestParameter = globallyBestParticle.BestParameters[index]; var velocity = _inertia * _velocities[index]; var particleMagnitude = SafeRandom.NextDouble() * _influence; velocity += particleMagnitude * (bestParameter.Value - parameter.Value); var swarmMagnitude = SafeRandom.NextDouble() * ParticleSwarm.Influence; velocity += swarmMagnitude * (bestSwarmParameter.Value - parameter.Value); var allSwarmsMagnitude = SafeRandom.NextDouble() * ParticleSwarms.Influence; velocity += allSwarmsMagnitude * (globallyBestParameter.Value - parameter.Value); _velocities[index] = velocity; } }
public void Iterate(Board Board, Search Search, Evaluation Evaluation) { var bestParticle = GetBestParticle(); for (var index = 0; index < Particles.Count; index++) { var particle = Particles[index]; if (!ReferenceEquals(particle, bestParticle) && (SafeRandom.NextDouble() <= _particleDeathPercent)) { // Recreate particle at random location. particle = new Particle(particle.PgnGames, particle.Parameters.DuplicateWithRandomValues()); Particles[index] = particle; } particle.Move(); particle.ConfigureEvaluation(Evaluation); particle.CalculateEvaluationError(Board, Search, _winPercentScale); } }
public double NextDouble() => _random.NextDouble();