public void SelectChromosomes_Generation_ChromosomesZeroFitness() { var target = new RankSelection(); var c1 = Substitute.ForPartsOf <ChromosomeBase>(2); c1.Fitness = 0; var c2 = Substitute.ForPartsOf <ChromosomeBase>(2); c2.Fitness = 0; var c3 = Substitute.ForPartsOf <ChromosomeBase>(2); c3.Fitness = 0; var c4 = Substitute.ForPartsOf <ChromosomeBase>(2); c4.Fitness = 0; var generation = new Generation(1, new List <IChromosome>() { c1, c2, c3, c4 }); var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); }
public void SelectChromosomes_InvalidNumber_Exception() { var target = new RankSelection(); Assert.Catch <ArgumentOutOfRangeException>(() => { target.SelectChromosomes(-1, null); }, "The number of selected chromosomes should be at least 2."); Assert.Catch <ArgumentOutOfRangeException>(() => { target.SelectChromosomes(0, null); }, "The number of selected chromosomes should be at least 2."); Assert.Catch <ArgumentOutOfRangeException>(() => { target.SelectChromosomes(1, null); }, "The number of selected chromosomes should be at least 2."); }
public void SelectChromosomes_NullGeneration_Exception() { var target = new RankSelection(); var actual = Assert.Catch <ArgumentNullException>(() => { target.SelectChromosomes(2, null); }); Assert.AreEqual("generation", actual.ParamName); }
public void SelectChromosomes_NullFitness_Exception() { var target = new RankSelection(); var generation = new Generation(1, new List <IChromosome> { new TspChromosome(10), new TspChromosome(10), new TspChromosome(10), new TspChromosome(10), new TspChromosome(10) }); var actual = Assert.Catch <SelectionException>(() => { target.SelectChromosomes(2, generation); }); Assert.AreEqual("RankSelection: There are chromosomes with null fitness.", actual.Message); }
public void SelectChromosomes_Generation_ChromosomesSelected() { var target = new RankSelection(); var c1 = Substitute.ForPartsOf <ChromosomeBase>(2); c1.Fitness = 0.1; var c2 = Substitute.ForPartsOf <ChromosomeBase>(2); c2.Fitness = 0.5; var c3 = Substitute.ForPartsOf <ChromosomeBase>(2); c3.Fitness = 0; var c4 = Substitute.ForPartsOf <ChromosomeBase>(2); c4.Fitness = 0.7; var generation = new Generation(1, new List <IChromosome>() { c1, c2, c3, c4 }); // Just one selected chromosome is c1. FlowAssert.IsAtLeastOneAttemptOk(100, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.AreEqual(1, actual.Count(c => c.Fitness == 0.1)); }); // All selected chromosome is c1. FlowAssert.IsAtLeastOneAttemptOk(1000, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.IsTrue(actual.All(c => c.Fitness == 0.1)); }); // Just one selected chromosome is c2. FlowAssert.IsAtLeastOneAttemptOk(100, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.AreEqual(1, actual.Count(c => c.Fitness == 0.5)); }); // All selected chromosome is c2. FlowAssert.IsAtLeastOneAttemptOk(1000, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.IsTrue(actual.All(c => c.Fitness == 0.5)); }); // Just one selected chromosome is c3. FlowAssert.IsAtLeastOneAttemptOk(100, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.AreEqual(1, actual.Count(c => c.Fitness == 0)); }); // All selected chromosome is c3. FlowAssert.IsAtLeastOneAttemptOk(1000, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.IsTrue(actual.All(c => c.Fitness == 0)); }); // Just one selected chromosome is c4. FlowAssert.IsAtLeastOneAttemptOk(100, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.AreEqual(1, actual.Count(c => c.Fitness == 0.7)); }); // All selected chromosome is c4. FlowAssert.IsAtLeastOneAttemptOk(1000, () => { var actual = target.SelectChromosomes(2, generation); Assert.AreEqual(2, actual.Count); Assert.IsTrue(actual.All(c => c.Fitness == 0.7)); }); }