Changeset 12076


Ignore:
Timestamp:
02/25/15 18:01:52 (6 years ago)
Author:
bburlacu
Message:

#2332: Changed the way the update counter is incremented in the PopulationSimilarityAnalyzer so that it's easier to use in the analyzers derived from this class.

File:
1 edited

Legend:

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

    r12069 r12076  
    125125        updateCounter = new IntValue(updateInterval);
    126126        UpdateCounterParameter.ActualValue = updateCounter;
    127       } else updateCounter.Value++;
    128 
     127      }
    129128      //analyze solutions only every 'updateInterval' times
    130       if (updateCounter.Value == updateInterval) {
    131         updateCounter.Value = 0;
    132 
    133         bool storeHistory = StoreHistoryParameter.Value.Value;
    134         int count = CurrentScopeParameter.ActualValue.SubScopes.Count;
    135 
    136         if (count > 1) {
    137           var similarityCalculator = SimilarityCalculatorParameter.Value;
    138           // calculate solution similarities
    139           var similarityMatrix = similarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScopeParameter.ActualValue);
    140           double[,] similarities = new double[similarityMatrix.Length, similarityMatrix[0].Length];
    141           for (int i = 0; i < similarityMatrix.Length; i++)
    142             for (int j = 0; j < similarityMatrix[0].Length; j++)
    143               similarities[i, j] = similarityMatrix[i][j];
    144 
    145           // calculate minimum, average and maximum similarities
    146           double similarity;
    147           double[] minSimilarities = new double[count];
    148           double[] avgSimilarities = new double[count];
    149           double[] maxSimilarities = new double[count];
    150           for (int i = 0; i < count; i++) {
    151             minSimilarities[i] = 1;
    152             avgSimilarities[i] = 0;
    153             maxSimilarities[i] = 0;
    154             for (int j = 0; j < count; j++) {
    155               if (i != j) {
    156                 similarity = similarities[i, j];
    157 
    158                 if ((similarity < 0) || (similarity > 1))
    159                   throw new InvalidOperationException("Solution similarities have to be in the interval [0;1].");
    160 
    161                 if (minSimilarities[i] > similarity) minSimilarities[i] = similarity;
    162                 avgSimilarities[i] += similarity;
    163                 if (maxSimilarities[i] < similarity) maxSimilarities[i] = similarity;
    164               }
    165             }
    166             avgSimilarities[i] = avgSimilarities[i] / (count - 1);
    167           }
    168           double avgMinSimilarity = minSimilarities.Average();
    169           double avgAvgSimilarity = avgSimilarities.Average();
    170           double avgMaxSimilarity = maxSimilarities.Average();
    171 
    172           // fetch results collection
    173           ResultCollection results;
    174           if (!ResultsParameter.ActualValue.ContainsKey(DiversityResultName)) {
    175             results = new ResultCollection();
    176             ResultsParameter.ActualValue.Add(new Result(DiversityResultName, results));
    177           } else {
    178             results = (ResultCollection)ResultsParameter.ActualValue[DiversityResultName].Value;
    179           }
    180 
    181           // store similarities
    182           HeatMap similaritiesHeatMap = new HeatMap(similarities, "Solution Similarities", 0.0, 1.0);
    183           if (!results.ContainsKey("Solution Similarities"))
    184             results.Add(new Result("Solution Similarities", similaritiesHeatMap));
    185           else
    186             results["Solution Similarities"].Value = similaritiesHeatMap;
    187 
    188           // store similarities history
    189           if (storeHistory) {
    190             if (!results.ContainsKey("Solution Similarities History")) {
    191               HeatMapHistory history = new HeatMapHistory();
    192               history.Add(similaritiesHeatMap);
    193               results.Add(new Result("Solution Similarities History", history));
    194             } else {
    195               ((HeatMapHistory)results["Solution Similarities History"].Value).Add(similaritiesHeatMap);
     129      if (updateCounter.Value != updateInterval) {
     130        updateCounter.Value++;
     131        return base.Apply();
     132      }
     133      updateCounter.Value = 1;
     134
     135      bool storeHistory = StoreHistoryParameter.Value.Value;
     136      int count = CurrentScopeParameter.ActualValue.SubScopes.Count;
     137
     138      if (count > 1) {
     139        var similarityCalculator = SimilarityCalculatorParameter.Value;
     140        // calculate solution similarities
     141        var similarityMatrix = similarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScopeParameter.ActualValue);
     142        double[,] similarities = new double[similarityMatrix.Length, similarityMatrix[0].Length];
     143        for (int i = 0; i < similarityMatrix.Length; i++)
     144          for (int j = 0; j < similarityMatrix[0].Length; j++)
     145            similarities[i, j] = similarityMatrix[i][j];
     146
     147        // calculate minimum, average and maximum similarities
     148        double similarity;
     149        double[] minSimilarities = new double[count];
     150        double[] avgSimilarities = new double[count];
     151        double[] maxSimilarities = new double[count];
     152        for (int i = 0; i < count; i++) {
     153          minSimilarities[i] = 1;
     154          avgSimilarities[i] = 0;
     155          maxSimilarities[i] = 0;
     156          for (int j = 0; j < count; j++) {
     157            if (i != j) {
     158              similarity = similarities[i, j];
     159
     160              if ((similarity < 0) || (similarity > 1))
     161                throw new InvalidOperationException("Solution similarities have to be in the interval [0;1].");
     162
     163              if (minSimilarities[i] > similarity) minSimilarities[i] = similarity;
     164              avgSimilarities[i] += similarity;
     165              if (maxSimilarities[i] < similarity) maxSimilarities[i] = similarity;
    196166            }
    197167          }
    198 
    199           // store average minimum, average and maximum similarity
    200           if (!results.ContainsKey("Average Minimum Solution Similarity"))
    201             results.Add(new Result("Average Minimum Solution Similarity", new DoubleValue(avgMinSimilarity)));
    202           else
    203             ((DoubleValue)results["Average Minimum Solution Similarity"].Value).Value = avgMinSimilarity;
    204 
    205           if (!results.ContainsKey("Average Average Solution Similarity"))
    206             results.Add(new Result("Average Average Solution Similarity", new DoubleValue(avgAvgSimilarity)));
    207           else
    208             ((DoubleValue)results["Average Average Solution Similarity"].Value).Value = avgAvgSimilarity;
    209 
    210           if (!results.ContainsKey("Average Maximum Solution Similarity"))
    211             results.Add(new Result("Average Maximum Solution Similarity", new DoubleValue(avgMaxSimilarity)));
    212           else
    213             ((DoubleValue)results["Average Maximum Solution Similarity"].Value).Value = avgMaxSimilarity;
    214 
    215           // store average minimum, average and maximum solution similarity data table
    216           DataTable minAvgMaxSimilarityDataTable;
    217           if (!results.ContainsKey("Average Minimum/Average/Maximum Solution Similarity")) {
    218             minAvgMaxSimilarityDataTable = new DataTable("Average Minimum/Average/Maximum Solution Similarity");
    219             minAvgMaxSimilarityDataTable.VisualProperties.XAxisTitle = "Iteration";
    220             minAvgMaxSimilarityDataTable.VisualProperties.YAxisTitle = "Solution Similarity";
    221             minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Minimum Solution Similarity", null));
    222             minAvgMaxSimilarityDataTable.Rows["Average Minimum Solution Similarity"].VisualProperties.StartIndexZero = true;
    223             minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Average Solution Similarity", null));
    224             minAvgMaxSimilarityDataTable.Rows["Average Average Solution Similarity"].VisualProperties.StartIndexZero = true;
    225             minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Maximum Solution Similarity", null));
    226             minAvgMaxSimilarityDataTable.Rows["Average Maximum Solution Similarity"].VisualProperties.StartIndexZero = true;
    227             results.Add(new Result("Average Minimum/Average/Maximum Solution Similarity", minAvgMaxSimilarityDataTable));
     168          avgSimilarities[i] = avgSimilarities[i] / (count - 1);
     169        }
     170        double avgMinSimilarity = minSimilarities.Average();
     171        double avgAvgSimilarity = avgSimilarities.Average();
     172        double avgMaxSimilarity = maxSimilarities.Average();
     173
     174        // fetch results collection
     175        ResultCollection results;
     176        if (!ResultsParameter.ActualValue.ContainsKey(DiversityResultName)) {
     177          results = new ResultCollection();
     178          ResultsParameter.ActualValue.Add(new Result(DiversityResultName, results));
     179        } else {
     180          results = (ResultCollection)ResultsParameter.ActualValue[DiversityResultName].Value;
     181        }
     182
     183        // store similarities
     184        HeatMap similaritiesHeatMap = new HeatMap(similarities, "Solution Similarities", 0.0, 1.0);
     185        if (!results.ContainsKey("Solution Similarities"))
     186          results.Add(new Result("Solution Similarities", similaritiesHeatMap));
     187        else
     188          results["Solution Similarities"].Value = similaritiesHeatMap;
     189
     190        // store similarities history
     191        if (storeHistory) {
     192          if (!results.ContainsKey("Solution Similarities History")) {
     193            HeatMapHistory history = new HeatMapHistory();
     194            history.Add(similaritiesHeatMap);
     195            results.Add(new Result("Solution Similarities History", history));
    228196          } else {
    229             minAvgMaxSimilarityDataTable = (DataTable)results["Average Minimum/Average/Maximum Solution Similarity"].Value;
     197            ((HeatMapHistory)results["Solution Similarities History"].Value).Add(similaritiesHeatMap);
    230198          }
    231           minAvgMaxSimilarityDataTable.Rows["Average Minimum Solution Similarity"].Values.Add(avgMinSimilarity);
    232           minAvgMaxSimilarityDataTable.Rows["Average Average Solution Similarity"].Values.Add(avgAvgSimilarity);
    233           minAvgMaxSimilarityDataTable.Rows["Average Maximum Solution Similarity"].Values.Add(avgMaxSimilarity);
    234 
    235           // store minimum, average, maximum similarities data table
    236           DataTable minAvgMaxSimilaritiesDataTable = new DataTable("Minimum/Average/Maximum Solution Similarities");
    237           minAvgMaxSimilaritiesDataTable.VisualProperties.XAxisTitle = "Solution Index";
    238           minAvgMaxSimilaritiesDataTable.VisualProperties.YAxisTitle = "Solution Similarity";
    239           minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Minimum Solution Similarity", null, minSimilarities));
    240           minAvgMaxSimilaritiesDataTable.Rows["Minimum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
    241           minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Average Solution Similarity", null, avgSimilarities));
    242           minAvgMaxSimilaritiesDataTable.Rows["Average Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
    243           minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Maximum Solution Similarity", null, maxSimilarities));
    244           minAvgMaxSimilaritiesDataTable.Rows["Maximum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
    245           if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities")) {
    246             results.Add(new Result("Minimum/Average/Maximum Solution Similarities", minAvgMaxSimilaritiesDataTable));
     199        }
     200
     201        // store average minimum, average and maximum similarity
     202        if (!results.ContainsKey("Average Minimum Solution Similarity"))
     203          results.Add(new Result("Average Minimum Solution Similarity", new DoubleValue(avgMinSimilarity)));
     204        else
     205          ((DoubleValue)results["Average Minimum Solution Similarity"].Value).Value = avgMinSimilarity;
     206
     207        if (!results.ContainsKey("Average Average Solution Similarity"))
     208          results.Add(new Result("Average Average Solution Similarity", new DoubleValue(avgAvgSimilarity)));
     209        else
     210          ((DoubleValue)results["Average Average Solution Similarity"].Value).Value = avgAvgSimilarity;
     211
     212        if (!results.ContainsKey("Average Maximum Solution Similarity"))
     213          results.Add(new Result("Average Maximum Solution Similarity", new DoubleValue(avgMaxSimilarity)));
     214        else
     215          ((DoubleValue)results["Average Maximum Solution Similarity"].Value).Value = avgMaxSimilarity;
     216
     217        // store average minimum, average and maximum solution similarity data table
     218        DataTable minAvgMaxSimilarityDataTable;
     219        if (!results.ContainsKey("Average Minimum/Average/Maximum Solution Similarity")) {
     220          minAvgMaxSimilarityDataTable = new DataTable("Average Minimum/Average/Maximum Solution Similarity");
     221          minAvgMaxSimilarityDataTable.VisualProperties.XAxisTitle = "Iteration";
     222          minAvgMaxSimilarityDataTable.VisualProperties.YAxisTitle = "Solution Similarity";
     223          minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Minimum Solution Similarity", null));
     224          minAvgMaxSimilarityDataTable.Rows["Average Minimum Solution Similarity"].VisualProperties.StartIndexZero = true;
     225          minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Average Solution Similarity", null));
     226          minAvgMaxSimilarityDataTable.Rows["Average Average Solution Similarity"].VisualProperties.StartIndexZero = true;
     227          minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Maximum Solution Similarity", null));
     228          minAvgMaxSimilarityDataTable.Rows["Average Maximum Solution Similarity"].VisualProperties.StartIndexZero = true;
     229          results.Add(new Result("Average Minimum/Average/Maximum Solution Similarity", minAvgMaxSimilarityDataTable));
     230        } else {
     231          minAvgMaxSimilarityDataTable = (DataTable)results["Average Minimum/Average/Maximum Solution Similarity"].Value;
     232        }
     233        minAvgMaxSimilarityDataTable.Rows["Average Minimum Solution Similarity"].Values.Add(avgMinSimilarity);
     234        minAvgMaxSimilarityDataTable.Rows["Average Average Solution Similarity"].Values.Add(avgAvgSimilarity);
     235        minAvgMaxSimilarityDataTable.Rows["Average Maximum Solution Similarity"].Values.Add(avgMaxSimilarity);
     236
     237        // store minimum, average, maximum similarities data table
     238        DataTable minAvgMaxSimilaritiesDataTable = new DataTable("Minimum/Average/Maximum Solution Similarities");
     239        minAvgMaxSimilaritiesDataTable.VisualProperties.XAxisTitle = "Solution Index";
     240        minAvgMaxSimilaritiesDataTable.VisualProperties.YAxisTitle = "Solution Similarity";
     241        minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Minimum Solution Similarity", null, minSimilarities));
     242        minAvgMaxSimilaritiesDataTable.Rows["Minimum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
     243        minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Average Solution Similarity", null, avgSimilarities));
     244        minAvgMaxSimilaritiesDataTable.Rows["Average Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
     245        minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Maximum Solution Similarity", null, maxSimilarities));
     246        minAvgMaxSimilaritiesDataTable.Rows["Maximum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
     247        if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities")) {
     248          results.Add(new Result("Minimum/Average/Maximum Solution Similarities", minAvgMaxSimilaritiesDataTable));
     249        } else {
     250          results["Minimum/Average/Maximum Solution Similarities"].Value = minAvgMaxSimilaritiesDataTable;
     251        }
     252
     253        // store minimum, average, maximum similarities history
     254        if (storeHistory) {
     255          if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities History")) {
     256            DataTableHistory history = new DataTableHistory();
     257            history.Add(minAvgMaxSimilaritiesDataTable);
     258            results.Add(new Result("Minimum/Average/Maximum Solution Similarities History", history));
    247259          } else {
    248             results["Minimum/Average/Maximum Solution Similarities"].Value = minAvgMaxSimilaritiesDataTable;
    249           }
    250 
    251           // store minimum, average, maximum similarities history
    252           if (storeHistory) {
    253             if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities History")) {
    254               DataTableHistory history = new DataTableHistory();
    255               history.Add(minAvgMaxSimilaritiesDataTable);
    256               results.Add(new Result("Minimum/Average/Maximum Solution Similarities History", history));
    257             } else {
    258               ((DataTableHistory)results["Minimum/Average/Maximum Solution Similarities History"].Value).Add(minAvgMaxSimilaritiesDataTable);
    259             }
     260            ((DataTableHistory)results["Minimum/Average/Maximum Solution Similarities History"].Value).Add(minAvgMaxSimilaritiesDataTable);
    260261          }
    261262        }
Note: See TracChangeset for help on using the changeset viewer.