1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace 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 | foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions)) {
|
---|
86 | if (SymbolFrequencies.Rows.ContainsKey(pair.Key))
|
---|
87 | SymbolFrequencies.Rows[pair.Key].Values.Add(pair.Value);
|
---|
88 | else {
|
---|
89 | int missingValues = SymbolFrequencies.Rows.Select(r => r.Values.Count()-1).DefaultIfEmpty().Max();
|
---|
90 | List<double> values = new List<double>(Enumerable.Repeat(0.0, missingValues));
|
---|
91 | values.Add(pair.Value);
|
---|
92 | DataRow row = new DataRow(pair.Key, "", values);
|
---|
93 | row.VisualProperties.StartIndexZero = true;
|
---|
94 | SymbolFrequencies.Rows.Add(row);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | int maxValues = SymbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().Max();
|
---|
99 | foreach (var row in SymbolFrequencies.Rows.Where(r => r.Values.Count != maxValues))
|
---|
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 (string symbolName in symbolFrequencies.Keys.ToList())
|
---|
118 | symbolFrequencies[symbolName] /= totalNumberOfSymbols;
|
---|
119 | return symbolFrequencies;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|