Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisTraceOverlapAnalyzer.cs @ 14536

Last change on this file since 14536 was 12966, checked in by bburlacu, 9 years ago

#1772: Fixed unit test errors.

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.EvolutionTracking;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
35  [Item("SymbolicDataAnalysisTraceOverlapAnalyzer", "An analyzer for trace graph overlap.")]
36  [StorableClass]
37  public class SymbolicDataAnalysisTraceOverlapAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
38    private const string PercentageOfBestSolutionsParameterName = "PercentageOfBestSolutions";
39
40    #region parameters
41    public IFixedValueParameter<DoubleValue> PercentageOfBestSolutionsParameter {
42      get { return (IFixedValueParameter<DoubleValue>)Parameters[PercentageOfBestSolutionsParameterName]; }
43    }
44    #endregion
45
46    public SymbolicDataAnalysisTraceOverlapAnalyzer() {
47      UpdateCounterParameter.ActualName = "TraceOverlapAnalyzerUpdateCounter";
48      Parameters.Add(new FixedValueParameter<DoubleValue>(PercentageOfBestSolutionsParameterName, new DoubleValue(1.0)));
49    }
50
51    protected SymbolicDataAnalysisTraceOverlapAnalyzer(SymbolicDataAnalysisTraceOverlapAnalyzer original, Cloner cloner)
52      : base(original, cloner) {
53    }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new SymbolicDataAnalysisTraceOverlapAnalyzer(this, cloner);
57    }
58
59    [StorableConstructor]
60    protected SymbolicDataAnalysisTraceOverlapAnalyzer(bool deserializing) : base(deserializing) { }
61
62    public override IOperation Apply() {
63      int updateInterval = UpdateIntervalParameter.Value.Value;
64      IntValue updateCounter = UpdateCounterParameter.ActualValue;
65      // if counter does not yet exist then initialize it with update interval
66      // to make sure the solutions are analyzed on the first application of this operator
67      if (updateCounter == null) {
68        updateCounter = new IntValue(updateInterval);
69        UpdateCounterParameter.ActualValue = updateCounter;
70      }
71      //analyze solutions only every 'updateInterval' times
72      if (updateCounter.Value != updateInterval) {
73        updateCounter.Value++;
74        return base.Apply();
75      }
76      updateCounter.Value = 1;
77
78      if (PopulationGraph == null)
79        return base.Apply();
80
81      var bestSolutionsPercentage = PercentageOfBestSolutionsParameter.Value.Value;
82      int n = (int)Math.Round(bestSolutionsPercentage * PopulationSize.Value);
83      double avgTraceOverlap = 0;
84
85      if (Generation.Value > 0) {
86        var nodes = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().OrderByDescending(x => x.Quality).Take(n);
87        avgTraceOverlap = nodes.Average(x => TraceCalculator.TraceSubtree(x, 2).Vertices.Count());
88      }
89
90      DataTable table;
91      if (!Results.ContainsKey("AverageTraceOverlap")) {
92        table = new DataTable("Average trace overlap");
93        var row = new DataRow("Average trace overlap") { VisualProperties = { StartIndexZero = true } };
94        row.Values.Add(avgTraceOverlap);
95        table.Rows.Add(row);
96        Results.Add(new Result("AverageTraceOverlap", table));
97      } else {
98        table = (DataTable)Results["AverageTraceOverlap"].Value;
99        table.Rows["Average trace overlap"].Values.Add(avgTraceOverlap);
100      }
101      return base.Apply();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.