Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/Fragments/SymbolicExpressionTreeFragmentLengthsAnalyzer.cs @ 9238

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

#1772: Added base class for the fragment analyzers. Improved analyzers, added SymbolicExpressionTreeRelativeFragmentDepthAnalyzer. Added LineageExplorer.

File size: 6.1 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("SymbolicExpressionTreeFragmentLengthAnalyzer", "An operator that provides statistics about fragment lengths")]
41  [StorableClass]
42  public sealed class SymbolicExpressionTreeFragmentLengthAnalyzer : SymbolicExpressionTreeFragmentsAnalyzer {
43    private const string FragmentLengthsParameterName = "Fragment Lengths";
44    // impact values calculator
45    public ILookupParameter<DataTable> FragmentLengthsParameter { get { return (ILookupParameter<DataTable>)Parameters[FragmentLengthsParameterName]; } }
46    public DataTable FragmentLengths {
47      get { return FragmentLengthsParameter.ActualValue; }
48      set { FragmentLengthsParameter.ActualValue = value; }
49    }
50
51    [StorableConstructor]
52    private SymbolicExpressionTreeFragmentLengthAnalyzer(bool deserializing) : base(deserializing) { }
53    private SymbolicExpressionTreeFragmentLengthAnalyzer(SymbolicExpressionTreeFragmentLengthAnalyzer original, Cloner cloner) : base(original, cloner) { }
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new SymbolicExpressionTreeFragmentLengthAnalyzer(this, cloner);
56    }
57    public SymbolicExpressionTreeFragmentLengthAnalyzer()
58      : base() {
59      #region Add parameters
60      Parameters.Add(new LookupParameter<DataTable>(FragmentLengthsParameterName));
61      #endregion
62
63      UpdateCounterParameter.Hidden = true;
64      UpdateIntervalParameter.Hidden = true;
65    }
66    #region After deserialization code
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69    }
70
71    #endregion
72    #region IStatefulItem members
73    public override void InitializeState() {
74      base.InitializeState();
75      UpdateCounter.Value = 0;
76    }
77
78    public override void ClearState() {
79      base.ClearState();
80      UpdateCounter.Value = 0;
81    }
82    #endregion
83
84    public override IOperation Apply() {
85      UpdateCounter.Value++;
86      if (UpdateCounter.Value == UpdateInterval.Value) {
87        UpdateCounter.Value = 0; // reset counter
88        if (Generations.Value > 1) {
89          SecondaryTraceMap = SecondaryTraceMap ?? new TraceMapType();
90          SecondaryFragmentMap = SecondaryFragmentMap ?? new CloneMapType();
91          SecondaryCloneMap = SecondaryCloneMap ?? new CloneMapType();
92
93          InitializeParameters();
94          InitializeRows();
95
96          var fragmentLengths = (from m in SecondaryFragmentMap
97                                 let fragment = (IFragment)m.Value
98                                 where fragment.Root != null
99                                 select fragment.Length
100                               ).ToList();
101
102          // fragments of selected offspring
103          var selected = new HashSet<ISymbolicExpressionTree>(GlobalTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()));
104          var goodFragmentlengths = (from m in SecondaryFragmentMap
105                                     let tree = (ISymbolicExpressionTree)m.Key
106                                     let fragment = (IFragment)m.Value
107                                     where fragment.Root != null
108                                     where selected.Contains(tree)
109                                     select fragment.Length
110                                    ).ToList();
111
112          FragmentLengths.Rows["Average fragment length (all)"].Values.Add(fragmentLengths.Count > 0 ? fragmentLengths.Average() : 0.0);
113          FragmentLengths.Rows["Average fragment length (good)"].Values.Add(goodFragmentlengths.Count > 0 ? goodFragmentlengths.Average() : 0.0);
114        }
115      }
116      return base.Apply();
117    }
118
119    protected override void InitializeParameters() {
120      var results = ResultsParameter.ActualValue;
121      if (!results.ContainsKey("Fragment lengths")) {
122        FragmentLengths = FragmentLengths ?? new DataTable("Fragment lengths") { VisualProperties = { YAxisTitle = "Number of nodes" } };
123        results.Add(new Result("Fragment lengths", FragmentLengths));
124      }
125      base.InitializeParameters();
126    }
127
128    private void InitializeRows() {
129      string[] rowNames =
130        {
131          "Average fragment length (good)", "Average fragment length (all)"
132        };
133      foreach (var rowName in rowNames.Where(rowName => !FragmentLengths.Rows.ContainsKey(rowName))) {
134        FragmentLengths.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true } });
135      }
136    }
137  } //SymbolicExpressionTreeFragmentLengthAnalyzer
138}
Note: See TracBrowser for help on using the repository browser.