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