Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs @ 5983

Last change on this file since 5983 was 5983, checked in by mkommend, 13 years ago

#1418: Added after deserialization hook to SymbolicExpressionSymbolFrequencyAnalyzer for newly added parameter.

File size: 7.4 KB
RevLine 
[5386]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5386]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;
[5983]27using HeuristicLab.Data;
[5386]28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
[5499]33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[5386]34  /// <summary>
[5499]35  /// An operator that tracks the frequencies of distinct symbols in symbolic expression trees.
[5386]36  /// </summary>
[5499]37  [Item("SymbolicExpressionSymbolFrequencyAnalyzer", "An operator that tracks frequencies of symbols in symbolic expression trees.")]
[5386]38  [StorableClass]
39  public class SymbolicExpressionSymbolFrequencyAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
40    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
41    private const string ResultsParameterName = "Results";
42    private const string SymbolFrequenciesParameterName = "SymbolFrequencies";
[5971]43    private const string AggregateSymbolsWithDifferentSubtreeCountParameterName = "AggregateSymbolsWithDifferentSubtreeCount";
[5386]44
45    #region parameter properties
[5510]46    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
47      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
[5386]48    }
49    public ILookupParameter<DataTable> SymbolFrequenciesParameter {
50      get { return (ILookupParameter<DataTable>)Parameters[SymbolFrequenciesParameterName]; }
51    }
52    public ILookupParameter<ResultCollection> ResultsParameter {
53      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
54    }
[5971]55    public IValueParameter<BoolValue> AggregateSymbolsWithDifferentSubtreeCountParameter {
56      get { return (IValueParameter<BoolValue>)Parameters[AggregateSymbolsWithDifferentSubtreeCountParameterName]; }
57    }
[5386]58    #endregion
59    #region properties
[5971]60    public BoolValue AggregrateSymbolsWithDifferentSubtreeCount {
61      get { return AggregateSymbolsWithDifferentSubtreeCountParameter.Value; }
62      set { AggregateSymbolsWithDifferentSubtreeCountParameter.Value = value; }
[5386]63    }
64    #endregion
65
66    [StorableConstructor]
67    protected SymbolicExpressionSymbolFrequencyAnalyzer(bool deserializing) : base(deserializing) { }
68    protected SymbolicExpressionSymbolFrequencyAnalyzer(SymbolicExpressionSymbolFrequencyAnalyzer original, Cloner cloner) : base(original, cloner) { }
69    public SymbolicExpressionSymbolFrequencyAnalyzer()
70      : base() {
[5510]71      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
[5971]72      Parameters.Add(new LookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies."));
[5499]73      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the symbol frequencies should be stored."));
[5971]74      Parameters.Add(new ValueParameter<BoolValue>(AggregateSymbolsWithDifferentSubtreeCountParameterName, "Flag that indicates if the frequencies of symbols with the same name but different number of sub-trees should be aggregated.", new BoolValue(true)));
[5386]75    }
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new SymbolicExpressionSymbolFrequencyAnalyzer(this, cloner);
78    }
79
[5983]80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      #region remove with HL 3.4
83      if (!Parameters.ContainsKey(AggregateSymbolsWithDifferentSubtreeCountParameterName))
84        Parameters.Add(new ValueParameter<BoolValue>(AggregateSymbolsWithDifferentSubtreeCountParameterName, "Flag that indicates if the frequencies of symbols with the same name but different number of sub-trees should be aggregated.", new BoolValue(true)));
85      #endregion
86    }
87
[5386]88    public override IOperation Apply() {
[5510]89      ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
[5386]90      ResultCollection results = ResultsParameter.ActualValue;
[5971]91      DataTable symbolFrequencies = SymbolFrequenciesParameter.ActualValue;
92      if (symbolFrequencies == null) {
93        symbolFrequencies = new DataTable("Symbol frequencies", "Relative frequency of symbols aggregated over the whole population.");
94        symbolFrequencies.VisualProperties.YAxisTitle = "Relative Symbol Frequency";
[5386]95
[5971]96        SymbolFrequenciesParameter.ActualValue = symbolFrequencies;
97        results.Add(new Result("Symbol frequencies", symbolFrequencies));
[5386]98      }
99
[5392]100      // all rows must have the same number of values so we can just take the first
[5971]101      int numberOfValues = symbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();
[5392]102
[5971]103      foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions, AggregrateSymbolsWithDifferentSubtreeCount.Value)) {
104        if (!symbolFrequencies.Rows.ContainsKey(pair.Key)) {
[5392]105          // initialize a new row for the symbol and pad with zeros
106          DataRow row = new DataRow(pair.Key, "", Enumerable.Repeat(0.0, numberOfValues));
[5386]107          row.VisualProperties.StartIndexZero = true;
[5971]108          symbolFrequencies.Rows.Add(row);
[5386]109        }
[5971]110        symbolFrequencies.Rows[pair.Key].Values.Add(pair.Value);
[5386]111      }
112
[5392]113      // add a zero for each data row that was not modified in the previous loop
[5971]114      foreach (var row in symbolFrequencies.Rows.Where(r => r.Values.Count != numberOfValues + 1))
[5386]115        row.Values.Add(0.0);
116
117      return base.Apply();
118    }
119
[5971]120    public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateDifferentNumberOfSubtrees = true) {
[5386]121      Dictionary<string, double> symbolFrequencies = new Dictionary<string, double>();
122      int totalNumberOfSymbols = 0;
123
124      foreach (var tree in trees) {
125        foreach (var node in tree.IterateNodesPrefix()) {
[5971]126          string symbolName;
127          if (aggregateDifferentNumberOfSubtrees) symbolName = node.Symbol.Name;
128          else symbolName = node.Symbol.Name + "-" + node.SubtreesCount;
129          if (symbolFrequencies.ContainsKey(symbolName)) symbolFrequencies[symbolName] += 1;
130          else symbolFrequencies.Add(symbolName, 1);
[5386]131          totalNumberOfSymbols++;
132        }
133      }
134
[5392]135      foreach (var pair in symbolFrequencies)
136        yield return new KeyValuePair<string, double>(pair.Key, pair.Value / totalNumberOfSymbols);
[5386]137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.