-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0a8c60
commit bd57f7e
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/GeneticSharp.Domain.UnitTests/Selections/TruncationSelectionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using NSubstitute; | ||
|
||
namespace GeneticSharp.Domain.UnitTests.Selections | ||
{ | ||
[TestFixture()] | ||
[Category("Selections")] | ||
public class TruncationSelectionTest | ||
{ | ||
[Test()] | ||
public void SelectChromosomes_InvalidNumber_Exception() | ||
{ | ||
var target = new TruncationSelection(); | ||
|
||
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."); | ||
} | ||
|
||
[Test()] | ||
public void SelectChromosomes_NullGeneration_Exception() | ||
{ | ||
var target = new TruncationSelection(); | ||
|
||
var actual = Assert.Catch<ArgumentNullException>(() => | ||
{ | ||
target.SelectChromosomes(2, null); | ||
}); | ||
|
||
Assert.AreEqual("generation", actual.ParamName); | ||
} | ||
|
||
[Test()] | ||
public void SelectChromosomes_Generation_ChromosomesSelected() | ||
{ | ||
var target = new TruncationSelection(); | ||
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 | ||
}); | ||
|
||
|
||
var actual = target.SelectChromosomes(2, generation); | ||
Assert.AreEqual(2, actual.Count); | ||
Assert.AreEqual(0.7, actual[0].Fitness); | ||
Assert.AreEqual(0.5, actual[1].Fitness); | ||
|
||
actual = target.SelectChromosomes(3, generation); | ||
Assert.AreEqual(3, actual.Count); | ||
Assert.AreEqual(0.7, actual[0].Fitness); | ||
Assert.AreEqual(0.5, actual[1].Fitness); | ||
Assert.AreEqual(0.1, actual[2].Fitness); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
|
||
namespace GeneticSharp | ||
{ | ||
/// <summary> | ||
/// Selects the chromosomes with the best fitness. | ||
/// </summary> | ||
/// <remarks> | ||
/// <see href="https://en.wikipedia.org/wiki/Truncation_selection"/> | ||
/// </remarks> | ||
[DisplayName("Truncation")] | ||
public sealed class TruncationSelection : SelectionBase | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="GeneticSharp.Domain.Selections.TruncationSelection"/> class. | ||
/// </summary> | ||
public TruncationSelection() : base(2) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Performs the selection of chromosomes from the generation specified. | ||
/// </summary> | ||
/// <param name="number">The number of chromosomes to select.</param> | ||
/// <param name="generation">The generation where the selection will be made.</param> | ||
/// <returns>The select chromosomes.</returns> | ||
protected override IList<IChromosome> PerformSelectChromosomes(int number, Generation generation) | ||
{ | ||
var ordered = generation.Chromosomes.OrderByDescending(c => c.Fitness); | ||
return ordered.Take(number).ToList(); | ||
} | ||
} | ||
} |