Пример #1
0
 public ElitismOpr(OptimisationTemplate Template, GAParameters gaParameters, Population.Population parentPopulation, Population.Population childPopulation)
 {
     this.Template         = Template;
     this.gaParameters     = gaParameters;
     this.parentPopulation = parentPopulation;
     this.childPopulation  = childPopulation;
 }
Пример #2
0
        public override void InitPopulation()
        {
            var fitnessLength       = Engine.Parameters.GetInt(ParameterNames.FitnessLength);
            var maxNumberOfEntities = Engine.Parameters.GetInt(ParameterNames.PopulationSize);
            var minNumberOfEntities = System.Convert.ToInt32(Engine.Parameters.GetValue(ParameterNames.SelectionRate) * maxNumberOfEntities);

            IEntityList initialEntities = new EntityList(maxNumberOfEntities);

            for (int i = 0; i < maxNumberOfEntities; i++)
            {
                var entity = CreateEntity();
                initialEntities.Add(entity);
            }

            initialEntities = Evaluate(initialEntities);

            Population = new Population.Population(fitnessLength, minNumberOfEntities, maxNumberOfEntities);

            for (int i = 0; i < maxNumberOfEntities; i++)
            {
                Population.Add(initialEntities[i]);
            }

            MergeToBests(Population);
        }
        //returns an array of the indices of selected chromosomes from the population after applying selection operator
        public override int[] ApplySelectionOpr(Population.Population population)
        {
            int populationSize = population.Size;

            //creating an array for storing shuffled indices of the chromosomes of population
            //array initialisation
            int[] shuffledIndex = new int[populationSize];
            //storing all the indices in ascending order (1 to population size) for later shuffling (thats why, the name shuffledIndex)
            for (int i = 0; i < populationSize; i++)
            {
                shuffledIndex[i] = i;
            }

            //initialising an array for storing indices of the chromosomes after selection for mating pool
            int[] matingPoolIndex = new int[populationSize];
            Chromosome.Chromosome winner;
            int populationCounter = 0;

            //if the tournament is between 2 chromosomes, we need to schedule 2 sets of tournaments in order to create mating pool of size equal to population size
            for (int i = 0; i < tournamentSize; i++)
            {
                //shuffling the indices of chromosomes in the population
                shuffleArray(shuffledIndex);


                for (int a = 0; a < shuffledIndex.Length; a++)
                {
                    System.Console.Write(shuffledIndex[a] + "*");
                }
                System.Console.WriteLine("");


                for (int j = 0; j < populationSize; j += tournamentSize)
                {
                    winner = population[shuffledIndex[j]];
                    matingPoolIndex[populationCounter] = shuffledIndex[j];
                    for (int k = 1; k < tournamentSize; k++)
                    {
                        if (winner.IsBetterThan(population[shuffledIndex[j + k]]))
                        {
                            winner = population[shuffledIndex[j]];
                            matingPoolIndex[populationCounter] = shuffledIndex[j];
                        }
                        else
                        {
                            winner = population[shuffledIndex[j + k]];
                            matingPoolIndex[populationCounter] = shuffledIndex[j + k];
                        }
                    }
                    populationCounter++;
                }
            }

            return(matingPoolIndex);
        }
        //implements 'quick sort' sorting algorithm
        //pop is the single objective population to be sorted
        //sortedIndex array will store the indices of sorted population
        private void PopulationSorting(Population.Population pop, int[] sortedIndex, int left, int right)
        {
            if (right > left)
            {
                var target = (SingleObjectiveChromosome)pop[sortedIndex[right - 1]];//this.popChromosomes[sortedCromList[right-1]]);
                int i      = left - 1;
                int j      = right - 1;
                while (true)
                {
                    while (((SingleObjectiveChromosome)pop[sortedIndex[++i]]).IsBetterThan(target))
                    {
                        if (i >= right - 1)
                        {
                            break;
                        }
                    }
                    if (i >= j)
                    {
                        break;
                    }
                    while (!((SingleObjectiveChromosome)pop[sortedIndex[--j]]).IsBetterThan(target))
                    {
                        if (j <= 0)
                        {
                            break;
                        }
                    }
                    if (i >= j)
                    {
                        break;
                    }
                    //swaping
                    Swap(ref sortedIndex[i], ref sortedIndex[j]);
                }
                //swaping
                Swap(ref sortedIndex[i], ref sortedIndex[right - 1]);

                PopulationSorting(pop, sortedIndex, left, i);
                PopulationSorting(pop, sortedIndex, i + 1, right);
            }
        }
 public ProportionalElitismOpr(OptimisationTemplate Template, GAParameters gaParameters, Population.Population parentPopulation, Population.Population childPopulation)
     : base(Template, gaParameters, parentPopulation, childPopulation)
 {
 }
Пример #6
0
 public abstract int[] ApplySelectionOpr(Population.Population population);