public void RealizeEvolution()
        {
            Initialisation initialisation = new Initialisation(initialPopulationCount);
            // Initialisation - validated
            List <Path> population = initialisation.GenerateInitialPopulation();

            for (int i = 0; i < population.Count; i++)
            {
                if (StaticOperations.ValidatePath(population[i]) == false)
                {
                    throw new NotSupportedException();
                }
            }
            // Evaluation
            Evaluation evaluation = new Evaluation();

            evaluation.EvaluatePopulation(population);
            // Encoding
            List <Representation> representations = new List <Representation>();
            Decoder decoder = new Decoder();

            foreach (Path path in population)
            {
                Representation representation = decoder.EncodePath(path);
                representations.Add(representation);
            }
            // Evolution cycle
            for (int i = 0; i < evolutionCycles; i++)
            {
                // Selection
                Selection             selection = new Selection(parentsCount, representations);
                List <Representation> parents   = selection.SelectParents();
                // Genetic operator - validated
                GeneticOperator       geneticOperator = new GeneticOperator(descendantsCount, parents);
                List <Representation> descendants     = geneticOperator.GenerateDescendants();
                // Decoding
                List <Path> descendantPaths = new List <Path>();
                foreach (Representation representation in descendants)
                {
                    Path path = decoder.DecodeRepresentation(representation);
                    if (StaticOperations.ValidatePath(path) == false)
                    {
                        throw new NotSupportedException();
                    }
                    descendantPaths.Add(path);
                }
                // Evaluation
                evaluation.EvaluatePopulation(descendantPaths);
                for (int j = 0; j < descendants.Count; j++)
                {
                    descendants[j].Fitness = descendantPaths[j].Fitness;
                }
                // Replacement
                Replacement replacement = new Replacement(parents, descendants, initialPopulationCount);
                representations = replacement.NextGeneration();
                // Save to export file
                SaveTwoBestMembers(representations);
            }
        }
示例#2
0
        /// <summary>
        /// |C|だけ個体を生成
        /// </summary>
        private void GeneticOperation()
        {
            var Parent = new Tree[2];
            Func <int, Tree> Selector = SelectByRoulette;

            for (var i = 0; i < Children.Length;)
            {
                double p = rand.NextDouble();
                Parent[0] = Selector(TournamentSize);


                if (p < RateOfCross && i < Children.Length - 1)
                {
                    // 交叉
                    Parent[1] = Selector(TournamentSize);

                    var children = GeneticOperator.Crossover(Parent, buySign);

                    for (var j = 0; j < children.Length; j++)
                    {
                        if (children[j].ToString() == Parent[j].ToString())
                        {
                            children[j] = GeneticOperator.Mutation(children[j], buySign);
                        }
                    }

                    Children[i]     = children[0];
                    Children[i + 1] = children[1];
                    i += 2;
                }
                else
                {
                    // 突然変異
                    Children[i] = GeneticOperator.Mutation(Parent[0], buySign);
                    i++;
                }
            }
        }
示例#3
0
        public void RealizeEvolution()
        {
            Random         random         = new Random();
            Initialisation initialisation = new Initialisation(initialPopulationCount);
            // Initialisation - validated
            List <Path> population = initialisation.GenerateInitialPopulation();

            for (int i = 0; i < population.Count; i++)
            {
                if (StaticOperations.ValidatePath(population[i]) == false)
                {
                    throw new NotSupportedException();
                }
            }
            // Evaluation
            Evaluation evaluation = new Evaluation();

            evaluation.EvaluatePopulation(population);
            // Encoding
            List <Representation> representations = new List <Representation>();
            Decoder decoder = new Decoder();

            foreach (Path path in population)
            {
                Representation representation = decoder.EncodePath(path);
                representations.Add(representation);
            }
            // Evolution cycle
            for (int i = 0; i < evolutionCycles; i++)
            {
                Console.Write("Epoch #" + i + ".");
                // Reinitialisation happens every 1/10th iteration and randomly resets half of population
                // Elite 10% is untouched by this process
                if ((i % 500) == 0 && i != 0)
                {
                    ReinitializePopulation(representations, (int)(3 * initialPopulationCount / 4));
                }
                // Remap fitness using exponential remapping
                //RemapFitness(representations, remapParameter);
                // Selection
                Selection             selection = new Selection(parentsCount, representations);
                List <Representation> parents   = selection.SelectParents();
                //List<Representation> parents = selection.SelectCombinedParents();
                // Genetic operator - validated
                GeneticOperator       geneticOperator = new GeneticOperator(descendantsCount, parents);
                List <Representation> descendants     = geneticOperator.GenerateDescendants();
                // Decoding
                List <Path> descendantPaths = new List <Path>();
                foreach (Representation representation in descendants)
                {
                    Path path = decoder.DecodeRepresentation(representation);
                    if (StaticOperations.ValidatePath(path) == false)
                    {
                        throw new NotSupportedException();
                    }
                    descendantPaths.Add(path);
                }
                // Evaluation
                evaluation.EvaluatePopulation(descendantPaths);
                for (int j = 0; j < descendants.Count; j++)
                {
                    descendants[j].Fitness = descendantPaths[j].Fitness;
                }
                // Revaluate current population after fitness remapping
                //List<Path> currentPaths = new List<Path>();
                //foreach (Representation representation in representations)
                //{
                //    Path path = decoder.DecodeRepresentation(representation);
                //    currentPaths.Add(path);
                //}
                //evaluation.EvaluatePopulation(currentPaths);
                //for (int j = 0; j < representations.Count; j++)
                //{
                //    representations[j].Fitness = currentPaths[j].Fitness;
                //}
                // Replacement
                Replacement replacement = new Replacement(representations, descendants, initialPopulationCount);
                //representations = replacement.GenerationReplacement();
                //representations = replacement.NextGeneration();
                representations = replacement.DuplicationElimination(7, representations.Count / 20 < 3 ? representations.Count / 20 : 3, 20);
                Console.Write(" Maximum fitness: " + representations.Max(item => item.Fitness));
                // Save to export file
                SaveSixBestMembers(representations);
            }
        }
示例#4
0
    private void NewPopulation()
    {
        KillAll();

        var genomes       = GetAllGenomes();
        var newPopulation = new List <Brain>();

        if (m_Save)
        {
            Save(m_Generation != 1, genomes);
        }

        var elite = GeneticOperator.Elitism(genomes);

        foreach (var genome in elite)
        {
            for (int j = 0; j < m_GrowthActions; j++)
            {
                genome.Growth();
            }

            newPopulation.Add(CreateKartWithBrain(genome));
        }

        for (int i = 0; i < (m_PopulationSize - GeneticOperator.ELITISM_NUMBER) / 2; i++)
        {
            Genome parent1 = GeneticOperator.TournamentSelection(genomes);
            Genome parent2 = GeneticOperator.TournamentSelection(genomes);
            Genome parent3 = GeneticOperator.TournamentSelection(genomes);

            Genome offspring1, offspring2;

            GeneticOperator.BlendCrossover(parent1, parent2, out offspring1);
            GeneticOperator.BlendCrossover(parent1, parent3, out offspring2);

            /*
             * Genome parent1 = GeneticOperator.TournamentSelection(genomes);
             * Genome parent2 = GeneticOperator.TournamentSelection(genomes);
             *
             * Genome offspring1, offspring2;
             *
             * GeneticOperator.UniformCrossover(parent1, parent2, out offspring1, out offspring2);
             */

            GeneticOperator.Mutate(ref offspring1);
            GeneticOperator.Mutate(ref offspring2);

            for (int j = 0; j < m_GrowthActions; j++)
            {
                offspring1.Growth();
                offspring2.Growth();
            }

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

        for (int i = 0; i < m_PopulationSize; i++)
        {
            Destroy(m_Population[i].gameObject);
        }

        m_Population.Clear();
        m_Population = newPopulation;
        InitializeBrains();

        m_CurrentActionNumber += m_GrowthActions;
        m_Generation++;
    }