Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 fixed cloning and made helper classes items

File size: 9.3 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    private ScatterPlotHelper plotHelper, childDiversityHelper, parentDiversityHelper;
80    private DataTableHelper performanceHelper, successHelper, equalParentsHelper;
81    private int cnt = 0;
82    private int success = 0;
83    private int lastGeneration = 0;
84    private int equalParents = 0;
85    private List<double> qualityPoints = new List<double>();
86
87    [StorableConstructor]
88    private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
89    private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner)
90      : base(original, cloner) {
91      cnt = original.cnt;
92      success = original.success;
93      lastGeneration = original.lastGeneration;
94      equalParents = original.equalParents;
95      qualityPoints = new List<double>(original.qualityPoints);
96      plotHelper = (ScatterPlotHelper)original.plotHelper.Clone(cloner);
97      childDiversityHelper = (ScatterPlotHelper)original.childDiversityHelper.Clone(cloner);
98      parentDiversityHelper = (ScatterPlotHelper)original.parentDiversityHelper.Clone(cloner);
99      performanceHelper = (DataTableHelper)original.performanceHelper.Clone(cloner);
100      successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
101      equalParentsHelper = (DataTableHelper)original.equalParentsHelper.Clone(cloner);
102    }
103
104    public CrossoverPerformanceAnalyzer()
105      : base() {
106      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
107      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
108
109      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
110      ParentsParameter.ActualName = "TSPTour";
111
112      Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
113      ChildParameter.ActualName = "TSPTour";
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<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
122
123      plotHelper = new ScatterPlotHelper();
124      childDiversityHelper = new ScatterPlotHelper();
125      parentDiversityHelper = new ScatterPlotHelper();
126      performanceHelper = new DataTableHelper();
127      successHelper = new DataTableHelper();
128      equalParentsHelper = new DataTableHelper();
129    }
130
131    public override IDeepCloneable Clone(Cloner cloner) {
132      return new CrossoverPerformanceAnalyzer(this, cloner);
133    }
134
135    public override IOperation Apply() {
136      SimilarityCalculatorParameter.Value.QualityVariableName = "TSPTourLength";
137      SimilarityCalculatorParameter.Value.SolutionVariableName = "TSPTour";
138
139      plotHelper.InitializePlot(Results, "Crossover Performance", "Solution Index", "Absolut Quality Difference");
140      childDiversityHelper.InitializePlot(Results, "Child Diversity", "Solution Index", "Diversity");
141      parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
142
143      performanceHelper.InitializeChart(Results, "Average Crossover Performance", "Average Crossover Performance per Generation");
144      successHelper.InitializeChart(Results, "Successfull Crossovers", "Successfull Crossovers per Generation");
145      equalParentsHelper.InitializeChart(Results, "Number of equal parents", "Absolut number of equal parents");
146
147      Point2D<double> qualityPoint, diversityPointChild, diversityPointParent;
148      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
149      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
150      var child = ChildParameter.ActualValue;
151      var parent1 = ParentsParameter.ActualValue.First();
152      var parent2 = ParentsParameter.ActualValue.Last();
153      var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
154      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
155
156
157      diversityPointParent = new Point2D<double>(cnt, parentDiversity);
158      double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
159      if (qualityParent1 > qualityParent2) {
160        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
161      } else {
162        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
163      }
164
165      qualityPoint = new Point2D<double>(cnt++, worseQuality - QualityParameter.ActualValue.Value);
166      if ((worseQuality - QualityParameter.ActualValue.Value) > 0) {
167        success++;
168      }
169      qualityPoints.Add(qualityPoint.Y);
170
171      if (TSPSimilarityCalculator.CalculateSimilarity(parent1, parent2) == 1.0) {
172        equalParents++;
173      }
174
175      if (GenerationsParameter.ActualValue.Value != 0) {
176        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
177          double avg = qualityPoints.Average();
178
179          performanceHelper.AddPoint(avg);
180          successHelper.AddPoint(success);
181          equalParentsHelper.AddPoint(equalParents);
182
183          Reset();
184        }
185
186        plotHelper.AddPoint(curGenStr, qualityPoint);
187        childDiversityHelper.AddPoint(curGenStr, diversityPointChild);
188        parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
189      } else {
190        Reset();
191      }
192
193      return base.Apply();
194    }
195
196    private void Reset() {
197      cnt = 0;
198      success = 0;
199      lastGeneration = GenerationsParameter.ActualValue.Value;
200      qualityPoints.Clear();
201      equalParents = 0;
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.