Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeRelativeFragmentDepthAnalyzer.cs @ 9082

Last change on this file since 9082 was 9082, checked in by bburlacu, 11 years ago

#1772: Renamed and refactored genealogy graph components. Added SymbolGraph and FPGraph (frequent pattern graph). Added evolvability and genetic operator average improvement 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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31// type definitions for convenience
32using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
33using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
34
35namespace HeuristicLab.EvolutionaryTracking {
36  /// <summary>
37  /// An operator that gathers information on tree fragments that get passed from one generation to the next via genetic operators
38  /// (Tracking of genetic variance and heredity)
39  /// </summary>
40  [Item("SymbolicExpressionTreeRelativeFragmentDepthAnalyzer", "An operator that provides statistics about crossover fragments")]
41  [StorableClass]
42  public sealed class SymbolicExpressionTreeRelativeFragmentDepthAnalyzer : SymbolicExpressionTreeFragmentsAnalyzer {
43    private const string RelativeFragmentDepthParameterName = "Relative fragment depth";
44    // impact values calculator
45    public ILookupParameter<DataTable> RelativeFragmentDepthParameter { get { return (ILookupParameter<DataTable>)Parameters[RelativeFragmentDepthParameterName]; } }
46
47    public DataTable RelativeFragmentDepths {
48      get { return RelativeFragmentDepthParameter.ActualValue; }
49      set { RelativeFragmentDepthParameter.ActualValue = value; }
50    }
51
52    [StorableConstructor]
53    private SymbolicExpressionTreeRelativeFragmentDepthAnalyzer(bool deserializing) : base(deserializing) { }
54    private SymbolicExpressionTreeRelativeFragmentDepthAnalyzer(SymbolicExpressionTreeRelativeFragmentDepthAnalyzer original, Cloner cloner) : base(original, cloner) { }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new SymbolicExpressionTreeRelativeFragmentDepthAnalyzer(this, cloner);
57    }
58    public SymbolicExpressionTreeRelativeFragmentDepthAnalyzer()
59      : base() {
60      #region Add parameters
61      Parameters.Add(new LookupParameter<DataTable>(RelativeFragmentDepthParameterName));
62      #endregion
63
64      UpdateCounterParameter.Hidden = true;
65      UpdateIntervalParameter.Hidden = true;
66
67    }
68    #region After deserialization code
69
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72    }
73
74    #endregion
75    #region IStatefulItem members
76    public override void InitializeState() {
77      base.InitializeState();
78      UpdateCounter.Value = 0;
79    }
80
81    public override void ClearState() {
82      base.ClearState();
83      UpdateCounter.Value = 0;
84    }
85    #endregion
86
87    public override IOperation Apply() {
88      UpdateCounter.Value++;
89      if (UpdateCounter.Value == UpdateInterval.Value) {
90        UpdateCounter.Value = 0; // reset counter
91        if (Generations.Value > 1) {
92          SecondaryTraceMap = SecondaryTraceMap ?? new TraceMapType();
93          SecondaryFragmentMap = SecondaryFragmentMap ?? new CloneMapType();
94          SecondaryCloneMap = SecondaryCloneMap ?? new CloneMapType();
95
96          InitializeParameters();
97          InitializeRows();
98
99          var fragmentDepths = (from m in SecondaryFragmentMap
100                                let tree = (ISymbolicExpressionTree)m.Key
101                                let fragment = (IFragment)m.Value
102                                where fragment.Root != null
103                                select tree.Root.GetBranchLevel(fragment.Root)
104                               ).ToList();
105
106          // fragments of selected offspring
107          var selected = new HashSet<ISymbolicExpressionTree>(GlobalTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()));
108          var goodFragmentDepths = (from m in SecondaryFragmentMap
109                                    let tree = (ISymbolicExpressionTree)m.Key
110                                    let fragment = (IFragment)m.Value
111                                    where fragment.Root != null
112                                    where selected.Contains(tree)
113                                    select tree.Root.GetBranchLevel(fragment.Root)
114                                   ).ToList();
115
116          RelativeFragmentDepths.Rows["Average relative fragment depth (all)"].Values.Add(fragmentDepths.Count > 0 ? fragmentDepths.Average() : 0.0);
117          RelativeFragmentDepths.Rows["Average relative fragment depth (good)"].Values.Add(goodFragmentDepths.Count > 0 ? goodFragmentDepths.Average() : 0.0);
118        }
119      }
120      return base.Apply();
121    }
122
123    protected override void InitializeParameters() {
124      var results = ResultsParameter.ActualValue;
125      if (!results.ContainsKey("Relative fragment depths")) {
126        RelativeFragmentDepths = RelativeFragmentDepths ?? new DataTable("Relative fragment depths") { VisualProperties = { YAxisTitle = "Relative depth" } };
127        results.Add(new Result("Relative fragment depths", RelativeFragmentDepths));
128      }
129      base.InitializeParameters();
130    }
131
132    private void InitializeRows() {
133      string[] rowNames =
134        {
135          "Average relative fragment depth (good)", "Average relative fragment depth (all)"
136        };
137      foreach (var rowName in rowNames.Where(rowName => !RelativeFragmentDepths.Rows.ContainsKey(rowName))) {
138        RelativeFragmentDepths.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true } });
139      }
140    }
141  } //SymbolicExpressionTreeFragmentLengthAnalyzer
142}
Note: See TracBrowser for help on using the repository browser.