Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 removed static method calls to similarity calculators

File size: 9.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.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.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 : SingleSuccessorOperator, IAnalyzer {
38    private const string ResultsParameterName = "Results";
39    private const string GenerationsParameterName = "Generations";
40
41    #region IAnalyzer Members
42    public bool EnabledByDefault {
43      get { return true; }
44    }
45    #endregion
46
47    #region Parameter properties
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<Permutation>> ParentsParameter {
55      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Parents"]; }
56    }
57    public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
58      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
59    }
60    public ILookupParameter<Permutation> ChildParameter {
61      get { return ((LookupParameter<Permutation>)Parameters["Child"]); }
62    }
63    public ILookupParameter<DoubleValue> QualityParameter {
64      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
65    }
66    public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
67      get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
68    }
69    public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
70      get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
71    }
72    #endregion
73
74    #region Properties
75    public ResultCollection Results {
76      get { return ResultsParameter.ActualValue; }
77    }
78    #endregion
79
80    [Storable]
81    private ScatterPlotHelper plotHelper, childDiversityHelper, parentDiversityHelper;
82    [Storable]
83    private DataTableHelper performanceHelper, successHelper, equalParentsHelper;
84    [Storable]
85    private int cnt = 0;
86    [Storable]
87    private int success = 0;
88    [Storable]
89    private int lastGeneration = 0;
90    [Storable]
91    private int equalParents = 0;
92    [Storable]
93    private List<double> qualityPoints = new List<double>();
94
95    [StorableConstructor]
96    private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
97    private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner)
98      : base(original, cloner) {
99      cnt = original.cnt;
100      success = original.success;
101      lastGeneration = original.lastGeneration;
102      equalParents = original.equalParents;
103      qualityPoints = new List<double>(original.qualityPoints);
104      plotHelper = (ScatterPlotHelper)original.plotHelper.Clone(cloner);
105      childDiversityHelper = (ScatterPlotHelper)original.childDiversityHelper.Clone(cloner);
106      parentDiversityHelper = (ScatterPlotHelper)original.parentDiversityHelper.Clone(cloner);
107      performanceHelper = (DataTableHelper)original.performanceHelper.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
132      plotHelper = new ScatterPlotHelper(false, true);
133      childDiversityHelper = new ScatterPlotHelper(false, true);
134      parentDiversityHelper = new ScatterPlotHelper(false, true);
135      performanceHelper = new DataTableHelper();
136      successHelper = new DataTableHelper();
137      equalParentsHelper = new DataTableHelper();
138    }
139
140    public override IDeepCloneable Clone(Cloner cloner) {
141      return new CrossoverPerformanceAnalyzer(this, cloner);
142    }
143
144    public override IOperation Apply() {
145      if (SimilarityCalculatorParameter.Value == null) {
146        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
147      }
148
149      plotHelper.InitializePlot(Results, "Crossover Performance", "Solution Index", "Absolut Quality Difference");
150      childDiversityHelper.InitializePlot(Results, "Child Diversity", "Solution Index", "Diversity");
151      parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
152
153      performanceHelper.InitializeChart(Results, "Average Crossover Performance", "Average Crossover Performance per Generation");
154      successHelper.InitializeChart(Results, "Successfull Crossovers", "Successfull Crossovers per Generation");
155      equalParentsHelper.InitializeChart(Results, "Number of equal parents", "Absolut number of equal parents");
156
157      Point2D<double> qualityPoint, diversityPointChild, diversityPointParent;
158      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
159      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
160      var child = ChildParameter.ActualValue;
161      var parent1 = ParentsParameter.ActualValue.First();
162      var parent2 = ParentsParameter.ActualValue.Last();
163      var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
164      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
165
166
167      diversityPointParent = new Point2D<double>(cnt, parentDiversity);
168      double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
169      if (qualityParent1 > qualityParent2) {
170        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
171      } else {
172        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
173      }
174
175      qualityPoint = new Point2D<double>(cnt++, worseQuality - QualityParameter.ActualValue.Value);
176      if ((worseQuality - QualityParameter.ActualValue.Value) > 0) {
177        success++;
178      }
179      qualityPoints.Add(qualityPoint.Y);
180
181      if (parentDiversity == 1.0) {
182        equalParents++;
183      }
184
185      if (GenerationsParameter.ActualValue.Value != 0) {
186        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
187          double avg = qualityPoints.Average();
188
189          performanceHelper.AddPoint(avg);
190          successHelper.AddPoint(success);
191          equalParentsHelper.AddPoint(equalParents);
192
193          Reset();
194        }
195
196        plotHelper.AddPoint(curGenStr, qualityPoint);
197        childDiversityHelper.AddPoint(curGenStr, diversityPointChild);
198        parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
199      } else {
200        Reset();
201      }
202
203      return base.Apply();
204    }
205
206    private void Reset() {
207      cnt = 0;
208      success = 0;
209      lastGeneration = GenerationsParameter.ActualValue.Value;
210      qualityPoints.Clear();
211      equalParents = 0;
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.