Ejemplo n.º 1
0
 private void CalcFitness()
 {
     foreach (var item in Population)
     {
         item.Fitness = Fitness.Calc(item);
     }
 }
Ejemplo n.º 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++;
        }