Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/MutationPerformanceAnalyzer.cs @ 8797

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

#1886 added a comparison factor parameter to the crossover analyzer so that it can be defined what a successful crossover is

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