Ejemplo n.º 1
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++;
        }
Ejemplo n.º 2
0
        //// most time consuming, can be run in parallel
        //private float[] calculatePopulationFitness()
        //{
        //    for (int i = 0; i < fitnessParams.Length; i++)
        //        fs[i] = Fitness.fitness(researchData, fitnessParams[i]);

        //    return fs;
        //}


        // distribute processing on threads
        public void calculatePopulationFitnessParallel()
        {
            int nExperiments = population.Length;

            _numerOfThreadsNotYetCompleted = nExperiments;
            _doneEvent     = new ManualResetEvent(false);
            fitnessObjects = new Fitness[nExperiments];
            fitnesses      = new float[nExperiments];

            for (int i = 0; i < nExperiments; i++)
            {
                fitnessObjects[i] = new Fitness(researchDatas[i], fitnessParams[i]);
                ThreadPool.QueueUserWorkItem(ThreadPoolCallback, i);
            }


            // Wait for all threads in pool to calculate.
            _doneEvent.WaitOne();
            Console.WriteLine("All calculations are complete.");
        }
Ejemplo n.º 3
0
 public int CompareTo(Chromosome other) => Fitness.CompareTo(other.Fitness);
Ejemplo n.º 4
0
 public GAMachine(Population population, ISelection selection, ICrossover crossover, IMutation mutation, Fitness fitness, StopCondition stopCondition, double crossoverProbability = DefaultCrossoverProbability, double mutationProbability = DefaultMutationProbability)
 {
     _population           = population;
     _selection            = selection;
     _crossover            = crossover;
     _mutation             = mutation;
     _adjustment           = new BestReservationAdjustment();
     _fitness              = fitness;
     _stopCondition        = stopCondition;
     _crossoverProbability = crossoverProbability;
     _mutationProbability  = mutationProbability;
     _population.ReInit();
 }