Exemplo n.º 1
0
        public RecombinationOutput Recombine(Genotype a, Genotype b)
        {
            IList<Genotype> offsprings = new List<Genotype>();

            int nbrChild = UnityEngine.Random.Range(minChild, maxChild);

            for (var i = 0; i < nbrChild; ++i)
            {
                Genotype g = new Genotype();
                Extension e;

                // 50% chance to inherit the base extension from one of the parents.
                if (Probability.Test(0.5))
                    e = a.Root;
                else
                    e = b.Root;

                g.Root = e.LocalClone();

                recombinaison(g.Root, a.Root, b.Root);

                // Mutations
                if (Probability.Test(0.3))
                {
                    Mutation mutation = new Mutation();
                    mutation.AddGeneticModifier(new Blur(new Set(new[] { "root" }, Set.Mode.Blacklist), new Set(new[] { "scale" }), 0.1f));
                    //mutation.AddGeneticModifier(new Addition(new Set(new[] { "Motor" }, Set.Mode.Whitelist), new Set(new[] { "scale" }), 2));
                    g.Mutate(mutation);
                }

                offsprings.Add(g);
            }

            return new RecombinationOutput(offsprings);
        }
Exemplo n.º 2
0
    public void Evolve()
    {
        //Evolve pop
        var newPopulation = new List <Genotype>();

        //Breed using routlette wheel selection
        for (int i = 0; i < Population.Count / 2; ++i)
        {
            var parent1 = RouletteWheelSelection(Population);
            var parent2 = RouletteWheelSelection(Population);
            //Crossover
            Genotype offspring1 = parent1, offspring2 = parent2;
            OnePointCrossover(parent1, parent2, ref offspring1, ref offspring2);
            //Mutate offspring
            offspring1.Mutate(mutationRate);
            offspring2.Mutate(mutationRate);

            offspring1.FixEndPoints();
            offspring2.FixEndPoints();

            newPopulation.Add(offspring1);
            newPopulation.Add(offspring2);
        }

        //Maintain fittest from previous population.
        newPopulation[0] = FittestGenotype();

        //Regenerate lower portion of the population to preserve diversity
        for (int i = newPopulation.Count - newPopulation.Count / 6; i < newPopulation.Count; i++)
        {
            newPopulation[i] = new Genotype();
        }

        Population = newPopulation;

        foreach (Genotype g in Population)
        {
            Phenotype.CreatePhenotype(g);
        }

        currentGeneration++;
        generationLabel.text = "Generation " + currentGeneration;
        if (autosaveToggle && currentGeneration % autosaveGenerationInterval == 0)
        {
            string   json = SavePopulation();
            FileInfo file = new FileInfo(Application.persistentDataPath + "/" + sessionNameInputField.text + "/"
                                         + "autosavePopulation" + currentGeneration / autosaveGenerationInterval + ".json");
            file.Directory.Create();
            File.WriteAllText(file.FullName, json);
        }

        results.Clear();
        internalTimer = 0.0f;
    }