public PathChosen SolveTrad()
        {
            if (Cities.Count == 0)
            {
                return(null);
            }

            PathChosen path = NonGASolution.NearestNeighbor(Cities);

            return(path);
        }
        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);
        }
        private static void DrawPath(Image canvas, GATravellingSalesmanContoller GA, PathChosen path, bool optimised)
        {
            City firstCity = GA.Cities[0];

            PathBuilder pathBuilder = new PathBuilder();

            var points = new List <PointF>();

            points.Add(new PointF(firstCity.X, firstCity.Y));
            for (int i = 0; i < path.CityIndexes.Count; i++)
            {
                City city = GA.Cities[path.CityIndexes[i]];
                var  p    = new PointF(city.X, city.Y);
                points.Add(p);
            }

            pathBuilder.AddLines(points);
            pathBuilder.CloseFigure();
            IPath spath = pathBuilder.Build();

            canvas.Mutate(ctx => ctx
                          .Draw(optimised ? Color.Blue : Color.Gray, 3, spath));


            var font = SystemFonts.CreateFont("Arial", 39, FontStyle.Regular);
            var textGraphicsOptions = new TextGraphicsOptions(true);

            for (int i = 0; i < path.CityIndexes.Count; i++)
            {
                City city   = GA.Cities[path.CityIndexes[i]];
                var  p      = new PointF(city.X + (optimised ? -1 : 1) * City.ClickRadius * 4, city.Y);
                var  glyphs = TextBuilder.GenerateGlyphs((i + 1).ToString(), p, new RendererOptions(font, textGraphicsOptions.DpiX, textGraphicsOptions.DpiY));
                canvas.Mutate(ctx => ctx
                              .Fill((GraphicsOptions)textGraphicsOptions, optimised ? Color.Blue : Color.Gray, glyphs));
            }
        }
Example #4
0
 public PathChosen(PathChosen pathChosen)
 {
 }