Esempio n. 1
0
    /// <summary>
    /// Recombine the genotypes in intermediate population, producing a new population. The recombination
    /// between two genotypes is made by <see cref="CompleteCrossover"/>. At the end a new population of
    /// size newPopulationSize is created. A number of genotypes, given by the parameter survivalGenotype
    /// are directly passed to the new generation. <br/><br/>
    /// Note, the intermediatePopulation and the current population have to be ordered in descending order by fitness.
    /// </summary>
    /// <param name="intermediatePopulation">The intermediate population</param>
    /// <param name="newPopulationSize">The size of the new population</param>
    /// <param name="survivalGenotype">The number of genotypes to be passed directly in the
    /// next generation</param>
    /// <returns></returns>
    private List <Genotype> Recombination(List <Genotype> intermediatePopulation, int newPopulationSize, int survivalGenotype)
    {
        if (intermediatePopulation.Count < 2)
        {
            throw new ArgumentException("The intermediate population has to be at least of size 2");
        }
        List <Genotype> newPopulation = new List <Genotype>();

        for (int i = 0; i < survivalGenotype; i++)
        {
            newPopulation.Add(this._currentPopulation[i]);
        }

        while (newPopulation.Count < newPopulationSize)
        {
            int randomIndex1 = randomizer.Next(0, intermediatePopulation.Count), randomIndex2;
            do
            {
                randomIndex2 = randomizer.Next(0, intermediatePopulation.Count);
            } while (randomIndex1 == randomIndex2);

            Genotype offspring1, offspring2;
            GeneticAlgorithm.CompleteCrossover(intermediatePopulation[randomIndex1], intermediatePopulation[randomIndex2], out offspring1, out offspring2);

            newPopulation.Add(offspring1);
            if (newPopulation.Count < newPopulationSize)
            {
                newPopulation.Add(offspring2);
            }
        }
        return(newPopulation);
    }
Esempio n. 2
0
    private List <Genotype> RandomRecombination(List <Genotype> intermediatePopulation, uint newPopulationSize)
    {
        if (intermediatePopulation.Count < 2)
        {
            throw new System.ArgumentException("The intermediate population has to be at least of size 2 for this operator.");
        }

        List <Genotype> newPopulation = new List <Genotype>();

        newPopulation.Add(intermediatePopulation[0]);
        newPopulation.Add(intermediatePopulation[1]);


        while (newPopulation.Count < newPopulationSize)
        {
            int randomIndex1 = randomizer.Next(0, intermediatePopulation.Count), randomIndex2;
            do
            {
                randomIndex2 = randomizer.Next(0, intermediatePopulation.Count);
            } while (randomIndex2 == randomIndex1);

            Genotype offspring1, offspring2;
            GeneticAlgorithm.CompleteCrossover(intermediatePopulation[randomIndex1], intermediatePopulation[randomIndex2],
                                               GeneticAlgorithm.DefCrossSwapProb, out offspring1, out offspring2);

            newPopulation.Add(offspring1);
            if (newPopulation.Count < newPopulationSize)
            {
                newPopulation.Add(offspring2);
            }
        }

        return(newPopulation);
    }
Esempio n. 3
0
    // Recombination operator for the genetic algorithm, recombining random genotypes of the intermediate population
    private List <Genotype> RandomRecombination(List <Genotype> intermediatePopulation, uint newPopulationSize)
    {
        //Check arguments
        if (intermediatePopulation.Count < 2)
        {
            throw new System.ArgumentException("The intermediate population has to be at least of size 2 for this operator.");
        }

        List <Genotype> newPopulation = new List <Genotype>();

        //Always add atlist best two (unmodified)
        for (int i = 0; i < GameData.instance.BreadAmountToSaveFromSelection; i++)
        {
            newPopulation.Add(intermediatePopulation[i]);
        }

        while (newPopulation.Count < newPopulationSize)
        {
            //Get two random indices that are not the same
            int randomIndex1 = randomizer.Next(0, intermediatePopulation.Count), randomIndex2;
            do
            {
                randomIndex2 = randomizer.Next(0, intermediatePopulation.Count);
            } while (randomIndex2 == randomIndex1);

            Genotype offspring1, offspring2;
            GeneticAlgorithm.CompleteCrossover(intermediatePopulation[randomIndex1], intermediatePopulation[randomIndex2],
                                               GameData.instance.SwapProb, out offspring1, out offspring2);

            newPopulation.Add(offspring1);
            if (newPopulation.Count < newPopulationSize)
            {
                newPopulation.Add(offspring2);
            }
        }

        return(newPopulation);
    }
Esempio n. 4
0
        // Recombination operator for the genetic algorithm, recombining random genotypes of the intermediate population
        private List <Genotype> RandomRecombination(List <Genotype> intermediatePopulation, uint newPopulationSize)
        {
            //Check arguments
            if (intermediatePopulation.Count < 2)
            {
                throw new System.ArgumentException("The intermediate population has to be at least of size 2 for this operator.");
            }

            List <Genotype> newPopulation = new List <Genotype>();

            //Always add best two (unmodified)
            newPopulation.Add(intermediatePopulation[0]);
            newPopulation.Add(intermediatePopulation[1]);


            while (newPopulation.Count < newPopulationSize)
            {
                //Get two random indices that are not the same
                int randomIndex1 = randomizer.Next(0, intermediatePopulation.Count), randomIndex2;
                do
                {
                    randomIndex2 = randomizer.Next(0, intermediatePopulation.Count);
                } while (randomIndex2 == randomIndex1);

                Genotype offspring1, offspring2;
                GeneticAlgorithm.CompleteCrossover(intermediatePopulation[randomIndex1], intermediatePopulation[randomIndex2],
                                                   GameStateManager.Instance.appSettings.defCrossSwapProb, out offspring1, out offspring2);

                newPopulation.Add(offspring1);
                if (newPopulation.Count < newPopulationSize)
                {
                    newPopulation.Add(offspring2);
                }
            }

            return(newPopulation);
        }