public void ProportionateParentSelection_VerifySelectionProbability() { var random = new DefaultRandomNumberGenerator("seed".GetHashCode()); var population = new Population(11); var index = 0; population.Fill(() => new TestPhenotype(index, index++)); population.Sort(EAMode.MaximizeFitness); var selection = new ProportionateParentSelection(); var selected = selection.SelectParents(population, 275000, EAMode.MaximizeFitness, random); var bucket = new int[population.Size]; foreach (var(a, b) in selected) { bucket[((TestPhenotype)a).Index]++; bucket[((TestPhenotype)b).Index]++; } for (int i = 0; i < bucket.Length; i++) { bucket[i].Should().BeCloseTo(i * 10000, 1000); } }
public void Shuffle_WithoutNParameter_AllValuesCanChangePosition() { var rng = new DefaultRandomNumberGenerator(); var shuffle = new SortingBasedWeightedLeftShuffle <int>(rng); Assert.That.AllValuesCanChangePositions(shuffle); }
public void Shuffle_WithNParameter_AllNValuesCanChangePosition() { var rng = new DefaultRandomNumberGenerator(); var shuffle = new StochasticAcceptanceBasedWeightedLeftShuffle <int>(rng); Assert.That.AllNValuesCanChangePositions(shuffle); }
public override void BeginInit() { _children = new VisualCollection(this); _rng = new DefaultRandomNumberGenerator(); base.BeginInit(); }
public void SigmaScalingParentSelection_VerifySelectionProbability() { var random = new DefaultRandomNumberGenerator("seed".GetHashCode()); var population = new Population(11); var index = 0; population.Fill(() => new TestPhenotype(index++, index * index)); population.Sort(EAMode.MaximizeFitness); var selection = new SigmaScalingParentSelection(); var selected = selection.SelectParents(population, 275000, EAMode.MaximizeFitness, random); foreach (var(a, b) in selected) { ((TestPhenotype)a).Count++; ((TestPhenotype)b).Count++; } var std = population.Select(p => p.Fitness).StandardDeviation(); var avg = population.Select(p => p.Fitness).Average(); var psum = population.Sum(p => 1 + (p.Fitness - avg) / (2 * std)); foreach (TestPhenotype p in population) { var prob = (1 + (p.Fitness - avg) / (2 * std)) / psum; var expectedCount = prob * 275000 * 2; p.Count.Should().BeCloseTo((int)expectedCount, 1000); } }
public void Shuffle_WithNParameter_AllNValuesCanChangePosition() { var rng = new DefaultRandomNumberGenerator(); var shuffle = new FisherYatesShuffle <ValuePriorityPair <int> >(rng); Assert.That.AllNValuesCanChangePositions(shuffle); }
public void OfOfT_WithRngReturnsValuesAccoringToTheRng() { var rng1 = new DefaultRandomNumberGenerator(987654); var rng2 = new DefaultRandomNumberGenerator(987654); var value = Out.Of <char>(rng1).Values(new[] { 'a', 'b', 'c', 'd' }).PickDistinct(3).ToList(); var value2 = Out.Of <char>(rng2).Values(new[] { 'a', 'b', 'c', 'd' }).PickDistinct(3).ToList(); CollectionAssert.AreEqual(value, value2); }
public void NullStrategyThrowsExceptionForAConstantAdjustment() { IBackoffStrategy decoratedStrategy = null; IRandomNumberGenerator randomNumberGenerator = new DefaultRandomNumberGenerator(); Assert.Throws <ArgumentNullException>(() => { new ConstantAdjustedBackoffStrategy(decoratedStrategy, TimeSpan.Zero); }); }
public void NullStrategyThrowsExceptionForAPercentageAdjustment() { IBackoffStrategy decoratedStrategy = null; IRandomNumberGenerator randomNumberGenerator = new DefaultRandomNumberGenerator(); Assert.Throws <ArgumentNullException>(() => { new PercentageAdjustedBackoffStrategy(decoratedStrategy, 1); }); }
public void NullStrategyThrowsException() { IBackoffStrategy decoratedStrategy = null; IRandomNumberGenerator randomNumberGenerator = new DefaultRandomNumberGenerator(); Assert.Throws <ArgumentNullException>(() => { new MultiplicativeJitterBackoffStrategy(decoratedStrategy, 0, 1); }); }
public void NextDouble_WithoutSeed_ReturnsValuesBetween0And1() { // Arrange var randomValues = new double[Iterations]; var rng = new DefaultRandomNumberGenerator(); // Action for (var i = 0; i < Iterations; i++) { randomValues[i] = rng.NextDouble(); } // Assert Assert.IsTrue(randomValues.All(x => x >= 0 && x < 1)); }
public void NextInt_TwoArgumentsWithoutSeed_ReturnsEquallyDistributedValues() { // Arrange var randomValues = new long[Iterations]; var rng = new DefaultRandomNumberGenerator(); const int AcceptedDeviation = 240; // Action for (var i = 0; i < Iterations; i++) { randomValues[i] = rng.NextInt(1_200, 3_600); } // Assert Assert.IsTrue(randomValues.Sum() / Iterations > 2_400 - AcceptedDeviation); Assert.IsTrue(randomValues.Sum() / Iterations < 2_400 + AcceptedDeviation); }
public void TournamentParentSelection_VerifySelectionProbability() { // Test tournament selection by comparing it to the old "established" tournament selection implementation. var random = new DefaultRandomNumberGenerator("seed".GetHashCode()); for (int tournamentSize = 2; tournamentSize <= 10; tournamentSize++) { var population = new Population(11); var index = 0; population.Fill(() => new TestPhenotype(index++, index * index)); population.Sort(EAMode.MaximizeFitness); var config = new EAConfiguration { TournamentSize = 3, TournamentProbability = 0.5 }; #pragma warning disable CS0618 // Type or member is obsolete var oldselection = new TournamentParentSelectionOld(config); #pragma warning restore CS0618 // Type or member is obsolete var oldselected = oldselection.SelectParents(population, 275000, EAMode.MaximizeFitness, random); foreach (var(a, b) in oldselected) { ((TestPhenotype)a).Count2++; ((TestPhenotype)b).Count2++; } var selection = new TournamentParentSelection(config); var selected = selection.SelectParents(population, 275000, EAMode.MaximizeFitness, random); foreach (var(a, b) in selected) { ((TestPhenotype)a).Count++; ((TestPhenotype)b).Count++; } var counts = population.Cast <TestPhenotype>().Select(p => p.Count).Zip(population.Cast <TestPhenotype>().Select(p => p.Count2), (c1, c2) => (c1, c2)); foreach (var count in counts) { (count.c1 - count.c2).Should().BeLessThan(1000, $"tournament size = {tournamentSize}"); } } }
public void NextDouble_WithoutSeed_ReturnsEquallyDistributedValues() { // Arrange var randomValues = new double[Iterations]; var rng = new DefaultRandomNumberGenerator(); const int AcceptedDeviation = 1000; // Action for (var i = 0; i < Iterations; i++) { randomValues[i] = rng.NextDouble(); } // Assert Assert.IsTrue(randomValues.Sum() > Iterations / 2 - AcceptedDeviation); Assert.IsTrue(randomValues.Sum() < Iterations / 2 + AcceptedDeviation); }
public void SelectParentsShouldSelectCorrectNumber() { var random = new DefaultRandomNumberGenerator("seed".GetHashCode()); var population = new Population(20); for (int i = 0; i < population.Size; i++) { var pmock = new Mock <IPhenotype>(); pmock.SetupGet(p => p.Fitness).Returns(i); pmock.SetupGet(p => p.IsEvaluated).Returns(true); population.Add(pmock.Object); } var selection = new ProportionateParentSelection(); var selected = selection.SelectParents(population, 20, EAMode.MaximizeFitness, random); selected.Count().Should().Be(20); }
public void NextInt_NoArgumentWithSeed_ReturnsAlwaysTheSameValue() { // Arrange var randomValues = new int[Iterations]; var seed = new System.Random().Next(); // Action for (var i = 0; i < Iterations; i++) { var rng = new DefaultRandomNumberGenerator(seed); randomValues[i] = rng.NextInt(); var a = randomValues[i]; Console.WriteLine(randomValues[i]); } // Assert Assert.IsTrue(randomValues.All(x => x == randomValues[0])); }
public void TournamentSelection_PerfTest() { var random = new DefaultRandomNumberGenerator(); var config = new EAConfiguration { TournamentSize = 20, TournamentProbability = 0.5 }; var popsize = 10000; var pop = new Population(popsize); pop.Fill(() => CreatePhenotypeMock(random.NextDouble() * 10).Object); pop.Sort(EAMode.MaximizeFitness); Console.WriteLine($"popsize = {popsize}, tournament size = {config.TournamentSize}"); var watch = new Stopwatch(); watch.Start(); #pragma warning disable CS0618 // Type or member is obsolete IParentSelection selection = new TournamentParentSelectionOld(config); #pragma warning restore CS0618 // Type or member is obsolete foreach (var selected in selection.SelectParents(pop, popsize, EAMode.MaximizeFitness, random)) { } watch.Stop(); Console.WriteLine($"old: {watch.ElapsedMilliseconds} ms"); watch.Restart(); selection = new TournamentParentSelection(config); foreach (var selected in selection.SelectParents(pop, popsize, EAMode.MaximizeFitness, random)) { } watch.Stop(); Console.WriteLine($"new: {watch.ElapsedMilliseconds} ms"); }
public void NextInt_NoArgumentWithoutSeed_ReturnsEquallyDistributedValues() { // Arrange var bitEquals1Counters = new int[31]; // to 31 because first bit indicates +- var rng = new DefaultRandomNumberGenerator(); const int AcceptedDeviation = Iterations / 100; // Action for (var i = 0; i < Iterations; i++) { var value = rng.NextInt(); for (var j = 0; j < 31; j++) { if ((value & (1 << j)) == (1 << j)) { bitEquals1Counters[j]++; } } } // Assert Assert.IsTrue(bitEquals1Counters.All(x => x >= Iterations / 2 - AcceptedDeviation)); Assert.IsTrue(bitEquals1Counters.All(x => x <= Iterations / 2 + AcceptedDeviation)); }
public void TestInitialize() { random = new DefaultRandomNumberGenerator("seed".GetHashCode()); }