Ejemplo n.º 1
0
        public void Generation()
        {
            batch.ForEach(evolvable => evolvable.Mutate(mutatability, random));
            batch.ForEach(evolvable => evolvable.CalculateFitness());

            double survivalFitness = survivalRate < 0 ? -survivalRate *batch.Min(x => x.Fitness) + (1 + survivalRate) * batch.Average(x => x.Fitness) :
                                     survivalRate *batch.Max(x => x.Fitness) + (1 - survivalRate) * batch.Average(x => x.Fitness);

            batch = batch
                    .Where(x => x.Fitness > 0 && random.NextDouble() < failureImmunity ||
                           (x.Fitness >= survivalFitness && random.NextDouble() >= suddenDeath))
                    .ToList();

            if (batch.Count < batchSize)
            {
                WeightedListRandom <T> genePool = new WeightedListRandom <T>(batch, random);

                while (batch.Count < batchSize)
                {
                    List <IEvolvable> parents = genePool.Next(genders, x => x.Fitness, false).Select(x => (IEvolvable)x).ToList();

                    batch.Add((T)parents[0].Crossover(parents.Skip(1), crossovers, random));
                }
            }
        }
Ejemplo n.º 2
0
        public void Generation()
        {
            evolvers.ForEach(x =>
            {
                x.genders         = genders;
                x.crossovers      = crossovers;
                x.mutatability    = mutatability;
                x.survivalRate    = survivalRate;
                x.failureImmunity = failureImmunity;
                x.suddenDeath     = suddenDeath;
            });
            evolvers.ForEach(x => x.Generation());

            if (generation++ % 10 == 0)
            {
                List <T> batch = Batch;
                batch.ForEach(x => x.CalculateFitness());

                double averageFitness  = batch.Average(x => x.Fitness);
                double survivalFitness = evolverSurvivalRate < 0 ? -evolverSurvivalRate *batch.Min(x => x.Fitness) + (1 + evolverSurvivalRate) * averageFitness :
                                         evolverSurvivalRate *batch.Max(x => x.Fitness) + (1 - evolverSurvivalRate) * averageFitness;

                evolvers = evolvers
                           .Where(x => (x.batch.Average(y => y.Fitness) > 0 &&
                                        random.NextDouble() < failureImmunity
                                        ||
                                        (x.batch.Average(y => y.Fitness) >= survivalFitness &&
                                         random.NextDouble() >= suddenDeath)))
                           .ToList();

                if (evolvers.Count < evolverCount)
                {
                    WeightedListRandom <Evolver <T> > genePool = new WeightedListRandom <Evolver <T> >(evolvers, random);

                    while (evolvers.Count < evolverCount)
                    {
                        List <Evolver <T> > parents = genePool.Next(genders, x => x.batch.Average(y => y.Fitness), false).ToList();

                        List <int> crossoverSpots = new List <int>(crossovers);
                        int        batchSize      = parents[0].batchSize;
                        for (int i = 0; i < evolverCrossovers; i++)
                        {
                            int nextSpot;

                            while (crossoverSpots.Contains(nextSpot = random.Next(0, batchSize)))
                            {
                                ;
                            }

                            crossoverSpots.Add(nextSpot);
                        }
                        crossoverSpots.Sort();

                        Evolver <T> evolver       = new Evolver <T>(new List <T>(batchSize), random);
                        int         currentParent = 0;
                        for (int i = 0; i < batchSize; i++)
                        {
                            if (crossoverSpots.Count > 0 && i > crossoverSpots[0])
                            {
                                currentParent = (currentParent + 1) % parents.Count;
                                crossoverSpots.RemoveAt(0);
                            }

                            evolver.batch.Add(parents[currentParent].batch[i]);
                        }
                        evolvers.Add(evolver);
                    }
                }
            }
        }