Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8501 was 8501, checked in by ascheibe, 10 years ago

#1886 fixed cloning and made helper classes items

File size: 6.7 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    #endregion
69
70    #region Properties
71    public ResultCollection Results {
72      get { return ResultsParameter.ActualValue; }
73    }
74    #endregion
75
76    private ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
77    private DataTableHelper avgDataTableHelper;
78    private int cnt = 0, lastGeneration = 0;
79    private List<double> qualityPoints = new List<double>();
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      avgDataTableHelper = (DataTableHelper)original.avgDataTableHelper.Clone(cloner);
88      cnt = original.cnt;
89      lastGeneration = original.lastGeneration;
90      qualityPoints = new List<double>(original.qualityPoints);
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<Permutation>("PermutationBeforeMutation"));
105      PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
106
107      Parameters.Add(new LookupParameter<Permutation>("PermutationAfterMutation"));
108      PermutationAfterMutationParameter.ActualName = "TSPTour";
109
110      diversityPlotHelper = new ScatterPlotHelper();
111      qualityPlotHelper = new ScatterPlotHelper();
112      avgDataTableHelper = new DataTableHelper();
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new MutationPerformanceAnalyzer(this, cloner);
117    }
118
119    public override IOperation Apply() {
120      Point2D<double> curPoint, divPoint;
121
122      var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
123      var qualityM = QualityAfterMutationParameter.ActualValue.Value;
124      var permutationBefore = PermutationBeforeMutationParameter.ActualValue;
125      var permutationAfter = PermutationAfterMutationParameter.ActualValue;
126
127      qualityPlotHelper.InitializePlot(Results, "Mutation Quality", "Solution Index", "Absolut Quality Difference");
128      diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
129      avgDataTableHelper.InitializeChart(Results, "Average Mutation Performance", "Average Mutation Performance per Generation");
130
131      divPoint = new Point2D<double>(cnt, TSPSimilarityCalculator.CalculateSimilarity(permutationBefore, permutationAfter));
132      curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
133      qualityPoints.Add(curPoint.Y);
134
135      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
136
137      qualityPlotHelper.AddPoint(curGenStr, curPoint);
138      diversityPlotHelper.AddPoint(curGenStr, divPoint);
139
140      if (GenerationsParameter.ActualValue.Value != 0) {
141        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
142          double avg = qualityPoints.Average();
143          avgDataTableHelper.AddPoint(avg);
144          Reset();
145        }
146      } else {
147        Reset();
148      }
149
150      return base.Apply();
151    }
152
153    private void Reset() {
154      cnt = 0;
155      lastGeneration = GenerationsParameter.ActualValue.Value;
156      qualityPoints.Clear();
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.