- Timestamp:
- 10/26/18 22:10:18 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashExtensions.cs
r16255 r16260 116 116 } 117 117 } 118 return simplified.UpdateNodeSizes(). Sort();118 return simplified.UpdateNodeSizes().Reduce().Sort(); 119 119 } 120 120 … … 170 170 var idx = i - 1; 171 171 for (int j = 0; j < arity; ++j) { 172 while (!nodes[idx].Enabled) { --idx; } // skip nodes possibly disabled by simplification173 172 children[j] = idx; 174 173 idx -= 1 + nodes[idx].Size; … … 192 191 } 193 192 194 /// <summary> 195 /// Reduce nodes of the same type according to arithmetic rules 196 /// </summary> 197 /// <typeparam name="T"></typeparam> 198 /// <param name="g">The given grammar (controls symbol arities)</param> 199 /// <param name="nodes">The node array</param> 200 /// <param name="i">Parent index</param> 201 /// <param name="j">Child index</param> 202 private static void Reduce<T>(this HashNode<T>[] nodes, int i, int j) where T : class { 203 var p = nodes[i]; // parent node 204 var c = nodes[j]; // child node 205 206 if (c.IsChild) 207 return; 208 209 foreach (int k in nodes.IterateChildren(j)) { 210 if (nodes[k].IsChild) continue; 211 nodes.Reduce(j, k); 212 } 213 214 // handle commutative symbols (add, mul) 215 if (p.IsCommutative && p.HashValue == c.HashValue) { 216 c.Enabled = false; 217 p.Arity += c.Arity - 1; 218 } 219 } 220 221 public static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class { 222 int i = nodes.Length - 1; 223 foreach (int c in nodes.IterateChildren(i)) { 224 if (nodes[c].IsChild) continue; 225 nodes.Reduce(i, c); 226 } 193 private static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class { 227 194 int count = 0; 228 foreach (var node in nodes) { 229 if (!node.Enabled) { ++count; } 195 for (int i = 0; i < nodes.Length; ++i) { 196 var node = nodes[i]; 197 if (node.IsChild || !node.IsCommutative) { 198 continue; 199 } 200 201 foreach (var j in nodes.IterateChildren(i)) { 202 if (node.HashValue == nodes[j].HashValue) { 203 nodes[j].Enabled = false; 204 node.Arity += nodes[j].Arity - 1; 205 ++count; 206 } 207 } 230 208 } 231 209 if (count == 0) … … 233 211 234 212 var reduced = new HashNode<T>[nodes.Length - count]; 235 i= 0;213 var idx = 0; 236 214 foreach (var node in nodes) { 237 if (node.Enabled) { reduced[i ++] = node; }215 if (node.Enabled) { reduced[idx++] = node; } 238 216 } 239 217 return reduced.UpdateNodeSizes();
Note: See TracChangeset
for help on using the changeset viewer.