コード例 #1
0
        /// <summary>
        /// Runs a random walk of n steps along a landscape defined by a crossover operator and returns its results.
        /// </summary>
        /// <param name="steps">Number of steps.</param>
        /// <param name="searchOperator">Operator defining a neighbourhood.</param>
        /// <returns></returns>
        public RandomWalk RandomWalk(int steps, CrossoverOperator searchOperator)
        {
            RandomWalk statistics = new RandomWalk(steps);
            Chromosome parent1 = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem);
            Chromosome parent2 = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem);
            bool       firstParent = RandomGeneratorThreadSafe.NextBool();
            Chromosome parent = (firstParent ? parent1 : parent2), child;

            GatherData(parent, 0, statistics);

            const int minPopSize = 8, maxPopSize = 15;
            int       popSize = RandomGeneratorThreadSafe.NextInt(minPopSize, maxPopSize);

            Chromosome[] supportPopulation = new Chromosome[popSize];
            for (int i = 0; i < popSize; ++i)
            {
                supportPopulation[i] = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem);
            }

            IGene[] childGenes1, childGenes2;
            for (int i = 0; i < steps; ++i)
            {
                searchOperator.Run(parent1.Genes, parent2.Genes, out childGenes1, out childGenes2);
                child = ChromosomeFactory.MakeChromosome(Problem, RandomGeneratorThreadSafe.NextBool() ? childGenes1 : childGenes2);
                GatherData(child, i, statistics);
                parent1 = child;
                parent2 = supportPopulation[RandomGeneratorThreadSafe.NextInt(popSize)];
            }

            return(statistics);
        }
 private bool[] PrepareUniform(int geneCount)
 {
     return(Enumerable.Repeat(0, geneCount).Select(i => RandomGeneratorThreadSafe.NextBool()).ToArray());
 }