private PathChosen AsyncGACall(int numIterations, int populationSize, int crossoverPercentage, double mutationPercentage)
        {
            PathChosen bestPath       = null;
            int        shortestLength = int.MaxValue;
            int        totalLength    = 0;
            DateTime   timeStart      = DateTime.Now;

            for (int i = 0; i < numIterations; i++)
            {
                //Dispatcher.BeginInvoke(new Action(() =>
                //{
                //    gaResultTB.Text = "Calculating #" + (i + 1) + "...";
                //}),
                //DispatcherPriority.Background);

                // do this long-running process in a non-UI thread
                int elitism     = 10;
                int tourneySize = 2;
                var path        = new GeneticAlgorithmEngine(populationSize, crossoverPercentage,
                                                             mutationPercentage, elitism, tourneySize).FindOptimalPath(Cities);
                int pathLength = path.GetTotalLength();
                totalLength += pathLength;

                if (pathLength < shortestLength)
                {
                    shortestLength = pathLength;
                    bestPath       = new PathChosen(path); // copy constructor
                }
            }

            var elapsed = DateTime.Now.Subtract(timeStart).TotalSeconds;

            Debug.WriteLine(numIterations + " iterations took " + elapsed + " seconds ");
            bestPath.AvLength = (int)(totalLength / numIterations);
            //// tell the UI thread to update the results, and display the best one
            //Dispatcher.BeginInvoke(new Action(() =>
            //{
            //    if (gaDrawnPath != null)
            //        canvas.Children.Remove(gaDrawnPath);

            //    gaDrawnPath = bestPath.Draw(canvas, Brushes.Green);
            //    gaResultTB.Text = "Avg. path length: " + (int)(totalLength / numIterations) + " \nBest path length: " + shortestLength;
            //}),
            //    DispatcherPriority.Background);
            return(bestPath);
        }
示例#2
0
文件: Form1.cs 项目: ptadamski/smutno
        public void algorithm_loop()
        {
            this.output.Focus();
            this.output.AppendText("START\n");

            do
            {
                var eval_list = population.Select(x => evalSelector.Evaluate(x));
                var avg = eval_list.Average();
                var max = eval_list.Max();

                series.Points.AddXY(it, avg);
                series2.Points.AddXY(it, max);

                this.output.AppendText(String.Format(" iteracja nr {0}  avg:{1:0.000}  max:{2:0.000}\n", it++, avg, max));

                //wydzielenie podzbiorow
                var planyGrup = new Dictionary<Grupa, IList<Timetable>>();

                foreach (var osobnik in population)
                {
                    var lista_grup = osobnik.Gens.Select(x => x.Grupa).Distinct();
                    foreach (var grupa in lista_grup)
                    {
                        Application.DoEvents();

                        var planGrupy = new Timetable(mutation);

                        IList<TimetableLocus> loci =
                            (from locus in osobnik.Loci
                             where osobnik[locus].Grupa.Equals(grupa)
                             select locus).ToList();

                        foreach (var locus in loci)
                            planGrupy[locus] = osobnik[locus];

                        IList<Timetable> lista;
                        if (!planyGrup.TryGetValue(grupa, out lista))
                        {
                            lista = new List<Timetable>();
                            planyGrup.Add(grupa, lista);
                        }

                        lista.Add(planGrupy);
                    }
                }

                var listaGrup = planyGrup.Keys.ToList();

                for (int i = 0; i < listaGrup.Count; i++)
                {
                    Application.DoEvents();

                    IGeneticAlgorithmEngine<Timetable> ga_grupy;
                    ga_grupy = new GeneticAlgorithmEngine<Timetable, TimetableLocus, Zajecia>(planyGrup[listaGrup[i]], selector, reproducer, 20);
                    ga_grupy.Iterate(() =>
                    {
                        //planGrupy.Value = ga_grupy.Population;
                        return ga_grupy.Population.Select(x => evalSelector.Evaluate(x)).Average() > 0.95;
                    });
                    planyGrup[listaGrup[i]] = ga_grupy.Population;
                }

                //polaczenie podzbiorow
                var nowaPopulacja = new List<Timetable>();
                for (int i = 0; i < population.Count; i++)
                {
                    Application.DoEvents();

                    var osobnik = new Timetable(mutation);
                    foreach (var planGrupy in planyGrup)
                    {
                        osobnik.Concat(planGrupy.Value[i]);
                    }
                }
            } while ((population.Select(x => evalSelector.Evaluate(x)).Average() < 0.95) && !stop_button);

            if (!stop_button)
            {
                //wypisanie wyniku

                this.output.AppendText("ZAKOŃCZONO\n");
                stop_Click(null, null);
                stop_button = false;
            }
            else
            {
                try
                {
                    output.AppendText("STOP\n");
                }
                catch (Exception){}
            }
        }
示例#3
0
        public void SolveWithGenetics()
        {
            var genetics = new GeneticAlgorithmEngine <ItemList, Knapsack>(100, 10, 10, new SolutionGenerator(), Randomizer);

            ResultContents = genetics.Run(this);
        }