Changeset 14357 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars
- Timestamp:
- 10/24/16 14:20:17 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/GrammarUtils.cs
r14356 r14357 44 44 } 45 45 46 private static I Enumerable<ISymbol> IterateBreadthReverse(this ISymbolicExpressionGrammarBase grammar, ISymbol topSymbol) {46 private static IReadOnlyCollection<ISymbol> IterateBreadthReverse(this ISymbolicExpressionGrammarBase grammar, ISymbol topSymbol) { 47 47 // sort symbols in reverse breadth order (starting from the topSymbol) 48 48 // each symbol is visited only once (this avoids infinite recursion) 49 var numberedSymbols = new List<Tuple<ISymbol, int>> { Tuple.Create(topSymbol, 0)};49 var symbols = new List<ISymbol> { topSymbol }; 50 50 var visited = new HashSet<ISymbol> { topSymbol }; 51 int i = 0 , index = 0;52 while (i < numberedSymbols.Count) {53 var symbol = numberedSymbols[i].Item1;51 int i = 0; 52 while (i < symbols.Count) { 53 var symbol = symbols[i]; 54 54 var minSubtreeCount = grammar.GetMinimumSubtreeCount(symbol); 55 55 … … 60 60 61 61 if (visited.Add(childSymbol)) 62 numberedSymbols.Add(Tuple.Create(childSymbol, ++index));62 symbols.Add(childSymbol); 63 63 } 64 64 } 65 65 ++i; 66 66 } 67 numberedSymbols.Reverse();68 return numberedSymbols.Select(x => x.Item1);67 symbols.Reverse(); 68 return symbols; 69 69 } 70 70 … … 77 77 minimumExpressionLengths.Clear(); 78 78 //terminal symbols => minimum expression length = 1 79 foreach (var s in grammar.Symbols.Where(x => grammar.GetMinimumSubtreeCount(x) == 0)) 80 minimumExpressionLengths[s.Name] = 1; 79 foreach (var s in grammar.Symbols) { 80 if (grammar.GetMinimumSubtreeCount(s) == 0) 81 minimumExpressionLengths[s.Name] = 1; 82 else 83 minimumExpressionLengths[s.Name] = int.MaxValue; 84 } 81 85 82 86 foreach (var topSymbol in grammar.GetTopmostSymbols()) { 83 87 // get all symbols below in reverse breadth order 84 88 // this way we ensure lengths are calculated bottom-up 85 var symbols = grammar.IterateBreadthReverse(topSymbol) .ToList();89 var symbols = grammar.IterateBreadthReverse(topSymbol); 86 90 foreach (var symbol in symbols) { 87 91 long minLength = 1; … … 112 116 } 113 117 } 114 115 //set minLength to int.MaxValue for all symbols that are not reacheable116 foreach (var remainingSymbols in grammar.Symbols) {117 if (!minimumExpressionLengths.ContainsKey(remainingSymbols.Name))118 minimumExpressionLengths[remainingSymbols.Name] = int.MaxValue;119 }120 118 } 121 119 … … 125 123 minimumExpressionDepths.Clear(); 126 124 //terminal symbols => minimum expression depth = 1 127 foreach (var s in grammar.Symbols.Where(s => grammar.GetMinimumSubtreeCount(s) == 0)) 128 minimumExpressionDepths[s.Name] = 1; 125 foreach (var s in grammar.Symbols) { 126 if (grammar.GetMinimumSubtreeCount(s) == 0) 127 minimumExpressionDepths[s.Name] = 1; 128 else 129 minimumExpressionDepths[s.Name] = int.MaxValue; 130 } 129 131 130 132 foreach (var topSymbol in grammar.GetTopmostSymbols()) { 131 133 // get all symbols below in reverse breadth order 132 134 // this way we ensure lengths are calculated bottom-up 133 var symbols = grammar.IterateBreadthReverse(topSymbol) .ToList();135 var symbols = grammar.IterateBreadthReverse(topSymbol); 134 136 foreach (var symbol in symbols) { 135 137 long minDepth = -1; … … 160 162 } 161 163 } 162 163 //set minDepth to int.Maxvalue for all symbols that are not reacheable164 foreach (var remainingSymbols in grammar.Symbols) {165 if (!minimumExpressionDepths.ContainsKey(remainingSymbols.Name))166 minimumExpressionDepths[remainingSymbols.Name] = int.MaxValue;167 }168 164 } 169 165 }
Note: See TracChangeset
for help on using the changeset viewer.