Ejemplo n.º 1
0
        public void TestGaussianDelta()
        {
            var strategy = DeltaWeightMutationStrategy.CreateGaussianDeltaStrategy(
                new SelectAllStrategy(),
                1.0);

            IRandomSource rng = RandomDefaults.CreateRandomSource(0);

            int iters = 100_000;

            double[] weightArr = new double[iters];
            for (int i = 0; i < iters; i++)
            {
                weightArr[i] = 1000.0;
            }

            strategy.Invoke(weightArr, rng);

            // Construct a histogram on the array of weights.
            HistogramData hist = NumericsUtils.BuildHistogramData(weightArr, 8);

            // We expect min and max to be close to be about -995.5 and +1004.5 respectively
            // (but they could be further from the mean of 1000, with no bound).
            Assert.IsTrue(hist.Max >= 1002.0);
            Assert.IsTrue(hist.Min <= 998.0);

            TestMean(weightArr, 1000.0);
        }
Ejemplo n.º 2
0
        public void TestUniformDelta()
        {
            double weightScale = 5.0;
            var    strategy    = DeltaWeightMutationStrategy.CreateUniformDeltaStrategy(
                new SelectAllStrategy(),
                weightScale);

            IRandomSource rng = RandomDefaults.CreateRandomSource(0);

            int iters = 10_000;

            double[] weightArr = new double[iters];
            for (int i = 0; i < iters; i++)
            {
                weightArr[i] = 1000.0;
            }

            strategy.Invoke(weightArr, rng);

            // Construct a histogram on the array of weights.
            HistogramData hist = NumericsUtils.BuildHistogramData(weightArr, 8);

            // We expect samples to be approximately evenly distributed over the histogram buckets.
            for (int i = 0; i < hist.FrequencyArray.Length; i++)
            {
                Assert.IsTrue(hist.FrequencyArray[i] > (iters / 8) * 0.8);
            }

            // We expect min and max to be close to 1000-weightScale and 1000+weightScale respectively.
            Assert.IsTrue(hist.Max <= (1000 + weightScale) && hist.Max > (1000 + weightScale) - 0.1);
            Assert.IsTrue(hist.Min >= (1000 - weightScale) && hist.Min < (1000 - weightScale) + 0.1);
        }
Ejemplo n.º 3
0
        public void GaussianReset()
        {
            var strategy = ResetWeightMutationStrategy <double> .CreateGaussianResetStrategy(
                new SelectAllStrategy(),
                1.0);

            IRandomSource rng = RandomDefaults.CreateRandomSource(0);

            int iters = 100_000;

            double[] weightArr = new double[iters];
            for (int i = 0; i < iters; i++)
            {
                weightArr[i] = 123.0;
            }

            strategy.Invoke(weightArr, rng);

            // Construct a histogram on the array of weights.
            HistogramData hist = NumericsUtils.BuildHistogramData(weightArr, 8);

            // We expect min and max to be close to be about -4.5 and +4.5 respectively
            // (but they could be higher in magnitude, with no bound).
            Assert.True(hist.Max >= 3.8);
            Assert.True(hist.Min <= -3.8);

            TestMean(weightArr);
            TestStandardDeviation(weightArr);
        }
Ejemplo n.º 4
0
        public void BuildHistogramData()
        {
            IRandomSource rng = RandomDefaults.CreateRandomSource(0);

            int iters = 10_000;

            double[] vals = new double[iters];
            for (int i = 0; i < iters; i++)
            {
                vals[i] = 1000.0 + (rng.NextDouble() * 2.0) - 1.0;
            }

            // Construct a histogram on the array of values.
            HistogramData hist = NumericsUtils.BuildHistogramData(vals, 8);

            // We expect samples to be approximately evenly distributed over the histogram buckets.
            for (int i = 0; i < hist.FrequencyArray.Length; i++)
            {
                Assert.True(hist.FrequencyArray[i] > (iters / 8) * 0.8);
            }

            // We expect min and max to be close to 999 and 1001 respectively.
            Assert.True(hist.Max <= (1001) && hist.Max > (1001) - 0.1);
            Assert.True(hist.Min >= (999) && hist.Min < (999) + 0.1);
        }
Ejemplo n.º 5
0
        public void UniformReset()
        {
            double weightScale = 5.0;
            var    strategy    = ResetWeightMutationStrategy <double> .CreateUniformResetStrategy(
                new SelectAllStrategy(),
                weightScale);

            IRandomSource rng = RandomDefaults.CreateRandomSource(0);

            int iters = 10_000;

            double[] weightArr = new double[iters];
            for (int i = 0; i < iters; i++)
            {
                weightArr[i] = 123.0;
            }

            strategy.Invoke(weightArr, rng);

            // Construct a histogram on the array of weights.
            HistogramData hist = NumericsUtils.BuildHistogramData(weightArr, 8);

            // We expect samples to be approximately evenly distributed over the histogram buckets.
            for (int i = 0; i < hist.FrequencyArray.Length; i++)
            {
                Assert.True(hist.FrequencyArray[i] > (iters / 8) * 0.8);
            }

            // We expect min and max to be close to -weightScale and +weightScale respectively.
            double delta = weightScale - hist.Max;

            Assert.True(delta >= 0.0 && delta < 0.1);

            delta = weightScale + hist.Min;
            Assert.True(delta >= 0.0 && delta < 0.1);

            // Mean should be near to zero.
            TestMean(weightArr);
        }