public void WorstGenomesAreNeverWinners() { // Create some genomes. int numberGenomes = 20; var genomes = this.CreateGenomesAdheringToParameterTree(numberGenomes); // Create a configuration using a certain winner percentage and mini tournament size. double winnerPercentage = 0.1; int miniTournamentSize = 10; AlgorithmTunerConfiguration configuration = new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder() .SetTournamentWinnerPercentage(winnerPercentage) .SetMaximumMiniTournamentSize(miniTournamentSize) .Build(maximumNumberParallelEvaluations: 1); // Create tournament selector using an evaluator which sorts by a certain parameter, highest first. var tournamentSelectorProps = Props.Create( () => new TournamentSelector <ExtractIntegerValue, TestInstance, IntegerResult>( new ExtractIntegerValueCreator(), new SortByValue(), configuration, this._resultStorageActorRef, TournamentSelectorTest.CreateParameterTree())); var tournamentSelector = this.Sys.ActorOf(tournamentSelectorProps, name: TournamentSelectorTest.SpecialTournamentSelectorName); // Try selecting the best genomes via mini tournaments multiple times and make sure the worst genomes never // get chosen: int worstPossibleWinnerValue = miniTournamentSize - (int)(winnerPercentage * miniTournamentSize); for (int i = 0; i < 15; i++) { // Send a select command. tournamentSelector.Tell(new SelectCommand <TestInstance>(genomes, this._singleTestInstance, 0)); // Wait for results. var results = this.ExpectMsg <SelectionResultMessage <IntegerResult> >(); // Make sure none belongs to the worst genomes. foreach (var genome in results.CompetitiveParents) { int geneValue = (int)genome.CreateMutableGenome().GetGeneValue(ExtractIntegerValue.ParameterName).GetValue(); Assert.True( geneValue >= worstPossibleWinnerValue, $"Found a winner with value {geneValue}, but expected at least {worstPossibleWinnerValue}."); } } }
public void WinnersAreSortedCorrectly() { // Build configuration with a specific winner percentage and tournament size. AlgorithmTunerConfiguration config = new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder() .SetTournamentWinnerPercentage(0.5) .SetMaximumMiniTournamentSize(2) .Build(maximumNumberParallelEvaluations: 1); // Build tournament selector selecting winners by a specific parameter. var tournamentSelector = this.Sys.ActorOf( props: Props.Create( () => new TournamentSelector <ExtractIntegerValue, TestInstance, IntegerResult>( new ExtractIntegerValueCreator(), new SortByValue(), config, this._resultStorageActorRef, TournamentSelectorTest.CreateParameterTree())), name: TournamentSelectorTest.SpecialTournamentSelectorName); // Send selection command. int numberGenomes = 4; var genomes = this.CreateGenomesAdheringToParameterTree(numberGenomes); tournamentSelector.Tell(new SelectCommand <TestInstance>(genomes, this._singleTestInstance, 0)); // Wait for results. var results = this.ExpectMsg <SelectionResultMessage <IntegerResult> >(); // Check that they are sorted correctly. var sortedResults = results.CompetitiveParents .OrderByDescending( genome => (int)genome.CreateMutableGenome().GetGeneValue(ExtractIntegerValue.ParameterName).GetValue()); Assert.True( sortedResults.SequenceEqual(results.CompetitiveParents, new ImmutableGenome.GeneValueComparer()), $"Expected winners sorted like {TestUtils.PrintList(sortedResults)}, but were {TestUtils.PrintList(results.CompetitiveParents)}."); }