Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/12/12 18:37:27 (12 years ago)
Author:
epitzer
Message:

#1696: Tweak appearance of VRP visualization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/FitnessLandscapeAnalysis/VRPProblemAnalyzer/PictureGenerator.cs

    r7316 r7321  
    99namespace VRPProblemAnalyzer {
    1010  public class PictureGenerator {
    11     private static int TourSize = 1000;
     11
     12    private static int TourWidth = 500;
     13    private static int TourHeight = 500;
    1214    private static double MinDemandSize = 2;
    13     private static double MaxDemandSize = TourSize / 25.0;
     15    private static double MaxDemandSize = Math.Min(TourWidth, TourHeight) / 25.0;
    1416
    1517    private static int TourBorder = (int)Math.Ceiling(MaxDemandSize);
    1618
    17     private static int ChartWidth = TourSize / 2;
    18     private static int ChartHeight = 250;
     19    private static int ChartWidth = 400;
     20    private static int ChartHeight = TourHeight/2;
    1921   
    20     private static int DemandSegments = 10;
    21 
    22     private static int DistanceSegments = 10;
     22    private static int DemandSegments = 20;
     23
     24    private static int DistanceSegments = 20;
    2325
    2426    public static Image GeneratePicture(TSPLIBParser problemInstance, SolutionParser solution) {   
     
    2729      Image distanceDistribution = GenerateDistanceDistribution(problemInstance);
    2830
    29       Bitmap bmp = new Bitmap(Math.Max(TourSize, ChartWidth * 2), tourVisualization.Height + ChartHeight);
    30       using (Graphics g = Graphics.FromImage(bmp)) {
    31         g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
    32         g.DrawImage(tourVisualization, 0, 0);
    33         g.DrawImage(demandDistribution, 0, tourVisualization.Height);
    34         g.DrawImage(distanceDistribution, ChartWidth, tourVisualization.Height);
    35       }
    36       return bmp;
    37     }
     31      SizeF titleSize;
     32      using (var font = new Font("Helvetica", 20)) {
     33        using (var b = new Bitmap(1, 1)) {
     34          using (Graphics g = Graphics.FromImage(b)) {
     35            titleSize = g.MeasureString(problemInstance.Name, font);
     36          }
     37        }
     38        Bitmap bmp = new Bitmap(TourWidth+ChartWidth,
     39                                Math.Max(TourHeight,  2*ChartHeight) + (int)titleSize.Height);
     40        using (Graphics g = Graphics.FromImage(bmp)) {
     41          g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
     42          g.DrawString(problemInstance.Name, font, Brushes.Black, (bmp.Width-titleSize.Width)/2, 0);
     43          g.DrawImage(tourVisualization, (TourWidth-tourVisualization.Width)/2, titleSize.Height+(TourHeight-tourVisualization.Height)/2);
     44          g.DrawImage(demandDistribution, TourWidth, titleSize.Height);
     45          g.DrawImage(distanceDistribution, TourWidth, ChartHeight + titleSize.Height);
     46        }
     47        return bmp;
     48      }
     49    }
     50
     51    private static Color[] TourColors = new[] {
     52      Color.FromArgb(100, 92, 20,237), Color.FromArgb(100,237,183, 20), Color.FromArgb(100,237, 20,219), Color.FromArgb(100, 20,237, 76),
     53      Color.FromArgb(100,237, 61, 20), Color.FromArgb(100,115, 78, 26), Color.FromArgb(100, 20,237,229), Color.FromArgb(100, 39,101, 19),
     54      Color.FromArgb(100,230,170,229), Color.FromArgb(100,142,136, 89), Color.FromArgb(100,157,217,166), Color.FromArgb(100, 31, 19,101),
     55      Color.FromArgb(100,173,237, 20), Color.FromArgb(100,230,231,161), Color.FromArgb(100,142, 89, 89), Color.FromArgb(100, 93, 89,142),
     56      Color.FromArgb(100,146,203,217), Color.FromArgb(100,101, 19, 75), Color.FromArgb(100,198, 20,237), Color.FromArgb(100,185,185,185),
     57      Color.FromArgb(100,179, 32, 32), Color.FromArgb(100, 18,119,115), Color.FromArgb(100,104,158,239), Color.Black};
    3858
    3959    private static Image GenerateTourVisualization(TSPLIBParser problemInstance, SolutionParser solution) {
     
    4767        if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
    4868      }
    49       double xWidth = xMax - xMin;
    50       double yHeight = yMax - yMin;
    51 
    52       int width, height;
    53       if (xWidth > yHeight) {
    54         width = TourSize;
    55         height = (int)Math.Round(TourSize * (yHeight / xWidth));
    56       } else {
    57         width = (int)Math.Round(TourSize * (xWidth / yHeight));
    58         height = TourSize;
    59       }
    60       Bitmap bitmap = new Bitmap(width, height);
    61 
    62       double xStep = (width - 2 * TourBorder) / xWidth;
    63       double yStep = (height - 2 * TourBorder) / yHeight;
    64 
    65       Pen[] pens = {new Pen(Color.FromArgb(100, 92,20,237)), new Pen(Color.FromArgb(100, 237,183,20)), new Pen(Color.FromArgb(100, 237,20,219)), new Pen(Color.FromArgb(100, 20,237,76)),
    66                     new Pen(Color.FromArgb(100, 237,61,20)), new Pen(Color.FromArgb(100, 115,78,26)), new Pen(Color.FromArgb(100, 20,237,229)), new Pen(Color.FromArgb(100, 39,101,19)),
    67                     new Pen(Color.FromArgb(100, 230,170,229)), new Pen(Color.FromArgb(100, 142,136,89)), new Pen(Color.FromArgb(100, 157,217,166)), new Pen(Color.FromArgb(100, 31,19,101)),
    68                     new Pen(Color.FromArgb(100, 173,237,20)), new Pen(Color.FromArgb(100, 230,231,161)), new Pen(Color.FromArgb(100, 142,89,89)), new Pen(Color.FromArgb(100, 93,89,142)),
    69                     new Pen(Color.FromArgb(100, 146,203,217)), new Pen(Color.FromArgb(100, 101,19,75)), new Pen(Color.FromArgb(100, 198,20,237)), new Pen(Color.FromArgb(100, 185,185,185)),
    70                     new Pen(Color.FromArgb(100, 179,32,32)), new Pen(Color.FromArgb(100, 18,119,115)), new Pen(Color.FromArgb(100, 104,158,239)), new Pen(Color.Black)};
    71 
    72       foreach (Pen pen in pens) {
    73         pen.Width = 2;
    74       }
    75 
    76       using (Graphics graphics = Graphics.FromImage(bitmap)) {
    77         graphics.FillRectangle(Brushes.White, 0, 0, width, height);
    78 
    79         int currentTour = 0;
    80         foreach (List<int> tour in solution.Routes) {
    81           Point[] tourPoints = new Point[tour.Count + 2];
    82           int[] customerSizes = new int[tour.Count];
    83           int lastCustomer = 0;
    84 
    85           for (int i = -1; i <= tour.Count; i++) {
    86             int location = 0;
    87 
    88             if (i == -1 || i == tour.Count)
    89               location = 0; //depot
    90             else
    91               location = tour[i];
    92 
    93             Point locationPoint = new Point(TourBorder + ((int)((coordinates[location, 0] - xMin) * xStep)),
    94                             bitmap.Height - (TourBorder + ((int)((coordinates[location, 1] - yMin) * yStep))));
    95             tourPoints[i + 1] = locationPoint;
    96 
    97             if (i != -1 && i != tour.Count) {
    98               customerSizes[i] = (int)Math.Round(MinDemandSize + problemInstance.Demands[location] / problemInstance.Capacity * (MaxDemandSize - MinDemandSize));
     69      double xRange = xMax - xMin;
     70      double yRange = yMax - yMin;
     71      double scaling = Math.Min((TourWidth-TourBorder*2)/xRange, (TourHeight-TourBorder*2)/yRange);
     72      Bitmap bitmap = new Bitmap((int)Math.Ceiling(xRange*scaling+TourBorder*2), (int)Math.Ceiling(yRange*scaling+TourBorder*2));
     73      var pens = TourColors.Select(c => new Pen(c, 2)).ToList();
     74      using (Graphics g = Graphics.FromImage(bitmap)) {
     75        using (var brush = new SolidBrush(Color.FromArgb(100, 0, 0, 0))) {
     76          g.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
     77
     78          int currentTour = 0;
     79          foreach (List<int> tour in solution.Routes) {
     80            Point[] tourPoints = new Point[tour.Count + 2];
     81            int[] customerSizes = new int[tour.Count];
     82
     83            for (int i = -1; i <= tour.Count; i++) {
     84              int location = (i == -1 || i == tour.Count) ? 0 : tour[i];
     85              Point locationPoint = new Point(TourBorder + ((int) ((coordinates[location, 0] - xMin)*scaling)),
     86                                              bitmap.Height -
     87                                              (TourBorder + ((int) ((coordinates[location, 1] - yMin)*scaling))));
     88              tourPoints[i + 1] = locationPoint;
     89              if (i != -1 && i != tour.Count) {
     90                customerSizes[i] =
     91                  (int)
     92                  Math.Round(MinDemandSize +
     93                             problemInstance.Demands[location]/problemInstance.Capacity*(MaxDemandSize - MinDemandSize));
     94              }
    9995            }
    100             lastCustomer = location;
    101           }
    102 
    103           graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
    104 
    105           using(Brush brush = new SolidBrush(Color.FromArgb(100, 0, 0, 0))) {
     96            g.DrawPolygon(pens[((currentTour >= pens.Count) ? (pens.Count - 1) : (currentTour))], tourPoints);
    10697            for (int i = 0; i < tour.Count; i++) {
    10798              int size = customerSizes[i];
    108               graphics.FillEllipse(brush, tourPoints[i + 1].X - size, tourPoints[i + 1].Y - size, size * 2, size * 2);
     99              g.FillEllipse(brush, tourPoints[i + 1].X - size, tourPoints[i + 1].Y - size, size*2, size*2);
    109100            }
     101            g.FillRectangle(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
     102            currentTour++;
    110103          }
    111 
    112           graphics.FillRectangle(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
    113 
    114           currentTour++;
    115         }
    116       }
    117 
    118       for (int i = 0; i < pens.Length; i++)
    119         pens[i].Dispose();
    120 
     104        }
     105      }
     106      foreach (var p in pens) p.Dispose();
    121107      return bitmap;
    122108    }
     
    141127      chart.Size = new Size(ChartWidth, ChartHeight);
    142128
    143       chart.Titles.Add("Demand Distribution");
    144129      chart.ChartAreas.Add("");
    145 
    146       chart.ChartAreas[0].BackColor = Color.White;
    147 
    148       chart.ChartAreas[0].AxisX.Title = "Demand";
     130      ConfigureChartAppearance(chart);
     131      chart.ChartAreas[0].AxisX.Title = "Demands";
    149132      chart.ChartAreas[0].AxisX.Minimum = 0;
    150133      chart.ChartAreas[0].AxisX.Maximum = problemInstance.Capacity;
    151       chart.ChartAreas[0].AxisX.Interval = problemInstance.Capacity / (double)DemandSegments;
    152       chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
    153 
     134      chart.ChartAreas[0].AxisX.Interval = problemInstance.Capacity/DemandSegments;
     135      chart.ChartAreas[0].AxisX.LabelStyle.Interval = problemInstance.Capacity/DemandSegments*2;
    154136      chart.ChartAreas[0].AxisY.Title = "Customers";
    155137      chart.ChartAreas[0].AxisY.Minimum = 0;
    156138      chart.ChartAreas[0].AxisY.Maximum = problemInstance.Vertices.GetLength(0);
    157       chart.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
    158       chart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(100, Color.Blue);
    159139
    160140      chart.Series.Add("");
     
    173153    }
    174154
     155    private static void ConfigureChartAppearance(Chart chart) {
     156      chart.ChartAreas[0].BackColor = Color.White;
     157      chart.ChartAreas[0].AxisX.TitleFont = new Font("Helvetica", 16);
     158      chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
     159      chart.ChartAreas[0].AxisY.TitleFont = new Font("Helvetica", 16);
     160      chart.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
     161      chart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(100, Color.LightBlue);
     162    }
     163
    175164    private static double GetAverageDistance(int customer, double[,] vertices) {
    176165      double dist = 0;
     
    211200      chart.Size = new Size(ChartWidth, ChartHeight);
    212201
    213       chart.Titles.Add("Distance Distribution");
    214202      chart.ChartAreas.Add("");
    215 
    216       chart.ChartAreas[0].BackColor = Color.White;
    217 
    218       chart.ChartAreas[0].AxisX.Title = "Distance";
     203      ConfigureChartAppearance(chart);
     204      chart.ChartAreas[0].AxisX.Title = "Distances";
    219205      chart.ChartAreas[0].AxisX.Minimum = 0;
    220206      chart.ChartAreas[0].AxisX.Maximum = 1;
    221       chart.ChartAreas[0].AxisX.Interval = 1.0 / (double)DistanceSegments;
    222       chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
    223 
     207      chart.ChartAreas[0].AxisX.Interval = 1.0/DistanceSegments;
     208      chart.ChartAreas[0].AxisX.LabelStyle.Interval = 1.0/DistanceSegments*2;
    224209      chart.ChartAreas[0].AxisY.Title = "Customers";
    225210      chart.ChartAreas[0].AxisY.Minimum = 0;
    226211      chart.ChartAreas[0].AxisY.Maximum = problemInstance.Vertices.GetLength(0);
    227       chart.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
    228       chart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(100, Color.Blue);
    229212
    230213      chart.Series.Add("");
Note: See TracChangeset for help on using the changeset viewer.