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.Optimization.Operators;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
37 | [Item("CrossoverPerformanceAnalyzer", "An operator that analyzes the performance of crossovers.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class CrossoverPerformanceAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
40 | private const string ResultsParameterName = "Results";
|
---|
41 | private const string GenerationsParameterName = "Generations";
|
---|
42 |
|
---|
43 | #region IAnalyzer Members
|
---|
44 | public bool EnabledByDefault {
|
---|
45 | get { return true; }
|
---|
46 | }
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 |
|
---|
50 | #region Parameter properties
|
---|
51 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
52 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
55 | get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<ItemArray<Permutation>> ParentsParameter {
|
---|
58 | get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Parents"]; }
|
---|
59 | }
|
---|
60 | public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
|
---|
61 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
|
---|
62 | }
|
---|
63 | public ILookupParameter<Permutation> ChildParameter {
|
---|
64 | get { return ((LookupParameter<Permutation>)Parameters["Child"]); }
|
---|
65 | }
|
---|
66 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
67 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
68 | }
|
---|
69 | public IValueParameter<SingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
70 | get { return (IValueParameter<SingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region Properties
|
---|
75 | public ResultCollection Results {
|
---|
76 | get { return ResultsParameter.ActualValue; }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | ScatterPlotHelper plotHelper, childDiversityHelper, parentDiversityHelper;
|
---|
81 | DataTableHelper performanceHelper, successHelper, equalParentsHelper;
|
---|
82 | int cnt = 0;
|
---|
83 | int success = 0;
|
---|
84 | int lastGeneration = 0;
|
---|
85 | int equalParents = 0;
|
---|
86 | List<double> qualityPoints = new List<double>();
|
---|
87 |
|
---|
88 | [StorableConstructor]
|
---|
89 | private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
90 | private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
91 | public CrossoverPerformanceAnalyzer()
|
---|
92 | : base() {
|
---|
93 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
94 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
95 |
|
---|
96 | Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
|
---|
97 | ParentsParameter.ActualName = "TSPTour";
|
---|
98 |
|
---|
99 | Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
|
---|
100 | ChildParameter.ActualName = "TSPTour";
|
---|
101 |
|
---|
102 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
|
---|
103 | ParentsQualityParameter.ActualName = "TSPTourLength";
|
---|
104 |
|
---|
105 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
|
---|
106 | QualityParameter.ActualName = "TSPTourLength";
|
---|
107 |
|
---|
108 | Parameters.Add(new ValueParameter<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
|
---|
109 |
|
---|
110 | plotHelper = new ScatterPlotHelper();
|
---|
111 | childDiversityHelper = new ScatterPlotHelper();
|
---|
112 | parentDiversityHelper = new ScatterPlotHelper();
|
---|
113 | performanceHelper = new DataTableHelper();
|
---|
114 | successHelper = new DataTableHelper();
|
---|
115 | equalParentsHelper = new DataTableHelper();
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
119 | return new CrossoverPerformanceAnalyzer(this, cloner);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override IOperation Apply() {
|
---|
123 | SimilarityCalculatorParameter.Value.QualityVariableName = "TSPTourLength";
|
---|
124 | SimilarityCalculatorParameter.Value.SolutionVariableName = "TSPTour";
|
---|
125 |
|
---|
126 | plotHelper.InitializePlot(Results, "Crossover Performance", "Solution Index", "Absolut Quality Difference");
|
---|
127 | childDiversityHelper.InitializePlot(Results, "Child Diversity", "Solution Index", "Diversity");
|
---|
128 | parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
|
---|
129 |
|
---|
130 | performanceHelper.InitializeChart(Results, "Average Crossover Performance", "Average Crossover Performance per Generation");
|
---|
131 | successHelper.InitializeChart(Results, "Successfull Crossovers", "Successfull Crossovers per Generation");
|
---|
132 | equalParentsHelper.InitializeChart(Results, "Number of equal parents", "Absolut number of equal parents");
|
---|
133 |
|
---|
134 | Point2D<double> qualityPoint, diversityPointChild, diversityPointParent;
|
---|
135 | var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
|
---|
136 | var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
|
---|
137 | var child = ChildParameter.ActualValue;
|
---|
138 | var parent1 = ParentsParameter.ActualValue.First();
|
---|
139 | var parent2 = ParentsParameter.ActualValue.Last();
|
---|
140 | var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
|
---|
141 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
142 |
|
---|
143 |
|
---|
144 | diversityPointParent = new Point2D<double>(cnt, parentDiversity);
|
---|
145 | double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
|
---|
146 | if (qualityParent1 > qualityParent2) {
|
---|
147 | diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
|
---|
148 | } else {
|
---|
149 | diversityPointChild = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
|
---|
150 | }
|
---|
151 |
|
---|
152 | qualityPoint = new Point2D<double>(cnt++, worseQuality - QualityParameter.ActualValue.Value);
|
---|
153 | if ((worseQuality - QualityParameter.ActualValue.Value) > 0) {
|
---|
154 | success++;
|
---|
155 | }
|
---|
156 | qualityPoints.Add(qualityPoint.Y);
|
---|
157 |
|
---|
158 | if (TSPSimilarityCalculator.CalculateSimilarity(parent1, parent2) == 1.0) {
|
---|
159 | equalParents++;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
163 | if (GenerationsParameter.ActualValue.Value > lastGeneration) {
|
---|
164 | double avg = qualityPoints.Average();
|
---|
165 |
|
---|
166 | performanceHelper.AddPoint(avg);
|
---|
167 | successHelper.AddPoint(success);
|
---|
168 | equalParentsHelper.AddPoint(equalParents);
|
---|
169 |
|
---|
170 | Reset();
|
---|
171 | }
|
---|
172 |
|
---|
173 | plotHelper.AddPoint(curGenStr, qualityPoint);
|
---|
174 | childDiversityHelper.AddPoint(curGenStr, diversityPointChild);
|
---|
175 | parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
|
---|
176 | } else {
|
---|
177 | Reset();
|
---|
178 | }
|
---|
179 |
|
---|
180 | return base.Apply();
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void Reset() {
|
---|
184 | cnt = 0;
|
---|
185 | success = 0;
|
---|
186 | lastGeneration = GenerationsParameter.ActualValue.Value;
|
---|
187 | qualityPoints.Clear();
|
---|
188 | equalParents = 0;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|