Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/CrossoverPerformanceAnalyzer.cs @ 9844

Last change on this file since 9844 was 9789, checked in by ascheibe, 11 years ago

#1886 fixed alglib reference and updated year and version information

File size: 17.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
34  [Item("CrossoverPerformanceAnalyzer", "An operator that analyzes the performance of crossovers.")]
35  [StorableClass]
36  public class CrossoverPerformanceAnalyzer : InitializableOperator, IStatefulItem {
37    private const string ResultsParameterName = "Results";
38    private const string GenerationsParameterName = "Generations";
39    private const string SuccessfullCrossoversRowName = "Successfull Crossovers per Generation with CompFact ";
40
41    #region Parameter properties
42    public IValueLookupParameter<BoolValue> MaximizationParameter {
43      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
44    }
45    public IValueLookupParameter<ItemCollection<DoubleValue>> ComparisonFactorParameter {
46      get { return (IValueLookupParameter<ItemCollection<DoubleValue>>)Parameters["ComparisonFactor"]; }
47    }
48    public ILookupParameter<ResultCollection> ResultsParameter {
49      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
50    }
51    public ILookupParameter<IntValue> GenerationsParameter {
52      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
53    }
54    public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
55      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
56    }
57    public ILookupParameter<DoubleValue> QualityParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
59    }
60    public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
61      get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
62    }
63    public IValueParameter<IUnwantedMutationAnalyzer> UnwantedMutationAnalyzerParameter {
64      get { return (IValueParameter<IUnwantedMutationAnalyzer>)Parameters["UnwantedMutationAnalyzer"]; }
65    }
66    public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
67      get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
68    }
69    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
70      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
71    }
72    public ILookupParameter<DoubleValue> WorstKnownQualityParameter {
73      get { return (ILookupParameter<DoubleValue>)Parameters["WorstKnownQuality"]; }
74    }
75    #endregion
76
77    #region Properties
78    public ResultCollection Results {
79      get { return ResultsParameter.ActualValue; }
80    }
81
82    [Storable]
83    private ScatterPlotHelper worseParentCrossoverPerformancePlot, betterParentCrossoverPerformancePlot, childDiversityToWorseParentHelper, childDiversityToBetterParentHelper, parentDiversityHelper, parentQualityHelper, unwantedMutationsHelper;
84    [Storable]
85    private DataTableHelper successHelper, equalParentsHelper;
86    [Storable]
87    private int cnt = 0;
88    [Storable]
89    private int[] success;
90    [Storable]
91    private int lastGeneration = 0;
92    [Storable]
93    private int equalParents = 0;
94    [Storable]
95    private bool scalingFinished = false;
96    #endregion
97
98
99    [StorableConstructor]
100    private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
101    private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner)
102      : base(original, cloner) {
103      cnt = original.cnt;
104      success = (int[])original.success.Clone();
105      lastGeneration = original.lastGeneration;
106      equalParents = original.equalParents;
107      worseParentCrossoverPerformancePlot = (ScatterPlotHelper)original.worseParentCrossoverPerformancePlot.Clone(cloner);
108      betterParentCrossoverPerformancePlot = (ScatterPlotHelper)original.betterParentCrossoverPerformancePlot.Clone(cloner);
109      childDiversityToWorseParentHelper = (ScatterPlotHelper)original.childDiversityToWorseParentHelper.Clone(cloner);
110      childDiversityToBetterParentHelper = (ScatterPlotHelper)original.childDiversityToBetterParentHelper.Clone(cloner);
111      parentDiversityHelper = (ScatterPlotHelper)original.parentDiversityHelper.Clone(cloner);
112      parentQualityHelper = (ScatterPlotHelper)original.parentQualityHelper.Clone(cloner);
113      unwantedMutationsHelper = (ScatterPlotHelper)original.unwantedMutationsHelper.Clone(cloner);
114      successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
115      equalParentsHelper = (DataTableHelper)original.equalParentsHelper.Clone(cloner);
116      scalingFinished = original.scalingFinished;
117    }
118
119    public CrossoverPerformanceAnalyzer()
120      : base() {
121      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
122      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
123
124      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
125      ParentsQualityParameter.ActualName = "TSPTourLength";
126
127      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
128      QualityParameter.ActualName = "TSPTourLength";
129
130      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
131      Parameters.Add(new ValueParameter<IUnwantedMutationAnalyzer>("UnwantedMutationAnalyzer"));
132      Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
133      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
134      Parameters.Add(new ValueLookupParameter<ItemCollection<DoubleValue>>("ComparisonFactor", "Determines if the quality should be compared to the better parent (1.0), to the worse (0.0) or to any linearly interpolated value between them."));
135      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
136      Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
137
138      worseParentCrossoverPerformancePlot = new ScatterPlotHelper(false, true, true, true);
139      betterParentCrossoverPerformancePlot = new ScatterPlotHelper(false, true, true, true);
140      childDiversityToWorseParentHelper = new ScatterPlotHelper(false, true, false, true);
141      childDiversityToBetterParentHelper = new ScatterPlotHelper(false, true, false, true);
142      unwantedMutationsHelper = new ScatterPlotHelper(false, true, false, true);
143      parentDiversityHelper = new ScatterPlotHelper(false, true, false, true);
144      parentQualityHelper = new ScatterPlotHelper(false, true, true, true);
145      successHelper = new DataTableHelper();
146      equalParentsHelper = new DataTableHelper();
147    }
148
149    public override IDeepCloneable Clone(Cloner cloner) {
150      return new CrossoverPerformanceAnalyzer(this, cloner);
151    }
152
153    protected override void InitializeAction() {
154      if (SimilarityCalculatorParameter.Value == null) {
155        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
156      }
157
158      if (ComparisonFactorParameter.ActualValue == null) {
159        ComparisonFactorParameter.Value = new ItemCollection<DoubleValue>();
160        ComparisonFactorParameter.Value.Add(new DoubleValue(0));
161        ComparisonFactorParameter.Value.Add(new DoubleValue(0.5));
162        ComparisonFactorParameter.Value.Add(new DoubleValue(1));
163        ComparisonFactorParameter.Value.Add(new DoubleValue(1.5));
164      }
165
166      if (success == null) {
167        success = new int[ComparisonFactorParameter.Value.Count];
168      }
169
170      worseParentCrossoverPerformancePlot.InitializePlot(Results, "Crossover Performance compared to worse parent", "Solution Index", "Absolut Quality Difference");
171      betterParentCrossoverPerformancePlot.InitializePlot(Results, "Crossover Performance compared to better parent", "Solution Index", "Absolut Quality Difference");
172      childDiversityToWorseParentHelper.InitializePlot(Results, "Child Diversity to Worse Parent", "Solution Index", "Diversity");
173      childDiversityToBetterParentHelper.InitializePlot(Results, "Child Diversity to Better Parent", "Solution Index", "Diversity");
174      parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
175      parentQualityHelper.InitializePlot(Results, "Parent Quality Difference", "Solution Index", "Absolut Quality Difference");
176      equalParentsHelper.InitializeChart(Results, "Number of equal parents", new string[] { "Absolut number of equal parents" });
177      unwantedMutationsHelper.InitializePlot(Results, "Unwanted Mutations", "Solution Index", "Unwanted Mutation");
178
179      List<string> successfullCXRowNames = new List<string>();
180      foreach (var value in ComparisonFactorParameter.Value) {
181        successfullCXRowNames.Add(SuccessfullCrossoversRowName + value.Value.ToString());
182      }
183      successHelper.InitializeChart(Results, "Successfull Crossovers", successfullCXRowNames.ToArray());
184
185      Reset();
186    }
187
188    public override IOperation Apply() {
189      Initialize();
190
191      Point2D<double> worseQualityPoint, betterQualityPoint, childDiversityToWorseParent, childDiversityToBetterParent, diversityPointParent, qualityPointParent, unwantedMutationPoint;
192      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
193      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
194      var qualityChild = QualityParameter.ActualValue.Value;
195
196      var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
197      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
198
199      diversityPointParent = new Point2D<double>(cnt, parentDiversity);
200      qualityPointParent = new Point2D<double>(cnt, Math.Abs(qualityParent1 - qualityParent2));
201      if (UnwantedMutationAnalyzerParameter.Value != null) {
202        unwantedMutationPoint = new Point2D<double>(cnt, UnwantedMutationAnalyzerParameter.Value.AnalyzeUnwantedMutations(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last(), ExecutionContext.Scope));
203      } else {
204        //TODO: this is not a good solution
205        unwantedMutationPoint = new Point2D<double>(cnt, 0.0);
206      }
207
208      double worseQuality, betterQuality;
209
210      if (qualityParent1 > qualityParent2) {
211        if (MaximizationParameter.ActualValue.Value) {
212          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
213          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
214          worseQuality = qualityParent2;
215          betterQuality = qualityParent1;
216        } else {
217          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
218          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
219          worseQuality = qualityParent1;
220          betterQuality = qualityParent2;
221        }
222      } else {
223        if (MaximizationParameter.ActualValue.Value) {
224          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
225          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
226          worseQuality = qualityParent1;
227          betterQuality = qualityParent2;
228        } else {
229          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
230          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
231          worseQuality = qualityParent2;
232          betterQuality = qualityParent1;
233        }
234      }
235
236      worseQualityPoint = new Point2D<double>(cnt, worseQuality - qualityChild);
237      betterQualityPoint = new Point2D<double>(cnt++, betterQuality - qualityChild);
238
239      if (GenerationsParameter.ActualValue.Value == lastGeneration) {
240        CountSuccess(qualityChild, qualityParent1, qualityParent2);
241
242        if (parentDiversity == 1.0) {
243          equalParents++;
244        }
245      }
246
247      if (WorstKnownQualityParameter.ActualValue != null && !scalingFinished) {
248        scalingFinished = true;
249        double bkQuality = BestKnownQualityParameter.ActualValue.Value;
250        double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
251
252        if (MaximizationParameter.ActualValue.Value) {
253          if (worseParentCrossoverPerformancePlot.Max == double.MinValue) {
254            worseParentCrossoverPerformancePlot.Max = bkQuality - wkQuality;
255            betterParentCrossoverPerformancePlot.Max = bkQuality - wkQuality;
256            parentQualityHelper.Max = bkQuality - wkQuality;
257            worseParentCrossoverPerformancePlot.Min = 0;
258            betterParentCrossoverPerformancePlot.Min = 0;
259            parentQualityHelper.Min = 0;
260          }
261        } else {
262          if (worseParentCrossoverPerformancePlot.Min == double.MaxValue) {
263            worseParentCrossoverPerformancePlot.Max = wkQuality - bkQuality;
264            betterParentCrossoverPerformancePlot.Max = wkQuality - bkQuality;
265            parentQualityHelper.Max = wkQuality - bkQuality;
266            worseParentCrossoverPerformancePlot.Min = 0;
267            betterParentCrossoverPerformancePlot.Min = 0;
268            parentQualityHelper.Min = 0;
269          }
270        }
271      }
272
273      if (GenerationsParameter.ActualValue.Value != 0) {
274        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
275          equalParentsHelper.AddPoint(equalParents);
276
277          for (int i = 0; i < ComparisonFactorParameter.Value.Count; i++) {
278            successHelper.AddPoint(SuccessfullCrossoversRowName + ComparisonFactorParameter.Value.ElementAt(i).Value.ToString(), (double)success[i] / (cnt - 1));
279          }
280
281          Reset();
282
283          CountSuccess(qualityChild, qualityParent1, qualityParent2);
284          if (parentDiversity == 1.0) {
285            equalParents++;
286          }
287        }
288
289        betterParentCrossoverPerformancePlot.AddPoint(curGenStr, betterQualityPoint);
290        worseParentCrossoverPerformancePlot.AddPoint(curGenStr, worseQualityPoint);
291        childDiversityToWorseParentHelper.AddPoint(curGenStr, childDiversityToWorseParent);
292        childDiversityToBetterParentHelper.AddPoint(curGenStr, childDiversityToBetterParent);
293        parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
294        parentQualityHelper.AddPoint(curGenStr, qualityPointParent);
295        unwantedMutationsHelper.AddPoint(curGenStr, unwantedMutationPoint);
296      }
297
298      return base.Apply();
299    }
300
301    private void CountSuccess(double qualityChild, double qualityParent1, double qualityParent2) {
302      //track successfull cx
303      var compFactsArray = ComparisonFactorParameter.Value.ToArray();
304      for (int i = 0; i < compFactsArray.Length; i++) {
305        if (WeightedParentsQualityComparer.Compare(qualityChild, qualityParent1, qualityParent2, compFactsArray[i].Value, MaximizationParameter.ActualValue.Value)) {
306          success[i]++;
307        }
308      }
309    }
310
311    private void Reset() {
312      cnt = 0;
313      success = new int[ComparisonFactorParameter.Value.Count];
314      lastGeneration = GenerationsParameter.ActualValue.Value;
315      equalParents = 0;
316    }
317
318    public override void ClearState() {
319      betterParentCrossoverPerformancePlot.CleanUp();
320      worseParentCrossoverPerformancePlot.CleanUp();
321      childDiversityToWorseParentHelper.CleanUp();
322      childDiversityToBetterParentHelper.CleanUp();
323      parentDiversityHelper.CleanUp();
324      parentQualityHelper.CleanUp();
325      unwantedMutationsHelper.CleanUp();
326      successHelper.CleanUpAndCompressData();
327      equalParentsHelper.CleanUpAndCompressData();
328      scalingFinished = false;
329    }
330  }
331}
Note: See TracBrowser for help on using the repository browser.