Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/PopulationSimilarityAnalysis/PopulationSimilarityAnalyzer.cs @ 13613

Last change on this file since 13613 was 13005, checked in by ascheibe, 9 years ago

#2377 applied and improved bburlacus patch

File size: 17.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Analysis {
34  /// <summary>
35  /// An operator for analyzing the solution similarity in a population.
36  /// </summary>
37  [Item("PopulationSimilarityAnalyzer", "An operator for analyzing the solution similarity in a population.")]
38  [StorableClass]
39  public class PopulationSimilarityAnalyzer : SingleSuccessorOperator, IAnalyzer, ISimilarityBasedOperator {
40    private const string DiversityResultNameParameterName = "DiversityResultsName";
41    private const string ExecuteInParallelParameterName = "ExecuteInParallel";
42    private const string MaxDegreeOfParallelismParameterName = "MaxDegreeOfParallelism";
43
44    #region Backwards compatible code, remove with 3.4
45    private ISolutionSimilarityCalculator oldSimilarityCalculator;
46    [Storable(AllowOneWay = true, Name = "SimilarityCalculator")]
47    [Obsolete]
48    private ISolutionSimilarityCalculator SimilarityCalculator { set { oldSimilarityCalculator = value; } }
49    #endregion
50
51    public virtual bool EnabledByDefault {
52      get { return false; }
53    }
54    public ScopeParameter CurrentScopeParameter {
55      get { return (ScopeParameter)Parameters["CurrentScope"]; }
56    }
57    public IValueLookupParameter<ResultCollection> ResultsParameter {
58      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
59    }
60    public IConstrainedValueParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
61      get { return (IConstrainedValueParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
62    }
63    public IValueParameter<BoolValue> StoreHistoryParameter {
64      get { return (IValueParameter<BoolValue>)Parameters["StoreHistory"]; }
65    }
66    public IValueParameter<IntValue> UpdateIntervalParameter {
67      get { return (IValueParameter<IntValue>)Parameters["UpdateInterval"]; }
68    }
69    public ILookupParameter<IntValue> UpdateCounterParameter {
70      get { return (ILookupParameter<IntValue>)Parameters["UpdateCounter"]; }
71    }
72    public IFixedValueParameter<StringValue> DiversityResultNameParameter {
73      get { return (FixedValueParameter<StringValue>)Parameters[DiversityResultNameParameterName]; }
74    }
75    public IFixedValueParameter<BoolValue> ExecuteInParallelParameter {
76      get { return (IFixedValueParameter<BoolValue>)Parameters[ExecuteInParallelParameterName]; }
77    }
78    public IFixedValueParameter<IntValue> MaxDegreeOfParallelismParameter {
79      get { return (IFixedValueParameter<IntValue>)Parameters[MaxDegreeOfParallelismParameterName]; }
80    }
81
82    public string DiversityResultName {
83      get { return DiversityResultNameParameter.Value.Value; }
84      set { DiversityResultNameParameter.Value.Value = value; }
85    }
86
87    public bool ExecuteInParallel {
88      get { return ExecuteInParallelParameter.Value.Value; }
89      set { ExecuteInParallelParameter.Value.Value = value; }
90    }
91
92    public int MaxDegreeOfParallelism {
93      get { return MaxDegreeOfParallelismParameter.Value.Value; }
94      set { MaxDegreeOfParallelismParameter.Value.Value = value; }
95    }
96
97    [StorableConstructor]
98    protected PopulationSimilarityAnalyzer(bool deserializing) : base(deserializing) { }
99
100    protected PopulationSimilarityAnalyzer(PopulationSimilarityAnalyzer original, Cloner cloner)
101      : base(original, cloner) {
102      RegisterParametersEventHandlers();
103    }
104
105    public PopulationSimilarityAnalyzer(IEnumerable<ISolutionSimilarityCalculator> validSimilarityCalculators)
106      : base() {
107      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solutions which should be analyzed."));
108      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the population diversity analysis results should be stored."));
109      Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate population similarity."));
110      Parameters.Add(new ValueParameter<BoolValue>("StoreHistory", "True if the history of the population diversity analysis should be stored.", new BoolValue(false)));
111      Parameters.Add(new ValueParameter<IntValue>("UpdateInterval", "The interval in which the population diversity analysis should be applied.", new IntValue(1)));
112      Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update.", "PopulationDiversityAnalyzerUpdateCounter"));
113      Parameters.Add(new FixedValueParameter<StringValue>(DiversityResultNameParameterName, "Specifies how the diversity results should be named.", new StringValue("PopulationDiversity")));
114      Parameters.Add(new FixedValueParameter<BoolValue>(ExecuteInParallelParameterName, "Specifies whether similarity calculations should be parallelized.", new BoolValue(false)));
115      Parameters.Add(new FixedValueParameter<IntValue>(MaxDegreeOfParallelismParameterName, "Specifies the maximum number of threads when calculating similarities in parallel.", new IntValue(-1)));
116
117      var similarityCalculators = SimilarityCalculatorParameter.ValidValues;
118      foreach (var sc in validSimilarityCalculators) {
119        similarityCalculators.Add(sc);
120      }
121
122      ResultsParameter.Hidden = true;
123      UpdateCounterParameter.Hidden = true;
124      ExecuteInParallelParameter.Hidden = true;
125      MaxDegreeOfParallelismParameter.Hidden = true;
126
127      RegisterParametersEventHandlers();
128    }
129
130    private void RegisterParametersEventHandlers() {
131      ExecuteInParallelParameter.Value.ValueChanged += Value_ValueChanged;
132      MaxDegreeOfParallelismParameter.Value.ValueChanged += Value_ValueChanged;
133    }
134
135    private void Value_ValueChanged(object sender, EventArgs e) {
136      var similarityCalculators = SimilarityCalculatorParameter.ValidValues;
137      foreach (var similarityCalculator in similarityCalculators) {
138        similarityCalculator.ExecuteInParallel = ExecuteInParallel;
139        similarityCalculator.MaxDegreeOfParallelism = MaxDegreeOfParallelism;
140      }
141    }
142
143    public override IDeepCloneable Clone(Cloner cloner) {
144      return new PopulationSimilarityAnalyzer(this, cloner);
145    }
146
147    [StorableHook(HookType.AfterDeserialization)]
148    private void AfterDeserialization() {
149      // BackwardsCompatibility3.3
150      #region Backwards compatible code, remove with 3.4
151      if (!Parameters.ContainsKey(DiversityResultNameParameterName))
152        Parameters.Add(new FixedValueParameter<StringValue>(DiversityResultNameParameterName, "Specifies how the diversity results should be named.", new StringValue("PopulationDiversity")));
153
154      if (!Parameters.ContainsKey("SimilarityCalculator"))
155        Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
156
157      if (oldSimilarityCalculator != null)
158        SimilarityCalculatorParameter.ValidValues.Add(oldSimilarityCalculator);
159
160      if (!Parameters.ContainsKey(ExecuteInParallelParameterName)) {
161        Parameters.Add(new FixedValueParameter<BoolValue>(ExecuteInParallelParameterName,
162          "Specifies whether similarity calculations should be parallelized.", new BoolValue(false)));
163        ExecuteInParallelParameter.Hidden = true;
164      }
165      if (!Parameters.ContainsKey(MaxDegreeOfParallelismParameterName)) {
166        Parameters.Add(new FixedValueParameter<IntValue>(MaxDegreeOfParallelismParameterName,
167          "Specifies the maximum number of threads when calculating similarities in parallel.", new IntValue(-1)));
168        MaxDegreeOfParallelismParameter.Hidden = true;
169      }
170
171      RegisterParametersEventHandlers();
172      #endregion
173    }
174
175    public override IOperation Apply() {
176      int updateInterval = UpdateIntervalParameter.Value.Value;
177      IntValue updateCounter = UpdateCounterParameter.ActualValue;
178      // if counter does not yet exist then initialize it with update interval
179      // to make sure the solutions are analyzed on the first application of this operator
180      if (updateCounter == null) {
181        updateCounter = new IntValue(updateInterval);
182        UpdateCounterParameter.ActualValue = updateCounter;
183      }
184      //analyze solutions only every 'updateInterval' times
185      if (updateCounter.Value != updateInterval) {
186        updateCounter.Value++;
187        return base.Apply();
188      }
189      updateCounter.Value = 1;
190
191      bool storeHistory = StoreHistoryParameter.Value.Value;
192      int count = CurrentScopeParameter.ActualValue.SubScopes.Count;
193
194      if (count > 1) {
195        var similarityCalculator = SimilarityCalculatorParameter.Value;
196        // calculate solution similarities
197        var similarityMatrix = similarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScopeParameter.ActualValue);
198        double[,] similarities = new double[similarityMatrix.Length, similarityMatrix[0].Length];
199        for (int i = 0; i < similarityMatrix.Length; i++)
200          for (int j = 0; j < similarityMatrix[0].Length; j++)
201            similarities[i, j] = similarityMatrix[i][j];
202
203        // calculate minimum, average and maximum similarities
204        double similarity;
205        double[] minSimilarities = new double[count];
206        double[] avgSimilarities = new double[count];
207        double[] maxSimilarities = new double[count];
208        for (int i = 0; i < count; i++) {
209          minSimilarities[i] = 1;
210          avgSimilarities[i] = 0;
211          maxSimilarities[i] = 0;
212          for (int j = 0; j < count; j++) {
213            if (i != j) {
214              similarity = similarities[i, j];
215
216              if ((similarity < 0) || (similarity > 1))
217                throw new InvalidOperationException("Solution similarities have to be in the interval [0;1].");
218
219              if (minSimilarities[i] > similarity) minSimilarities[i] = similarity;
220              avgSimilarities[i] += similarity;
221              if (maxSimilarities[i] < similarity) maxSimilarities[i] = similarity;
222            }
223          }
224          avgSimilarities[i] = avgSimilarities[i] / (count - 1);
225        }
226        double avgMinSimilarity = minSimilarities.Average();
227        double avgAvgSimilarity = avgSimilarities.Average();
228        double avgMaxSimilarity = maxSimilarities.Average();
229
230        // fetch results collection
231        ResultCollection results;
232        if (!ResultsParameter.ActualValue.ContainsKey(DiversityResultName)) {
233          results = new ResultCollection();
234          ResultsParameter.ActualValue.Add(new Result(DiversityResultName, results));
235        } else {
236          results = (ResultCollection)ResultsParameter.ActualValue[DiversityResultName].Value;
237        }
238
239        // store similarities
240        HeatMap similaritiesHeatMap = new HeatMap(similarities, "Solution Similarities", 0.0, 1.0);
241        if (!results.ContainsKey("Solution Similarities"))
242          results.Add(new Result("Solution Similarities", similaritiesHeatMap));
243        else
244          results["Solution Similarities"].Value = similaritiesHeatMap;
245
246        // store similarities history
247        if (storeHistory) {
248          if (!results.ContainsKey("Solution Similarities History")) {
249            HeatMapHistory history = new HeatMapHistory();
250            history.Add(similaritiesHeatMap);
251            results.Add(new Result("Solution Similarities History", history));
252          } else {
253            ((HeatMapHistory)results["Solution Similarities History"].Value).Add(similaritiesHeatMap);
254          }
255        }
256
257        // store average minimum, average and maximum similarity
258        if (!results.ContainsKey("Average Minimum Solution Similarity"))
259          results.Add(new Result("Average Minimum Solution Similarity", new DoubleValue(avgMinSimilarity)));
260        else
261          ((DoubleValue)results["Average Minimum Solution Similarity"].Value).Value = avgMinSimilarity;
262
263        if (!results.ContainsKey("Average Average Solution Similarity"))
264          results.Add(new Result("Average Average Solution Similarity", new DoubleValue(avgAvgSimilarity)));
265        else
266          ((DoubleValue)results["Average Average Solution Similarity"].Value).Value = avgAvgSimilarity;
267
268        if (!results.ContainsKey("Average Maximum Solution Similarity"))
269          results.Add(new Result("Average Maximum Solution Similarity", new DoubleValue(avgMaxSimilarity)));
270        else
271          ((DoubleValue)results["Average Maximum Solution Similarity"].Value).Value = avgMaxSimilarity;
272
273        // store average minimum, average and maximum solution similarity data table
274        DataTable minAvgMaxSimilarityDataTable;
275        if (!results.ContainsKey("Average Minimum/Average/Maximum Solution Similarity")) {
276          minAvgMaxSimilarityDataTable = new DataTable("Average Minimum/Average/Maximum Solution Similarity");
277          minAvgMaxSimilarityDataTable.VisualProperties.XAxisTitle = "Iteration";
278          minAvgMaxSimilarityDataTable.VisualProperties.YAxisTitle = "Solution Similarity";
279          minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Minimum Solution Similarity", null));
280          minAvgMaxSimilarityDataTable.Rows["Average Minimum Solution Similarity"].VisualProperties.StartIndexZero = true;
281          minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Average Solution Similarity", null));
282          minAvgMaxSimilarityDataTable.Rows["Average Average Solution Similarity"].VisualProperties.StartIndexZero = true;
283          minAvgMaxSimilarityDataTable.Rows.Add(new DataRow("Average Maximum Solution Similarity", null));
284          minAvgMaxSimilarityDataTable.Rows["Average Maximum Solution Similarity"].VisualProperties.StartIndexZero = true;
285          results.Add(new Result("Average Minimum/Average/Maximum Solution Similarity", minAvgMaxSimilarityDataTable));
286        } else {
287          minAvgMaxSimilarityDataTable = (DataTable)results["Average Minimum/Average/Maximum Solution Similarity"].Value;
288        }
289        minAvgMaxSimilarityDataTable.Rows["Average Minimum Solution Similarity"].Values.Add(avgMinSimilarity);
290        minAvgMaxSimilarityDataTable.Rows["Average Average Solution Similarity"].Values.Add(avgAvgSimilarity);
291        minAvgMaxSimilarityDataTable.Rows["Average Maximum Solution Similarity"].Values.Add(avgMaxSimilarity);
292
293        // store minimum, average, maximum similarities data table
294        DataTable minAvgMaxSimilaritiesDataTable = new DataTable("Minimum/Average/Maximum Solution Similarities");
295        minAvgMaxSimilaritiesDataTable.VisualProperties.XAxisTitle = "Solution Index";
296        minAvgMaxSimilaritiesDataTable.VisualProperties.YAxisTitle = "Solution Similarity";
297        minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Minimum Solution Similarity", null, minSimilarities));
298        minAvgMaxSimilaritiesDataTable.Rows["Minimum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
299        minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Average Solution Similarity", null, avgSimilarities));
300        minAvgMaxSimilaritiesDataTable.Rows["Average Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
301        minAvgMaxSimilaritiesDataTable.Rows.Add(new DataRow("Maximum Solution Similarity", null, maxSimilarities));
302        minAvgMaxSimilaritiesDataTable.Rows["Maximum Solution Similarity"].VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
303        if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities")) {
304          results.Add(new Result("Minimum/Average/Maximum Solution Similarities", minAvgMaxSimilaritiesDataTable));
305        } else {
306          results["Minimum/Average/Maximum Solution Similarities"].Value = minAvgMaxSimilaritiesDataTable;
307        }
308
309        // store minimum, average, maximum similarities history
310        if (storeHistory) {
311          if (!results.ContainsKey("Minimum/Average/Maximum Solution Similarities History")) {
312            DataTableHistory history = new DataTableHistory();
313            history.Add(minAvgMaxSimilaritiesDataTable);
314            results.Add(new Result("Minimum/Average/Maximum Solution Similarities History", history));
315          } else {
316            ((DataTableHistory)results["Minimum/Average/Maximum Solution Similarities History"].Value).Add(minAvgMaxSimilaritiesDataTable);
317          }
318        }
319      }
320      return base.Apply();
321    }
322  }
323}
Note: See TracBrowser for help on using the repository browser.