Changeset 14354 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars
- Timestamp:
- 10/23/16 19:33:03 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/GrammarUtils.cs
r14353 r14354 66 66 continue; 67 67 68 if (visited.Add(childSymbol)) 68 if (visited.Add(childSymbol)) { 69 69 numberedSymbols.Add(Tuple.Create(childSymbol, ++index)); 70 } 70 71 } 71 72 } … … 87 88 minLength = Math.Min(minLength, oldLength); 88 89 minimumExpressionLengths[symbol.Name] = (int)Math.Min(int.MaxValue, minLength); 89 }90 // correction step for cycles91 bool changed = true;92 while (changed) {93 changed = false;94 foreach (var symbol in numberedSymbols.Select(x => x.Item1)) {95 long minLength = Enumerable.Range(0, grammar.GetMinimumSubtreeCount(symbol))96 .Sum(x => grammar.GetAllowedChildSymbols(symbol, x)97 .Min(s => (long)minimumExpressionLengths[s.Name])) + 1;98 if (minLength < minimumExpressionLengths[symbol.Name]) {99 minimumExpressionLengths[symbol.Name] = (int)Math.Min(minLength, int.MaxValue);100 changed = true;101 }102 }103 90 } 104 91 } … … 134 121 continue; 135 122 136 if (visited.Add(childSymbol)) 123 if (visited.Add(childSymbol)) { 137 124 numberedSymbols.Add(Tuple.Create(childSymbol, ++index)); 125 } 138 126 } 139 127 } … … 144 132 // going bottom-up (reverse breadth order), we ensure depths are calculated bottom-up 145 133 foreach (var symbol in numberedSymbols.Select(x => x.Item1)) { 146 long minDepth = -1;134 long minDepth = int.MaxValue; 147 135 for (int argIndex = 0; argIndex < grammar.GetMinimumSubtreeCount(symbol); ++argIndex) { 148 136 long depth = grammar.GetAllowedChildSymbols(symbol, argIndex) 149 137 .Where(x => minimumExpressionDepths.ContainsKey(x.Name)) 150 .Select(x => (long)minimumExpressionDepths[x.Name]).DefaultIfEmpty(int.MaxValue).Min() + 1;151 minDepth = Math.M ax(minDepth, depth);138 .Select(x => minimumExpressionDepths[x.Name]).DefaultIfEmpty(int.MaxValue).Min(); 139 minDepth = Math.Min(minDepth, depth + 1); 152 140 } 153 141 int oldDepth; … … 155 143 minDepth = Math.Min(minDepth, oldDepth); 156 144 minimumExpressionDepths[symbol.Name] = (int)Math.Min(int.MaxValue, minDepth); 157 }158 // correction step for cycles159 bool changed = true;160 while (changed) {161 changed = false;162 foreach (var symbol in numberedSymbols.Select(x => x.Item1)) {163 long minDepth = Enumerable.Range(0, grammar.GetMinimumSubtreeCount(symbol))164 .Max(x => grammar.GetAllowedChildSymbols(symbol, x)165 .Min(s => (long)minimumExpressionDepths[s.Name])) + 1;166 if (minDepth < minimumExpressionDepths[symbol.Name]) {167 minimumExpressionDepths[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);168 changed = true;169 }170 }171 145 } 172 146 }
Note: See TracChangeset
for help on using the changeset viewer.