Esempio n. 1
0
        /**
         * Creates a new population by recombining the parents to create children. Selects the parents to stay in the new population with roulette wheel selection.
         * @see ParentRecombination()
         */
        Population RefreshPopulation(Population population, ConnectedBlocksGraph connectedBlocks, GeneticOptions geneticOptions, Random random)
        {
            RouletteWheelSelection rouletteWheelSelection = new RouletteWheelSelection();
            int childrenAmount = (int)(geneticOptions.PopulationSize * geneticOptions.PopulationRefreshing);

            Individual[] children = new Individual[geneticOptions.PopulationSize];
            for (int i = 0; i < childrenAmount; i++)
            {
                int[,] parents = ParentSelection(population, random);
                children[i]    = ParentRecombination(new Individual(Util.GetRow(parents, 0)), new Individual(Util.GetRow(parents, 1)), random);
                children[i].CalculateFitness(connectedBlocks, geneticOptions);
            }
            for (int i = 0; i < geneticOptions.PopulationSize - childrenAmount; i++)
            {
                children[i + childrenAmount] = population.Individuals[rouletteWheelSelection.Selection(population.GetFitness(), random)];
            }
            return(new Population(children));
        }
Esempio n. 2
0
        /**
         * A method that selects two different parents to be used for recombination.
         * @return A 2D array containing each parent in each row.
         */
        int[,] ParentSelection(Population population, Random random)
        {
            RouletteWheelSelection rouletteWheelSelection = new RouletteWheelSelection();

            //Select two parents with roulette wheel selection
            int[,] selectedParents = new int[2, population.Individuals[0].Sequence.Length];
            int firstParentPosition  = rouletteWheelSelection.Selection(population.GetFitness(), random);
            int secondParentPosition = rouletteWheelSelection.Selection(population.GetFitness(), random);

            //Ensure that the two parents are different
            while (firstParentPosition == secondParentPosition)
            {
                secondParentPosition = rouletteWheelSelection.Selection(population.GetFitness(), random);
            }
            //Add parents in list to return
            for (int i = 0; i < population.Individuals[0].Sequence.Length; i++)
            {
                selectedParents[0, i] = population.Individuals[firstParentPosition].Sequence[i];
                selectedParents[1, i] = population.Individuals[secondParentPosition].Sequence[i];
            }

            return(selectedParents);
        }