Beispiel #1
0
        private IEnumerable <Chromosome <T> > DoCrossover(Population <T> generationPopulation,
                                                          SecureRandom random, int childCount)
        {
            const int ParentCount       = 2;
            var       RouletteSelection = (uint)Math.Ceiling((double)generationPopulation.Chromosomes.Count * 0.05);

            var crossovers = new ConcurrentBag <Chromosome <T> >();

            var tasks = new Task[this.Parameters.TaskCount];

            for (var c = 0; c < this.Parameters.TaskCount; c++)
            {
                tasks[c] = Task.Factory.StartNew(() =>
                {
                    do
                    {
                        var parents = new List <Chromosome <T> >();
                        var indexes = random.GetInt32Values(RouletteSelection,
                                                            new Range <int>(0, generationPopulation.Chromosomes.Count), ValueGeneration.UniqueValuesOnly);

                        for (var i = 0; i < ParentCount; i++)
                        {
                            var parent = (from chromosome in
                                          (from index in indexes
                                           select generationPopulation.Chromosomes[index])
                                          orderby chromosome.Fitness descending
                                          select chromosome).Take(1).First();

                            parents.Add(parent);
                        }

                        var children = random.NextDouble() < this.Parameters.CrossoverProbability ?
                                       this.Parameters.Crossover(parents.AsReadOnly()) :
                                       new List <T>(from parent in parents
                                                    select this.Parameters.Copy(parent.Value)).AsReadOnly();

                        foreach (var child in children)
                        {
                            var mutatedChild = this.Parameters.Mutator(child);

                            if (mutatedChild != null)
                            {
                                crossovers.Add(new Chromosome <T>(
                                                   mutatedChild, this.Parameters.FitnessEvaluator(mutatedChild)));
                            }
                        }
                    } while (crossovers.Count < childCount);
                });
            }

            Task.WaitAll(tasks);
            return(crossovers.AsEnumerable());
        }
 public void GetInt32ValuesUniqueValuesOnlyButElementNumberIsTooBig()
 {
     using (var random = new SecureRandom())
     {
         random.GetInt32Values((uint)int.MaxValue + 10u, ValueGeneration.UniqueValuesOnly);
     }
 }
        public void GetInt32ValuesUniqueValuesOnly()
        {
            var generator = new MockRandomNumberGeneratorForGetInt32Values(ValueGeneration.UniqueValuesOnly);

            using (var random = new SecureRandom(generator))
            {
                var elements = random.GetInt32Values(
                    8, ValueGeneration.UniqueValuesOnly);

                Assert.AreEqual(8, elements.Length);

                for (var i = 0; i < elements.Length; i++)
                {
                    var element = elements[i];
                    Assert.IsTrue(element >= int.MinValue);
                    Assert.IsTrue(element <= int.MaxValue);
                }

                Assert.AreEqual(elements.Length, new HashSet<int>(elements).Count);
                Assert.AreEqual(9, generator.MethodCallCount);
            }
        }
        public void GetInt32ValuesDuplicatesAllowed()
        {
            var generator = new MockRandomNumberGeneratorForGetInt32Values(ValueGeneration.DuplicatesAllowed);

            using (var random = new SecureRandom(generator))
            {
                var elements = random.GetInt32Values(8, ValueGeneration.DuplicatesAllowed);

                Assert.AreEqual(8, elements.Length);

                for (var i = 0; i < elements.Length; i++)
                {
                    var element = elements[i];
                    Assert.IsTrue(element >= int.MinValue);
                    Assert.IsTrue(element < int.MaxValue);
                }

                Assert.AreEqual(8, generator.MethodCallCount);
            }
        }
        public void GetInt32ValuesAfterDisposing()
        {
            SecureRandom random = null;

            using (random = new SecureRandom())
            {
            }

            random.GetInt32Values(1, ValueGeneration.DuplicatesAllowed);
        }