Exemplo n.º 1
0
        public void Decreasing_Minimize(List <Particle> particle)
        {
            //Initializing linear decreasing inertia
            this.inertia.Initialize_LinearDecreasingInertia();

            //for timestep=1
            //Finding index of best particle
            this.best_index = this.gbest.Return_gbest_Minimization(particle, this.numberOfParticles);

            //calculating average fitness
            this.average = this.averageFitness.Return_AverageFitness(particle, this.numberOfParticles);

            //calculating population mean dispersion
            this.meanDispersion = disp.calculateDispersion(particle);

            //adding best particle fitness, average fitness of population and mean population dispersion to output class
            this.output[this.run].Add_Values(1, this.average, particle[this.best_index].ReturnFitness(), this.meanDispersion);

            //for rest of timesteps
            for (int i = 2; i <= this.numberOfTimeSteps; i++)
            {
                //updating velocity and position
                for (int j = 0; j < this.numberOfParticles; j++)
                {
                    //every particle calculating its own personal best
                    particle[j].Calculate_Pbest_Minimize();

                    //updating velocity
                    particle[j].Update_LinearDecreasingVelocity(particle[this.best_index], this.inertia.wt);

                    //updating position
                    particle[j].Update_Position();
                }

                //updating inertia
                this.inertia.Update_LinearDecreasingInertia(this.numberOfTimeSteps, i);

                //calculating best particle
                this.best_index = this.gbest.Return_gbest_Minimization(particle, this.numberOfParticles);

                //calculating average fitness
                this.average = this.averageFitness.Return_AverageFitness(particle, this.numberOfParticles);

                //calculating population mean dispersion
                this.meanDispersion = disp.calculateDispersion(particle);

                //adding best particle fitness, average fitness of population and mean population dispersion to output class
                this.output[this.run].Add_Values(i, this.average, particle[this.best_index].ReturnFitness(), this.meanDispersion);
            }
        }
Exemplo n.º 2
0
        //method that runs GA Algorithm
        public List <Output> RunGA()
        {
            //clearing output list
            this.output.Clear();

            //initializing list of individuals
            List <Individual> ind = new List <Individual>();

            //initializing list of offsprings
            List <Offspring> offspring = new List <Offspring>();

            //variable that stores best individual fitness value
            double best_fitness_in_generation;

            //setting run to 0
            this.run = 0;

            while (this.run < this.number_runs)
            {
                //clear lists of individuals and offsrpings
                ind.Clear();
                offspring.Clear();

                //initializing values in dispersion class
                this.disp.InitializeDispersion(this.pop_size, this.uniqueGenesFound);

                //add an instance of output class to list in each run
                this.output.Add(new Output());

                //initializing list of individuals
                for (int i = 0; i < this.pop_size; i++)
                {
                    //initialize individuals here
                    ind.Add(new Individual(this.uniqueGenesFound, this.number_genes, this.higher_range, this.lower_range, this.input_function, this.random));
                }

                //initializing list of offspring
                for (int i = 0; i < this.offspring_size; i++)
                {
                    offspring.Add(new Offspring(this.uniqueGenesFound, this.input_function, this.number_genes));
                }

                //running GA Algorithnm

                //calculations for time step1

                //calculating average fitness of population
                this.average = average_fitness.return_averageFitness(ind, this.pop_size);

                //calculating population mean dispersion
                this.mean_dispersion = disp.calculateDispersion(ind);

                //calculating population best fitness here
                this.best_index            = best_individual.return_best_individual(ind, this.pop_size, this.optimization_type);
                best_fitness_in_generation = ind[this.best_index].CalculateFitness();

                //adding generation1 values to output class
                this.output[this.run].Add_Values(1, this.average, best_fitness_in_generation, this.mean_dispersion);

                //for rest of generations
                for (int i = 2; i <= this.number_generations; i++)
                {
                    //performing mating selection
                    switch (this.selection_method)
                    {
                    case "Tournament":
                        offspring = this.mating.tournament_selection(ind, offspring, this.pop_size, this.offspring_size, this.tournament_size, this.number_genes, this.optimization_type, this.random);
                        break;

                    case "Fitness_Proportionate":
                        offspring = this.mating.fitness_proportionate(ind, offspring, this.pop_size, this.offspring_size, this.number_genes, this.optimization_type, random);
                        break;

                    case "Rank":
                        offspring = this.mating.rank_selection(ind, offspring, this.pop_size, this.offspring_size, this.number_genes, this.optimization_type, this.random, this.s);
                        break;
                    }

                    //performing recombination
                    switch (this.recombination_type)
                    {
                    case "One_point_crossover":
                        offspring = this.recomb.onepoint_crossover(offspring, this.offspring_size, this.crossover_rate, this.number_genes, this.random);
                        break;

                    case "Arithmentic_single_crossover":
                        offspring = this.recomb.arithmetic_single_recombination(offspring, this.offspring_size, this.crossover_rate, this.a, this.number_genes, this.random);
                        break;

                    case "Arithmetic_simple_crossover":
                        offspring = this.recomb.arithmetic_simple_recombination(offspring, this.offspring_size, this.crossover_rate, this.a, this.number_genes, this.random);
                        break;

                    case "Arithmetic_whole_crossover":
                        offspring = this.recomb.arithmetic_whole_recombination(offspring, this.offspring_size, this.crossover_rate, this.a, this.number_genes, this.random);
                        break;
                    }

                    //performing mutation
                    switch (this.mutation_type)
                    {
                    case "Normal_Distribution":
                        offspring = mutation.gaussian_mutaion(offspring, this.offspring_size, this.mutation_rate, this.number_genes, this.random, this.normalDist, this.lower_range, this.higher_range);
                        break;

                    case "Cauchy_Distribution":
                        offspring = mutation.cauchy_mutation(offspring, this.offspring_size, this.mutation_rate, this.number_genes, this.random, this.cauchyDist, this.lower_range, this.higher_range);
                        break;
                    }

                    //performing survivor selection
                    switch (this.survivor_selection_method)
                    {
                    case "Truncation":
                        ind = survivor.truncation_selection(ind, offspring, this.pop_size, this.offspring_size, this.number_genes, this.optimization_type, this.random);
                        break;
                    }

                    //calculating average fitness of population
                    this.average = average_fitness.return_averageFitness(ind, this.pop_size);

                    //calculating population mean dispersion
                    this.mean_dispersion = disp.calculateDispersion(ind);

                    //calculating population best fitness here
                    this.best_index            = best_individual.return_best_individual(ind, this.pop_size, this.optimization_type);
                    best_fitness_in_generation = ind[this.best_index].CalculateFitness();

                    //adding generation1 values to output class
                    this.output[this.run].Add_Values(i, this.average, best_fitness_in_generation, this.mean_dispersion);
                }

                this.run = this.run + 1;
            }

            //returning instance of output class
            return(this.output);
        }