Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8382 was 8382, checked in by ascheibe, 10 years ago

#1886 added more plots for crossover performance analysis

File size: 10.6 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;
34
35namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
36  [Item("CrossoverPerformanceAnalyzer", "An operator that analyzes the performance of crossovers.")]
37  [StorableClass]
38  public class CrossoverPerformanceAnalyzer : SingleSuccessorOperator, IAnalyzer {
39    private const string ResultsParameterName = "Results";
40    private const string GenerationsParameterName = "Generations";
41
42    #region IAnalyzer Members
43    public bool EnabledByDefault {
44      get { return true; }
45    }
46    #endregion
47
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    ScatterPlot plot, childDiversityPlot, parentDiversityPlot;
80    DataRow dtRow, dtRowSuccess;
81    int cnt = 0;
82    int success = 0;
83
84    [StorableConstructor]
85    private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
86    private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
87    public CrossoverPerformanceAnalyzer()
88      : base() {
89      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
90      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
91
92      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
93      ParentsParameter.ActualName = "TSPTour";
94
95      Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
96      ChildParameter.ActualName = "TSPTour";
97
98      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
99      ParentsQualityParameter.ActualName = "TSPTourLength";
100
101      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
102      QualityParameter.ActualName = "TSPTourLength";
103
104      Parameters.Add(new ValueParameter<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
105    }
106
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new CrossoverPerformanceAnalyzer(this, cloner);
109    }
110
111    public override IOperation Apply() {
112      SimilarityCalculatorParameter.Value.QualityVariableName = "TSPTourLength";
113      SimilarityCalculatorParameter.Value.SolutionVariableName = "TSPTour";
114
115      Point2D<double> qualityPoint, diversityPointChild, diversityPointParent;
116      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
117      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
118      var child = ChildParameter.ActualValue;
119      var parent1 = ParentsParameter.ActualValue.First();
120      var parent2 = ParentsParameter.ActualValue.Last();
121      var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
122
123      diversityPointParent = new Point2D<double>(cnt, parentDiversity);
124      double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
125      if (qualityParent1 > qualityParent2) {
126        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
127      } else {
128        diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
129      }
130
131      qualityPoint = new Point2D<double>(cnt++, worseQuality - QualityParameter.ActualValue.Value);
132      if ((worseQuality - QualityParameter.ActualValue.Value) > 0) {
133        success++;
134      }
135
136      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
137
138      if (!Results.ContainsKey("Scatter Plot")) {
139        InitializePlot();
140        InitializeChildDiversityPlot();
141        InitializeParentDiversityPlot();
142
143        Results.Add(new Result("Scatter Plot", plot));
144        Results.Add(new Result("Scatter Plot History", new ScatterPlotHistory()));
145
146        Results.Add(new Result("Child Diversity Scatter Plot", childDiversityPlot));
147        Results.Add(new Result("Child Diversity Scatter Plot History", new ScatterPlotHistory()));
148
149        Results.Add(new Result("Parent Diversity Scatter Plot", parentDiversityPlot));
150        Results.Add(new Result("Parent Diversity Scatter Plot History", new ScatterPlotHistory()));
151        cnt = 0;
152
153        DataTable dt = new DataTable("Average Crossover Performance");
154        dtRow = new DataRow("Average Crossover Performance per Generation");
155        dt.Rows.Add(dtRow);
156        Results.Add(new Result("Average Crossover Performance", dt));
157
158        DataTable dtSuccess = new DataTable("Successfull Crossovers");
159        dtRowSuccess = new DataRow("Successfull Crossovers per Generation");
160        dtSuccess.Rows.Add(dtRowSuccess);
161        Results.Add(new Result("Successfull Crossovers", dtSuccess));
162
163        success = 0;
164      }
165
166      if (!plot.Rows.ContainsKey(curGenStr)) {
167        if (GenerationsParameter.ActualValue.Value != 0) {
168          double avg = plot.Rows[(GenerationsParameter.ActualValue.Value - 1).ToString()].Points.Average(x => x.Y);
169          dtRow.Values.Add(avg);
170          dtRowSuccess.Values.Add(success);
171
172          ((ScatterPlotHistory)Results["Scatter Plot History"].Value).Add(plot);
173          ((ScatterPlotHistory)Results["Child Diversity Scatter Plot History"].Value).Add(childDiversityPlot);
174          ((ScatterPlotHistory)Results["Parent Diversity Scatter Plot History"].Value).Add(parentDiversityPlot);
175          InitializePlot();
176          InitializeChildDiversityPlot();
177          InitializeParentDiversityPlot();
178          Results["Scatter Plot"].Value = plot;
179          Results["Child Diversity Scatter Plot"].Value = childDiversityPlot;
180          Results["Parent Diversity Scatter Plot"].Value = parentDiversityPlot;
181          cnt = 0;
182          success = 0;
183        }
184
185        var points = new List<Point2D<double>>();
186        points.Add(qualityPoint);
187        ScatterPlotDataRow row;
188        row = new ScatterPlotDataRow(curGenStr, null, points);
189        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
190        row.VisualProperties.PointSize = 5;
191        plot.Rows.Add(row);
192
193        points = new List<Point2D<double>>();
194        points.Add(diversityPointChild);
195        row = new ScatterPlotDataRow(curGenStr, null, points);
196        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
197        row.VisualProperties.PointSize = 5;
198        childDiversityPlot.Rows.Add(row);
199
200        points = new List<Point2D<double>>();
201        points.Add(diversityPointParent);
202        row = new ScatterPlotDataRow(curGenStr, null, points);
203        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
204        row.VisualProperties.PointSize = 5;
205        parentDiversityPlot.Rows.Add(row);
206      } else {
207        plot.Rows[curGenStr].Points.Add(qualityPoint);
208        childDiversityPlot.Rows[curGenStr].Points.Add(diversityPointChild);
209        parentDiversityPlot.Rows[curGenStr].Points.Add(diversityPointParent);
210      }
211
212      return base.Apply();
213    }
214
215    private void InitializePlot() {
216      plot = new ScatterPlot("Crossover Performance", null);
217      plot.VisualProperties.XAxisTitle = "Solution Index";
218      plot.VisualProperties.YAxisTitle = "Absolut Quality Difference";
219    }
220    private void InitializeChildDiversityPlot() {
221      childDiversityPlot = new ScatterPlot("Child Diversity", null);
222      childDiversityPlot.VisualProperties.XAxisTitle = "Solution Index";
223      childDiversityPlot.VisualProperties.YAxisTitle = "Diversity";
224    }
225    private void InitializeParentDiversityPlot() {
226      parentDiversityPlot = new ScatterPlot("Parent Diversity", null);
227      parentDiversityPlot.VisualProperties.XAxisTitle = "Solution Index";
228      parentDiversityPlot.VisualProperties.YAxisTitle = "Diversity";
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.