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 |
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
34 |
|
---|
35 | namespace 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 | ScatterPlot plot, diversityPlot;
|
---|
77 | DataRow dtRow;
|
---|
78 | int cnt = 0;
|
---|
79 |
|
---|
80 | [StorableConstructor]
|
---|
81 | private MutationPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
82 | private MutationPerformanceAnalyzer(MutationPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
83 | public MutationPerformanceAnalyzer()
|
---|
84 | : base() {
|
---|
85 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
86 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
87 |
|
---|
88 | Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterCrossover", "The evaluated quality of the child solution."));
|
---|
89 | QualityAfterCrossoverParameter.ActualName = "TSPTourLength";
|
---|
90 |
|
---|
91 | Parameters.Add(new LookupParameter<DoubleValue>("QualityAfterMutation", "The evaluated quality of the child solution."));
|
---|
92 | QualityAfterMutationParameter.ActualName = "TSPTourLengthM";
|
---|
93 |
|
---|
94 | Parameters.Add(new LookupParameter<Permutation>("PermutationBeforeMutation"));
|
---|
95 | PermutationBeforeMutationParameter.ActualName = "TSPTourClone";
|
---|
96 |
|
---|
97 | Parameters.Add(new LookupParameter<Permutation>("PermutationAfterMutation"));
|
---|
98 | PermutationAfterMutationParameter.ActualName = "TSPTour";
|
---|
99 | }
|
---|
100 |
|
---|
101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
102 | return new MutationPerformanceAnalyzer(this, cloner);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public override IOperation Apply() {
|
---|
106 | Point2D<double> curPoint, divPoint;
|
---|
107 | var qualityCX = QualityAfterCrossoverParameter.ActualValue.Value;
|
---|
108 | var qualityM = QualityAfterMutationParameter.ActualValue.Value;
|
---|
109 | var permutationBefore = PermutationBeforeMutationParameter.ActualValue;
|
---|
110 | var permutationAfter = PermutationAfterMutationParameter.ActualValue;
|
---|
111 |
|
---|
112 | divPoint = new Point2D<double>(cnt, TSPSimilarityCalculator.CalculateSimilarity(permutationBefore, permutationAfter));
|
---|
113 | curPoint = new Point2D<double>(cnt++, qualityCX - qualityM);
|
---|
114 |
|
---|
115 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
116 | ScatterPlotDataRow row, divRow;
|
---|
117 |
|
---|
118 | if (!Results.ContainsKey("Mutation Scatter Plot")) {
|
---|
119 | InitializePlot();
|
---|
120 | InitializeDiversityPlot();
|
---|
121 | Results.Add(new Result("Mutation Scatter Plot", plot));
|
---|
122 | Results.Add(new Result("Mutation Scatter Plot History", new ScatterPlotHistory()));
|
---|
123 | Results.Add(new Result("Mutation Diversity Plot", diversityPlot));
|
---|
124 | Results.Add(new Result("Mutation Diversity Plot History", new ScatterPlotHistory()));
|
---|
125 | cnt = 0;
|
---|
126 |
|
---|
127 | DataTable dt = new DataTable("Average Mutation Performance");
|
---|
128 | dtRow = new DataRow("Average Mutation Performance per Generation");
|
---|
129 | dt.Rows.Add(dtRow);
|
---|
130 | Results.Add(new Result("Average Mutation Performance", dt));
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (!plot.Rows.ContainsKey(curGenStr)) {
|
---|
134 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
135 | if (plot.Rows.ContainsKey((GenerationsParameter.ActualValue.Value - 1).ToString())) {
|
---|
136 | double avg = plot.Rows[(GenerationsParameter.ActualValue.Value - 1).ToString()].Points.Average(x => x.Y);
|
---|
137 | dtRow.Values.Add(avg);
|
---|
138 | ((ScatterPlotHistory)Results["Mutation Scatter Plot History"].Value).Add(plot);
|
---|
139 | ((ScatterPlotHistory)Results["Mutation Diversity Plot History"].Value).Add(diversityPlot);
|
---|
140 | }
|
---|
141 | InitializePlot();
|
---|
142 | InitializeDiversityPlot();
|
---|
143 | Results["Mutation Scatter Plot"].Value = plot;
|
---|
144 | Results["Mutation Diversity Plot"].Value = diversityPlot;
|
---|
145 | cnt = 0;
|
---|
146 | }
|
---|
147 |
|
---|
148 | var points = new List<Point2D<double>>();
|
---|
149 | points.Add(curPoint);
|
---|
150 | row = new ScatterPlotDataRow(curGenStr, null, points);
|
---|
151 | row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
|
---|
152 | row.VisualProperties.PointSize = 5;
|
---|
153 | plot.Rows.Add(row);
|
---|
154 |
|
---|
155 | points = new List<Point2D<double>>();
|
---|
156 | points.Add(divPoint);
|
---|
157 | divRow = new ScatterPlotDataRow(curGenStr, null, points);
|
---|
158 | divRow.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
|
---|
159 | divRow.VisualProperties.PointSize = 5;
|
---|
160 | diversityPlot.Rows.Add(divRow);
|
---|
161 | } else {
|
---|
162 | plot.Rows[curGenStr].Points.Add(curPoint);
|
---|
163 | diversityPlot.Rows[curGenStr].Points.Add(divPoint);
|
---|
164 | }
|
---|
165 |
|
---|
166 | return base.Apply();
|
---|
167 | }
|
---|
168 |
|
---|
169 | private void InitializePlot() {
|
---|
170 | plot = new ScatterPlot("Mutation Performance", null);
|
---|
171 | plot.VisualProperties.XAxisTitle = "Solution Index";
|
---|
172 | plot.VisualProperties.YAxisTitle = "Absolut Quality Difference";
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void InitializeDiversityPlot() {
|
---|
176 | diversityPlot = new ScatterPlot("Mutation Diversity", null);
|
---|
177 | diversityPlot.VisualProperties.XAxisTitle = "Solution Index";
|
---|
178 | diversityPlot.VisualProperties.YAxisTitle = "Diversity";
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|