Example #1
0
 public void DrawTour(TSPStats stats)
 {
     lock (Lock) {
         Graphics g = this.GetGraphics();
         this.Clear(g);
         TSPTour   Tour = (TSPTour)stats.solution;
         TSPCity[] cities = Tour.GetCities();
         TSPCity   from, to;
         int[]     tour = Tour.GetTour();
         // 1. draw tour
         for (int i = 0; i < tour.Length; i++)
         {
             from = cities[tour[i]];
             to   = cities[tour[(i + 1) % tour.Length]];
             this.DrawPath(g, from, to);
         }
         // 2. draw cities
         foreach (TSPCity city in cities)
         {
             this.DrawCity(g, city);
         }
         // 3. draw stats
         this.DrawStats(g, stats);
         g.Flush();
         g.Dispose();
     }
     // refresh display
     this.form.Invoke(new Action(() => {
         lock (Lock) {
             this.pb.Image = this.bitmap;
         }
     }));
 }
Example #2
0
        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
            }
        }
        public IAnnealingSolution Neighbor()
        {
            Random  rand  = new Random();
            TSPTour other = new TSPTour(this.cities, this.tour);

            int x, y;

            do
            {
                x = rand.Next(this.tour.Length);
                y = rand.Next(this.tour.Length);
            } while (x == y);
            other.tour[x] = this.tour[y];
            other.tour[y] = this.tour[x];
            return(other);
        }
Example #4
0
        private void DrawStats(Graphics g, TSPStats stats)
        {
            TSPTour Tour = (TSPTour)stats.solution;
            string  text = "";

            text += String.Format("Score: {0}\n", Tour.Score());
            text += String.Format("Iterations: {0}\n", stats.iterations);
            text += String.Format("Cities: {0}\n", Tour.GetCities().Length);
            if (stats.working)
            {
                text += String.Format("Heat: {0}\n", stats.heat);
                text += "Searching...";
            }
            RectangleF pos = new RectangleF(2, 2, this.bitmap.Width - 2, this.bitmap.Height - 2);

            g.DrawString(text, TEXT_FONT, TEXT_BRUSH, pos);
        }