Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/08/10 03:00:46 (14 years ago)
Author:
swagner
Message:

Worked on population diversity analysis (#1188)

Location:
trunk/sources/HeuristicLab.Analysis/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Analysis/3.3/HeatMap.cs

    r4722 r4739  
    2020#endregion
    2121
    22 using System.Collections.Generic;
     22using System;
    2323using System.Drawing;
    2424using HeuristicLab.Common;
     
    3535    }
    3636
     37    private string title;
     38    public string Title {
     39      get { return title; }
     40      set {
     41        if (!(title.Equals(value) || (value == null) && (title == string.Empty))) {
     42          title = value == null ? string.Empty : value;
     43          OnTitleChanged();
     44        }
     45      }
     46    }
     47    private double minimum;
     48    public double Minimum {
     49      get { return minimum; }
     50      set {
     51        if (minimum != value) {
     52          minimum = value;
     53          if (minimum >= maximum) Maximum = minimum + 1.0;
     54          OnMinimumChanged();
     55        }
     56      }
     57    }
     58    private double maximum;
     59    public double Maximum {
     60      get { return maximum; }
     61      set {
     62        if (maximum != value) {
     63          maximum = value;
     64          if (maximum <= minimum) Minimum = maximum - 1.0;
     65          OnMaximumChanged();
     66        }
     67      }
     68    }
     69
     70    #region Storable Properties
     71    [Storable(Name = "Title")]
     72    private string StorableTitle {
     73      get { return title; }
     74      set { title = value; }
     75    }
     76    [Storable(Name = "Minimum")]
     77    private double StorableMinimum {
     78      get { return minimum; }
     79      set { minimum = value; }
     80    }
     81    [Storable(Name = "Maximum")]
     82    private double StorableMaximum {
     83      get { return maximum; }
     84      set { maximum = value; }
     85    }
     86    #endregion
     87
    3788    [StorableConstructor]
    3889    protected HeatMap(bool deserializing) : base(deserializing) { }
    39     protected HeatMap(HeatMap original, Cloner cloner) : base(original, cloner) { }
    40     public HeatMap() : base() { }
    41     public HeatMap(int rows, int columns) : base(rows, columns) { }
    42     public HeatMap(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
    43     public HeatMap(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
    44     public HeatMap(double[,] elements) : base(elements) { }
    45     public HeatMap(double[,] elements, IEnumerable<string> columnNames) : base(elements, columnNames) { }
    46     public HeatMap(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements, columnNames, rowNames) { }
     90    protected HeatMap(HeatMap original, Cloner cloner)
     91      : base(original, cloner) {
     92      this.title = original.title;
     93      this.minimum = original.minimum;
     94      this.maximum = original.maximum;
     95    }
     96    public HeatMap()
     97      : base() {
     98      this.title = "Heat Map";
     99      this.minimum = 0.0;
     100      this.maximum = 1.0;
     101    }
     102    public HeatMap(int rows, int columns)
     103      : base(rows, columns) {
     104      this.title = "Heat Map";
     105      this.minimum = 0.0;
     106      this.maximum = 1.0;
     107    }
     108    public HeatMap(int rows, int columns, string title)
     109      : base(rows, columns) {
     110      this.title = title == null ? string.Empty : title;
     111      this.minimum = 0.0;
     112      this.maximum = 1.0;
     113    }
     114    public HeatMap(double[,] elements)
     115      : base(elements) {
     116      this.title = "Heat Map";
     117      this.minimum = 0.0;
     118      this.maximum = 1.0;
     119    }
     120    public HeatMap(double[,] elements, string title)
     121      : base(elements) {
     122      this.title = title == null ? string.Empty : title;
     123      this.minimum = 0.0;
     124      this.maximum = 1.0;
     125    }
     126    public HeatMap(double[,] elements, string title, double minimum, double maximum)
     127      : base(elements) {
     128      this.title = title == null ? string.Empty : title;
     129      if (minimum >= maximum) throw new ArgumentException("Minimum is larger than or equal to maximum");
     130      this.minimum = minimum;
     131      this.maximum = maximum;
     132    }
    47133
    48134    public override IDeepCloneable Clone(Cloner cloner) {
     
    51137
    52138    public override string ToString() {
    53       return ItemName;
     139      return Title;
     140    }
     141
     142    public event EventHandler TitleChanged;
     143    protected virtual void OnTitleChanged() {
     144      var handler = TitleChanged;
     145      if (handler != null) handler(this, EventArgs.Empty);
     146    }
     147    public event EventHandler MinimumChanged;
     148    protected virtual void OnMinimumChanged() {
     149      var handler = MinimumChanged;
     150      if (handler != null) handler(this, EventArgs.Empty);
     151    }
     152    public event EventHandler MaximumChanged;
     153    protected virtual void OnMaximumChanged() {
     154      var handler = MaximumChanged;
     155      if (handler != null) handler(this, EventArgs.Empty);
    54156    }
    55157  }
  • trunk/sources/HeuristicLab.Analysis/3.3/PopulationDiversityAnalyzer.cs

    r4722 r4739  
    8787        ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
    8888        bool storeHistory = StoreHistoryParameter.Value.Value;
    89 
    90         // sort solutions by quality
    91         T[] sortedSolutions = null;
    92         if (max)
    93           sortedSolutions = solutions.Select((x, index) => new { Solution = x, Quality = qualities[index] }).OrderByDescending(x => x.Quality).Select(x => x.Solution).ToArray();
    94         else
    95           sortedSolutions = solutions.Select((x, index) => new { Solution = x, Quality = qualities[index] }).OrderBy(x => x.Quality).Select(x => x.Solution).ToArray();
    96 
    97         // calculate solution similarities
    98         double[,] similarities = CalculateSimilarities(sortedSolutions);
    99 
    100         // calculate minimum, average and maximum similarities
    101         double similarity;
    102         int count = sortedSolutions.Length;
    103         double[] minSimilarities = new double[sortedSolutions.Length];
    104         double[] avgSimilarities = new double[sortedSolutions.Length];
    105         double[] maxSimilarities = new double[sortedSolutions.Length];
    106         double avgSimilarity = 0;
    107         for (int i = 0; i < count; i++) {
    108           minSimilarities[i] = 1;
    109           avgSimilarities[i] = 0;
    110           maxSimilarities[i] = 0;
    111           for (int j = 0; j < count; j++) {
    112             if (i != j) {
    113               similarity = similarities[i, j];
    114               avgSimilarity += similarity;
    115               if (minSimilarities[i] > similarity) minSimilarities[i] = similarity;
    116               avgSimilarities[i] += similarity;
    117               if (maxSimilarities[i] < similarity) maxSimilarities[i] = similarity;
     89        int count = solutions.Length;
     90
     91        if (count > 1) {
     92          // sort solutions by quality
     93          T[] sortedSolutions = null;
     94          if (max)
     95            sortedSolutions = solutions.Select((x, index) => new { Solution = x, Quality = qualities[index] }).OrderByDescending(x => x.Quality).Select(x => x.Solution).ToArray();
     96          else
     97            sortedSolutions = solutions.Select((x, index) => new { Solution = x, Quality = qualities[index] }).OrderBy(x => x.Quality).Select(x => x.Solution).ToArray();
     98
     99          // calculate solution similarities
     100          double[,] similarities = CalculateSimilarities(sortedSolutions);
     101
     102          // calculate minimum, average and maximum similarities
     103          double similarity;
     104          double[] minSimilarities = new double[sortedSolutions.Length];
     105          double[] avgSimilarities = new double[sortedSolutions.Length];
     106          double[] maxSimilarities = new double[sortedSolutions.Length];
     107          for (int i = 0; i < count; i++) {
     108            minSimilarities[i] = 1;
     109            avgSimilarities[i] = 0;
     110            maxSimilarities[i] = 0;
     111            for (int j = 0; j < count; j++) {
     112              if (i != j) {
     113                similarity = similarities[i, j];
     114                if (minSimilarities[i] > similarity) minSimilarities[i] = similarity;
     115                avgSimilarities[i] += similarity;
     116                if (maxSimilarities[i] < similarity) maxSimilarities[i] = similarity;
     117              }
    118118            }
    119           }
    120           avgSimilarities[i] = avgSimilarities[i] / (count - 1);
    121         }
    122         double avgMinSimilarity = minSimilarities.Average();
    123         double avgAvgSimilarity = avgSimilarities.Average();
    124         double avgMaxSimilarity = maxSimilarities.Average();
    125         avgSimilarity = avgSimilarity / (count * count - count);
    126 
    127         // fetch results collection
    128         ResultCollection results;
    129         if (!ResultsParameter.ActualValue.ContainsKey("Population Diversity Analysis Results")) {
    130           results = new ResultCollection();
    131           ResultsParameter.ActualValue.Add(new Result("Population Diversity Analysis Results", results));
    132         } else {
    133           results = (ResultCollection)ResultsParameter.ActualValue["Population Diversity Analysis Results"].Value;
    134         }
    135 
    136         // store similarities
    137         HeatMap similaritiesHeatMap = new HeatMap(similarities);
    138         if (!results.ContainsKey("Solution Similarities"))
    139           results.Add(new Result("Solution Similarities", similaritiesHeatMap));
    140         else
    141           results["Solution Similarities"].Value = similaritiesHeatMap;
    142 
    143         // store similarities history
    144         if (storeHistory) {
    145           if (!results.ContainsKey("Solution Similarities History")) {
    146             HeatMapHistory history = new HeatMapHistory();
    147             history.Add(similaritiesHeatMap);
    148             results.Add(new Result("Solution Similarities History", history));
     119            avgSimilarities[i] = avgSimilarities[i] / (count - 1);
     120          }
     121          double avgMinSimilarity = minSimilarities.Average();
     122          double avgAvgSimilarity = avgSimilarities.Average();
     123          double avgMaxSimilarity = maxSimilarities.Average();
     124
     125          // fetch results collection
     126          ResultCollection results;
     127          if (!ResultsParameter.ActualValue.ContainsKey("Population Diversity Analysis Results")) {
     128            results = new ResultCollection();
     129            ResultsParameter.ActualValue.Add(new Result("Population Diversity Analysis Results", results));
    149130          } else {
    150             ((HeatMapHistory)results["Solution Similarities History"].Value).Add(similaritiesHeatMap);
    151           }
    152         }
    153 
    154         // store average similarity
    155         if (!results.ContainsKey("Average Population Similarity"))
    156           results.Add(new Result("Average Population Similarity", new DoubleValue(avgSimilarity)));
    157         else
    158           ((DoubleValue)results["Average Population Similarity"].Value).Value = avgSimilarity;
    159 
    160         // store average minimum, average and maximum similarity
    161         if (!results.ContainsKey("Average Minimum Solution Similarity"))
    162           results.Add(new Result("Average Minimum Solution Similarity", new DoubleValue(avgMinSimilarity)));
    163         else
    164           ((DoubleValue)results["Average Minimum Solution Similarity"].Value).Value = avgMinSimilarity;
    165 
    166         if (!results.ContainsKey("Average Average Solution Similarity"))
    167           results.Add(new Result("Average Average Solution Similarity", new DoubleValue(avgAvgSimilarity)));
    168         else
    169           ((DoubleValue)results["Average Average Solution Similarity"].Value).Value = avgAvgSimilarity;
    170 
    171         if (!results.ContainsKey("Average Maximum Solution Similarity"))
    172           results.Add(new Result("Average Maximum Solution Similarity", new DoubleValue(avgMaxSimilarity)));
    173         else
    174           ((DoubleValue)results["Average Maximum Solution Similarity"].Value).Value = avgMaxSimilarity;
    175 
    176         // store population similarity data table
    177         DataTable similarityDataTable;
    178         if (!results.ContainsKey("Average Solution Similarity")) {
    179           similarityDataTable = new DataTable("Average Solution Similarity");
    180           results.Add(new Result("Average Solution Similarity", similarityDataTable));
    181           DataRowVisualProperties visualProperties = new DataRowVisualProperties();
    182           visualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Line;
    183           visualProperties.StartIndexZero = true;
    184           similarityDataTable.Rows.Add(new DataRow("Average Population Similarity", null, visualProperties));
    185           similarityDataTable.Rows.Add(new DataRow("Average Minimum Solution Similarity", null, visualProperties));
    186           similarityDataTable.Rows.Add(new DataRow("Average Average Solution Similarity", null, visualProperties));
    187           similarityDataTable.Rows.Add(new DataRow("Average Maximum Solution Similarity", null, visualProperties));
    188         } else {
    189           similarityDataTable = (DataTable)results["Average Solution Similarity"].Value;
    190         }
    191         similarityDataTable.Rows["Average Population Similarity"].Values.Add(avgSimilarity);
    192         similarityDataTable.Rows["Average Minimum Solution Similarity"].Values.Add(avgMinSimilarity);
    193         similarityDataTable.Rows["Average Average Solution Similarity"].Values.Add(avgAvgSimilarity);
    194         similarityDataTable.Rows["Average Maximum Solution Similarity"].Values.Add(avgMaxSimilarity);
    195 
    196         // store maximum similarities
    197         DataTable minAvgMaxSimilaritiesDataTable = new DataTable("Minimum/Average/Maximum Solution Similarities");
    198         minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Minimum Solution Similarity"));
    199         minAvgMaxSimilaritiesDataTable.Rows["Minimum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Columns;
    200         minAvgMaxSimilaritiesDataTable.Rows["Minimum Solution Similarity"].Values.AddRange(minSimilarities);
    201         minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Average Solution Similarity"));
    202         minAvgMaxSimilaritiesDataTable.Rows["Average Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Columns;
    203         minAvgMaxSimilaritiesDataTable.Rows["Average Solution Similarity"].Values.AddRange(avgSimilarities);
    204         minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Maximum Solution Similarity"));
    205         minAvgMaxSimilaritiesDataTable.Rows["Maximum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Columns;
    206         minAvgMaxSimilaritiesDataTable.Rows["Maximum Solution Similarity"].Values.AddRange(maxSimilarities);
    207         if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities")) {
    208           results.Add(new Result("Minimum/Average/Maximum Solution Similarities", minAvgMaxSimilaritiesDataTable));
    209         } else {
    210           results["Minimum/Average/Maximum Solution Similarities"].Value = minAvgMaxSimilaritiesDataTable;
    211         }
    212 
    213         // store maximum similarities history
    214         if (storeHistory) {
    215           if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities History")) {
    216             DataTableHistory history = new DataTableHistory();
    217             history.Add(minAvgMaxSimilaritiesDataTable);
    218             results.Add(new Result("Minimum/Average/Maximum Solution Similarities History", history));
     131            results = (ResultCollection)ResultsParameter.ActualValue["Population Diversity Analysis Results"].Value;
     132          }
     133
     134          // store similarities
     135          HeatMap similaritiesHeatMap = new HeatMap(similarities, "Solution Similarities", 0.0, 1.0);
     136          if (!results.ContainsKey("Solution Similarities"))
     137            results.Add(new Result("Solution Similarities", similaritiesHeatMap));
     138          else
     139            results["Solution Similarities"].Value = similaritiesHeatMap;
     140
     141          // store similarities history
     142          if (storeHistory) {
     143            if (!results.ContainsKey("Solution Similarities History")) {
     144              HeatMapHistory history = new HeatMapHistory();
     145              history.Add(similaritiesHeatMap);
     146              results.Add(new Result("Solution Similarities History", history));
     147            } else {
     148              ((HeatMapHistory)results["Solution Similarities History"].Value).Add(similaritiesHeatMap);
     149            }
     150          }
     151
     152          // store average minimum, average and maximum similarity
     153          if (!results.ContainsKey("Average Minimum Solution Similarity"))
     154            results.Add(new Result("Average Minimum Solution Similarity", new DoubleValue(avgMinSimilarity)));
     155          else
     156            ((DoubleValue)results["Average Minimum Solution Similarity"].Value).Value = avgMinSimilarity;
     157
     158          if (!results.ContainsKey("Average Average Solution Similarity"))
     159            results.Add(new Result("Average Average Solution Similarity", new DoubleValue(avgAvgSimilarity)));
     160          else
     161            ((DoubleValue)results["Average Average Solution Similarity"].Value).Value = avgAvgSimilarity;
     162
     163          if (!results.ContainsKey("Average Maximum Solution Similarity"))
     164            results.Add(new Result("Average Maximum Solution Similarity", new DoubleValue(avgMaxSimilarity)));
     165          else
     166            ((DoubleValue)results["Average Maximum Solution Similarity"].Value).Value = avgMaxSimilarity;
     167
     168          // store average minimum, average and maximum solution similarity data table
     169          DataTable minAvgMaxSimilarityDataTable;
     170          if (!results.ContainsKey("Average Minimum/Average/Maximum Solution Similarity")) {
     171            minAvgMaxSimilarityDataTable = new DataTable("Average Minimum/Average/Maximum Solution Similarity");
     172            results.Add(new Result("Average Minimum/Average/Maximum Solution Similarity", minAvgMaxSimilarityDataTable));
     173            DataRowVisualProperties visualProperties = new DataRowVisualProperties();
     174            visualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Line;
     175            visualProperties.StartIndexZero = true;
     176            minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Minimum Solution Similarity", null, visualProperties));
     177            minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Average Solution Similarity", null, visualProperties));
     178            minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Maximum Solution Similarity", null, visualProperties));
    219179          } else {
    220             ((DataTableHistory)results["Minimum/Average/Maximum Solution Similarities History"].Value).Add(minAvgMaxSimilaritiesDataTable);
     180            minAvgMaxSimilarityDataTable = (DataTable)results["Average Minimum/Average/Maximum Solution Similarity"].Value;
     181          }
     182          minAvgMaxSimilarityDataTable.Rows["Average Minimum Solution Similarity"].Values.Add(avgMinSimilarity);
     183          minAvgMaxSimilarityDataTable.Rows["Average Average Solution Similarity"].Values.Add(avgAvgSimilarity);
     184          minAvgMaxSimilarityDataTable.Rows["Average Maximum Solution Similarity"].Values.Add(avgMaxSimilarity);
     185
     186          // store minimum, average, maximum similarities data table
     187          DataTable minAvgMaxSimilaritiesDataTable = new DataTable("Minimum/Average/Maximum Solution Similarities");
     188          minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Minimum Solution Similarity"));
     189          minAvgMaxSimilaritiesDataTable.Rows["Minimum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Columns;
     190          minAvgMaxSimilaritiesDataTable.Rows["Minimum Solution Similarity"].Values.AddRange(minSimilarities);
     191          minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Average Solution Similarity"));
     192          minAvgMaxSimilaritiesDataTable.Rows["Average Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Columns;
     193          minAvgMaxSimilaritiesDataTable.Rows["Average Solution Similarity"].Values.AddRange(avgSimilarities);
     194          minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Maximum Solution Similarity"));
     195          minAvgMaxSimilaritiesDataTable.Rows["Maximum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Columns;
     196          minAvgMaxSimilaritiesDataTable.Rows["Maximum Solution Similarity"].Values.AddRange(maxSimilarities);
     197          if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities")) {
     198            results.Add(new Result("Minimum/Average/Maximum Solution Similarities", minAvgMaxSimilaritiesDataTable));
     199          } else {
     200            results["Minimum/Average/Maximum Solution Similarities"].Value = minAvgMaxSimilaritiesDataTable;
     201          }
     202
     203          // store minimum, average, maximum similarities history
     204          if (storeHistory) {
     205            if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities History")) {
     206              DataTableHistory history = new DataTableHistory();
     207              history.Add(minAvgMaxSimilaritiesDataTable);
     208              results.Add(new Result("Minimum/Average/Maximum Solution Similarities History", history));
     209            } else {
     210              ((DataTableHistory)results["Minimum/Average/Maximum Solution Similarities History"].Value).Add(minAvgMaxSimilaritiesDataTable);
     211            }
    221212          }
    222213        }
Note: See TracChangeset for help on using the changeset viewer.