-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #92 - Multiple occurances of same chromosome instance in generation
- Loading branch information
1 parent
5a2c2f5
commit 381bc8e
Showing
9 changed files
with
117 additions
and
51 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
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
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
73 changes: 73 additions & 0 deletions
73
src/GeneticSharp.Domain.UnitTests/Selections/SelectionIssuesTest.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,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using NSubstitute; | ||
using GeneticSharp.Extensions; | ||
|
||
namespace GeneticSharp.Domain.UnitTests.Selections | ||
{ | ||
[TestFixture()] | ||
[Category("Selections")] | ||
public class SelectionIssuesTest | ||
{ | ||
[SetUp] | ||
public void Cleanup() | ||
{ | ||
RandomizationProvider.Current = new BasicRandomization(); | ||
} | ||
|
||
/// <summary> | ||
/// https://github.com/giacomelli/GeneticSharp/issues/92 | ||
/// </summary> | ||
[Test] | ||
[TestCase("Rank")] | ||
[TestCase("Roulette Wheel")] | ||
[TestCase("Tournament")] | ||
public void SelectChromosomes_Issue92_Solved(string selectionName) | ||
{ | ||
var target = SelectionService.CreateSelectionByName(selectionName); | ||
var c1 = new SelectionStubChromosome(); | ||
c1.Fitness = 1; | ||
|
||
var c2 = new SelectionStubChromosome(); | ||
var c3 = new SelectionStubChromosome(); | ||
var c4 = new SelectionStubChromosome(); | ||
|
||
var generation = new Generation(1, new List<IChromosome>() { | ||
c1, c2, c3, c4 | ||
}); | ||
|
||
var actual = target.SelectChromosomes(10, generation); | ||
Assert.AreEqual(10, actual.Count); | ||
|
||
var previousChromosomes = actual.Select(c => c.GetGenes().ToArray()).ToArray(); | ||
var mutation = new UniformMutation(true); | ||
|
||
|
||
for (int i = 0; i < actual.Count; i++) | ||
{ | ||
if (actual[i].Fitness == 1) | ||
{ | ||
mutation.Mutate(actual[i], 1); | ||
|
||
Assert.AreEqual(1, actual.Count(c => c.GetGene(0).Value != null), "Mutation has changed more than one chromosome at the time"); | ||
break; | ||
} | ||
} | ||
|
||
for (int i = 0; i < actual.Count; i++) | ||
{ | ||
for (int j = 0; j < actual.Count; j++) | ||
{ | ||
if (i == j) | ||
continue; | ||
|
||
if (object.ReferenceEquals(actual[i], actual[j])) | ||
Assert.Fail($"Chromosomes on index {i} and {j} are the same."); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/GeneticSharp.Domain.UnitTests/Selections/SelectionStubChromosome.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,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace GeneticSharp.Domain.UnitTests.Selections | ||
{ | ||
class SelectionStubChromosome : ChromosomeBase | ||
{ | ||
public SelectionStubChromosome() : base(2) | ||
{ | ||
Fitness = 0; | ||
} | ||
|
||
public override IChromosome CreateNew() => new SelectionStubChromosome(); | ||
|
||
public override Gene GenerateGene(int geneIndex) => new Gene(0); | ||
} | ||
} |
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
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
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
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