Exemple #1
0
        // Functionality
        public static Population randomized(Tour t, int n)
        {
            List<Tour> tmp = new List<Tour>();

            for (int i = 0; i < n; ++i)
                tmp.Add( t.shuffle() );

            return new Population(tmp);
        }
Exemple #2
0
Fichier : Tour.cs Projet : 7lb/TSP
 public Tour crossover(Tour m)
 {
     int i = Program.r.Next(0, m.t.Count);
     int j = Program.r.Next(i, m.t.Count);
     List<City> s = this.t.GetRange(i, j - i + 1);
     List<City> ms = m.t.Except(s).ToList();
     List<City> c = ms.Take(i)
                      .Concat(s)
                      .Concat( ms.Skip(i) )
                      .ToList();
     return new Tour(c);
 }
Exemple #3
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();

            if (btnStart.Text.Equals("Start"))
            {
                btnStart.Text             = "Stop";
                gaThreadCancellationToken = new CancellationTokenSource();
                populationSize            = Convert.ToInt32(numPop.Value);
                iterations           = Convert.ToInt32(numGeneration.Value);
                MUTATION_PROBABILITY = Convert.ToInt32(numMutate.Value);
                int duplicatesTimer = 0;
                stopwatch.Start();
                Task tMain = Task.Run(() =>
                {
                    globalTour.Clear();
                    tourPopulation.Clear();
                    globalTourLength = Double.MaxValue;
                    Populate();
                    CalculateCostAllToursInPolulation();

                    tourPopulation.Sort();
                    for (int k = 0; k < iterations; k++)
                    {
                        duplicatesTimer++;
                        if (gaThreadCancellationToken.IsCancellationRequested)
                        {
                            break;
                        }
                        SimulateGeneration();
                        CalculateCostAllToursInPolulation();

                        tourPopulation.Sort();
                        if (globalTourLength > tourPopulation[0].GetDistance())
                        {
                            Tour Changed     = findReplacement(tourPopulation[0], globalTour);
                            globalTour       = tourPopulation[0];
                            globalTourLength = tourPopulation[0].GetDistance();
                            DrawTour(Changed);
                            drawnTourLengthLabel.BeginInvoke(new MethodInvoker(() =>
                            {
                                drawnTourLengthLabel.Text = "Drawn tour length: " + globalTourLength;
                            }));
                            labelLastSolution.BeginInvoke(new MethodInvoker(() =>
                            {
                                labelLastSolution.Text = "Best solution found @: " + k;
                            }));
                        }
                        iterationLabel.BeginInvoke(new MethodInvoker(() =>
                        {
                            iterationLabel.Text = "Iteration: " + k;
                        }));
                        if (duplicatesTimer > duplicatesRemovalInterval)
                        {
                            duplicatesTimer = 0;
                            Tour Prev       = tourPopulation[0];
                            Tour Next;
                            for (int i = 1; i < tourPopulation.Count; i++)
                            {
                                Next = tourPopulation[i];
                                if (Prev.GetDistance() == Next.GetDistance())
                                {
                                    if (Prev.Hash() == Next.Hash())
                                    {
                                        Mutate(tourPopulation[i]);
                                    }
                                    else
                                    {
                                        Prev = Next;
                                    }
                                }
                                else
                                {
                                    Prev = Next;
                                }
                            }
                            CalculateCostAllToursInPolulation();

                            tourPopulation.Sort();
                        }
                    }
                    btnStart.BeginInvoke(new MethodInvoker(() =>
                    {
                        btnStart.Text = "Start";
                    }));
                }, gaThreadCancellationToken.Token);
            }
            else
            {
                gaThreadCancellationToken.Cancel();
            }
        }