Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added an analyzer for comparing the current solution to the rest of the population

File size: 15.7 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.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    #endregion
70
71    #region Properties
72    public ResultCollection Results {
73      get { return ResultsParameter.ActualValue; }
74    }
75
76    [Storable]
77    private ScatterPlotHelper worseParentCrossoverPerformancePlot, betterParentCrossoverPerformancePlot, childDiversityToWorseParentHelper, childDiversityToBetterParentHelper, parentDiversityHelper, parentQualityHelper, unwantedMutationsHelper;
78    [Storable]
79    private DataTableHelper successHelper, equalParentsHelper;
80    [Storable]
81    private int cnt = 0;
82    [Storable]
83    private int[] success;
84    [Storable]
85    private int lastGeneration = 0;
86    [Storable]
87    private int equalParents = 0;
88    #endregion
89
90
91    [StorableConstructor]
92    private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
93    private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner)
94      : base(original, cloner) {
95      cnt = original.cnt;
96      success = (int[])original.success.Clone();
97      lastGeneration = original.lastGeneration;
98      equalParents = original.equalParents;
99      worseParentCrossoverPerformancePlot = (ScatterPlotHelper)original.worseParentCrossoverPerformancePlot.Clone(cloner);
100      betterParentCrossoverPerformancePlot = (ScatterPlotHelper)original.betterParentCrossoverPerformancePlot.Clone(cloner);
101      childDiversityToWorseParentHelper = (ScatterPlotHelper)original.childDiversityToWorseParentHelper.Clone(cloner);
102      childDiversityToBetterParentHelper = (ScatterPlotHelper)original.childDiversityToBetterParentHelper.Clone(cloner);
103      parentDiversityHelper = (ScatterPlotHelper)original.parentDiversityHelper.Clone(cloner);
104      parentQualityHelper = (ScatterPlotHelper)original.parentQualityHelper.Clone(cloner);
105      unwantedMutationsHelper = (ScatterPlotHelper)original.unwantedMutationsHelper.Clone(cloner);
106      successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
107      equalParentsHelper = (DataTableHelper)original.equalParentsHelper.Clone(cloner);
108    }
109
110    public CrossoverPerformanceAnalyzer()
111      : base() {
112      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
113      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
114
115      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
116      ParentsQualityParameter.ActualName = "TSPTourLength";
117
118      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
119      QualityParameter.ActualName = "TSPTourLength";
120
121      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
122      Parameters.Add(new ValueParameter<IUnwantedMutationAnalyzer>("UnwantedMutationAnalyzer"));
123      Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
124      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
125      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."));
126
127
128      worseParentCrossoverPerformancePlot = new ScatterPlotHelper(false, true);
129      betterParentCrossoverPerformancePlot = new ScatterPlotHelper(false, true);
130      childDiversityToWorseParentHelper = new ScatterPlotHelper(false, true);
131      childDiversityToBetterParentHelper = new ScatterPlotHelper(false, true);
132      unwantedMutationsHelper = new ScatterPlotHelper(false, true);
133      parentDiversityHelper = new ScatterPlotHelper(false, true);
134      parentQualityHelper = new ScatterPlotHelper(false, true);
135      successHelper = new DataTableHelper();
136      equalParentsHelper = new DataTableHelper();
137    }
138
139    public override IDeepCloneable Clone(Cloner cloner) {
140      return new CrossoverPerformanceAnalyzer(this, cloner);
141    }
142
143    protected override void InitializeAction() {
144      if (SimilarityCalculatorParameter.Value == null) {
145        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
146      }
147
148      if (ComparisonFactorParameter.ActualValue == null) {
149        ComparisonFactorParameter.Value = new ItemCollection<DoubleValue>();
150        ComparisonFactorParameter.Value.Add(new DoubleValue(0));
151        ComparisonFactorParameter.Value.Add(new DoubleValue(0.5));
152        ComparisonFactorParameter.Value.Add(new DoubleValue(1));
153        ComparisonFactorParameter.Value.Add(new DoubleValue(1.5));
154      }
155
156      if (success == null) {
157        success = new int[ComparisonFactorParameter.Value.Count];
158      }
159
160      worseParentCrossoverPerformancePlot.InitializePlot(Results, "Crossover Performance compared to worse parent", "Solution Index", "Absolut Quality Difference");
161      betterParentCrossoverPerformancePlot.InitializePlot(Results, "Crossover Performance compared to better parent", "Solution Index", "Absolut Quality Difference");
162      childDiversityToWorseParentHelper.InitializePlot(Results, "Child Diversity to Worse Parent", "Solution Index", "Diversity");
163      childDiversityToBetterParentHelper.InitializePlot(Results, "Child Diversity to Better Parent", "Solution Index", "Diversity");
164      parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
165      parentQualityHelper.InitializePlot(Results, "Parent Quality Difference", "Solution Index", "Absolut Quality Difference");
166      equalParentsHelper.InitializeChart(Results, "Number of equal parents", new string[] { "Absolut number of equal parents" });
167      unwantedMutationsHelper.InitializePlot(Results, "Unwanted Mutations", "Solution Index", "Unwanted Mutation");
168
169      List<string> successfullCXRowNames = new List<string>();
170      foreach (var value in ComparisonFactorParameter.Value) {
171        successfullCXRowNames.Add(SuccessfullCrossoversRowName + value.Value.ToString());
172      }
173      successHelper.InitializeChart(Results, "Successfull Crossovers", successfullCXRowNames.ToArray());
174      Reset();
175    }
176
177    public override IOperation Apply() {
178      Initialize();
179
180      Point2D<double> worseQualityPoint, betterQualityPoint, childDiversityToWorseParent, childDiversityToBetterParent, diversityPointParent, qualityPointParent, unwantedMutationPoint;
181      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
182      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
183      var qualityChild = QualityParameter.ActualValue.Value;
184
185      var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
186      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
187
188      diversityPointParent = new Point2D<double>(cnt, parentDiversity);
189      qualityPointParent = new Point2D<double>(cnt, Math.Abs(qualityParent1 - qualityParent2));
190      if (UnwantedMutationAnalyzerParameter.Value != null) {
191        unwantedMutationPoint = new Point2D<double>(cnt, UnwantedMutationAnalyzerParameter.Value.AnalyzeUnwantedMutations(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last(), ExecutionContext.Scope));
192      } else {
193        //TODO: this is not a good solution
194        unwantedMutationPoint = new Point2D<double>(cnt, 0.0);
195      }
196
197      double worseQuality, betterQuality;
198
199      if (qualityParent1 > qualityParent2) {
200        if (MaximizationParameter.ActualValue.Value) {
201          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
202          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
203          worseQuality = qualityParent2;
204          betterQuality = qualityParent1;
205        } else {
206          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
207          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
208          worseQuality = qualityParent1;
209          betterQuality = qualityParent2;
210        }
211      } else {
212        if (MaximizationParameter.ActualValue.Value) {
213          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
214          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
215          worseQuality = qualityParent1;
216          betterQuality = qualityParent2;
217        } else {
218          childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
219          childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
220          worseQuality = qualityParent2;
221          betterQuality = qualityParent1;
222        }
223      }
224
225      worseQualityPoint = new Point2D<double>(cnt, Math.Abs(worseQuality - qualityChild));
226      betterQualityPoint = new Point2D<double>(cnt++, Math.Abs(betterQuality - qualityChild));
227
228      if (GenerationsParameter.ActualValue.Value == lastGeneration) {
229        CountSuccess(qualityChild, qualityParent1, qualityParent2);
230
231        if (parentDiversity == 1.0) {
232          equalParents++;
233        }
234      }
235
236      if (GenerationsParameter.ActualValue.Value != 0) {
237        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
238          equalParentsHelper.AddPoint(equalParents);
239
240          for (int i = 0; i < ComparisonFactorParameter.Value.Count; i++) {
241            successHelper.AddPoint(SuccessfullCrossoversRowName + ComparisonFactorParameter.Value.ElementAt(i).Value.ToString(), success[i]);
242          }
243
244          Reset();
245
246          CountSuccess(qualityChild, qualityParent1, qualityParent2);
247          if (parentDiversity == 1.0) {
248            equalParents++;
249          }
250        }
251
252        betterParentCrossoverPerformancePlot.AddPoint(curGenStr, betterQualityPoint);
253        worseParentCrossoverPerformancePlot.AddPoint(curGenStr, worseQualityPoint);
254        childDiversityToWorseParentHelper.AddPoint(curGenStr, childDiversityToWorseParent);
255        childDiversityToBetterParentHelper.AddPoint(curGenStr, childDiversityToBetterParent);
256        parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
257        parentQualityHelper.AddPoint(curGenStr, qualityPointParent);
258        unwantedMutationsHelper.AddPoint(curGenStr, unwantedMutationPoint);
259      }
260
261      return base.Apply();
262    }
263
264    private void CountSuccess(double qualityChild, double qualityParent1, double qualityParent2) {
265      //track successfull cx
266      var compFactsArray = ComparisonFactorParameter.Value.ToArray();
267      for (int i = 0; i < compFactsArray.Length; i++) {
268        if (WeightedParentsQualityComparer.Compare(qualityChild, qualityParent1, qualityParent2, compFactsArray[i].Value, MaximizationParameter.ActualValue.Value)) {
269          success[i]++;
270        }
271      }
272    }
273
274    private void Reset() {
275      cnt = 0;
276      success = new int[ComparisonFactorParameter.Value.Count];
277      lastGeneration = GenerationsParameter.ActualValue.Value;
278      equalParents = 0;
279    }
280
281    public override void ClearState() {
282      betterParentCrossoverPerformancePlot.CleanUp();
283      worseParentCrossoverPerformancePlot.CleanUp();
284      childDiversityToWorseParentHelper.CleanUp();
285      childDiversityToBetterParentHelper.CleanUp();
286      parentDiversityHelper.CleanUp();
287      parentQualityHelper.CleanUp();
288      unwantedMutationsHelper.CleanUp();
289    }
290  }
291}
Note: See TracBrowser for help on using the repository browser.