/// <summary> /// Initializes a new instance of the <see cref="ContinuizedGenomeSearchPoint"/> class. /// </summary> /// <param name="values"> /// The real-valued point to base this 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="parameterTree">Specifies the parameters.</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 ContinuizedGenomeSearchPoint( Vector <double> values, ParameterTree parameterTree, GenomeBuilder genomeBuilder, double[] lowerBounds, double[] upperBounds) : base(values, lowerBounds, upperBounds) { if (parameterTree == null) { throw new ArgumentNullException(nameof(parameterTree)); } if (genomeBuilder == null) { throw new ArgumentNullException(nameof(genomeBuilder)); } var transformator = new TolerantGenomeTransformation(parameterTree); var geneValues = transformator.RoundToValidValues(this.MapIntoBounds().ToArray()); var genome = transformator.ConvertBack(geneValues); // 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 void RoundToValidValuesWorks() { // Create parameter tree with many different domains. var root = new AndNode(); root.AddChild(new ValueNode <string>("a", new CategoricalDomain <string>(new List <string> { "red", "blue" }))); root.AddChild(new ValueNode <double>("b", new ContinuousDomain())); root.AddChild(new ValueNode <double>("c", new LogDomain(1, 16))); root.AddChild(new ValueNode <int>("d", new IntegerDomain())); root.AddChild(new ValueNode <int>("e", new DiscreteLogDomain(2, 16))); var parameterTree = new ParameterTree(root); var transformator = new TolerantGenomeTransformation(parameterTree); double[] continuousValues = { 0.2, 0.3, 0.6, 0.8, 1.5 }; double[] expectedValues = { 0, 0.3, 0.6, 1, 2 }; Assert.Equal( expectedValues, transformator.RoundToValidValues(continuousValues)); }