Ejemplo n.º 1
0
        public EvoResults Evolve(IEnumerable <double[]> initialPopulation)
        {
            EvoResults results = new EvoResults()
            {
                Generations = new GenerationResults[Generations]
            };

            Individual[] pop = new Individual[Parents + Children];
            for (int i = 0; i < initialPopulation.Count(); i++)
            {
                pop[i] = new Individual()
                {
                    Fitness = 0, Genome = initialPopulation.ElementAt(i)
                }
            }
            ;
            for (int i = initialPopulation.Count(); i < pop.Length; i++)
            {
                pop[i] = new Individual()
                {
                    Fitness = 0, Genome = createChild(pop[i % initialPopulation.Count()].Genome)
                }
            }
            ;

            for (int curGen = 0; curGen < Generations; curGen++)
            {
                Console.WriteLine("Generation #{0}", curGen);
                FitnessFunction.Evaluate(pop);

                Array.Sort(pop, delegate(Individual ind1, Individual ind2)
                {
                    int compare = ind2.Fitness.CompareTo(ind1.Fitness);
                    return(compare);
                });
                if (pop[0].Fitness < pop[1].Fitness)
                {
                    throw new Exception("Sorted the wrong way!");
                }

                GenerationResults gen = new GenerationResults()
                {
                    Champion = new double[pop[0].Genome.Length],
                    Fitness  = pop[0].Fitness
                };
                pop[0].Genome.CopyTo(gen.Champion, 0);
                results.Generations[curGen] = gen;

                for (int i = Parents; i < pop.Length; i++)
                {
                    createChild(pop[i % Parents].Genome, pop[i].Genome);
                }
            }

            return(results);
        }
Ejemplo n.º 2
0
        public EvoResults Evolve(IEnumerable<double[]> initialPopulation)
        {
            EvoResults results = new EvoResults() { Generations = new GenerationResults[Generations] };
            Individual[] pop = new Individual[Parents + Children];
            for (int i = 0; i < initialPopulation.Count(); i++)
                pop[i] = new Individual() { Fitness = 0, Genome = initialPopulation.ElementAt(i) };
            for (int i = initialPopulation.Count(); i < pop.Length; i++)
                pop[i] = new Individual() { Fitness = 0, Genome = createChild(pop[i%initialPopulation.Count()].Genome)};

            for (int curGen = 0; curGen < Generations; curGen++)
            {
                Console.WriteLine("Generation #{0}", curGen);
                FitnessFunction.Evaluate(pop);

                Array.Sort(pop, delegate(Individual ind1, Individual ind2)
                {
                    int compare = ind2.Fitness.CompareTo(ind1.Fitness);
                    return compare;
                });
                if (pop[0].Fitness < pop[1].Fitness)
                    throw new Exception("Sorted the wrong way!");

                GenerationResults gen = new GenerationResults()
                {
                    Champion = new double[pop[0].Genome.Length],
                    Fitness = pop[0].Fitness
                };
                pop[0].Genome.CopyTo(gen.Champion, 0);
                results.Generations[curGen] = gen;

                for (int i = Parents; i < pop.Length; i++)
                    createChild(pop[i % Parents].Genome, pop[i].Genome);
            }

            return results;
        }