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;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
35 | [Item("CrossoverPerformanceAnalyzer", "An operator that analyzes the performance of crossovers.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class CrossoverPerformanceAnalyzer : InitializableOperator {
|
---|
38 | private const string ResultsParameterName = "Results";
|
---|
39 | private const string GenerationsParameterName = "Generations";
|
---|
40 | private const string SuccessfullCrossoversRowName = "Successfull Crossovers per Generation with CompFact ";
|
---|
41 |
|
---|
42 | #region Parameter properties
|
---|
43 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
44 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
45 | }
|
---|
46 | public IValueLookupParameter<ItemCollection<DoubleValue>> ComparisonFactorParameter {
|
---|
47 | get { return (IValueLookupParameter<ItemCollection<DoubleValue>>)Parameters["ComparisonFactor"]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
50 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
53 | get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<ItemArray<Permutation>> ParentsParameter {
|
---|
56 | get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Parents"]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
|
---|
59 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<Permutation> ChildParameter {
|
---|
62 | get { return ((LookupParameter<Permutation>)Parameters["Child"]); }
|
---|
63 | }
|
---|
64 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
65 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
66 | }
|
---|
67 | public IValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
68 | get { return (IValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
69 | }
|
---|
70 | public ILookupParameter<ItemCollection<IItem>> OperatorsParameter {
|
---|
71 | get { return (ILookupParameter<ItemCollection<IItem>>)Parameters["Operators"]; }
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | #region Properties
|
---|
76 | public ResultCollection Results {
|
---|
77 | get { return ResultsParameter.ActualValue; }
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | [Storable]
|
---|
82 | private ScatterPlotHelper plotHelper, childDiversityToWorseParentHelper, childDiversityToBetterParentHelper, parentDiversityHelper, parentQualityHelper;
|
---|
83 | [Storable]
|
---|
84 | private DataTableHelper performanceHelper, successHelper, equalParentsHelper;
|
---|
85 | [Storable]
|
---|
86 | private int cnt = 0;
|
---|
87 | [Storable]
|
---|
88 | private int[] success;
|
---|
89 | [Storable]
|
---|
90 | private int lastGeneration = 0;
|
---|
91 | [Storable]
|
---|
92 | private int equalParents = 0;
|
---|
93 | [Storable]
|
---|
94 | private List<double> qualityPoints = new List<double>();
|
---|
95 |
|
---|
96 | [StorableConstructor]
|
---|
97 | private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
98 | private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner)
|
---|
99 | : base(original, cloner) {
|
---|
100 | cnt = original.cnt;
|
---|
101 | success = (int[])original.success.Clone();
|
---|
102 | lastGeneration = original.lastGeneration;
|
---|
103 | equalParents = original.equalParents;
|
---|
104 | qualityPoints = new List<double>(original.qualityPoints);
|
---|
105 | plotHelper = (ScatterPlotHelper)original.plotHelper.Clone(cloner);
|
---|
106 | childDiversityToWorseParentHelper = (ScatterPlotHelper)original.childDiversityToWorseParentHelper.Clone(cloner);
|
---|
107 | childDiversityToBetterParentHelper = (ScatterPlotHelper)original.childDiversityToBetterParentHelper.Clone(cloner);
|
---|
108 | parentDiversityHelper = (ScatterPlotHelper)original.parentDiversityHelper.Clone(cloner);
|
---|
109 | parentQualityHelper = (ScatterPlotHelper)original.parentQualityHelper.Clone(cloner);
|
---|
110 | performanceHelper = (DataTableHelper)original.performanceHelper.Clone(cloner);
|
---|
111 | successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
|
---|
112 | equalParentsHelper = (DataTableHelper)original.equalParentsHelper.Clone(cloner);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public CrossoverPerformanceAnalyzer()
|
---|
116 | : base() {
|
---|
117 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
118 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
119 |
|
---|
120 | Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
|
---|
121 | ParentsParameter.ActualName = "TSPTour";
|
---|
122 |
|
---|
123 | Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
|
---|
124 | ChildParameter.ActualName = "TSPTour";
|
---|
125 |
|
---|
126 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
|
---|
127 | ParentsQualityParameter.ActualName = "TSPTourLength";
|
---|
128 |
|
---|
129 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
|
---|
130 | QualityParameter.ActualName = "TSPTourLength";
|
---|
131 |
|
---|
132 | Parameters.Add(new ValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
|
---|
133 | Parameters.Add(new LookupParameter<ItemCollection<IItem>>("Operators", "The operators and items that the problem provides to the algorithms."));
|
---|
134 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
135 | Parameters.Add(new ValueLookupParameter<ItemCollection<DoubleValue>>("ComparisonFactor", "Determines if the quality should be compared to the better parent (1.0), to the worse (0.0) or to any linearly interpolated value between them."));
|
---|
136 |
|
---|
137 |
|
---|
138 | plotHelper = new ScatterPlotHelper(false, true);
|
---|
139 | childDiversityToWorseParentHelper = new ScatterPlotHelper(false, true);
|
---|
140 | childDiversityToBetterParentHelper = new ScatterPlotHelper(false, true);
|
---|
141 | parentDiversityHelper = new ScatterPlotHelper(false, true);
|
---|
142 | parentQualityHelper = new ScatterPlotHelper(false, true);
|
---|
143 | performanceHelper = new DataTableHelper();
|
---|
144 | successHelper = new DataTableHelper();
|
---|
145 | equalParentsHelper = new DataTableHelper();
|
---|
146 | }
|
---|
147 |
|
---|
148 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
149 | return new CrossoverPerformanceAnalyzer(this, cloner);
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected override void InitializeAction() {
|
---|
153 | if (SimilarityCalculatorParameter.Value == null) {
|
---|
154 | SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (ComparisonFactorParameter.ActualValue == null) {
|
---|
158 | ComparisonFactorParameter.Value = new ItemCollection<DoubleValue>();
|
---|
159 | ComparisonFactorParameter.Value.Add(new DoubleValue(0));
|
---|
160 | ComparisonFactorParameter.Value.Add(new DoubleValue(0.5));
|
---|
161 | ComparisonFactorParameter.Value.Add(new DoubleValue(1));
|
---|
162 | ComparisonFactorParameter.Value.Add(new DoubleValue(1.5));
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (success == null) {
|
---|
166 | success = new int[ComparisonFactorParameter.Value.Count];
|
---|
167 | }
|
---|
168 |
|
---|
169 | plotHelper.InitializePlot(Results, "Crossover Performance", "Solution Index", "Absolut Quality Difference");
|
---|
170 | childDiversityToWorseParentHelper.InitializePlot(Results, "Child Diversity to Worse Parent", "Solution Index", "Diversity");
|
---|
171 | childDiversityToBetterParentHelper.InitializePlot(Results, "Child Diversity to Better Parent", "Solution Index", "Diversity");
|
---|
172 | parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
|
---|
173 | parentQualityHelper.InitializePlot(Results, "Parent Quality Difference", "Solution Index", "Absolut Quality Difference");
|
---|
174 | performanceHelper.InitializeChart(Results, "Average Crossover Performance", new string[] { "Average Crossover Performance per Generation" });
|
---|
175 | equalParentsHelper.InitializeChart(Results, "Number of equal parents", new string[] { "Absolut number of equal parents" });
|
---|
176 |
|
---|
177 | List<string> successfullCXRowNames = new List<string>();
|
---|
178 | foreach (var value in ComparisonFactorParameter.Value) {
|
---|
179 | successfullCXRowNames.Add(SuccessfullCrossoversRowName + value.Value.ToString());
|
---|
180 | }
|
---|
181 | successHelper.InitializeChart(Results, "Successfull Crossovers", successfullCXRowNames.ToArray());
|
---|
182 | Reset();
|
---|
183 | }
|
---|
184 |
|
---|
185 | public override IOperation Apply() {
|
---|
186 | Initialize();
|
---|
187 |
|
---|
188 | Point2D<double> qualityPoint, childDiversityToWorseParent, childDiversityToBetterParent, diversityPointParent, qualityPointParent;
|
---|
189 | var parent1 = ParentsParameter.ActualValue.First();
|
---|
190 | var parent2 = ParentsParameter.ActualValue.Last();
|
---|
191 | var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
|
---|
192 | var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
|
---|
193 | var child = ChildParameter.ActualValue;
|
---|
194 | var qualityChild = QualityParameter.ActualValue.Value;
|
---|
195 |
|
---|
196 | var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes.First(), ExecutionContext.Scope.SubScopes.Last());
|
---|
197 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
198 |
|
---|
199 | diversityPointParent = new Point2D<double>(cnt, parentDiversity);
|
---|
200 | qualityPointParent = new Point2D<double>(cnt, Math.Abs(qualityParent1 - qualityParent2));
|
---|
201 |
|
---|
202 | if (qualityParent1 > qualityParent2) {
|
---|
203 | if (MaximizationParameter.ActualValue.Value) {
|
---|
204 | childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
|
---|
205 | childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
|
---|
206 | } else {
|
---|
207 | childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
|
---|
208 | childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
|
---|
209 | }
|
---|
210 | } else {
|
---|
211 | if (MaximizationParameter.ActualValue.Value) {
|
---|
212 | childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
|
---|
213 | childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
|
---|
214 | } else {
|
---|
215 | childDiversityToWorseParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.Last()));
|
---|
216 | childDiversityToBetterParent = new Point2D<double>(cnt, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope, ExecutionContext.Scope.SubScopes.First()));
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
|
---|
221 | qualityPoint = new Point2D<double>(cnt++, worseQuality - qualityChild);
|
---|
222 | qualityPoints.Add(qualityPoint.Y);
|
---|
223 |
|
---|
224 | //track successfull cx
|
---|
225 | var compFactsArray = ComparisonFactorParameter.Value.ToArray();
|
---|
226 | for (int i = 0; i < compFactsArray.Length; i++) {
|
---|
227 | if (WeightedParentsQualityComparer.Compare(qualityChild, qualityParent1, qualityParent2, compFactsArray[i].Value, MaximizationParameter.ActualValue.Value)) {
|
---|
228 | success[i]++;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (parentDiversity == 1.0) {
|
---|
233 | equalParents++;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
237 | if (GenerationsParameter.ActualValue.Value > lastGeneration) {
|
---|
238 | double avg = qualityPoints.Average();
|
---|
239 |
|
---|
240 | performanceHelper.AddPoint(avg);
|
---|
241 | equalParentsHelper.AddPoint(equalParents);
|
---|
242 |
|
---|
243 | for (int i = 0; i < compFactsArray.Length; i++) {
|
---|
244 | successHelper.AddPoint(SuccessfullCrossoversRowName + compFactsArray[i].Value.ToString(), success[i]);
|
---|
245 | }
|
---|
246 |
|
---|
247 | Reset();
|
---|
248 | }
|
---|
249 |
|
---|
250 | plotHelper.AddPoint(curGenStr, qualityPoint);
|
---|
251 | childDiversityToWorseParentHelper.AddPoint(curGenStr, childDiversityToWorseParent);
|
---|
252 | childDiversityToBetterParentHelper.AddPoint(curGenStr, childDiversityToBetterParent);
|
---|
253 | parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
|
---|
254 | parentQualityHelper.AddPoint(curGenStr, qualityPointParent);
|
---|
255 | }
|
---|
256 |
|
---|
257 | return base.Apply();
|
---|
258 | }
|
---|
259 |
|
---|
260 | private void Reset() {
|
---|
261 | cnt = 0;
|
---|
262 | success = new int[ComparisonFactorParameter.Value.Count];
|
---|
263 | lastGeneration = GenerationsParameter.ActualValue.Value;
|
---|
264 | qualityPoints.Clear();
|
---|
265 | equalParents = 0;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | }
|
---|