Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 worked on crossover performance analyzer

File size: 6.4 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    DataRow dtRow;
77    int cnt = 0;
78
79    [StorableConstructor]
80    private CrossoverPerformanceAnalyzer(bool deserializing) : base(deserializing) { }
81    private CrossoverPerformanceAnalyzer(CrossoverPerformanceAnalyzer original, Cloner cloner) : base(original, cloner) { }
82    public CrossoverPerformanceAnalyzer()
83      : base() {
84      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
85      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
86
87      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Parents", "The parent permutations which have been crossed."));
88      ParentsParameter.ActualName = "TSPTour";
89
90      Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
91      ChildParameter.ActualName = "TSPTour";
92
93      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
94      ParentsQualityParameter.ActualName = "TSPTourLength";
95
96      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the child solution."));
97      QualityParameter.ActualName = "TSPTourLength";
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new CrossoverPerformanceAnalyzer(this, cloner);
102    }
103
104    public override IOperation Apply() {
105      var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
106      var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
107      Point2D<double> curPoint;
108
109      double worseQuality = qualityParent1 > qualityParent2 ? qualityParent1 : qualityParent2;
110      curPoint = new Point2D<double>(cnt++, worseQuality - QualityParameter.ActualValue.Value);
111
112      string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
113      ScatterPlotDataRow row;
114
115      if (!Results.ContainsKey("Scatter Plot")) {
116        InitializePlot();
117        Results.Add(new Result("Scatter Plot", plot));
118        Results.Add(new Result("Scatter Plot History", new ScatterPlotHistory()));
119        cnt = 0;
120
121        DataTable dt = new DataTable("Average Crossover Performance");
122        dtRow = new DataRow("Average Crossover Performance per Generation");
123        dt.Rows.Add(dtRow);
124        Results.Add(new Result("Average Crossover Performance", dt));
125      }
126
127      if (!plot.Rows.ContainsKey(curGenStr)) {
128        if (GenerationsParameter.ActualValue.Value != 0) {
129          double avg = plot.Rows[(GenerationsParameter.ActualValue.Value - 1).ToString()].Points.Average(x => x.Y);
130          dtRow.Values.Add(avg);
131
132          ((ScatterPlotHistory)Results["Scatter Plot History"].Value).Add(plot);
133          InitializePlot();
134          Results["Scatter Plot"].Value = plot;
135          cnt = 0;
136        }
137
138        var points = new List<Point2D<double>>();
139        points.Add(curPoint);
140        row = new ScatterPlotDataRow(curGenStr, null, points);
141        row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle;
142        row.VisualProperties.PointSize = 5;
143        plot.Rows.Add(row);
144      } else {
145        plot.Rows[curGenStr].Points.Add(curPoint);
146      }
147
148      return base.Apply();
149    }
150
151    private void InitializePlot() {
152      plot = new ScatterPlot("Crossover Performance", null);
153      plot.VisualProperties.XAxisTitle = "Solution Index";
154      plot.VisualProperties.YAxisTitle = "Absolut Quality Difference";
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.