- Timestamp:
- 04/15/11 14:54:43 (14 years ago)
- Location:
- branches/histogram
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/histogram
- Property svn:mergeinfo changed
/trunk/sources (added) merged: 5962-5963,5971-5972,5975-5976,5983-5984,5987,5993,5997-5998,6002-6003,6009
- Property svn:mergeinfo changed
-
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs
r5809 r6011 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 27 28 using HeuristicLab.Operators; 28 29 using HeuristicLab.Optimization; … … 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) { … … 72 78 } 73 79 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 74 88 public override IOperation Apply() { 75 89 ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue; 76 90 ResultCollection results = ResultsParameter.ActualValue; 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"; 77 95 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)); 96 SymbolFrequenciesParameter.ActualValue = symbolFrequencies; 97 results.Add(new Result("Symbol frequencies", symbolFrequencies)); 82 98 } 83 99 84 100 // 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();101 int numberOfValues = symbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First(); 86 102 87 foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions )) {88 if (! SymbolFrequencies.Rows.ContainsKey(pair.Key)) {103 foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions, AggregrateSymbolsWithDifferentSubtreeCount.Value)) { 104 if (!symbolFrequencies.Rows.ContainsKey(pair.Key)) { 89 105 // initialize a new row for the symbol and pad with zeros 90 106 DataRow row = new DataRow(pair.Key, "", Enumerable.Repeat(0.0, numberOfValues)); 91 107 row.VisualProperties.StartIndexZero = true; 92 SymbolFrequencies.Rows.Add(row);108 symbolFrequencies.Rows.Add(row); 93 109 } 94 SymbolFrequencies.Rows[pair.Key].Values.Add(pair.Value);110 symbolFrequencies.Rows[pair.Key].Values.Add(pair.Value); 95 111 } 96 112 97 113 // 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))114 foreach (var row in symbolFrequencies.Rows.Where(r => r.Values.Count != numberOfValues + 1)) 99 115 row.Values.Add(0.0); 100 116 … … 102 118 } 103 119 104 public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees ) {120 public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateDifferentNumberOfSubtrees = true) { 105 121 Dictionary<string, double> symbolFrequencies = new Dictionary<string, double>(); 106 122 int totalNumberOfSymbols = 0; … … 108 124 foreach (var tree in trees) { 109 125 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); 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); 112 131 totalNumberOfSymbols++; 113 132 } -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r5925 r6011 103 103 public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode, 104 104 int maxLength, int maxDepth) { 105 // make sure it is possible to create a trees smaller than maxLength and maxDepth 106 if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength) 107 throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength"); 108 if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth) 109 throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth"); 110 105 111 // tree length is limited by the grammar and by the explicit size constraints 106 112 int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol); … … 149 155 int argumentIndex = nextExtension.ChildIndex; 150 156 int extensionDepth = nextExtension.ExtensionPointDepth; 151 if ( extensionDepth + parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth) {157 if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth - extensionDepth) { 152 158 ReplaceWithMinimalTree(random, root, parent, argumentIndex); 153 159 } else { … … 155 161 where s.InitialFrequency > 0.0 156 162 where parent.Grammar.IsAllowedChildSymbol(parent.Symbol, s, argumentIndex) 157 where parent.Grammar.GetMinimumExpressionDepth(s) + extensionDepth - 1 < maxDepth163 where parent.Grammar.GetMinimumExpressionDepth(s) < maxDepth - extensionDepth + 1 158 164 where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength 159 165 select s) -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r5809 r6011 229 229 230 230 public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 231 return from s in Symbols where IsAllowedChildSymbol(parent, s) select s;231 return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s; 232 232 } 233 233 … … 265 265 cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 266 266 long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 267 let minForSlot = (long)(from s in Symbols267 let minForSlot = (long)(from s in AllowedSymbols 268 268 where IsAllowedChildSymbol(symbol, s, argIndex) 269 269 select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min() … … 282 282 cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 283 283 long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) 284 let maxForSlot = (long)(from s in Symbols284 let maxForSlot = (long)(from s in AllowedSymbols 285 285 where IsAllowedChildSymbol(symbol, s, argIndex) 286 286 select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max() 287 287 select maxForSlot).DefaultIfEmpty(0).Sum(); 288 long limit = int.MaxValue; 289 cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit); 288 cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, int.MaxValue); 290 289 return cachedMaxExpressionLength[symbol.Name]; 291 290 } … … 298 297 if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) { 299 298 cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion 300 cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 301 let minForSlot = (from s in Symbols 302 where IsAllowedChildSymbol(symbol, s, argIndex) 303 select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min() 304 select minForSlot).DefaultIfEmpty(0).Max(); 299 long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 300 let minForSlot = (long)(from s in AllowedSymbols 301 where IsAllowedChildSymbol(symbol, s, argIndex) 302 select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min() 303 select minForSlot).DefaultIfEmpty(0).Max(); 304 cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue); 305 305 return cachedMinExpressionDepth[symbol.Name]; 306 306 }
Note: See TracChangeset
for help on using the changeset viewer.