Exemplo n.º 1
0
        private void RunHillClimbingAlgorithmAsync()
        {
            HillClimbing evr = new HillClimbing(Cities: Cities, StartedCity: StartCityIndex);

            evr.lookAtCity  += WritePossibleCity;
            evr.ChooseACity += WriteCity;

            SalesmanTour HillClimbing = new SalesmanTour(evr.Run);
            int          MutationRate = MutationTrackBar.Value;


            HillClimbing.BeginInvoke(AsyncResult =>
            {
                int[] tour  = HillClimbing.EndInvoke(AsyncResult);
                double Cost = TourModel.Cost(tour, Cities);
                DrawMap();
                DrawTour(tour, Cost);
                salesmen.Insert(new Population.Individual(tour, Cost, MutationRate));
                Invoke((Action) delegate
                {
                    UnlockInterface(true);
                    DistanceToolStripStatusLabel.Text = String.Empty;
                });
            }, null);
        }
Exemplo n.º 2
0
        private async void RunHillClimbingAlgorithmIterAsync()
        {
            await Task.Factory.StartNew((object MutationRate) =>
            {
                HillClimbing evr = new HillClimbing(Cities: Cities, StartedCity: StartCityIndex);

                foreach (HillClimbingArgs evrRes in evr.RunIter())
                {
                    if (evr.Tour.Length > 1) // tour can only be if there are two and more cities
                    {
                        DrawPartTour(evr.Tour, ChoosenCityBlackPen);
                    }
                    if (evrRes.IsChosen)
                    {
                        graphic.DrawLine(ChoosenCityBlackPen, Cities[evrRes.CurrentCityIndex].Coordinate, Cities[evrRes.NextCityIndex].Coordinate);

                        Invoke((Action) delegate { DistanceToolStripStatusLabel.Text = "shortest distance = " + evrRes.Distance.ToString(DoubleNumberFormat); });

                        Thread.Sleep(delay);
                    }
                    else
                    {
                        graphic.DrawLine(PossibleCityBluePen, Cities[evrRes.CurrentCityIndex].Coordinate, Cities[evrRes.NextCityIndex].Coordinate);
                        Invoke((Action) delegate { DistanceToolStripStatusLabel.Text = "distance = " + evrRes.Distance.ToString(DoubleNumberFormat); });

                        Thread.Sleep(delay / 10);
                    }

                    if (!run)
                    {
                        return;
                    }
                    // Clean up
                    DrawMap();
                }
                int[] tour = evr.result;

                double Cost = TourModel.Cost(tour, Cities);
                DrawMap();
                DrawTour(tour, Cost);
                salesmen.Insert(new Population.Individual(tour, Cost, (int)MutationRate));
            }, state : MutationTrackBar.Value, creationOptions : TaskCreationOptions.LongRunning);


            UnlockInterface(true);
            DistanceToolStripStatusLabel.Text = String.Empty;
        }