/// <summary> /// Initializes a new instance of the <see cref="GgaResult{TResult}" /> class. /// </summary> /// <param name="competitiveParents"> /// The best genomes found, ordered by fitness. /// </param> /// <param name="genomeToTournamentRank"> /// Genome with a rank for every tournament it participated in. /// </param> /// <param name="generationBest"> /// The incumbent genome. /// </param> /// <param name="generationBestResult"> /// The result of the incumbent genome. /// </param> public GgaResult( List <ImmutableGenome> competitiveParents, Dictionary <ImmutableGenome, List <GenomeTournamentRank> > genomeToTournamentRank, ImmutableGenome generationBest, IEnumerable <TResult> generationBestResult) { if (competitiveParents == null) { throw new ArgumentNullException(nameof(competitiveParents)); } if (generationBest == null) { throw new ArgumentNullException(nameof(generationBest)); } Debug.Assert( ImmutableGenome.GenomeComparer.Equals(competitiveParents.First(), generationBest), "The incumbent should be in first position of ordered genome list."); this.CompetitiveParents = competitiveParents.ToImmutableList(); this.GenomeToTournamentRank = genomeToTournamentRank.ToImmutableDictionary(ImmutableGenome.GenomeComparer); this.GenerationBest = generationBest; this.GenerationBestResult = generationBestResult.ToImmutableList(); }
/// <summary> /// Initializes a new instance of the <see cref="StatusTest"/> class. /// </summary> public StatusTest() { var mutableDefaultGenome = new Genome(1); mutableDefaultGenome.SetGene("default", new Allele <int>(0)); this._defaultGenome = new ImmutableGenome(mutableDefaultGenome); }
/// <summary> /// Initializes a new instance of the <see cref="SelectionResultMessage{TResult}" /> class. /// </summary> /// <param name="orderedGenomes"> /// The best genomes found, ordered by fitness. /// </param> /// <param name="genomeToRank"> /// Genome with a rank for every tournament it participated in. /// </param> /// <param name="generationBest"> /// The incumbent genome. /// </param> /// <param name="generationBestResult"> /// The result of the incumbent genome. /// </param> public SelectionResultMessage( ImmutableList <ImmutableGenome> orderedGenomes, Dictionary <ImmutableGenome, List <GenomeTournamentResult> > genomeToRank, ImmutableGenome generationBest, IEnumerable <TResult> generationBestResult) { // Verify parameter. if (orderedGenomes == null) { throw new ArgumentNullException("orderedGenomes"); } if (ReferenceEquals(generationBest, null)) { throw new ArgumentNullException(nameof(generationBest)); } Debug.Assert( new ImmutableGenome.GeneValueComparer().Equals(orderedGenomes.FirstOrDefault(), generationBest), "Incumbent should be in first position of ordered genome list."); this.CompetitiveParents = orderedGenomes.ToImmutableList(); this.GenomeToRank = genomeToRank.ToImmutableDictionary(new ImmutableGenome.GeneValueComparer()); this.GenerationBest = generationBest; this.GenerationBestResult = generationBestResult.ToImmutableList(); }
public void RunResultsAreImmutable() { // Create run results. var runResults = new Dictionary <ImmutableGenome, IDictionary <TestInstance, TestResult> >(); var genome1 = new ImmutableGenome(new Genome()); var instance1 = new TestInstance("1"); var results1 = new Dictionary <TestInstance, TestResult> { { instance1, new TestResult(1) } }; runResults.Add(genome1, results1); // Create message out of them. var resultMessage = new AllResults <TestInstance, TestResult>(runResults); Assert.True(runResults.Keys.SequenceEqual(resultMessage.RunResults.Keys)); Assert.True(resultMessage.RunResults[genome1].SequenceEqual(results1)); // Add more results to original dictionary. results1.Add(new TestInstance("2"), new TestResult()); // Make sure that is not reflected in message. Assert.False(resultMessage.RunResults[genome1].SequenceEqual(results1)); // Remove all genome results from original results. runResults.Remove(genome1); // Make sure that is not reflected in message. Assert.False(runResults.Keys.SequenceEqual(resultMessage.RunResults.Keys)); }
/// <summary> /// Initializes a new instance of the <see cref="PartialGenomeSearchPoint"/> class. /// </summary> /// <param name="underlyingGenome">The underlying <see cref="ImmutableGenome"/>.</param> /// <param name="values"> /// The real-valued point to base continuous parameters on. /// This is the internal representation, not the one in the parameter space. /// <para>Use <see cref="CreateFromGenome"/> if starting from search space.</para> /// </param> /// <param name="genomeSearchPointConverter"> /// Responsible for converting between full-fledged <see cref="Genome"/>s and continuous values in /// parameter space. /// </param> /// <param name="genomeBuilder">Responsible for checking validity and repairing.</param> /// <param name="lowerBounds">The lower bounds by dimension.</param> /// <param name="upperBounds">The upper bounds by dimension.</param> public PartialGenomeSearchPoint( ImmutableGenome underlyingGenome, Vector <double> values, GenomeSearchPointConverter genomeSearchPointConverter, GenomeBuilder genomeBuilder, double[] lowerBounds, double[] upperBounds) : base(values, lowerBounds, upperBounds) { if (genomeSearchPointConverter == null) { throw new ArgumentNullException(nameof(genomeSearchPointConverter)); } if (genomeBuilder == null) { throw new ArgumentNullException(nameof(genomeBuilder)); } // To create the complete genome, map the continuous values into parameter search space again. var genome = genomeSearchPointConverter.MergeIntoGenome(this.MapIntoBounds(), underlyingGenome); // Remember whether there was no direct mapping to a valid genome. this.IsRepaired = false; if (!genomeBuilder.IsGenomeValid(genome)) { genomeBuilder.MakeGenomeValid(genome); this.IsRepaired = true; } this.Genome = new ImmutableGenome(genome); }
public Status( int generation, Population population, AlgorithmTunerConfiguration configuration, GeneticEngineering <TLearnerModel, TPredictorModel, TSamplingStrategy> geneticEngineering, int currentUpdateStrategyIndex, List <double> incumbentQuality, IncumbentGenomeWrapper <TResult> incumbentGenomeWrapper, List <GenerationInformation> informationHistory, TimeSpan elapsedTime, ImmutableGenome defaultGenome = null) { this.Generation = generation; this.Population = population ?? throw new ArgumentNullException(nameof(population)); this.Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this.GeneticEngineering = geneticEngineering ?? throw new ArgumentNullException(nameof(geneticEngineering)); this.CurrentUpdateStrategyIndex = currentUpdateStrategyIndex; this.IncumbentQuality = incumbentQuality ?? throw new ArgumentNullException(nameof(incumbentQuality)); this.IncumbentGenomeWrapper = incumbentGenomeWrapper ?? throw new ArgumentNullException(nameof(incumbentGenomeWrapper)); this.InformationHistory = informationHistory ?? throw new ArgumentNullException(nameof(informationHistory)); this.ElapsedTime = elapsedTime; this.DefaultGenome = defaultGenome; }
/// <summary> /// Initializes a new instance of the <see cref="EvaluationActorTest"/> class. /// </summary> public EvaluationActorTest() : base(ConfigurationFactory.Load().WithFallback(TestKit.DefaultConfig)) { TestUtils.InitializeLogger(); // Initialize a genome adhering to the parameter tree. var genomeData = new Genome(); genomeData.SetGene("value", new Allele <int>(1)); this._genome = new ImmutableGenome(genomeData); // Initialize the actors. var targetAlgorithmFactory = new TargetAlgorithmFactory <NoOperation, TestInstance, TestResult>( targetAlgorithmCreator: () => new NoOperation(TimeSpan.FromSeconds(1))); var resultStorage = this.Sys.ActorOf <ResultStorageActor <TestInstance, TestResult> >(); this._generationEvaluationActorRef = this.Sys.ActorOf( Props.Create( () => new GenerationEvaluationActor <NoOperation, TestInstance, TestResult>( targetAlgorithmFactory, new KeepSuggestedOrder <TestInstance, TestResult>(), new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder().Build(1), resultStorage, new ParameterTree(new ValueNode <int>("value", new IntegerDomain(0, 10, new Allele <int>(0)))), null))); this._evaluationActorRef = this.Sys.ActorOf( props: new EvaluationActorPropsBuilder().Build(this._generationEvaluationActorRef), name: "EvaluationActor"); }
public void DescendingSorterHandlesInvalidResultsCorrectly() { var worstGenome = new ImmutableGenome(new Genome(1)); var middleWorseGenome = new ImmutableGenome(new Genome(2)); var middleBetterGenome = new ImmutableGenome(new Genome(3)); var bestGenome = new ImmutableGenome(new Genome(4)); var worstResults = Enumerable.Range(1, 4).Select(i => new ContinuousResult(double.NaN, TimeSpan.Zero)); var middleWorseResults = Enumerable.Range(1, 2).Select(i => new ContinuousResult(double.NaN, TimeSpan.Zero)) .Concat(Enumerable.Range(1, 2).Select(i => new ContinuousResult(5, TimeSpan.Zero))); var middleBetterResults = Enumerable.Range(1, 2).Select(i => new ContinuousResult(double.PositiveInfinity, TimeSpan.Zero)) .Concat(Enumerable.Range(1, 2).Select(i => new ContinuousResult(10, TimeSpan.Zero))); var bestResults = Enumerable.Range(1, 4).Select(i => new ContinuousResult(5, TimeSpan.Zero)); var runResults = new Dictionary <ImmutableGenome, IEnumerable <ContinuousResult> >(capacity: 4); runResults.Add(worstGenome, worstResults); runResults.Add(middleWorseGenome, middleWorseResults); runResults.Add(middleBetterGenome, middleBetterResults); runResults.Add(bestGenome, bestResults); var sortedGenomes = SortByValueTest.descendingSorter.Sort(runResults); Assert.Equal(bestGenome, sortedGenomes.First()); Assert.Equal(middleBetterGenome, sortedGenomes.Skip(1).First()); Assert.Equal(middleWorseGenome, sortedGenomes.Skip(2).First()); Assert.Equal(worstGenome, sortedGenomes.Last()); }
/// <summary> /// Initializes a new instance of the <see cref="GenerationInformation"/> class. /// </summary> /// <param name="generation">The generation index.</param> /// <param name="totalNumberOfEvaluations"> /// The total number of evaluations at the end of the generation. /// </param> /// <param name="strategy">The strategy type used in the generation.</param> /// <param name="incumbent">The incumbent.</param> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown if <paramref name="generation"/> or <paramref name="totalNumberOfEvaluations"/> are negative. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="strategy"/> or <paramref name="incumbent"/> are <c>null</c>. /// </exception> public GenerationInformation( int generation, int totalNumberOfEvaluations, Type strategy, ImmutableGenome incumbent) { if (generation < 0) { throw new ArgumentOutOfRangeException( nameof(generation), $"Generation must not be negative, but was {generation}."); } if (totalNumberOfEvaluations < 0) { throw new ArgumentOutOfRangeException( nameof(totalNumberOfEvaluations), $"Number of evaluations must not be negative, but was {totalNumberOfEvaluations}."); } this.Generation = generation; this.TotalNumberOfEvaluations = totalNumberOfEvaluations; this.Strategy = strategy ?? throw new ArgumentNullException(nameof(strategy)); this.Incumbent = incumbent ?? throw new ArgumentNullException(nameof(incumbent)); }
ReuseOldTreesStrategy> CreateStatus( int generation, Population population, AlgorithmTunerConfiguration configuration, int currentUpdateStrategyIndex, List <GenerationInformation> informationHistory, ImmutableGenome defaultGenome = null) { if (this.DummyEngineering == null) { this.DummyEngineering = new GeneticEngineering <GenomePredictionRandomForest <ReuseOldTreesStrategy>, GenomePredictionForestModel <GenomePredictionTree>, ReuseOldTreesStrategy>(this.DummyTree, configuration); } return(new Status <TestInstance, TestResult, GenomePredictionRandomForest <ReuseOldTreesStrategy>, GenomePredictionForestModel <GenomePredictionTree>, ReuseOldTreesStrategy>( generation, population, configuration, this.DummyEngineering, currentUpdateStrategyIndex, new List <double>(), new IncumbentGenomeWrapper <TestResult>() { IncumbentGenome = new Genome(), IncumbentGeneration = 0, IncumbentInstanceResults = new List <TestResult>().ToImmutableList(), }, informationHistory, elapsedTime: TimeSpan.Zero, defaultGenome)); }
public void GeneValueComparerReturnsFalseForSecondGenomeNull() { var genome = new ImmutableGenome(this._originalGenome); Assert.False( ImmutableGenomeTest.geneValueComparer.Equals(genome, null), $"Genome {genome} was identified to be equal to null."); }
public void ToStringEqualsGenomesToString() { var immutableGenome = new ImmutableGenome(this._originalGenome); Assert.Equal( this._originalGenome.ToString(), immutableGenome.ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="GenomeResultsRequest" /> class. /// </summary> /// <param name="genome">The genome the run results are requested for.</param> public GenomeResultsRequest(ImmutableGenome genome) { if (genome == null) { throw new ArgumentNullException(nameof(genome)); } this.Genome = genome; }
/// <summary> /// Averages the compare values of a certain genome's results on the provided instances. /// </summary> /// <param name="instances"> /// The instances to take the average on. All of them should already be evaluated for <paramref name="genome"/>. /// </param> /// <param name="genome">The genome.</param> /// <returns>The average compare value.</returns> private double AverageResultsOn(IEnumerable <TInstance> instances, ImmutableGenome genome) { var resultRequest = this._resultStorageActor.Ask <GenomeResults <TInstance, TResult> >( new GenomeResultsRequest(genome)); resultRequest.Wait(); var allResults = resultRequest.Result.RunResults; return(instances.Average(instance => this._runEvaluator.GetMetricRepresentation(allResults[instance]))); }
/// <summary> /// Initializes a new instance of the <see cref="PartialGenomeSearchPoint"/> class. /// </summary> /// <param name="genome"> /// The <see cref="Genome"/> this search point is based on. /// Must be valid and match <paramref name="values"/>, else use different constructor. /// </param> /// <param name="values"> /// The real-valued point to base continuous parameters on. /// This is the internal representation, not the one in the parameter space. /// </param> /// <param name="lowerBounds">The lower bounds by dimension.</param> /// <param name="upperBounds">The upper bounds by dimension.</param> private PartialGenomeSearchPoint( ImmutableGenome genome, Vector <double> values, double[] lowerBounds, double[] upperBounds) : base(values, lowerBounds, upperBounds) { this.Genome = genome ?? throw new ArgumentNullException(nameof(genome)); this.IsRepaired = false; }
/// <summary> /// Handles a <see cref="GenomeResultsRequest"/> sent by a certain actor. /// </summary> /// <param name="genome">The genome the results are requested for.</param> /// <param name="sender">The sender.</param> private void HandleGenomeResultsRequest(ImmutableGenome genome, IActorRef sender) { IDictionary <TInstance, TResult> results; if (!this._runResults.TryGetValue(genome, out results)) { results = new Dictionary <TInstance, TResult>(0); } sender.Tell(new GenomeResults <TInstance, TResult>(genome, results)); }
/// <summary> /// Initializes a new instance of the <see cref="PartialGenomeSearchPointTest"/> class. /// </summary> public PartialGenomeSearchPointTest() { Randomizer.Configure(); this._parameterTree = this.CreateParameterTree(); this._genomeBuilder = new GenomeBuilder( this._parameterTree, new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder().SetEnableRacing(true).Build(1)); this._genomeSearchPointConverter = new GenomeSearchPointConverter(this._parameterTree, this._minimumDomainSize); this._baseGenome = new ImmutableGenome(this._genomeBuilder.CreateRandomGenome(age: 0)); }
/// <summary> /// Initializes a new instance of the <see cref="GenerationInformationTest"/> class. /// </summary> public GenerationInformationTest() { // Create an interesting genome as incumbent. var genome = new Genome(age: 2) { IsEngineered = true }; genome.SetGene("a", new Allele <int>(-23)); this._incumbent = new ImmutableGenome(genome); }
/// <summary> /// Initializes a new instance of the <see cref="GenomeStats{TInstance,TResult}"/> class. /// </summary> /// <param name="genome">The genome.</param> /// <param name="finishedInstances">The finished instances dictionary.</param> public GenomeStats(ImmutableGenome genome, Dictionary <TInstance, TResult> finishedInstances) { this.Genome = genome; this._openInstances = new HashSet <TInstance>(); this._runningInstances = new HashSet <TInstance>(); this._finishedInstances = finishedInstances; this._cancelledByRacingInstances = new HashSet <TInstance>(); this.TotalInstanceCount = this._openInstances.Count + this._runningInstances.Count + this._finishedInstances.Count + this._cancelledByRacingInstances.Count; }
public void GetFilteredGenesReturnsSameForImmutableAndMutable() { // Create immutable genome. var immutableGenome = new ImmutableGenome(this._originalGenome); // Make sure GetFilteredGenes returns the same values as it does for the original genome used at // initialization. Assert.True( immutableGenome.GetFilteredGenes(ImmutableGenomeTest.parameterTree) .SequenceEqual(this._originalGenome.GetFilteredGenes(ImmutableGenomeTest.parameterTree)), "GetFilteredGenes returns different values for immutable and mutable genome."); }
/// <summary> /// Initializes a new instance of the <see cref="GenomeResults{I,R}"/> class. /// </summary> /// <param name="genome">The genome.</param> /// <param name="runResults">The run results.</param> /// <exception cref="ArgumentNullException">Thrown if result parameter is null.</exception> public GenomeResults(ImmutableGenome genome, IDictionary <TInstance, TResult> runResults) { this.Genome = genome ?? throw new ArgumentNullException(nameof(genome)); if (runResults == null) { throw new ArgumentNullException(nameof(runResults)); } // Translate the dictionary into an immutable one to keep the message immutable. this.RunResults = runResults.ToImmutableDictionary(); }
public void GenomeResultsRequestMayReturnEmptyCollection() { // Create genome. var genome = new ImmutableGenome(new Genome(age: 1)); // Ask for all results for that genome. this._resultStorageActorRef.Tell(new GenomeResultsRequest(genome)); var results = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >(); // Check no results for that genome have been returned. Assert.True(results.RunResults.IsEmpty, "Did not expect any results for the genome."); }
public void CreateMutableGenomeReturnsGenomeWithOriginalGeneValues() { // Create immutable genome. var immutableGenome = new ImmutableGenome(this._originalGenome); // Get mutable genome out of it. var mutableGenome = immutableGenome.CreateMutableGenome(); // Make sure it has the same values as the original genome. Assert.True( Genome.GenomeComparer.Equals(mutableGenome, this._originalGenome), "New mutable genome should have had the same gene values as the original mutable genome."); }
public void GeneValueComparerReturnsFalseForDifferentValue() { this._originalGenome.SetGene(ImmutableGenomeTest.ParameterId, new Allele <int>(2)); var genome = new ImmutableGenome(this._originalGenome); var otherGenome = new Genome(); otherGenome.SetGene(ImmutableGenomeTest.ParameterId, new Allele <int>(3)); var other = new ImmutableGenome(otherGenome); Assert.False( ImmutableGenomeTest.geneValueComparer.Equals(genome, other), $"Genome {genome} has supposedly the same gene values as {other}."); }
public void SortReturnsGenomeWithLowerAverageRuntimeFirst() { var worseGenome = new ImmutableGenome(new Genome()); var betterGenome = new ImmutableGenome(new Genome()); var runResults = new Dictionary <ImmutableGenome, IEnumerable <RuntimeResult> >(capacity: 2); runResults.Add(worseGenome, Enumerable.Range(1, 10).Select(i => new RuntimeResult(TimeSpan.FromMilliseconds(i)))); runResults.Add(betterGenome, Enumerable.Range(0, 10).Select(i => new RuntimeResult(TimeSpan.FromMilliseconds(i)))); var sortedGenomes = SortByRuntimeTest.sorter.Sort(runResults); Assert.Equal(betterGenome, sortedGenomes.First()); Assert.Equal(worseGenome, sortedGenomes.Last()); }
public void AscendingSorterReturnsGenomeWithLowerAverageValueFirst() { var worseGenome = new ImmutableGenome(new Genome(1)); var betterGenome = new ImmutableGenome(new Genome(2)); var runResults = new Dictionary <ImmutableGenome, IEnumerable <ContinuousResult> >(capacity: 2); runResults.Add(worseGenome, Enumerable.Range(1, 10).Select(i => new ContinuousResult(10, TimeSpan.Zero))); runResults.Add(betterGenome, Enumerable.Range(1, 10).Select(i => new ContinuousResult(i, TimeSpan.Zero))); var sortedGenomes = SortByValueTest.ascendingSorter.Sort(runResults); Assert.Equal(betterGenome, sortedGenomes.First()); Assert.Equal(worseGenome, sortedGenomes.Last()); }
public void DescendingSorterHandlesDifferentNumberOfRunsPerGenomeCorrectly() { var worseGenome = new ImmutableGenome(new Genome(1)); var betterGenome = new ImmutableGenome(new Genome(2)); var runResults = new Dictionary <ImmutableGenome, IEnumerable <ContinuousResult> >(capacity: 2); runResults.Add(worseGenome, new ContinuousResult[] { new ContinuousResult(2, TimeSpan.Zero) }); runResults.Add(betterGenome, Enumerable.Range(1, 10).Select(i => new ContinuousResult(1, TimeSpan.Zero))); var sortedGenomes = SortByValueTest.descendingSorter.Sort(runResults); Assert.Equal(betterGenome, sortedGenomes.First()); Assert.Equal(worseGenome, sortedGenomes.Last()); }
public void SortCanHandleDifferentNumberOfRunsPerGenome() { var worseGenome = new ImmutableGenome(new Genome()); var betterGenome = new ImmutableGenome(new Genome()); var runResults = new Dictionary <ImmutableGenome, IEnumerable <RuntimeResult> >(capacity: 2); runResults.Add(worseGenome, new RuntimeResult[] { new RuntimeResult(TimeSpan.FromMilliseconds(100)) }); runResults.Add(betterGenome, Enumerable.Range(0, 100).Select(i => new RuntimeResult(TimeSpan.FromMilliseconds(2)))); var sortedGenomes = SortByRuntimeTest.sorter.Sort(runResults); Assert.Equal(betterGenome, sortedGenomes.First()); Assert.Equal(worseGenome, sortedGenomes.Last()); }
public void SortCanHandleDifferentNumberOfRunsPerGenome() { var worseGenome = new ImmutableGenome(new Genome(1)); var betterGenome = new ImmutableGenome(new Genome(2)); var worseStats = this.CreateStats(worseGenome, SortByPenalizedRuntimeTest.Instances, i => TimeSpan.FromMilliseconds(100), 1); var betterStats = this.CreateStats(betterGenome, SortByPenalizedRuntimeTest.Instances, i => TimeSpan.FromMilliseconds(20)); var runResults = new[] { worseStats.ToImmutable(), betterStats.ToImmutable() }; var sortedGenomes = SortByPenalizedRuntimeTest.Sorter.Sort(runResults).ToList(); sortedGenomes.Count.ShouldBe(2); Assert.Equal(betterGenome, sortedGenomes.First().Genome); Assert.Equal(worseGenome, sortedGenomes.Last().Genome); }
public void SortReturnsGenomeWithLowerAverageRuntimeFirst() { var worseGenome = new ImmutableGenome(new Genome(1)); var betterGenome = new ImmutableGenome(new Genome(2)); var worseStats = this.CreateStats(worseGenome, SortByPenalizedRuntimeTest.Instances, i => TimeSpan.FromSeconds(i + 1)); var betterStats = this.CreateStats(betterGenome, SortByPenalizedRuntimeTest.Instances, i => TimeSpan.FromSeconds(i)); var runResults = new[] { worseStats.ToImmutable(), betterStats.ToImmutable() }; var sortedGenomes = SortByPenalizedRuntimeTest.Sorter.Sort(runResults).ToList(); sortedGenomes.Count.ShouldBe(2); Assert.Equal(betterGenome, sortedGenomes.First().Genome); Assert.Equal(worseGenome, sortedGenomes.Last().Genome); }