/// <summary> /// Evaluate the provided black box against the function regression task, /// and return its fitness score. /// </summary> /// <param name="box">The black box to evaluate.</param> public FitnessInfo Evaluate(IBlackBox <double> box) { // Probe the black box over the full range of the input parameter. _blackBoxProbe.Probe(box, _yArr); // Calc gradients. FuncRegressionUtils.CalcGradients(_paramSamplingInfo, _yArr, _gradientArr); // Calc y position mean squared error (MSE), and apply weighting. double yMse = MathArrayUtils.MeanSquaredDelta(_yArr, _yArrTarget); yMse *= _yMseWeight; // Calc gradient mean squared error. double gradientMse = MathArrayUtils.MeanSquaredDelta(_gradientArr, _gradientArrTarget); gradientMse *= _gradientMseWeight; // Calc fitness as the inverse of MSE (higher value is fitter). // Add a constant to avoid divide by zero, and to constrain the fitness range between bad and good solutions; // this allows the selection strategy to select solutions that are mediocre and therefore helps preserve diversity. double fitness = 20.0 / (yMse + gradientMse + 0.02); return(new FitnessInfo(fitness)); }
private static void MeanSquaredDelta(UniformDistributionSampler sampler, int len) { // Alloc arrays and fill with uniform random noise. double[] a = new double[len]; double[] b = new double[len]; sampler.Sample(a); sampler.Sample(b); // Calc results and compare. double expected = SumSquaredDelta(a, b) / a.Length; double actual = MathArrayUtils.MeanSquaredDelta(a, b); Assert.AreEqual(expected, actual, 1e-10); }
/// <summary> /// Evaluate the provided IBlackBox against the XOR problem domain and return its fitness score. /// </summary> public FitnessInfo Evaluate(IBlackBox box) { int sampleCount = _paramSamplingInfo._sampleCount; // TODO: We can avoid a memory allocation here by allocating at construction time, but this requires modification of // ParallelGenomeListEvaluator to utilise multiple evaluators (one per thread). double[] yArr = new double[sampleCount]; double[] gradientArr = new double[sampleCount]; // Probe the black box over the full range of the input parameter. _blackBoxProbe.Probe(box, yArr); // Calc gradients. FnRegressionUtils.CalcGradients(_paramSamplingInfo, yArr, gradientArr); // Calc y position mean squared error (MSE), and apply weighting. double yMse = MathArrayUtils.MeanSquaredDelta(yArr, _yArrTarget); yMse *= _yMseWeight; // Calc gradient mean squared error. double gradientMse = MathArrayUtils.MeanSquaredDelta(gradientArr, _gradientArrTarget); gradientMse *= _gradientMseWeight; // Calc fitness as the inverse of MSE (higher value is fitter). // Add a constant to avoid divide by zero, and to constrain the fitness range between bad and good solutions; // this allows the selection strategy to select solutions that are mediocre and therefore helps preserve diversity. double fitness = 20.0 / (yMse + gradientMse + 0.02); // Test for stopping condition (near perfect response). if (fitness >= 100000.0) { _stopConditionSatisfied = true; } _evalCount++; return(new FitnessInfo(fitness, fitness)); }