コード例 #1
0
ファイル: Program.cs プロジェクト: turbana/TravelingSalesman
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            TSPImage image  = new TSPImage(IMAGE_WIDTH, IMAGE_HEIGHT);
            Thread   thread = new Thread(() => Solve(image));

            thread.Start();
            image.Display();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: turbana/TravelingSalesman
        static void Solve(TSPImage image)
        {
            TSPCity[] cities  = TSPTour.RandomCities(CITIES_COUNT, IMAGE_WIDTH, IMAGE_HEIGHT);
            TSPTour   initial = TSPTour.RandomTour(cities);
            TSPStats  results;

            try {
                results = SimulatedAnnealing.Solve(ANNEALING_PARAMETERS, initial, image);
                image.DrawTour(results);
            } catch (ObjectDisposedException e) {
                // ignore window closed
            }
        }
コード例 #3
0
        public static TSPStats Solve(AnnealingParameters parms, IAnnealingSolution initial, TSPImage image)
        {
            IAnnealingSolution best, state, neighbor;

            best  = initial;
            state = initial;
            Random rand       = new Random();
            double heat       = parms.Heat;
            int    iterations = 0;

            while (heat > parms.MinHeat)
            {
                for (int i = 0; i < parms.Settle; i++)
                {
                    iterations++;
                    if ((iterations % 10000) == 0)
                    {
                        Console.WriteLine("{0,4}: {1}", best.Score(), heat);
                        image.DrawTour(new TSPStats {
                            solution   = best,
                            iterations = iterations,
                            working    = true,
                            heat       = heat,
                        });
                    }
                    neighbor = state.Neighbor();
                    if (neighbor.Score() < state.Score())
                    {
                        state = neighbor;
                    }
                    else if (rand.NextDouble() < Math.Exp((state.Score() - neighbor.Score()) / heat))
                    {
                        state = neighbor;
                    }

                    if (state.Score() < best.Score())
                    {
                        best = state;
                    }
                }
                heat *= parms.Alpha;
            }

            return(new TSPStats {
                solution = best,
                iterations = iterations,
                working = false,
                heat = heat,
            });
        }