Ejemplo n.º 1
0
        public void MedianOfSorted()
        {
            // Empty array.
            var arr = Array.Empty <float>();

            Assert.Throws <ArgumentException>(() => MathSpan.MedianOfSorted(arr));

            // Single element.
            arr = new float[] { 5 };
            double actual = MathSpan.MedianOfSorted(arr);

            Assert.Equal(5, actual);

            // Two elements.
            arr    = new float[] { 2, 4 };
            actual = MathSpan.MedianOfSorted(arr);
            Assert.Equal(3.0, actual);

            // Three elements.
            arr    = new float[] { 1, 2, 3 };
            actual = MathSpan.MedianOfSorted(arr);
            Assert.Equal(2, actual);

            // Five elements.
            arr    = new float[] { 1, 2, 3, 4, 5 };
            actual = MathSpan.MedianOfSorted(arr);
            Assert.Equal(3, actual);

            // Six elements.
            arr    = new float[] { 1, 2, 3, 4, 5, 6 };
            actual = MathSpan.MedianOfSorted(arr);
            Assert.Equal(3.5, actual);
        }
        /// <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 = MathSpan.MeanSquaredDelta(_yArr, _yArrTarget);

            yMse *= _yMseWeight;

            // Calc gradient mean squared error.
            double gradientMse = MathSpan.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.º 3
0
        private static void Max_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            float[] a = new float[len];
            sampler.Sample(a);

            // Calc results and compare.
            float expected = PointwiseMax(a);
            float actual   = MathSpan.Max(a);

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 4
0
        private static void Min_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.
            double expected = PointwiseMin(a);
            double actual   = MathSpan.Min(a);

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 5
0
        private static void Max_Inner(ISampler <int> sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            int[] a = new int[len];
            sampler.Sample(a);

            // Calc results and compare.
            int expected = PointwiseMax(a);
            int actual   = MathSpan.Max(a);

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

            // Sum the array elements.
            float expected = PointwiseSumOfSquares(x);
            float actual   = MathSpan.SumOfSquares(x);

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

            // Sum the array elements.
            double expected = PointwiseSum(x);
            double actual   = MathSpan.Sum(x);

            // Compare expected and actual sum.
            Assert.Equal(expected, actual, 12);
        }
Ejemplo n.º 8
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);
            MathSpan.MinMax(a, out double actualMin, out double actualMax);

            Assert.Equal(expectedMin, actualMin, 10);
            Assert.Equal(expectedMax, actualMax, 10);
        }
Ejemplo n.º 9
0
        private static void Sum_Inner(ISampler <int> sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            int[] x = new int[len];
            sampler.Sample(x);

            // Sum the array elements.
            int expected = PointwiseSum(x);
            int actual   = MathSpan.Sum(x);

            // Compare expected and actual sum.
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 10
0
        private static void MeanSquaredDelta_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            float[] a = new float[len];
            float[] b = new float[len];
            sampler.Sample(a);
            sampler.Sample(b);

            // Calc results and compare.
            float expected = PointwiseSumSquaredDelta(a, b) / a.Length;
            float actual   = MathSpan.MeanSquaredDelta(a, b);

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

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

            // Clip the elements of the array.
            float[] actual = (float[])x.Clone();
            MathSpan.Clip(actual, -1.1f, 18.8f);

            // Compare expected with actual array.
            Assert.True(SpanUtils.Equal <float>(expected, actual));
        }
Ejemplo n.º 12
0
        private static void Clip_Inner(ISampler <int> sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            int[] x = new int[len];
            sampler.Sample(x);

            // Clip the elements of the array with the safe routine.
            int[] expected = (int[])x.Clone();
            PointwiseClip(expected, -1, 18);

            // Clip the elements of the array.
            int[] actual = (int[])x.Clone();
            MathSpan.Clip(actual, -1, 18);

            // Compare expected with actual array.
            Assert.True(SpanUtils.Equal <int>(expected, actual));
        }
Ejemplo n.º 13
0
    private static LightweightList <int>[] BuildNodesByLayer(DirectedGraph digraph)
    {
        // Build an array that gives the node layer for each node, keyed by node index.
        int[] nodeLayerByIdx = BuildNodeLayerByIdx(digraph);

        // Group nodes into layers.
        int layerCount   = MathSpan.Max(nodeLayerByIdx) + 1;
        var nodesByLayer = new LightweightList <int> [layerCount];

        for (int i = 0; i < layerCount; i++)
        {
            nodesByLayer[i] = new LightweightList <int>();
        }

        for (int nodeIdx = 0; nodeIdx < nodeLayerByIdx.Length; nodeIdx++)
        {
            int depth = nodeLayerByIdx[nodeIdx];
            nodesByLayer[depth].Add(nodeIdx);
        }
        return(nodesByLayer);
    }
Ejemplo n.º 14
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.
            MathSpan.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 binIdx = (int)((vals[i] - min) / incr);

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

                frequencyArr[binIdx]++;
            }

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