/// <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>
        /// <returns>A new instance of <see cref="FitnessInfo"/>.</returns>
        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 = MathSpanUtils.MeanSquaredDelta(_yArr, _yArrTarget);

            yMse *= _yMseWeight;

            // Calc gradient mean squared error.
            double gradientMse = MathSpanUtils.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));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Calculate a frequency distribution for the provided array of values.
        /// 1) The minimum and maximum values are found.
        /// 2) The resulting value range is divided into equal sized sub-ranges (categoryCount).
        /// 3) The number of values that fall into each category is determined.
        /// </summary>
        public static HistogramData BuildHistogramData(double[] valArr, int categoryCount)
        {
            // Determine min/max.
            MathSpanUtils.MinMax(valArr, out double min, out double max);

            // Note. each bucket's range has interval [low,high), i.e. samples exactly equal to 'high'
            // will fall into the next highest bucket. Therefore to prevent the maximum sample vAalue falling into the
            // last bucket by itself, we inflate the range by a small proportion so that the max value falls just below
            // the max range covered by the distribution.
            double range = (max - min) * 1.01;

            // Handle special case where the data series contains a single value.
            if (0.0 == range)
            {
                return(new HistogramData(min, max, 0.0, new int[] { valArr.Length }));
            }

            // Loop values and for each one increment the relevant category's frequency count.
            double incr = range / categoryCount;

            int[] frequencyArr = new int[categoryCount];
            for (int i = 0; i < valArr.Length; i++)
            {
                frequencyArr[(int)((valArr[i] - min) / incr)]++;
            }
            return(new HistogramData(min, max, incr, frequencyArr));
        }
Ejemplo n.º 3
0
        private static void MinMax_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            double[] a = new double[len];
            sampler.Sample(a);

            // Calc results and compare.
            PointwiseMinMax(a, out double expectedMin, out double expectedMax);
            MathSpanUtils.MinMax(a, out double actualMin, out double actualMax);

            Assert.Equal(expectedMin, actualMin, 10);
            Assert.Equal(expectedMax, actualMax, 10);
        }
Ejemplo n.º 4
0
        private static void MeanSquaredDelta_Inner(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 = PointwiseSumSquaredDelta(a, b) / a.Length;
            double actual   = MathSpanUtils.MeanSquaredDelta(a, b);

            Assert.Equal(expected, actual, 10);
        }
Ejemplo n.º 5
0
        private static void Clamp_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            double[] x = new double[len];
            sampler.Sample(x);

            // Clip the elements of the array with the safe routine.
            double[] expected = (double[])x.Clone();
            PointwiseClip(expected, -1.1, 18.8);

            // Clip the elements of the array.
            double[] actual = (double[])x.Clone();
            MathSpanUtils.Clamp(actual, -1.1, 18.8);

            // Compare expected with actual array.
            Assert.True(SpanUtils.Equal <double>(expected, actual));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Calculate a histogram for the provided span of values.
        /// 1) The minimum and maximum values are found.
        /// 2) The resulting value range is divided into equal sized sub-ranges or bins.
        /// 3) The number of values that fall into each bin is determined.
        /// </summary>
        /// <param name="vals">The values to calculate a histogram for.</param>
        /// <param name="binCount">The number of histogram bins to use.</param>
        /// <returns>A new instance of <see cref="HistogramData"/>.</returns>
        public static HistogramData BuildHistogramData(Span <double> vals, int binCount)
        {
            // Determine min/max.
            MathSpanUtils.MinMax(vals, out double min, out double max);

            // Note. each bin's range has interval [low,high), i.e. samples exactly equal to 'high' will fall
            // into the next highest bin. Except for the last bin which has interval [low, high].
            double range = max - min;

            // Handle special case where the data series contains a single value.
            if (range == 0.0)
            {
                return(new HistogramData(min, max, 0.0, new int[] { vals.Length }));
            }

            // Loop values, and for each one increment the relevant category's frequency count.
            double incr = range / binCount;

            int[] frequencyArr = new int[binCount];

            for (int i = 0; i < vals.Length; i++)
            {
                // Determine which bin the value falls within.
                int idx = (int)((vals[i] - min) / incr);

                // Values that equal max, are placed into the last bin.
                if (idx == vals.Length)
                {
                    idx--;
                }

                frequencyArr[idx]++;
            }

            return(new HistogramData(min, max, incr, frequencyArr));
        }