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("PathRelinkerPerformanceAnalyzer", "An operator that analyzes the performance of path relinkers.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class PathRelinkerPerformanceAnalyzer : 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 | #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<ItemArray<Permutation>> ParentsParameter {
|
---|
57 | get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Parents"]; }
|
---|
58 | }
|
---|
59 | public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
|
---|
60 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
|
---|
61 | }
|
---|
62 | public ILookupParameter<ItemArray<Permutation>> ChildParameter {
|
---|
63 | get { return ((ScopeTreeLookupParameter<Permutation>)Parameters["Child"]); }
|
---|
64 | }
|
---|
65 | public IValueParameter<SingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
66 | get { return (IValueParameter<SingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
67 | }
|
---|
68 | public ILookupParameter<TSPRoundedEuclideanPathEvaluator> EvaluatorParameter {
|
---|
69 | get { return (ILookupParameter<TSPRoundedEuclideanPathEvaluator>)Parameters["Evaluator"]; }
|
---|
70 | }
|
---|
71 | private ScopeParameter CurrentScopeParameter {
|
---|
72 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
73 | }
|
---|
74 | public IScope CurrentScope {
|
---|
75 | get { return CurrentScopeParameter.ActualValue; }
|
---|
76 | }
|
---|
77 | public ILookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
78 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | #region Properties
|
---|
83 | public ResultCollection Results {
|
---|
84 | get { return ResultsParameter.ActualValue; }
|
---|
85 | }
|
---|
86 | #endregion
|
---|
87 |
|
---|
88 | [Storable]
|
---|
89 | private ScatterPlotHelper plotHelper, childDiversityHelper, parentDiversityHelper;
|
---|
90 | [Storable]
|
---|
91 | private DataTableHelper performanceHelper, successHelper, equalParentsHelper;
|
---|
92 | [Storable]
|
---|
93 | private int cnt = 0;
|
---|
94 | [Storable]
|
---|
95 | private int cntChild = 0;
|
---|
96 | [Storable]
|
---|
97 | private int success = 0;
|
---|
98 | [Storable]
|
---|
99 | private int lastGeneration = 0;
|
---|
100 | [Storable]
|
---|
101 | private int equalParents = 0;
|
---|
102 | [Storable]
|
---|
103 | private List<double> qualityPoints = new List<double>();
|
---|
104 |
|
---|
105 | [StorableConstructor]
|
---|
106 | private PathRelinkerPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
107 | private PathRelinkerPerformanceAnalyzer(PathRelinkerPerformanceAnalyzer original, Cloner cloner)
|
---|
108 | : base(original, cloner) {
|
---|
109 | cnt = original.cnt;
|
---|
110 | cntChild = original.cntChild;
|
---|
111 | success = original.success;
|
---|
112 | lastGeneration = original.lastGeneration;
|
---|
113 | equalParents = original.equalParents;
|
---|
114 | qualityPoints = new List<double>(original.qualityPoints);
|
---|
115 | plotHelper = (ScatterPlotHelper)original.plotHelper.Clone(cloner);
|
---|
116 | childDiversityHelper = (ScatterPlotHelper)original.childDiversityHelper.Clone(cloner);
|
---|
117 | parentDiversityHelper = (ScatterPlotHelper)original.parentDiversityHelper.Clone(cloner);
|
---|
118 | performanceHelper = (DataTableHelper)original.performanceHelper.Clone(cloner);
|
---|
119 | successHelper = (DataTableHelper)original.successHelper.Clone(cloner);
|
---|
120 | equalParentsHelper = (DataTableHelper)original.equalParentsHelper.Clone(cloner);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public PathRelinkerPerformanceAnalyzer()
|
---|
124 | : base() {
|
---|
125 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
126 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
127 | GenerationsParameter.ActualName = "Iterations";
|
---|
128 |
|
---|
129 | Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
|
---|
130 | ParentsParameter.ActualName = "TSPTour";
|
---|
131 |
|
---|
132 | Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Child", "The child permutation resulting from the crossover.", 2));
|
---|
133 | ChildParameter.ActualName = "TSPTour";
|
---|
134 |
|
---|
135 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
|
---|
136 | ParentsQualityParameter.ActualName = "TSPTourLength";
|
---|
137 |
|
---|
138 | Parameters.Add(new ValueParameter<SingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator"));
|
---|
139 |
|
---|
140 | Parameters.Add(new LookupParameter<TSPRoundedEuclideanPathEvaluator>("Evaluator"));
|
---|
141 |
|
---|
142 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose variables and sub-scopes should be removed."));
|
---|
143 |
|
---|
144 | Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
145 |
|
---|
146 | plotHelper = new ScatterPlotHelper(false, true);
|
---|
147 | childDiversityHelper = new ScatterPlotHelper(false, true);
|
---|
148 | parentDiversityHelper = new ScatterPlotHelper(false, true);
|
---|
149 | performanceHelper = new DataTableHelper();
|
---|
150 | successHelper = new DataTableHelper();
|
---|
151 | equalParentsHelper = new DataTableHelper();
|
---|
152 | }
|
---|
153 |
|
---|
154 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
155 | return new PathRelinkerPerformanceAnalyzer(this, cloner);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public override IOperation Apply() {
|
---|
159 | SimilarityCalculatorParameter.Value.QualityVariableName = "TSPTourLength";
|
---|
160 | SimilarityCalculatorParameter.Value.SolutionVariableName = "TSPTour";
|
---|
161 |
|
---|
162 | plotHelper.InitializePlot(Results, "Crossover Performance", "Solution Index", "Absolut Quality Difference");
|
---|
163 | childDiversityHelper.InitializePlot(Results, "Child Diversity", "Solution Index", "Diversity");
|
---|
164 | parentDiversityHelper.InitializePlot(Results, "Parent Diversity", "Solution Index", "Diversity");
|
---|
165 |
|
---|
166 | performanceHelper.InitializeChart(Results, "Average Crossover Performance", "Average Crossover Performance per Generation");
|
---|
167 | successHelper.InitializeChart(Results, "Successfull Crossovers", "Successfull Crossovers per Generation");
|
---|
168 | equalParentsHelper.InitializeChart(Results, "Number of equal parents", "Absolut number of equal parents");
|
---|
169 |
|
---|
170 | Point2D<double> qualityPoint, diversityPointChild, diversityPointParent;
|
---|
171 |
|
---|
172 | var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
|
---|
173 | var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
|
---|
174 | var childs = ChildParameter.ActualValue;
|
---|
175 | var parent1 = ParentsParameter.ActualValue.First();
|
---|
176 | var parent2 = ParentsParameter.ActualValue.Last();
|
---|
177 | var parentDiversity = SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(ExecutionContext.Scope.SubScopes[0], ExecutionContext.Scope.SubScopes[1]);
|
---|
178 | string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
|
---|
179 |
|
---|
180 |
|
---|
181 | diversityPointParent = new Point2D<double>(cnt++, parentDiversity);
|
---|
182 | List<Point2D<double>> tmpDiversityPointChilds = new List<Point2D<double>>();
|
---|
183 |
|
---|
184 | double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
|
---|
185 | if (qualityParent1 > qualityParent2) {
|
---|
186 | foreach (var child in ExecutionContext.Scope.SubScopes.Last().SubScopes) {
|
---|
187 | diversityPointChild = new Point2D<double>(cntChild++, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(child, ExecutionContext.Scope.SubScopes[0]));
|
---|
188 | tmpDiversityPointChilds.Add(diversityPointChild);
|
---|
189 | }
|
---|
190 | } else {
|
---|
191 | foreach (var child in ExecutionContext.Scope.SubScopes.Last().SubScopes) {
|
---|
192 | diversityPointChild = new Point2D<double>(cntChild++, SimilarityCalculatorParameter.Value.CalculateSolutionSimilarity(child, ExecutionContext.Scope.SubScopes[1]));
|
---|
193 | tmpDiversityPointChilds.Add(diversityPointChild);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | List<Point2D<double>> tmpQualityPoints = new List<Point2D<double>>();
|
---|
198 | int tmpCnt = cntChild - childs.Length;
|
---|
199 | foreach (var child in childs) {
|
---|
200 | double childQuality = TSPRoundedEuclideanPathEvaluator.Apply(EvaluatorParameter.ActualValue, CoordinatesParameter.ActualValue, child);
|
---|
201 |
|
---|
202 | qualityPoint = new Point2D<double>(tmpCnt++, worseQuality - childQuality);
|
---|
203 | tmpQualityPoints.Add(qualityPoint);
|
---|
204 | if ((worseQuality - childQuality) > 0) {
|
---|
205 | success++;
|
---|
206 | }
|
---|
207 | qualityPoints.Add(qualityPoint.Y);
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (TSPSimilarityCalculator.CalculateSimilarity(parent1, parent2) == 1.0) {
|
---|
211 | equalParents++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (GenerationsParameter.ActualValue.Value != 0) {
|
---|
215 | if (GenerationsParameter.ActualValue.Value > lastGeneration) {
|
---|
216 | double avg = qualityPoints.Average();
|
---|
217 |
|
---|
218 | performanceHelper.AddPoint(avg);
|
---|
219 | successHelper.AddPoint(success);
|
---|
220 | equalParentsHelper.AddPoint(equalParents);
|
---|
221 |
|
---|
222 | Reset();
|
---|
223 | }
|
---|
224 |
|
---|
225 | tmpQualityPoints.ForEach(x => plotHelper.AddPoint(curGenStr, x));
|
---|
226 | tmpDiversityPointChilds.ForEach(x => childDiversityHelper.AddPoint(curGenStr, x));
|
---|
227 | parentDiversityHelper.AddPoint(curGenStr, diversityPointParent);
|
---|
228 | } else {
|
---|
229 | Reset();
|
---|
230 | }
|
---|
231 |
|
---|
232 | return base.Apply();
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void Reset() {
|
---|
236 | cnt = 0;
|
---|
237 | success = 0;
|
---|
238 | lastGeneration = GenerationsParameter.ActualValue.Value;
|
---|
239 | qualityPoints.Clear();
|
---|
240 | equalParents = 0;
|
---|
241 | cntChild = 0;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|