示例#1
0
        private static Chromosome Roulette(Chromosome[] population)
        {
            double partsum = 0, sum = 0;
            double worstFitness = double.MaxValue;

            for (int i = 0; i < population.Length; i++)
            {
                sum += population[i].Fitness;
                if (worstFitness > population[i].Fitness)
                {
                    worstFitness = population[i].Fitness;
                }
            }

            // fitness scaling -> F(x) = f(x) - worst
            sum -= worstFitness * population.Length;

            double r = ExtendedRandom.NextUniform() * sum;

            for (int i = 0; i < population.Length; i++)
            {
                partsum += (population[i].Fitness - worstFitness);
                if (partsum >= r)
                {
                    return(population[i]);
                }
            }

            return(null);
        }
        public void TestInt64()
        {
            var random = new ExtendedRandom();
            var x      = random.NextInt64();
            var y      = random.NextInt64();

            Assert.AreNotEqual(x, y);
        }
示例#3
0
        public void GenRandomString()
        {
            var length = 5;

            var newString = ExtendedRandom.NextString(length);

            Assert.Equal(length, newString.Length);
        }
示例#4
0
        public void GenRandomStringMinMaxLength()
        {
            var min = 1;
            var max = 10;

            var newString = ExtendedRandom.NextString(min, max);

            Assert.True(newString.Length >= min && newString.Length <= max);
        }
示例#5
0
        private static Tuple <T, T, T> Randomize <T>()
        {
            var random = new ExtendedRandom();
            var item1  = random.Randomize <T>();

            var item2 = Random.Randomize <T>();
            var item3 = Random.Randomize <T>();

            return(new Tuple <T, T, T>(item1, item2, item3));
        }
示例#6
0
        public void GenRandomDouble()
        {
            var min = 1.2;
            var max = 2.5;

            for (int i = 0; i < 20; i++)
            {
                var newDouble = ExtendedRandom.NextDouble(min, max);
                Assert.True(newDouble >= min && newDouble <= max);
            }
        }
示例#7
0
        public Distribution(double mean, double variance)
        {
            Mean     = mean;
            Variance = variance;

            if (Settings.FixSeed)
            {
                rnd = new ExtendedRandom(200844210);
            }
            else
            {
                rnd = new ExtendedRandom();
            }
        }
示例#8
0
        private static Chromosome Rank(Chromosome[] population, double alpha, double beta)
        {
            // assumes that the population has been previously sorted
            // each chromosome has the rank computed in "ranks"

            double partsum = 0;
            double rankSum = population.Length * (population.Length + 1) / 2.0;
            double r       = ExtendedRandom.NextUniform() * rankSum;

            for (int i = 0; i < population.Length; i++)
            {
                partsum += _ranks[i];
                if (partsum > r)
                {
                    return(population[i]);
                }
            }

            return(null);
        }
        public void WeightedChoice_WithIntegerWeights_ReturnsCorrectWeighting()
        {
            var weights = new[] {2, 3, 5};
            var items = new[] {"first", "second", "third"};
            var totals = new[] {0, 0, 0};

            for (var i = 0; i < 1000000; i++)
            {
                var item = Random.WeightedChoice(items, weights);
                for (var j = 0; j < items.Length; j++)
                {
                    if (items[j] == item)
                    {
                        totals[j]++;
                    }
                }
            }

            // Statistically, this should never fail...
            Assert.AreEqual(totals[0], 200000, 2000);
            Assert.AreEqual(totals[1], 300000, 2000);
            Assert.AreEqual(totals[2], 500000, 2000);
        }
示例#10
0
 public void GenRandomDate()
 {
     var dateTime = ExtendedRandom.NextDateTime();
 }
示例#11
0
 public Distribution(double mean, double variance)
 {
     Mean     = mean;
     Variance = variance;
     rnd      = new ExtendedRandom(Settings.SeedGenerator.Next());
 }
示例#12
0
        public object Generate()
        {
            var obj = new TSource();

            foreach (var prop in typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (Ignore.Contains(prop.Name))
                {
                    continue;
                }

                if (Values.ContainsKey(prop.Name))
                {
                    prop.SetValue(obj, Values[prop.Name], null);
                    continue;
                }

                if (Generators.ContainsKey(prop.Name))
                {
                    try {
                        prop.SetValue(obj, Generators[prop.Name].Generate(), null);
                    } catch (ArgumentException) {
                        throw new ArgumentException($"Mismatching generator type for property {prop.Name}");
                    }
                    continue;
                }

                if (OnlyCustom)
                {
                    continue;
                }

                if (TypeGenerators.ContainsKey(prop.PropertyType))
                {
                    try {
                        prop.SetValue(obj, TypeGenerators[prop.PropertyType].Generate(), null);
                    } catch (ArgumentException) {
                        throw new ArgumentException($"Mismatching generator type for type {prop.PropertyType}");
                    }
                    continue;
                }

                Type type = prop.PropertyType;

                if (type == typeof(string))
                {
                    prop.SetValue(obj, ExtendedRandom.NextString(10), null);
                }
                else if (type == typeof(int) || type == typeof(long) || type == typeof(uint) || type == typeof(ulong))
                {
                    prop.SetValue(obj, Random.Next(1, 500), null);
                }
                else if (type == typeof(byte))
                {
                    prop.SetValue(obj, Random.Next(0, 256), null);
                }
                else if (type == typeof(sbyte))
                {
                    prop.SetValue(obj, Random.Next(-128, 128), null);
                }
                else if (type == typeof(short) || type == typeof(ushort))
                {
                    prop.SetValue(obj, Random.Next(0, 32768), null);
                }
                else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
                {
                    prop.SetValue(obj, Convert.ChangeType(ExtendedRandom.NextDouble(0, 500), type), null);
                }
                else if (type == typeof(bool))
                {
                    prop.SetValue(obj, ExtendedRandom.NextBoolean(), null);
                }
                else if (type == typeof(char))
                {
                    prop.SetValue(obj, ExtendedRandom.NextChar(), null);
                }
                else if (type == typeof(Guid))
                {
                    prop.SetValue(obj, Guid.NewGuid(), null);
                }
                else if (type == typeof(DateTime))
                {
                    prop.SetValue(obj, ExtendedRandom.NextDateTime(), null);
                }
            }
            return(obj);
        }
        public void WeightedChoice_WithWeightsBasedOnLength_ReturnsCorrectWeighting()
        {
            var items = new[] {"to", "one", "three"};
            var totals = new[] {0, 0, 0};

            for (var i = 0; i < 1000000; i++)
            {
                var item = Random.WeightedChoice(items, s => s.Length);
                for (var j = 0; j < items.Length; j++)
                {
                    if (items[j] == item)
                    {
                        totals[j]++;
                    }
                }
            }

            // Statistically, this should never fail...
            Assert.AreEqual(totals[0], 200000, 2000);
            Assert.AreEqual(totals[1], 300000, 2000);
            Assert.AreEqual(totals[2], 500000, 2000);
        }
 public void SetSeed(int seed)
 {
     rnd = new ExtendedRandom(seed);
 }