Skip to content

Commit

Permalink
Create the Truncation Selection
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomelli committed Sep 8, 2022
1 parent d0a8c60 commit bd57f7e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/GeneticSharp.Benchmarks/SelectionsBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,21 @@ public ISelection Tournament()
target.SelectChromosomes(_chromosomesNumber, _generation);
return target;
}

[Benchmark]
public ISelection Rank()
{
var target = new RankSelection();
target.SelectChromosomes(_chromosomesNumber, _generation);
return target;
}

[Benchmark]
public ISelection Truncation()
{
var target = new TruncationSelection();
target.SelectChromosomes(_chromosomesNumber, _generation);
return target;
}
}
}
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);
}
}
}

35 changes: 35 additions & 0 deletions src/GeneticSharp.Domain/Selections/TruncationSelection.cs
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();
}
}
}

0 comments on commit bd57f7e

Please sign in to comment.