Exemple #1
0
        public Phenotype Crossover(Phenotype other)
        {
            Random    r            = new Random();
            int       splice_point = r.Next(parameters.Input.Size);
            Phenotype child        = new Phenotype(parameters);

            for (int i = 0; i < splice_point; i++)
            {
                child.Genotype[i] = this.Genotype[i];
            }
            for (int i = splice_point; i < child.Genotype.Length; i++)
            {
                if (child.Genotype.Contains(other.Genotype[i]) == false) //ideally - if we can take the second half from the second parent - that's great, let's go for it
                {
                    child.Genotype[i] = other.Genotype[i];
                }
                else if (child.Genotype.Contains(other.Genotype[i]) == false) //let's see if we can grab the second half from the first parent
                {
                    child.Genotype[i] = this.Genotype[i];
                }
                else
                { //shove whatever we can
                    int[] available = Enumerable.Range(1, parameters.Input.Size)
                                      .Where(n => child.Genotype.Contains(n) == false)
                                      .OrderBy(m => r.Next())
                                      .ToArray();
                    child.Genotype[i] = available[0];
                }
            }

            return(child);
        }
        override protected Phenotype Selection()
        {
            int       bestRating  = BestRating();
            int       worstRating = WorstRating();
            Phenotype chosen      = null;

            Random r = new Random();
            Dictionary <Phenotype, int> selectionPoints = new Dictionary <Phenotype, int>();
            int sumOfPoints = 0;

            foreach (Phenotype ph in Population)
            {
                int points = worstRating - parameters.Evaluate(ph.Genotype);
                selectionPoints.Add(ph, points);
                sumOfPoints += points;
            }

            int randomized    = r.Next(sumOfPoints);
            int cumulativeSum = 0;

            for (int i = 0; i < Population.Length && chosen == null; i++)
            {
                cumulativeSum += selectionPoints[Population[i]];
                if (randomized < cumulativeSum)
                {
                    chosen = Population[i];
                }
            }

            return(chosen);
        }
        public override int[] Run()
        {
            Initialize();
            for (int i = 0; i < NOfGenerations; i++)
            {
                //report results
                string GenerationResults = $"{i};{cur_generation.BestRating()};{cur_generation.AverageRating()};{cur_generation.WorstRating()}\n";
                sw.Write(GenerationResults);
                //next generation
                cur_generation = cur_generation.NextGeneration();
                if (Evaluate(cur_generation.BestPhenotype().Genotype) < Evaluate(best.Genotype))
                {
                    best = cur_generation.BestPhenotype();
                }
            }

            sw.WriteLine();
            for (int i = 0; i < Input.Size; i++)
            {
                sw.Write($"{best.Genotype[i]}, ");
            }
            sw.WriteLine(Evaluate(best.Genotype));

            sw.Close();

            return(best.Genotype);
        }
Exemple #4
0
 public Generation(GeneticAlgorithm parameters)
 {
     this.parameters = parameters;
     this.Population = new Phenotype[parameters.Population_size];
     for (int i = 0; i < parameters.Population_size; i++)
     {
         Population[i] = new Phenotype(parameters);
     }
 }
Exemple #5
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);
        }
Exemple #6
0
        override protected Phenotype Selection()
        {
            int t_size = parameters.Tournament_size;

            Phenotype[] tournament = new Phenotype[t_size];
            for (int i = 0; i < t_size; i++)
            {
                Random r = new Random();
                tournament[i] = Population[r.Next(Population.Length)];
            }
            return(BestPhenotype(tournament));
        }
Exemple #7
0
        public Phenotype BestPhenotype()
        {
            Phenotype best = Population[0];

            foreach (Phenotype p in Population)
            {
                if (parameters.Evaluate(p.Genotype) < parameters.Evaluate(best.Genotype))
                {
                    best = p;
                }
            }
            return(best);
        }
Exemple #8
0
        protected Phenotype BestPhenotype(Phenotype[] population)
        {
            Phenotype best = population[0];

            foreach (Phenotype p in population)
            {
                if (parameters.Evaluate(p.Genotype) < parameters.Evaluate(best.Genotype))
                {
                    best = p;
                }
            }
            return(best);
        }