Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/MutationPerformanceAnalyzer.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: 8.1 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;
33using HeuristicLab.Problems.TravelingSalesman;
34
35namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
36  [Item("MutationPerformanceAnalyzer", "An operator that analyzes the performance of mutation.")]
37  [StorableClass]
38  public class MutationPerformanceAnalyzer : 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<DoubleValue> QualityAfterCrossoverParameter {
57      get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterCrossover"]; }
58    }
59    public ILookupParameter<DoubleValue> QualityAfterMutationParameter {
60      get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterMutation"]; }
61    }
62    public ILookupParameter<Permutation> PermutationBeforeMutationParameter {
63      get { return (ILookupParameter<Permutation>)Parameters["PermutationBeforeMutation"]; }
64    }
65    public ILookupParameter<Permutation> PermutationAfterMutationParameter {
66      get { return (ILookupParameter<Permutation>)Parameters["PermutationAfterMutation"]; }
67    }
68    public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
69      get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
70    }
71    public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
72      get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
73    }
74    #endregion
75
76    #region Properties
77    public ResultCollection Results {
78      get { return ResultsParameter.ActualValue; }
79    }
80    #endregion
81
82    [Storable]
83    private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
84    [Storable]
85    private DataTableHelper avgDataTableHelper;
86    [Storable]
87    private int cnt = 0, lastGeneration = 0;
88    [Storable]
89    private List<double> qualityPoints = new List<double>();
90
91    [StorableConstructor]
92    private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
93    private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner)
94      : base(original, cloner) {
95      diversityPlotHelper = (ScatterPlotHelper)original.diversityPlotHelper.Clone(cloner);
96      qualityPlotHelper = (ScatterPlotHelper)original.qualityPlotHelper.Clone(cloner);
97      avgDataTableHelper = (DataTableHelper)original.avgDataTableHelper.Clone(cloner);
98      cnt = original.cnt;
99      lastGeneration = original.lastGeneration;
100      qualityPoints = new List<double>(original.qualityPoints);
101    }
102
103    public MutationPerformanceAnalyzer()
104      : base() {
105      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
106      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
107
108      Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterCrossover", "The evaluated quality of the child solution."));
109      QualityAfterCrossoverParameter.ActualName = "TSPTourLength";
110
111      Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterMutation", "The evaluated quality of the child solution."));
112      QualityAfterMutationParameter.ActualName = "TSPTourLengthM";
113
114      Parameters.Add(new LookupParameter<Permutation>("PermutationBeforeMutation"));
115      PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
116
117      Parameters.Add(new LookupParameter<Permutation>("PermutationAfterMutation"));
118      PermutationAfterMutationParameter.ActualName = "TSPTour";
119
120      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
121      Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
122
123      diversityPlotHelper = new ScatterPlotHelper(false, true);
124      qualityPlotHelper = new ScatterPlotHelper(false, true);
125      avgDataTableHelper = new DataTableHelper();
126    }
127
128    public override IDeepCloneable Clone(Cloner cloner) {
129      return new MutationPerformanceAnalyzer(this, cloner);
130    }
131
132    public override IOperation Apply() {
133      Point2D<double> curPoint, divPoint;
134
135      if (SimilarityCalculatorParameter.Value == null) {
136        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
137      }
138
139      var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
140      var qualityM = QualityAfterMutationParameter.ActualValue.Value;
141      var permutationBefore = PermutationBeforeMutationParameter.ActualValue;
142      var permutationAfter = PermutationAfterMutationParameter.ActualValue;
143
144      if (permutationBefore != null) {
145        qualityPlotHelper.InitializePlot(Results, "Mutation Quality", "Solution Index", "Absolut Quality Difference");
146        diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
147        avgDataTableHelper.InitializeChart(Results, "Average Mutation Performance", "Average Mutation Performance per Generation");
148
149        Scope permutationBeforeScope = new Scope();
150        Scope permutationAfterScope = new Scope();
151        permutationBeforeScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, permutationBefore));
152        permutationAfterScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, permutationAfter));
153
154        divPoint = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(permutationBeforeScope, permutationAfterScope));
155        curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
156        qualityPoints.Add(curPoint.Y);
157
158        string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
159
160        qualityPlotHelper.AddPoint(curGenStr, curPoint);
161        diversityPlotHelper.AddPoint(curGenStr, divPoint);
162
163        if (GenerationsParameter.ActualValue.Value != 0) {
164          if (GenerationsParameter.ActualValue.Value > lastGeneration) {
165            double avg = qualityPoints.Average();
166            avgDataTableHelper.AddPoint(avg);
167            Reset();
168          }
169        } else {
170          Reset();
171        }
172      }
173
174      return base.Apply();
175    }
176
177    private void Reset() {
178      cnt = 0;
179      lastGeneration = GenerationsParameter.ActualValue.Value;
180      qualityPoints.Clear();
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.