コード例 #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
        public override void Run(IGene[] genes)
        {
            int startPosition = RandomGeneratorThreadSafe.NextInt(genes.Length - 1);
            int length        = RandomGeneratorThreadSafe.NextInt(genes.Length - startPosition - 2) + 2;

            Inverse(genes, startPosition, length);
        }
        public override List <Chromosome> Run(List <Chromosome> chromosomes, Parameters parameters)
        {
            int        k = 3, index;
            var        chosenOnes = new Chromosome[chromosomes.Count];
            var        tournament = new Chromosome[k];
            Chromosome winner     = null;

            for (int i = 0; i < chromosomes.Count; ++i)
            {
                for (int j = 0; j < k; ++j)
                {
                    index         = RandomGeneratorThreadSafe.NextInt(chromosomes.Count);
                    tournament[j] = chromosomes[index];
                }
                //if (parameters.FitnessStrategy == Fitness.FitnessStrategy.MAXIMIZE)
                winner = tournament[0];
                for (int j = 1; j < k; ++j)
                {
                    if (parameters.Fitness.Get(tournament[j]) < parameters.Fitness.Get(winner))
                    {
                        winner = tournament[j];
                    }
                }
                // winner = tournament.Aggregate((c1, c2) => parameters.Fitness.Get(c1) < parameters.Fitness.Get(c2) ? c1 : c2);
                chosenOnes[i] = (Chromosome)winner.Clone();
            }
            return(chosenOnes.ToList());
        }
コード例 #4
0
        public override void Run(ref Chromosome solution)
        {
            int startPosition = RandomGeneratorThreadSafe.NextInt(solution.Genes.Length - 1);
            int length        = RandomGeneratorThreadSafe.NextInt(solution.Genes.Length - startPosition - 2) + 2;

            Inverse(solution.Genes, startPosition, length);
            solution.Refresh();
        }
        public override void Run(IGene[] genes)
        {
            int startIndex, destIndex, length;

            RandomGeneratorThreadSafe.NextTwoDifferentInts(genes.Length - 1, out startIndex, out destIndex);
            int upperLimit = startIndex > destIndex ? startIndex : destIndex;

            length = RandomGeneratorThreadSafe.NextInt(1, genes.Length - upperLimit + 1);
            Displace(genes, startIndex, destIndex, length);
        }
        public override void Run(ref Chromosome solution)
        {
            int startIndex, destIndex, length;

            RandomGeneratorThreadSafe.NextTwoDifferentInts(solution.Genes.Length - 1, out startIndex, out destIndex);
            int upperLimit = startIndex > destIndex ? startIndex : destIndex;

            length = RandomGeneratorThreadSafe.NextInt(1, solution.Genes.Length - upperLimit + 1);
            Displace(solution.Genes, startIndex, destIndex, length);
            solution.Refresh();
        }
        public override void Run(IGene[] parent1, IGene[] parent2, out IGene[] child1, out IGene[] child2)
        {
            int customerCount = parent1.Count();

            IGene[] c1         = new IGene[customerCount];
            IGene[] c2         = new IGene[customerCount];
            int     slicePoint = RandomGeneratorThreadSafe.NextInt(customerCount);

            DoCycleCrossover(parent1, parent2, c1, c2, slicePoint, true);
            child1 = c1;
            child2 = c2;
        }
コード例 #8
0
        public static void Shuffle <T>(this IList <T> list)
        {
            int n = list.Count, k;

            while (n > 1)
            {
                --n;
                k = RandomGeneratorThreadSafe.NextInt(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }