コード例 #1
0
        public override Genome Start()
        {
            int    i        = 0;
            Genome lastBest = new Genome(null);

            RandomPopulation(Functions.ParamSize);
            Console.Write(i + " iteration. Current best: ");
            Program.PrintParameters(BestGenome.Genes);
            Console.WriteLine("with fitness: " + BestGenome.Fitness.ToString("G10"));
            while (BestGenome.Fitness > _data.MinError && ++i < _data.MaxIterations)
            {
                lastBest.Copy(BestGenome);
                if (_data.Elitism)
                {
                    _tempPopulation[0].Copy(BestGenome);
                }
                double unused = CalculateFitness();
                Parallel.For(_data.Elitism ? 1 : 0, _data.PopulationSize, SingleThread);
                SwapBuffers();
                DeterminePopulationFitness();
                if (!(BestGenome.Fitness < lastBest.Fitness))
                {
                    continue;
                }
                Console.Write(i + " iteration. Current best: ");
                Program.PrintParameters(BestGenome.Genes);
                Console.WriteLine("with fitness: " + BestGenome.Fitness.ToString("G10"));
            }
            return(BestGenome);
        }
コード例 #2
0
        public override Genome Start()
        {
            int    i           = 0;
            int    howManyDies = (int)(_data.Mortality * _data.PopulationSize);
            Genome lastBest    = new Genome(null);

            RandomPopulation(Functions.ParamSize);
            Console.Write(i + " iteration. Current best: ");
            Program.PrintParameters(BestGenome.Genes);
            Console.WriteLine("with fitness: " + BestGenome.Fitness.ToString("G10"));
            while (BestGenome.Fitness > _data.MinError && ++i < _data.MaxIterations)
            {
                lastBest.Copy(BestGenome);
                Parallel.For(0, howManyDies, ThreeTournament);                  // Mortality determines how many times we should do the Tournaments
                DetermineBestFitness();
                if (!(BestGenome.Fitness < lastBest.Fitness))
                {
                    continue;
                }
                Console.Write(i + " iteration. Current best: ");
                Program.PrintParameters(BestGenome.Genes);
                Console.WriteLine("with fitness: " + BestGenome.Fitness.ToString("G10"));
            }
            return(BestGenome);
        }
コード例 #3
0
ファイル: GA.cs プロジェクト: coldwarrior5/Genetic-algorithm
        public static void SingleArithmeticRecombination(Genome firstParent, Genome secondParent, ref Genome firstChild, Random rand)
        {
            int location = rand.Next(0, firstChild.Genes.Length);

            firstChild.Copy(firstParent);
            firstChild.Fitness         = Single.MaxValue;
            firstChild.Genes[location] = Average(firstParent.Genes[location], secondParent.Genes[location]);
        }
コード例 #4
0
ファイル: GA.cs プロジェクト: coldwarrior5/Genetic-algorithm
        protected void DeterminePopulationFitness()
        {
            object syncObject = new object();

            Parallel.ForEach(Population, () => new Genome(new float[] {}, float.MaxValue), (genome, loopState, localState) =>
            {
                DetermineGenomeFitness(ref genome);
                return(genome.Fitness < localState.Fitness ? genome : localState);
            },
                             localState =>
            {
                lock (syncObject)
                {
                    if (localState.Fitness < BestGenome.Fitness)
                    {
                        BestGenome.Copy(localState);
                    }
                }
            });
        }
コード例 #5
0
        /// <summary>
        /// Breed the population in the world.
        /// </summary>
        /// <param name="world">The world the population lives in.</param>
        /// <param name="population">The population we want to breed.</param>
        public static void Breed(SimulationWorld world, Population population)
        {
            AIEntity[] entitys = population.GetEntitys();

            //Bread the top half section to the lower half.
            int topAmount = (int)(population.GetAmount() * 0.5f);

            for (int i = 0; i < topAmount; i += 2)
            {
                Genome a = entitys[i].GetGenome();
                Genome b = entitys[i + 1].GetGenome();
                BreedGenome(world, a, b, i, entitys, population.GetAmount());
                entitys[i]     = new AIEntity(world, a.Copy());
                entitys[i + 1] = new AIEntity(world, b.Copy());
            }
        }