예제 #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);
        }
예제 #2
0
        /// <summary>
        /// Runs a random walk of n steps along a landscape defined by a mutation 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, MutationOperator searchOperator)
        {
            RandomWalk statistics      = new RandomWalk(steps);
            Chromosome currentSolution = ChromosomeFactory.RandomSolution(Problem.GeneCount(), Problem);

            GatherData(currentSolution, 0, statistics);
            for (int i = 1; i < steps; ++i)
            {
                searchOperator.Run(ref currentSolution);
                GatherData(currentSolution, i, statistics);
            }
            return(statistics);
        }