示例#1
0
        public Generation NextGeneration()
        {
            Generation nextGeneration;

            //brzydkie to to cokolwiek
            if (this is GenerationTournament)
            {
                nextGeneration = new GenerationTournament(this.parameters);
            }
            else if (this is GenerationRoulette)
            {
                nextGeneration = new GenerationRoulette(this.parameters);
            }
            else
            {
                nextGeneration = null;
                throw new Exception("unknown generation type");
            }
            for (int i = 0; i < Population.Length; i += 2) //using 2 phenotypes from current generation we create 2 for the next one
            {
                Random r = new Random();

                //SELECTION
                Phenotype parent_a = Selection();
                Phenotype parent_b = Selection();
                Phenotype child_a;
                Phenotype child_b;

                //CROSSOVER
                if (r.NextDouble() < parameters.P_crossover)
                {
                    child_a = parent_a.Crossover(parent_b);
                    child_b = parent_b.Crossover(parent_a);
                }
                else
                {
                    //TU ZEPSUŁEŚ!
                    child_a = parent_a.Clone();
                    child_b = parent_b.Clone();
                }

                //MUTATION
                child_a.Mutate();
                child_b.Mutate();

                //ADD TO NEW GENERATION
                nextGeneration.Population[i] = child_a;
                if (i + 1 < Population.Length)
                {
                    nextGeneration.Population[i + 1] = child_b;
                }
            }

            return(nextGeneration);
        }
示例#2
0
 public GeneticAlgorithm(int nOfGenerations, int population_size, double p_crossover, double p_mutation, SelectionMethod selection, int tournament_size, string inputFileName) : base(inputFileName)
 {
     this.NOfGenerations  = nOfGenerations;
     this.P_crossover     = p_crossover;
     this.P_mutation      = p_mutation;
     this.Tournament_size = tournament_size;
     this.Population_size = population_size;
     if (selection == SelectionMethod.Roulette)
     {
         cur_generation = new GenerationRoulette(this);
     }
     else if (selection == SelectionMethod.Tournament)
     {
         cur_generation = new GenerationTournament(this);
     }
 }