Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added configuration of frequency with which the ab measures are calculated

File size: 10.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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
32  [Item("MutationPerformanceAnalyzer", "An operator that analyzes the performance of mutation.")]
33  [StorableClass]
34  public class MutationPerformanceAnalyzer : InitializableOperator, IStatefulItem {
35    private const string ResultsParameterName = "Results";
36    private const string GenerationsParameterName = "Generations";
37
38    #region Parameter properties
39    public ILookupParameter<ResultCollection> ResultsParameter {
40      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
41    }
42    public ILookupParameter<IntValue> GenerationsParameter {
43      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
44    }
45    public ILookupParameter<DoubleValue> QualityAfterCrossoverParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterCrossover"]; }
47    }
48    public ILookupParameter<DoubleValue> QualityAfterMutationParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["QualityAfterMutation"]; }
50    }
51    public ILookupParameter<IItem> PermutationBeforeMutationParameter {
52      get { return (ILookupParameter<IItem>)Parameters["PermutationBeforeMutation"]; }
53    }
54    public ILookupParameter<IItem> PermutationAfterMutationParameter {
55      get { return (ILookupParameter<IItem>)Parameters["PermutationAfterMutation"]; }
56    }
57    public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
58      get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
59    }
60    public IValueLookupParameter<BoolValue> MaximizationParameter {
61      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
62    }
63    public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
64      get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
65    }
66    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
67      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
68    }
69    public ILookupParameter<DoubleValue> WorstKnownQualityParameter {
70      get { return (ILookupParameter<DoubleValue>)Parameters["WorstKnownQuality"]; }
71    }
72    #endregion
73
74    #region Properties
75    public ResultCollection Results {
76      get { return ResultsParameter.ActualValue; }
77    }
78    #endregion
79
80    [Storable]
81    private DataTableHelper successHelper;
82    [Storable]
83    private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
84    [Storable]
85    private int cnt = 0, lastGeneration = 0, success = 0;
86    [Storable]
87    private bool scalingFinished = false;
88
89    [StorableConstructor]
90    private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
91    private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner)
92      : base(original, cloner) {
93      diversityPlotHelper = (ScatterPlotHelper)original.diversityPlotHelper.Clone(cloner);
94      qualityPlotHelper = (ScatterPlotHelper)original.qualityPlotHelper.Clone(cloner);
95      successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
96      cnt = original.cnt;
97      lastGeneration = original.lastGeneration;
98      success = original.success;
99      scalingFinished = original.scalingFinished;
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<IItem>("PermutationBeforeMutation"));
114      PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
115
116      Parameters.Add(new LookupParameter<IItem>("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      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
122      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
123      Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
124
125      diversityPlotHelper = new ScatterPlotHelper(false, true, false, true);
126      qualityPlotHelper = new ScatterPlotHelper(false, true, true, true);
127      successHelper = new DataTableHelper();
128    }
129
130    public override IDeepCloneable Clone(Cloner cloner) {
131      return new MutationPerformanceAnalyzer(this, cloner);
132    }
133
134    protected override void InitializeAction() {
135      if (SimilarityCalculatorParameter.Value == null) {
136        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
137      }
138      qualityPlotHelper.InitializePlot(Results, "Mutation Performance compared to parent", "Solution Index", "Absolut Quality Difference");
139      diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
140      successHelper.InitializeChart(Results, "Successfull Mutations", new string[] { "Successfull Mutations per Generation" });
141      Reset();
142    }
143
144    public override IOperation Apply() {
145      Initialize();
146      Point2D<double> curPoint, divPoint;
147
148      var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
149      var qualityM = QualityAfterMutationParameter.ActualValue.Value;
150      var solutionBefore = PermutationBeforeMutationParameter.ActualValue;
151      var solutionAfter = PermutationAfterMutationParameter.ActualValue;
152
153      Scope permutationBeforeScope = new Scope();
154      Scope permutationAfterScope = new Scope();
155      permutationBeforeScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionBefore));
156      permutationAfterScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionAfter));
157
158      divPoint = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(permutationBeforeScope, permutationAfterScope));
159      curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
160
161      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
162
163      qualityPlotHelper.AddPoint(curGenStr, curPoint);
164      diversityPlotHelper.AddPoint(curGenStr, divPoint);
165
166      if (GenerationsParameter.ActualValue.Value == lastGeneration) {
167        CountSuccess(qualityCX, qualityM);
168      }
169
170      if (WorstKnownQualityParameter.ActualValue != null && !scalingFinished) {
171        scalingFinished = true;
172        double bkQuality = BestKnownQualityParameter.ActualValue.Value;
173        double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
174
175        if (MaximizationParameter.ActualValue.Value) {
176          if (qualityPlotHelper.Max == double.MinValue) {
177            qualityPlotHelper.Max = bkQuality - wkQuality;
178            qualityPlotHelper.Min = 0;
179          }
180        } else {
181          if (qualityPlotHelper.Min == double.MaxValue) {
182            qualityPlotHelper.Max = wkQuality - bkQuality;
183            qualityPlotHelper.Min = 0;
184          }
185        }
186      }
187
188
189      if (GenerationsParameter.ActualValue.Value != 0) {
190        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
191          if (cnt > 1) {
192            successHelper.AddPoint((double)success / (cnt - 1));
193          } else {
194            successHelper.AddPoint(0.0);
195          }
196
197          Reset();
198          CountSuccess(qualityCX, qualityM);
199        }
200      }
201
202      return base.Apply();
203    }
204
205    private void Reset() {
206      cnt = 0;
207      lastGeneration = GenerationsParameter.ActualValue.Value;
208      success = 0;
209    }
210
211    public override void ClearState() {
212      qualityPlotHelper.CleanUp();
213      diversityPlotHelper.CleanUp();
214      successHelper.CleanUpAndCompressData();
215      scalingFinished = false;
216    }
217
218    private void CountSuccess(double qualityCX, double qualityM) {
219      if (!MaximizationParameter.ActualValue.Value && qualityCX > qualityM) {
220        success++;
221      }
222
223      if (MaximizationParameter.ActualValue.Value && qualityCX < qualityM) {
224        success++;
225      }
226    }
227  }
228}
Note: See TracBrowser for help on using the repository browser.