public void Run()
        {
            var selection  = new EliteSelection();
            var crossover  = new TwoPointCrossover();
            var mutation   = new MyMutation();
            var fitness    = new MyProblemFitness(pointList.ToArray());
            var chromosome = new MyProblemChromosome(pointList.Count);
            var population = new Population(100, 100, chromosome);

            ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                Termination          = new FitnessStagnationTermination(100),
                MutationProbability  = 0.5f, //KS:变异概率50%
                CrossoverProbability = 0.5f, //KS:交配概率50%
            };

            int index = 0; //KS:计算代数

            ga.GenerationRan += delegate
            {
                var bestChromosome = ga.Population.BestChromosome;

                Console.Write("Index: " + index);
                Console.Write(", Fitness: {0}", bestChromosome.Fitness);

                Console.Write(", Genes: {0}", string.Join("-", bestChromosome.GetGenes()));

                Console.WriteLine();

                index++;
            };



            Console.WriteLine("GA running...");
            Stopwatch SW = new Stopwatch();

            SW.Start();

            ga.Start();//调用GA线程


            SW.Stop();
            //Console.Write(", Time: {0}", SW.ElapsedMilliseconds);
            //Console.WriteLine("Best solution found has {0} fitness.", ga.BestChromosome.Fitness);
            fitness.Evaluate(ga.BestChromosome); //KS: 评价基因的优劣


            startPoint = new Point(0, (double)fitness.lr.Alpha);
            endPoint   = new Point(screenWidth, (double)(fitness.lr.Alpha + screenWidth * fitness.lr.Beta));
        }
Esempio n. 2
0
        public void Run(ref double Minfitness, ref Point startPoint, ref Point endPoint)
        {
            if (linePointArr.Length == 0)
            {
                return;
            }
            else
            {
                var selection = new EliteSelection();
                var crossover = new TwoPointCrossover();
                var mutation  = new MyMutation();
                fitness = new MyProblemFitness2(linePointArr, startPoint, endPoint);
                var chromosome = new MyProblemChromosome2(linePointArr.Length < 3 ? 3 : linePointArr.Length);
                var population = new Population(linePointArr.Length + 3, linePointArr.Length + 3, chromosome);

                ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
                {
                    Termination          = new TimeEvolvingTermination(TimeSpan.FromMilliseconds(100)),
                    MutationProbability  = 2f,   //KS:变异概率50%
                    CrossoverProbability = 0.5f, //KS:交配概率50%
                };

                int index = 0; //KS:计算代数

                ga.GenerationRan += delegate
                {
                    var bestChromosome = ga.Population.BestChromosome;

                    /*                Console.Write("Index: " + index);
                     *              Console.Write(", Fitness: {0}", bestChromosome.Fitness);
                     *
                     *              Console.Write(", Genes: {0}", string.Join("-", bestChromosome.GetGenes()));
                     *
                     *              Console.WriteLine();*/

                    index++;
                };

                /*            Console.WriteLine("GA running...");
                 *          Stopwatch SW = new Stopwatch();
                 *          SW.Start();
                 */
                ga.Start();//调用GA线程


                // SW.Stop();
                //Console.Write(", Time: {0}", SW.ElapsedMilliseconds);
                Console.WriteLine("Best solution found has {0} fitness : {1}.", string.Join("-", ga.Population.BestChromosome.GetGenes()), ga.Population.BestChromosome.Fitness);
            }

            fitness.Evaluate(ga.Population.BestChromosome); //KS: 评价基因的优劣
            Minfitness = ga.Population.BestChromosome.Fitness.Value;
            startPoint = fitness.start.ToPoint();
            endPoint   = fitness.end.ToPoint();

            /*
             * if (ga.Population.BestChromosome.Fitness > Minfitness)
             * {
             *  Minfitness = ga.Population.BestChromosome.Fitness.Value;
             *  startPoint = fitness.start.ToPoint();
             *  endPoint = fitness.end.ToPoint();
             * }
             * else
             * {
             *  Minfitness = math.lerp((float)Minfitness, (float)ga.Population.BestChromosome.Fitness, 0.5f);
             * }
             */
        }