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); }
// METHODS public Individual GrowChildren(City[] Cities) { Individual children = this.MemberwiseClone() as Individual; children.tour = this.tour.Clone() as int[]; Transform(children); children.cost = TourModel.Cost(children.tour, Cities); return(children); }
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; }
private void DrawTour(int[] Tour, Pen TourPen, Pen HomeWay) { Point[] way = TourModel.GetWay(Tour, Cities); graphic.DrawLines(TourPen, way); graphic.DrawLine(HomeWay, way[0], way[way.Length - 1]);// sweet home }
// DRAWING private void DrawPartTour(int[] Tour, Pen TourPen) { Point[] way = TourModel.GetWay(Tour, Cities); graphic.DrawLines(TourPen, way); }