Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added helper classes for generating charts

File size: 6.2 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
77    ScatterPlotHelper diversityPlotHelper, qualityPlotHelper;
78    DataTableHelper avgDataTableHelper;
79    int cnt = 0, lastGeneration = 0;
80    List<double> qualityPoints = new List<double>();
81
82    [StorableConstructor]
83    private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
84    private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
85    public MutationPerformanceAnalyzer()
86      : base() {
87      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
88      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
89
90      Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterCrossover", "The evaluated quality of the child solution."));
91      QualityAfterCrossoverParameter.ActualName = "TSPTourLength";
92
93      Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterMutation", "The evaluated quality of the child solution."));
94      QualityAfterMutationParameter.ActualName = "TSPTourLengthM";
95
96      Parameters.Add(new LookupParameter<Permutation>("PermutationBeforeMutation"));
97      PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
98
99      Parameters.Add(new LookupParameter<Permutation>("PermutationAfterMutation"));
100      PermutationAfterMutationParameter.ActualName = "TSPTour";
101
102      diversityPlotHelper = new ScatterPlotHelper();
103      qualityPlotHelper = new ScatterPlotHelper();
104      avgDataTableHelper = new DataTableHelper();
105    }
106
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new MutationPerformanceAnalyzer(this, cloner);
109    }
110
111    public override IOperation Apply() {
112      Point2D<double> curPoint, divPoint;
113
114      var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
115      var qualityM = QualityAfterMutationParameter.ActualValue.Value;
116      var permutationBefore = PermutationBeforeMutationParameter.ActualValue;
117      var permutationAfter = PermutationAfterMutationParameter.ActualValue;
118
119      qualityPlotHelper.InitializePlot(Results, "Mutation Quality", "Solution Index", "Absolut Quality Difference");
120      diversityPlotHelper.InitializePlot(Results, "Mutation Diversity", "Solution Index", "Diversity");
121      avgDataTableHelper.InitializeChart(Results, "Average Mutation Performance", "Average Mutation Performance per Generation");
122
123      divPoint = new Point2D<double>(cnt, TSPSimilarityCalculator.CalculateSimilarity(permutationBefore, permutationAfter));
124      curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
125      qualityPoints.Add(curPoint.Y);
126
127      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
128
129      qualityPlotHelper.AddPoint(curGenStr, curPoint);
130      diversityPlotHelper.AddPoint(curGenStr, divPoint);
131
132      if (GenerationsParameter.ActualValue.Value != 0) {
133        if (GenerationsParameter.ActualValue.Value > lastGeneration) {
134          double avg = qualityPoints.Average();
135          avgDataTableHelper.AddPoint(avg);
136          cnt = 0;
137          lastGeneration = GenerationsParameter.ActualValue.Value;
138          qualityPoints.Clear();
139        }
140      }
141
142      return base.Apply();
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.