Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added mutation performance analyzer for test functions

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