Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 improved mutation analyzer

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