Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8511 was 8511, checked in by ascheibe, 12 years ago

#1886 added an option so that the scatter plot helper can now also produce data tables

File size: 9.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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Problems.TravelingSalesman;
35
36namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
37  [Item("CrossoverPerformanceAnalyzer", "An operator that analyzes the performance of crossovers.")]
38  [StorableClass]
39  public class CrossoverPerformanceAnalyzer : SingleSuccessorOperator, IAnalyzer {
40    private const string ResultsParameterName = "Results";
41    private const string GenerationsParameterName = "Generations";
42
43    #region IAnalyzer Members
44    public bool EnabledByDefault {
45      get { return true; }
46    }
47    #endregion
48
49    #region Parameter properties
50    public ILookupParameter<ResultCollection> ResultsParameter {
51      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
52    }
53    public ILookupParameter<IntValue> GenerationsParameter {
54      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
55    }
56    public ILookupParameter<ItemArray<Permutation>> ParentsParameter {
57      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Parents"]; }
58    }
59    public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
60      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
61    }
62    public ILookupParameter<Permutation> ChildParameter {
63      get { return ((LookupParameter<Permutation>)Parameters["Child"]); }
64    }
65    public ILookupParameter<DoubleValue> QualityParameter {
66      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
67    }
68    public IValueParameter<SingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
69      get { return (IValueParameter<SingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
70    }
71    #endregion
72
73    #region Properties
74    public ResultCollection Results {
75      get { return ResultsParameter.ActualValue; }
76    }
77    #endregion
78
79    [Storable]
80    private ScatterPlotHelper plotHelper, childDiversityHelper, parentDiversityHelper;
81    [Storable]
82    private DataTableHelper performanceHelper, successHelper, equalParentsHelper;
83    [Storable]
84    private int cnt = 0;
85    [Storable]
86    private int success = 0;
87    [Storable]
88    private int lastGeneration = 0;
89    [Storable]
90    private int equalParents = 0;
91    [Storable]
92    private List<double> qualityPoints = new List<double>();
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 = original.success;
100      lastGeneration = original.lastGeneration;
101      equalParents = original.equalParents;
102      qualityPoints = new List<double>(original.qualityPoints);
103      plotHelper = (ScatterPlotHelper)original.plotHelper.Clone(cloner);
104      childDiversityHelper = (ScatterPlotHelper)original.childDiversityHelper.Clone(cloner);
105      parentDiversityHelper = (ScatterPlotHelper)original.parentDiversityHelper.Clone(cloner);
106      performanceHelper = (DataTableHelper)original.performanceHelper.Clone(cloner);
107      successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
108      equalParentsHelper = (DataTableHelper)original.equalParentsHelper.Clone(cloner);
109    }
110
111    public CrossoverPerformanceAnalyzer()
112      : base() {
113      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
114      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
115
116      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
117      ParentsParameter.ActualName = "TSPTour";
118
119      Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
120      ChildParameter.ActualName = "TSPTour";
121
122      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
123      ParentsQualityParameter.ActualName = "TSPTourLength";
124
125      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
126      QualityParameter.ActualName = "TSPTourLength";
127
128      Parameters.Add(new ValueParameter<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
129
130      plotHelper = new ScatterPlotHelper(false, true);
131      childDiversityHelper = new ScatterPlotHelper(false, true);
132      parentDiversityHelper = new ScatterPlotHelper(false, true);
133      performanceHelper = new DataTableHelper();
134      successHelper = new DataTableHelper();
135      equalParentsHelper = new DataTableHelper();
136    }
137
138    public override IDeepCloneable Clone(Cloner cloner) {
139      return new CrossoverPerformanceAnalyzer(this, cloner);
140    }
141
142    public override IOperation Apply() {
143      SimilarityCalculatorParameter.Value.QualityVariableName = "TSPTourLength";
144      SimilarityCalculatorParameter.Value.SolutionVariableName = "TSPTour";
145
146      plotHelper.InitializePlot(Results, "Crossover Performance", "Solution Index", "Absolut Quality Difference");
147      childDiversityHelper.InitializePlot(Results, "Child Diversity", "Solution Index", "Diversity");
148      parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
149
150      performanceHelper.InitializeChart(Results, "Average Crossover Performance", "Average Crossover Performance per Generation");
151      successHelper.InitializeChart(Results, "Successfull Crossovers", "Successfull Crossovers per Generation");
152      equalParentsHelper.InitializeChart(Results, "Number of equal parents", "Absolut number of equal parents");
153
154      Point2D<double> qualityPoint, diversityPointChild, diversityPointParent;
155      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
156      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
157      var child = ChildParameter.ActualValue;
158      var parent1 = ParentsParameter.ActualValue.First();
159      var parent2 = ParentsParameter.ActualValue.Last();
160      var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
161      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
162
163
164      diversityPointParent = new Point2D<double>(cnt, parentDiversity);
165      double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
166      if (qualityParent1 > qualityParent2) {
167        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
168      } else {
169        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
170      }
171
172      qualityPoint = new Point2D<double>(cnt++, worseQuality - QualityParameter.ActualValue.Value);
173      if ((worseQuality - QualityParameter.ActualValue.Value) > 0) {
174        success++;
175      }
176      qualityPoints.Add(qualityPoint.Y);
177
178      if (TSPSimilarityCalculator.CalculateSimilarity(parent1, parent2) == 1.0) {
179        equalParents++;
180      }
181
182      if (GenerationsParameter.ActualValue.Value != 0) {
183        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
184          double avg = qualityPoints.Average();
185
186          performanceHelper.AddPoint(avg);
187          successHelper.AddPoint(success);
188          equalParentsHelper.AddPoint(equalParents);
189
190          Reset();
191        }
192
193        plotHelper.AddPoint(curGenStr, qualityPoint);
194        childDiversityHelper.AddPoint(curGenStr, diversityPointChild);
195        parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
196      } else {
197        Reset();
198      }
199
200      return base.Apply();
201    }
202
203    private void Reset() {
204      cnt = 0;
205      success = 0;
206      lastGeneration = GenerationsParameter.ActualValue.Value;
207      qualityPoints.Clear();
208      equalParents = 0;
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.