Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 adapted operators so that they can be used with Scatter Search

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