Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/CrossoverPerformanceAnalyzer.cs @ 8336

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

#1886 worked on crossover performance analyzer

File size: 6.0 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.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
35  [Item("CrossoverPerformanceAnalyzer", "An operator that analyzes the performance of crossovers.")]
36  [StorableClass]
37  public class CrossoverPerformanceAnalyzer : SingleSuccessorOperator, IAnalyzer {
38    private const string ResultsParameterName = "Results";
39    private const string GenerationsParameterName = "Generations";
40
41    #region IAnalyzer Members
42    public bool EnabledByDefault {
43      get { return true; }
44    }
45    #endregion
46
47
48    #region Parameter properties
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    #endregion
68
69    #region Properties
70    public ResultCollection Results {
71      get { return ResultsParameter.ActualValue; }
72    }
73    #endregion
74
75    ScatterPlot plot;
76    int cnt = 0;
77
78    [StorableConstructor]
79    private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
80    private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
81    public CrossoverPerformanceAnalyzer()
82      : base() {
83      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
84      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
85
86      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
87      ParentsParameter.ActualName = "TSPTour";
88
89      Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
90      ChildParameter.ActualName = "TSPTour";
91
92      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
93      ParentsQualityParameter.ActualName = "TSPTourLength";
94
95      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
96      QualityParameter.ActualName = "TSPTourLength";
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new CrossoverPerformanceAnalyzer(this, cloner);
101    }
102
103    public override IOperation Apply() {
104      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
105      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
106      Point2D<double> curPoint;
107
108      double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
109      curPoint = new Point2D<double>(cnt++, worseQuality - QualityParameter.ActualValue.Value);
110
111      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
112      ScatterPlotDataRow row;
113
114      if (!Results.ContainsKey("Scatter Plot")) {
115        InitializePlot();
116        Results.Add(new Result("Scatter Plot", plot));
117        Results.Add(new Result("Scatter Plot History", new ScatterPlotHistory()));
118        cnt = 0;
119      }
120
121      if (!plot.Rows.ContainsKey(curGenStr)) {
122        if (GenerationsParameter.ActualValue.Value != 0) {
123          ((ScatterPlotHistory)Results["Scatter Plot History"].Value).Add(plot);
124          InitializePlot();
125          Results["Scatter Plot"].Value = plot;
126          cnt = 0;
127        }
128
129        var points = new List<Point2D<double>>();
130        points.Add(curPoint);
131        row = new ScatterPlotDataRow(curGenStr, null, points);
132        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
133        row.VisualProperties.PointSize = 5;
134        plot.Rows.Add(row);
135      } else {
136        plot.Rows[curGenStr].Points.Add(curPoint);
137      }
138
139      return base.Apply();
140    }
141
142    private void InitializePlot() {
143      plot = new ScatterPlot("Crossover Performance", null);
144      plot.VisualProperties.XAxisTitle = "Solution Index";
145      plot.VisualProperties.YAxisTitle = "Absolut Quality Difference";
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.