Ejemplo n.º 1
0
        public void CrossOver(Random random = null)
        {
            EnsurePopulationIsCreated();

            if (random == null)
            {
                random = _random;
            }

            // Figure out if crossover should occur for this generation, based on a roll of a random number
            decimal percentage   = _crossOverChance;
            int     randomNumber = random.Next(1, 100);

            if (percentage > 0 && randomNumber <= percentage)
            {
                // Loop through all genome pairs
                for (int i = 0; i < Population.Count; i += 2)
                {
                    if (i > Population.Count)
                    {
                        break;
                    }

                    Genome genome1 = Population[i];
                    Genome genome2 = Population[i + 1];

                    // Pick a random position to swap at
                    int position = random.Next(0, _geneSize);

                    // Create 2 new genomes with the two parts swapped
                    Genome newGenome1 = genome1.Clone();
                    Genome newGenome2 = genome2.Clone();

                    newGenome1.SwapWith(genome2, position);
                    newGenome2.SwapWith(genome1, position);

                    Population[i]     = newGenome1;
                    Population[i + 1] = newGenome2;
                }
            }
            else
            {
                // (No cross over, return)
                if (ShowDebugMessages)
                {
                    Console.WriteLine("No crossover performed - the random {0}% was over the {1}% threshold.", randomNumber, percentage);
                }
            }
        }
        public void CrossOver(Random random = null)
        {
            EnsurePopulationIsCreated();

            if (random == null)
            {
                random = _random;
            }

            decimal percentage   = _crossOverChance;
            int     randomNumber = random.Next(1, 100);

            if (percentage > 0 && randomNumber <= percentage)
            {
                for (int i = 0; i < Population.Count; i += 2)
                {
                    if (i > Population.Count)
                    {
                        break;
                    }

                    Genome genome1 = Population[i];
                    Genome genome2 = Population[i + 1];

                    int position = random.Next(0, _geneSize);

                    Genome newGenome1 = genome1.Clone();
                    Genome newGenome2 = genome2.Clone();

                    newGenome1.SwapWith(genome2, position);
                    newGenome2.SwapWith(genome1, position);

                    Population[i]     = newGenome1;
                    Population[i + 1] = newGenome2;
                }
            }
            else
            {
                if (ShowDebugMessages)
                {
                    Console.WriteLine("No crossover performed - the random {0}% was over the {1}% threshold.", randomNumber, percentage);
                }
            }
        }