Changeset 5971
- Timestamp:
- 04/07/11 07:49:15 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs
r5809 r5971 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Data; 31 32 32 33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 40 41 private const string ResultsParameterName = "Results"; 41 42 private const string SymbolFrequenciesParameterName = "SymbolFrequencies"; 43 private const string AggregateSymbolsWithDifferentSubtreeCountParameterName = "AggregateSymbolsWithDifferentSubtreeCount"; 42 44 43 45 #region parameter properties … … 51 53 get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; } 52 54 } 55 public IValueParameter<BoolValue> AggregateSymbolsWithDifferentSubtreeCountParameter { 56 get { return (IValueParameter<BoolValue>)Parameters[AggregateSymbolsWithDifferentSubtreeCountParameterName]; } 57 } 53 58 #endregion 54 59 #region properties 55 public DataTable SymbolFrequencies{56 get { return SymbolFrequenciesParameter.ActualValue; }57 set { SymbolFrequenciesParameter.ActualValue = value; }60 public BoolValue AggregrateSymbolsWithDifferentSubtreeCount { 61 get { return AggregateSymbolsWithDifferentSubtreeCountParameter.Value; } 62 set { AggregateSymbolsWithDifferentSubtreeCountParameter.Value = value; } 58 63 } 59 64 #endregion … … 65 70 : base() { 66 71 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze.")); 67 Parameters.Add(new ValueLookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies."));72 Parameters.Add(new LookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies.")); 68 73 Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the symbol frequencies should be stored.")); 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))); 69 75 } 70 76 public override IDeepCloneable Clone(Cloner cloner) { … … 75 81 ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue; 76 82 ResultCollection results = ResultsParameter.ActualValue; 83 DataTable symbolFrequencies = SymbolFrequenciesParameter.ActualValue; 84 if (symbolFrequencies == null) { 85 symbolFrequencies = new DataTable("Symbol frequencies", "Relative frequency of symbols aggregated over the whole population."); 86 symbolFrequencies.VisualProperties.YAxisTitle = "Relative Symbol Frequency"; 77 87 78 if (SymbolFrequencies == null) { 79 SymbolFrequencies = new DataTable("Symbol frequencies", "Relative frequency of symbols aggregated over the whole population."); 80 SymbolFrequencies.VisualProperties.YAxisTitle = "Relative Symbol Frequency"; 81 results.Add(new Result("Symbol frequencies", SymbolFrequencies)); 88 SymbolFrequenciesParameter.ActualValue = symbolFrequencies; 89 results.Add(new Result("Symbol frequencies", symbolFrequencies)); 82 90 } 83 91 84 92 // all rows must have the same number of values so we can just take the first 85 int numberOfValues = SymbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();93 int numberOfValues = symbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First(); 86 94 87 foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions )) {88 if (! SymbolFrequencies.Rows.ContainsKey(pair.Key)) {95 foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions, AggregrateSymbolsWithDifferentSubtreeCount.Value)) { 96 if (!symbolFrequencies.Rows.ContainsKey(pair.Key)) { 89 97 // initialize a new row for the symbol and pad with zeros 90 98 DataRow row = new DataRow(pair.Key, "", Enumerable.Repeat(0.0, numberOfValues)); 91 99 row.VisualProperties.StartIndexZero = true; 92 SymbolFrequencies.Rows.Add(row);100 symbolFrequencies.Rows.Add(row); 93 101 } 94 SymbolFrequencies.Rows[pair.Key].Values.Add(pair.Value);102 symbolFrequencies.Rows[pair.Key].Values.Add(pair.Value); 95 103 } 96 104 97 105 // add a zero for each data row that was not modified in the previous loop 98 foreach (var row in SymbolFrequencies.Rows.Where(r => r.Values.Count != numberOfValues + 1))106 foreach (var row in symbolFrequencies.Rows.Where(r => r.Values.Count != numberOfValues + 1)) 99 107 row.Values.Add(0.0); 100 108 … … 102 110 } 103 111 104 public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees ) {112 public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateDifferentNumberOfSubtrees = true) { 105 113 Dictionary<string, double> symbolFrequencies = new Dictionary<string, double>(); 106 114 int totalNumberOfSymbols = 0; … … 108 116 foreach (var tree in trees) { 109 117 foreach (var node in tree.IterateNodesPrefix()) { 110 if (symbolFrequencies.ContainsKey(node.Symbol.Name)) symbolFrequencies[node.Symbol.Name] += 1; 111 else symbolFrequencies.Add(node.Symbol.Name, 1); 118 string symbolName; 119 if (aggregateDifferentNumberOfSubtrees) symbolName = node.Symbol.Name; 120 else symbolName = node.Symbol.Name + "-" + node.SubtreesCount; 121 if (symbolFrequencies.ContainsKey(symbolName)) symbolFrequencies[symbolName] += 1; 122 else symbolFrequencies.Add(symbolName, 1); 112 123 totalNumberOfSymbols++; 113 124 }
Note: See TracChangeset
for help on using the changeset viewer.