Пример #1
0
        public static Population BreedPopulation(Population p, Evaluations e, getRandomGeneFunct getRandGene, double mutate_probability = MUTATION_RATE)
        {
            Population children = new Population();
            DNAMateResult breed;

            // Apply mate_at_random until enough children have been
            // bred to match the size of the original population

            while (children.Count != p.Count)
            {
                breed = Mate(p);

                children.AddRange(breed);
            }

            return children;
        }
Пример #2
0
        public static Population Evolve(Population p, int generations, printPopulationFunct print, evaluateFunct evaluate, getRandomGeneFunct getRandGene, double mutation_rate = MUTATION_RATE)
        {
            Population c;
            MutatedPopulation mutated;
            Population children;
            int gen = 0;

            c = p;

            while (gen < generations)
            {
                /*
                 * Rather than apply mutation while mating is occurring,
                 * we'll apply a population-wide possible mutation step before breeding commences.
                 * Each DNA string will have a chance of mutation. */

                mutated = MutatePopulation(c, evaluate, getRandGene, mutation_rate);

                children = BreedPopulation(mutated, mutated.evaluations, getRandGene, mutation_rate);

                gen++;

                if (print!= null) print(c, gen);

                c = children;
            }

            return c;
        }
Пример #3
0
 public static Population Evolve(Population p, int generations, Evaluator evaluator, getRandomGeneFunct getRandGene, double mutation_rate = MUTATION_RATE)
 {
     return Evolve(p, generations, null, evaluator.eval, getRandGene, mutation_rate);
 }
Пример #4
0
        /// <summary>
        /// Mutation Predicate.
        /// Performs a mutation on chance. 
        /// Mutation occurs if calculated chance is less than required probability, otherwise the DNA is not affected.
        /// </summary>
        /// <param name="probability">
        /// Probability of mutation occuring.
        /// Value must be between 0.0 and 1.0
        /// </param>
        /// <returns>
        /// Either a mutated DNA or unaffected DNA
        /// </returns>
        public static Mutant mutate_by_chance(DNA original, getRandomGeneFunct randomGene,
            double probability = MUTATION_RATE)
        {
            Mutant m;
            int mut_index; // a random point in the DNA at which mutation will occur

            Random rand = new Random();
            if (rand.NextDouble() < probability)
            {
                mut_index = rand.Next(1, original.Count);

                return mutate(mut_index, original, randomGene);
            }
            else
            {
                m = new Mutant(original);
            }

            return m;
        }
Пример #5
0
        public static MutatedPopulation MutatePopulation(Population p, evaluateFunct evaluate, getRandomGeneFunct getRandGene, double mutate_probability = MUTATION_RATE)
        {
            MutatedPopulation mutated;

            mutated = new MutatedPopulation(p.Select((child) =>
                mutate_by_chance(child, getRandGene, mutate_probability)),null);

            mutated.evaluations = mutated.RunFitnessTests(evaluate);

            return mutated;
        }
Пример #6
0
        /// <summary>
        /// Mutation Predicate.
        ///  Modifies a DNA string by changing one of its bases to be a random new value
        /// </summary>
        /// <param name="mut_index">
        /// The point in which mutation is applied.
        /// </param>
        /// <param name="randomGene">
        /// The function used to supply random Genes
        /// </param>
        public static Mutant mutate(int mut_index, DNA original, getRandomGeneFunct randomGene)
        {
            Mutant m;
            DNA g_dna;
            DNAPartial g_partial;
            DNAPartial rest;
            Gene g;

            g=randomGene();

            while (original.Contains(g))
                g = randomGene();

            g_dna = new DNA();
            g_dna.Add(g);

            var sp_mut = split(mut_index-1, original);
            g_partial = split(0, g_dna).back;

            // remove the base at the location that mutation is to occur
            DNA back = join(new DNAPartial(), sp_mut.back);
            back.RemoveAt(0);

            // add mutated item
            back = join(g_partial, split(0,back).back);
            DNA front = join(new DNAPartial(), sp_mut.front);

            // join front, mutation, and rest, lists together
            rest = split(0, join(split(0,front).back, split(0, back).back)).back;

            m = new Mutant(join(new DNAPartial(), rest));

            return m;
        }