Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added an operator to clone variables

File size: 7.1 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.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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    public IValueParameter<SingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
69      get { return (IValueParameter<SingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
70    }
71    #endregion
72
73    #region Properties
74    public ResultCollection Results {
75      get { return ResultsParameter.ActualValue; }
76    }
77    #endregion
78
79    ScatterPlot plot, diversityPlot;
80    DataRow dtRow, dtDivRow;
81    int cnt = 0;
82
83    [StorableConstructor]
84    private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
85    private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
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<Permutation>("PermutationBeforeMutation"));
98      QualityAfterMutationParameter.ActualName = "TSPTourClone";
99
100      Parameters.Add(new LookupParameter<Permutation>("PermutationAfterMutation"));
101      QualityAfterMutationParameter.ActualName = "TSPTour";
102
103      Parameters.Add(new ValueParameter<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
104    }
105
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new MutationPerformanceAnalyzer(this, cloner);
108    }
109
110    public override IOperation Apply() {
111      Point2D<double> curPoint;
112      var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
113      var qualityM = QualityAfterMutationParameter.ActualValue.Value;
114
115      curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
116
117      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
118      ScatterPlotDataRow row;
119
120      if (!Results.ContainsKey("Mutation Scatter Plot")) {
121        InitializePlot();
122        Results.Add(new Result("Mutation Scatter Plot", plot));
123        Results.Add(new Result("Mutation Scatter Plot History", new ScatterPlotHistory()));
124        cnt = 0;
125
126        DataTable dt = new DataTable("Average Mutatioin Performance");
127        dtRow = new DataRow("Average Mutation Performance per Generation");
128        dt.Rows.Add(dtRow);
129        Results.Add(new Result("Average Mutation Performance", dt));
130      }
131
132      if (!plot.Rows.ContainsKey(curGenStr)) {
133        if (GenerationsParameter.ActualValue.Value != 0) {
134          if (plot.Rows.ContainsKey((GenerationsParameter.ActualValue.Value - 1).ToString())) {
135            double avg = plot.Rows[(GenerationsParameter.ActualValue.Value - 1).ToString()].Points.Average(x => x.Y);
136            dtRow.Values.Add(avg);
137            ((ScatterPlotHistory)Results["Mutation Scatter Plot History"].Value).Add(plot);
138          }
139          InitializePlot();
140          Results["Mutation Scatter Plot"].Value = plot;
141          cnt = 0;
142        }
143
144        var points = new List<Point2D<double>>();
145        points.Add(curPoint);
146        row = new ScatterPlotDataRow(curGenStr, null, points);
147        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
148        row.VisualProperties.PointSize = 5;
149        plot.Rows.Add(row);
150      } else {
151        plot.Rows[curGenStr].Points.Add(curPoint);
152      }
153
154      return base.Apply();
155    }
156
157    private void InitializePlot() {
158      plot = new ScatterPlot("Mutation Performance", null);
159      plot.VisualProperties.XAxisTitle = "Solution Index";
160      plot.VisualProperties.YAxisTitle = "Absolut Quality Difference";
161    }
162
163    private void InitializeDiversityPlot() {
164      diversityPlot = new ScatterPlot("Mutation Diversity", null);
165      diversityPlot.VisualProperties.XAxisTitle = "Solution Index";
166      diversityPlot.VisualProperties.YAxisTitle = "Diversity";
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.