Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs @ 5594

Last change on this file since 5594 was 5445, checked in by swagner, 13 years ago

Updated year of copyrights (#1406)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Interfaces;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers {
34  /// <summary>
35  /// An operator that tracks the frequencies of distinc symbols.
36  /// </summary>
37  [Item("SymbolicExpressionSymbolFrequencyAnalyzer", "An operator that tracks frequencies of symbols.")]
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";
43
44    #region parameter properties
45    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
46      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
47    }
48    public ILookupParameter<DataTable> SymbolFrequenciesParameter {
49      get { return (ILookupParameter<DataTable>)Parameters[SymbolFrequenciesParameterName]; }
50    }
51    public ILookupParameter<ResultCollection> ResultsParameter {
52      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
53    }
54    #endregion
55    #region properties
56    public DataTable SymbolFrequencies {
57      get { return SymbolFrequenciesParameter.ActualValue; }
58      set { SymbolFrequenciesParameter.ActualValue = value; }
59    }
60    #endregion
61
62    [StorableConstructor]
63    protected SymbolicExpressionSymbolFrequencyAnalyzer(bool deserializing) : base(deserializing) { }
64    protected SymbolicExpressionSymbolFrequencyAnalyzer(SymbolicExpressionSymbolFrequencyAnalyzer original, Cloner cloner) : base(original, cloner) { }
65    public SymbolicExpressionSymbolFrequencyAnalyzer()
66      : base() {
67      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
68      Parameters.Add(new ValueLookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies."));
69      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new SymbolicExpressionSymbolFrequencyAnalyzer(this, cloner);
73    }
74
75    public override IOperation Apply() {
76      ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
77      ResultCollection results = ResultsParameter.ActualValue;
78
79      if (SymbolFrequencies == null) {
80        SymbolFrequencies = new DataTable("Symbol frequencies", "Relative frequency of symbols aggregated over the whole population.");
81        SymbolFrequencies.VisualProperties.YAxisTitle = "Relative Symbol Frequency";
82        results.Add(new Result("Symbol frequencies", SymbolFrequencies));
83      }
84
85      // all rows must have the same number of values so we can just take the first
86      int numberOfValues = SymbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();
87
88      foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions)) {
89        if (!SymbolFrequencies.Rows.ContainsKey(pair.Key)) {
90          // initialize a new row for the symbol and pad with zeros
91          DataRow row = new DataRow(pair.Key, "", Enumerable.Repeat(0.0, numberOfValues));
92          row.VisualProperties.StartIndexZero = true;
93          SymbolFrequencies.Rows.Add(row);
94        }
95        SymbolFrequencies.Rows[pair.Key].Values.Add(pair.Value);
96      }
97
98      // add a zero for each data row that was not modified in the previous loop
99      foreach (var row in SymbolFrequencies.Rows.Where(r => r.Values.Count != numberOfValues + 1))
100        row.Values.Add(0.0);
101
102      return base.Apply();
103    }
104
105    public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<SymbolicExpressionTree> trees) {
106      Dictionary<string, double> symbolFrequencies = new Dictionary<string, double>();
107      int totalNumberOfSymbols = 0;
108
109      foreach (var tree in trees) {
110        foreach (var node in tree.IterateNodesPrefix()) {
111          if (symbolFrequencies.ContainsKey(node.Symbol.Name)) symbolFrequencies[node.Symbol.Name] += 1;
112          else symbolFrequencies.Add(node.Symbol.Name, 1);
113          totalNumberOfSymbols++;
114        }
115      }
116
117      foreach (var pair in symbolFrequencies)
118        yield return new KeyValuePair<string, double>(pair.Key, pair.Value / totalNumberOfSymbols);
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.