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)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.