コード例 #1
0
        private void Optimize_Load(object sender, EventArgs e)
        {
            var selection  = new EliteSelection();
            var crossover  = new OrderedCrossover();
            var mutation   = new ReverseSequenceMutation();
            var fitness    = new TrajectoryFitness();
            var chromosome = new TrajectoryChromosome(TrajectoryChromosome.ChromosomeLength(3), 3);
            var population = new Population(35, 40, chromosome);
            //72 (6)
            //67 (2)
            //49 (3)
            //102 (1)
            var ga       = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            var executor = new SmartThreadPoolTaskExecutor();

            executor.MinThreads = 1;
            executor.MaxThreads = 1;
            ga.TaskExecutor     = executor;
            ga.Termination      = new GenerationNumberTermination(10);

            Console.WriteLine("GA running...");
            var t = DateTime.Now;

            ga.Start();
            Console.WriteLine("Time:" + DateTime.Now.Subtract(t).TotalSeconds);

            Console.WriteLine("Best solution found has {0} fitness.", int.MaxValue - ga.BestChromosome.Fitness);
            Console.WriteLine(ga.BestChromosome.GetGene(0).Value + " " + ga.BestChromosome.GetGene(1).Value + " " + ga.BestChromosome.GetGene(2).Value);
        }
コード例 #2
0
        protected void RunGeneticAlgorithm()
        {
            try
            {
                var referencePoint = new ReferencePoint(new GeoPoint3D(_view.StartLongitude, _view.StartLatitude), new Point3D(0, 0));
                TrajectoryFitness.ReferencePoint = referencePoint;
                TrajectoryFitness.GeoEndPoint    = new GeoPoint3D(_view.EndLongitude, _view.EndLatitude);
                TrajectoryFitness.OptimiseFuel   = !_view.MinimiseNoise;
                TrajectoryFitness.TakeoffHeading = _view.TakeoffHeading;
                TrajectoryFitness.TakeoffSpeed   = _view.TakeoffSpeed;
                TrajectoryFitness.trajectories   = null;
                TrajectoryFitness.Best           = int.MinValue;
                TrajectoryFitness.PopulationData = new PopulationData2("population.dat", referencePoint);

                var selection  = new EliteSelection();
                var crossover  = new OrderedCrossover();
                var mutation   = new ReverseSequenceMutation();
                var fitness    = new TrajectoryFitness();
                var chromosome = new TrajectoryChromosome(TrajectoryChromosome.ChromosomeLength(_view.NumberOfSegments), _view.NumberOfSegments);
                var population = new Population(_view.PopulationSize, _view.PopulationSize, chromosome);
                ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
                var executor = new SmartThreadPoolTaskExecutor();
                executor.MinThreads = 1;
                executor.MaxThreads = 1;
                ga.TaskExecutor     = executor;
                ga.Termination      = new GenerationNumberTermination(_view.NumberOfGenerations);

                startTime = DateTime.Now;

                ga.TerminationReached += OptimisationCompleted;
                ga.GenerationRan      += UpdatePercentage;
                ga.Start();
            }
            catch (ThreadAbortException)
            {
                // ignore it
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }