Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9150 was 9150, checked in by ascheibe, 11 years ago

#1886 fixed successor configuration and added successful mutation chart

File size: 8.5 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    #endregion
67
68    #region Properties
69    public ResultCollection Results {
70      get { return ResultsParameter.ActualValue; }
71    }
72    #endregion
73
74    [Storable]
75    private DataTableHelper successHelper;
76    [Storable]
77    private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
78    [Storable]
79    private int cnt = 0, lastGeneration = 0, success = 0;
80
81    [StorableConstructor]
82    private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
83    private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner)
84      : base(original, cloner) {
85      diversityPlotHelper = (ScatterPlotHelper)original.diversityPlotHelper.Clone(cloner);
86      qualityPlotHelper = (ScatterPlotHelper)original.qualityPlotHelper.Clone(cloner);
87      successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
88      cnt = original.cnt;
89      lastGeneration = original.lastGeneration;
90      success = original.success;
91    }
92
93    public MutationPerformanceAnalyzer()
94      : base() {
95      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
96      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
97
98      Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterCrossover", "The evaluated quality of the child solution."));
99      QualityAfterCrossoverParameter.ActualName = "TSPTourLength";
100
101      Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterMutation", "The evaluated quality of the child solution."));
102      QualityAfterMutationParameter.ActualName = "TSPTourLengthM";
103
104      Parameters.Add(new LookupParameter<IItem>("PermutationBeforeMutation"));
105      PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
106
107      Parameters.Add(new LookupParameter<IItem>("PermutationAfterMutation"));
108      PermutationAfterMutationParameter.ActualName = "TSPTour";
109
110      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
111      Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
112      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
113
114      diversityPlotHelper = new ScatterPlotHelper(false, true);
115      qualityPlotHelper = new ScatterPlotHelper(false, true, true);
116      successHelper = new DataTableHelper();
117    }
118
119    public override IDeepCloneable Clone(Cloner cloner) {
120      return new MutationPerformanceAnalyzer(this, cloner);
121    }
122
123    protected override void InitializeAction() {
124      if (SimilarityCalculatorParameter.Value == null) {
125        SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
126      }
127      qualityPlotHelper.InitializePlot(Results, "Mutation Quality", "Solution Index", "Absolut Quality Difference");
128      diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
129      successHelper.InitializeChart(Results, "Successfull Mutations", new string[] { "Successfull Mutations per Generation" });
130      Reset();
131    }
132
133    public override IOperation Apply() {
134      Initialize();
135      Point2D<double> curPoint, divPoint;
136
137      var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
138      var qualityM = QualityAfterMutationParameter.ActualValue.Value;
139      var solutionBefore = PermutationBeforeMutationParameter.ActualValue;
140      var solutionAfter = PermutationAfterMutationParameter.ActualValue;
141
142      Scope permutationBeforeScope = new Scope();
143      Scope permutationAfterScope = new Scope();
144      permutationBeforeScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionBefore));
145      permutationAfterScope.Variables.Add(new Variable(PermutationAfterMutationParameter.ActualName, solutionAfter));
146
147      divPoint = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(permutationBeforeScope, permutationAfterScope));
148      curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
149
150      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
151
152      qualityPlotHelper.AddPoint(curGenStr, curPoint);
153      diversityPlotHelper.AddPoint(curGenStr, divPoint);
154
155      if (GenerationsParameter.ActualValue.Value == lastGeneration) {
156        CountSuccess(qualityCX, qualityM);
157      }
158
159      if (GenerationsParameter.ActualValue.Value != 0) {
160        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
161          successHelper.AddPoint((double)success / (cnt - 1));
162
163          Reset();
164          CountSuccess(qualityCX, qualityM);
165        }
166      }
167
168      return base.Apply();
169    }
170
171    private void Reset() {
172      cnt = 0;
173      lastGeneration = GenerationsParameter.ActualValue.Value;
174      success = 0;
175    }
176
177    public override void ClearState() {
178      qualityPlotHelper.CleanUp();
179      diversityPlotHelper.CleanUp();
180    }
181
182    private void CountSuccess(double qualityCX, double qualityM) {
183      if (!MaximizationParameter.ActualValue.Value && qualityCX > qualityM) {
184        success++;
185      }
186
187      if (MaximizationParameter.ActualValue.Value && qualityCX < qualityM) {
188        success++;
189      }
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.