Example #1
0
        public void NewGeneration()
        {
            int finalCount = Settings.PopulationSize + Settings.NumNewDNA;
            List <Chromosome> newPopulation = new List <Chromosome>();

            if (finalCount <= 0)
            {
                return;
            }

            if (Population.Count > 0)
            {
                CalculateAllFitness();
                Population = Population.OrderBy(chrmsm => chrmsm.Fitness).ToList();
            }

            for (int i = 0; i < finalCount; i++)
            {
                // Keep only top individuals of the previous generation
                if (i < Elitism && i < Settings.PopulationSize)
                {
                    newPopulation.Add(Population[i]);
                    //Console.WriteLine("Generation: " + Generation);
                    //Console.WriteLine("/n");
                    //Console.WriteLine("genes: [{0}]", string.Join(",\t", Population[i].genes));
                    //Console.WriteLine(Population[i].fitness);
                }
                else if (i < Settings.PopulationSize)
                {
                    Chromosome parent1 = Selection.PerformSelection(Population);
                    Chromosome parent2 = Selection.PerformSelection(Population);
                    Chromosome child   = Crossover.PerformCrossover(parent1, parent2);
                    child = Mutation.PerformMutation(child);
                    newPopulation.Add(child);
                }
                else if (Settings.EnableCrossoverNewDNA)
                {
                    newPopulation.Add(new Chromosome(shouldInitGenes: true));
                }
            }

            Population = newPopulation;
            Generation++;
        }
Example #2
0
        public void Epoch()
        {
            CalcFitness();

            var newpopulations = new List <IGenome>();

            Population = Population.OrderBy(o => o.Fitness).ToList();

            for (int j = 0; j < GASettings.BestSolutionToPick; j++)
            {
                Population[j].Fitness = Fitness.Calc(Population[j]);
                Population[j].Score   = 0;
                newpopulations.Add(Population[j]);
            }

            int ran = Rand.Next(1, Population.Count);

            while (newpopulations.Count < Population.Count)
            {
                // Selection
                var nodemom = Selection.Select(Population);
                var nodedad = Selection.Select(Population);
                // CrossOver
                var cross = Crossover.Calc(new CrossoverOperation(nodemom, nodedad));
                //// Mutation
                nodemom = Mutate.Calc(cross.Mom);
                nodedad = Mutate.Calc(cross.Dad);
                // Adaptation
                nodemom = Adaptation.Calc(nodemom);
                nodedad = Adaptation.Calc(nodedad);

                nodemom.Fitness = Fitness.Calc(nodemom);
                nodedad.Fitness = Fitness.Calc(nodedad);

                // Add in new population
                newpopulations.Add(nodemom);
                newpopulations.Add(nodedad);
            }
            Population = null;
            Population = newpopulations.ToList();

            Generation++;
        }