public void InvalidGenomeIsHandledCorrectly() { var positiveGenomeBuilder = new ConfigurableGenomeBuilder( this._parameterTree, g => (int)g.GetGeneValue("discrete").GetValue() != 0, mutationRate: 1); var genome = this._genomeBuilder.CreateRandomGenome(0); genome.SetGene("discrete", new Allele <int>(0)); var genomeValues = new[] { 1.4, 0.3, 0, 1.5 }; var standardizedValues = BoundedSearchPoint.StandardizeValues(genomeValues, this._lowerBounds, this._upperBounds); var searchPoint = new PartialGenomeSearchPoint( underlyingGenome: new ImmutableGenome(genome), values: standardizedValues, genomeSearchPointConverter: this._genomeSearchPointConverter, genomeBuilder: positiveGenomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); var associatedGenome = searchPoint.Genome.CreateMutableGenome(); Assert.True( 0 != (int)associatedGenome.GetGeneValue("discrete").GetValue(), "Genome should have been repaired."); Assert.True(searchPoint.IsRepaired, "Repair flag should have been set."); }
public void CreateFromGenomeExtractsCorrectValues() { var genome = this._genomeBuilder.CreateRandomGenome(age: 2); var point = PartialGenomeSearchPoint.CreateFromGenome( genome, this._parameterTree, this._minimumDomainSize); var continuousIdentifiers = new[] { "continuous", "log", "quasi-continuous", "quasi-continuous-log" }; Assert.True( continuousIdentifiers.Length == point.Values.Count, $"There should be four continuous parameters: {TestUtils.PrintList(continuousIdentifiers)}."); var values = BoundedSearchPoint.StandardizeValues( continuousIdentifiers.Select(id => Convert.ToDouble(genome.GetGeneValue(id).GetValue())).ToArray(), this._lowerBounds, this._upperBounds); for (int i = 0; i < continuousIdentifiers.Length; i++) { var identifier = continuousIdentifiers[i]; Console.Out.WriteLine($"Checking value of parameter {identifier}..."); Assert.Equal( values[i], point.Values[i]); } }
public void ConstructionIsConsistent() { // We do not care about age --> set it to default age. var genome = this._genomeBuilder.CreateRandomGenome(age: 0); var searchPoint = ContinuizedGenomeSearchPoint.CreateFromGenome(genome, this._parameterTree); var samePoint = new ContinuizedGenomeSearchPoint( values: searchPoint.Values, parameterTree: this._parameterTree, genomeBuilder: this._genomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); samePoint.Genome.ToCappedDecimalString().ShouldBe(genome.ToCappedDecimalString()); var values = BoundedSearchPoint.StandardizeValues(new[] { 0, 0.2, -3, 0 }, this._lowerBounds, this._upperBounds); searchPoint = new ContinuizedGenomeSearchPoint( values: values, parameterTree: this._parameterTree, genomeBuilder: this._genomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); samePoint = ContinuizedGenomeSearchPoint.CreateFromGenome( searchPoint.Genome.CreateMutableGenome(), this._parameterTree); for (int i = 0; i < values.Count; i++) { Assert.Equal( values[i], samePoint.Values[i], 4); } }
public void ConstructorAssociatesCorrectGenome() { var genomeValues = new[] { 0.6, 1.4, -4.3, 123.8 }; var offset = 20 * 5; var searchPoint = new ContinuizedGenomeSearchPoint( values: BoundedSearchPoint.StandardizeValues(genomeValues, this._lowerBounds, this._upperBounds) + offset, parameterTree: this._parameterTree, genomeBuilder: this._genomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); var genome = searchPoint.Genome.CreateMutableGenome(); // Categorical value should be rounded up to 1 and then be mapped to the second value. Assert.Equal( -12.5, genome.GetGeneValue("categorical").GetValue()); Assert.Equal(1.4, genome.GetGeneValue("continuous").GetValue()); Assert.True(-4 == (int)genome.GetGeneValue("discrete").GetValue(), "Integer value should be rounded up."); Assert.True( "foo" == (string)genome.GetGeneValue("single_categorical").GetValue(), "Single categorical value only has one valid value."); Assert.False(searchPoint.IsRepaired, "The genome should not have needed repair."); }
public void StandardizeValuesThrowsForMissingUpperBounds() { Assert.Throws <ArgumentNullException>( () => BoundedSearchPoint.StandardizeValues( this._values.ToArray(), this._lowerBounds, upperBounds: null)); }
public void StandardizeValuesThrowsForMissingValueSet() { Assert.Throws <ArgumentNullException>( () => BoundedSearchPoint.StandardizeValues( values: null, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds)); }
public void StandardizeValuesThrowsForLowerBoundHigherUpperBound() { this._lowerBounds[1] = this._upperBounds[1] + 0.1; Assert.Throws <ArgumentOutOfRangeException>( () => BoundedSearchPoint.StandardizeValues( this._values.ToArray(), this._lowerBounds, this._upperBounds)); }
public void StandardizeValuesThrowsForWrongNumberOfUpperBounds() { var tooManyUpperBounds = new[] { 104.56, 0.02, 0, 7, 46 }; Assert.Throws <ArgumentOutOfRangeException>( () => BoundedSearchPoint.StandardizeValues( this._values.ToArray(), this._lowerBounds, tooManyUpperBounds)); }
public void StandardizeValuesThrowsForWrongNumberOfLowerBounds() { var notEnoughLowerBounds = new[] { 0.1, 0, -12.4 }; Assert.Throws <ArgumentOutOfRangeException>( () => BoundedSearchPoint.StandardizeValues( this._values.ToArray(), notEnoughLowerBounds, this._upperBounds)); }
public void MapIntoValuesUndoesStandardizeValues() { var boundedValues = new[] { 23.67, 0.01, -12.4, 7 }; var standardized = BoundedSearchPoint.StandardizeValues(boundedValues, this._lowerBounds, this._upperBounds); var backIntoBounds = new BoundedSearchPoint(standardized, this._lowerBounds, this._upperBounds).MapIntoBounds(); for (int i = 0; i < boundedValues.Length; i++) { Assert.Equal(boundedValues[i], backIntoBounds[i], 4); } }
public void ConstructionIsConsistent() { // We do not care about age --> set it to default age. var genome = this._genomeBuilder.CreateRandomGenome(age: 0); genome.SetGene("quasi-continuous-log", new Allele <int>(3)); var searchPoint = PartialGenomeSearchPoint.CreateFromGenome( genome, this._parameterTree, this._minimumDomainSize); var samePoint = new PartialGenomeSearchPoint( underlyingGenome: new ImmutableGenome(genome), values: searchPoint.Values, genomeSearchPointConverter: this._genomeSearchPointConverter, genomeBuilder: this._genomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); var genomeString = genome.ToCappedDecimalString(); var samePointString = samePoint.Genome.ToCappedDecimalString(); genomeString.ShouldBe(samePointString, "Genome should stay the same if transformed into values and back."); var values = BoundedSearchPoint.StandardizeValues(new[] { 0, 0.2, 3, 2 }, this._lowerBounds, this._upperBounds); searchPoint = new PartialGenomeSearchPoint( underlyingGenome: new ImmutableGenome(genome), values: values, genomeSearchPointConverter: this._genomeSearchPointConverter, genomeBuilder: this._genomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); samePoint = PartialGenomeSearchPoint.CreateFromGenome( searchPoint.Genome.CreateMutableGenome(), this._parameterTree, this._minimumDomainSize); for (int i = 0; i < values.Count; i++) { Assert.Equal( values[i], samePoint.Values[i], 4); } }
public void CreateFromGenomeSetsPropertiesCorrectly() { var genome = new Genome(age: 2); genome.SetGene("categorical", new Allele <double>(123.6)); genome.SetGene("continuous", new Allele <double>(0.2)); genome.SetGene("discrete", new Allele <int>(-3)); genome.SetGene("single_categorical", new Allele <string>("foo")); var searchPoint = ContinuizedGenomeSearchPoint.CreateFromGenome(genome, this._parameterTree); Assert.Equal( genome.ToString(), searchPoint.Genome.ToString()); // Values be transformed into [0, 10]. Assert.Equal( BoundedSearchPoint.StandardizeValues(new[] { 0, 0.2, -3, 0 }, this._lowerBounds, this._upperBounds), searchPoint.Values); Assert.False( searchPoint.IsRepaired, "Search points directly created from genomes do not have to be repaired."); }
public void ConstructorAssociatesCorrectGenome() { var genomeValues = new[] { 1.4, 0.8, 2.7, 1.2 }; var offset = 20 * 5; var searchPoint = new PartialGenomeSearchPoint( underlyingGenome: this._baseGenome, values: BoundedSearchPoint.StandardizeValues(genomeValues, this._lowerBounds, this._upperBounds) + offset, genomeSearchPointConverter: this._genomeSearchPointConverter, genomeBuilder: this._genomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); var genome = searchPoint.Genome.CreateMutableGenome(); Assert.Equal( this._baseGenome.CreateMutableGenome().GetGeneValue("categorical").GetValue(), genome.GetGeneValue("categorical").GetValue()); Assert.Equal( 1.4, genome.GetGeneValue("continuous").GetValue()); Assert.Equal( this._baseGenome.CreateMutableGenome().GetGeneValue("discrete").GetValue(), genome.GetGeneValue("discrete").GetValue()); Assert.Equal( this._baseGenome.CreateMutableGenome().GetGeneValue("discrete-log").GetValue(), genome.GetGeneValue("discrete-log").GetValue()); Assert.Equal( 0.8, (double)genome.GetGeneValue("log").GetValue(), 4); Assert.False(searchPoint.IsRepaired, "The genome should not have needed repair."); Assert.True( 3 == (int)genome.GetGeneValue("quasi-continuous").GetValue(), "Quasi continuous value should be rounded to nearest integer."); Assert.True( 1 == (int)genome.GetGeneValue("quasi-continuous-log").GetValue(), "Quasi continuous value should be rounded to nearest integer."); }
public void InvalidGenomeIsHandledCorrectly() { var positiveGenomeBuilder = new ConfigurableGenomeBuilder( this._parameterTree, g => (int)g.GetGeneValue("discrete").GetValue() != -4, mutationRate: 1); var genomeValues = new[] { 0, 1.4, -4, 0 }; var standardizedValues = BoundedSearchPoint.StandardizeValues(genomeValues, this._lowerBounds, this._upperBounds); var searchPoint = new ContinuizedGenomeSearchPoint( values: standardizedValues, parameterTree: this._parameterTree, genomeBuilder: positiveGenomeBuilder, lowerBounds: this._lowerBounds, upperBounds: this._upperBounds); var genome = searchPoint.Genome.CreateMutableGenome(); Assert.True( -4 != (int)genome.GetGeneValue("discrete").GetValue(), "Genome should have been repaired."); Assert.Equal(standardizedValues, searchPoint.Values); Assert.True(searchPoint.IsRepaired, "Repair flag should have been set."); }