- Timestamp:
- 12/15/21 11:50:57 (3 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/3140_NumberSymbol (added) merged: 18091,18093,18100,18112-18121,18123-18131
- Property svn:mergeinfo changed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic (added) merged: 18093,18100,18112-18116,18118,18121,18123-18124,18129-18130
- Property svn:mergeinfo changed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs
r17180 r18132 28 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 29 29 public static class SymbolicExpressionTreeHash { 30 private static readonly Addition add = new Addition(); 31 private static readonly Subtraction sub = new Subtraction(); 32 private static readonly Multiplication mul = new Multiplication(); 33 private static readonly Division div = new Division(); 34 private static readonly Logarithm log = new Logarithm(); 35 private static readonly Exponential exp = new Exponential(); 36 private static readonly Sine sin = new Sine(); 37 private static readonly Cosine cos = new Cosine(); 38 private static readonly Constant constant = new Constant(); 30 private static readonly Number number = new Number(); 39 31 40 32 private static ISymbolicExpressionTreeNode ActualRoot(this ISymbolicExpressionTree tree) => tree.Root.GetSubtree(0).GetSubtree(0); … … 66 58 var symbol = node.Symbol; 67 59 var name = symbol.Name; 68 if (node is ConstantTreeNode constantNode) {69 name = strict ? constantNode.Value.ToString() : symbol.Name;60 if (node is INumericTreeNode numNode) { 61 name = strict ? numNode.Value.ToString() : "Number"; 70 62 } else if (node is VariableTreeNode variableNode) { 71 63 name = strict ? variableNode.Weight.ToString() + variableNode.VariableName : variableNode.VariableName; … … 222 214 variableTreeNode.VariableName = variable.VariableName; 223 215 variableTreeNode.Weight = variable.Weight; 224 } else if (node.Data is ConstantTreeNode @const) {225 var constantTreeNode = (ConstantTreeNode)treeNodes[i];226 constantTreeNode.Value = @const.Value;216 } else if (node.Data is INumericTreeNode existingNumNode) { 217 var newNumNode = (INumericTreeNode)treeNodes[i]; 218 newNumNode.Value = existingNumNode.Value; 227 219 } 228 220 continue; … … 272 264 } 273 265 274 // simplify multiplications by reducing constants and div terms266 // simplify multiplications by reducing numbers and div terms 275 267 public static void SimplifyMultiplication(ref HashNode<ISymbolicExpressionTreeNode>[] nodes, int i) { 276 268 var node = nodes[i]; … … 285 277 286 278 var symbol = child.Data.Symbol; 287 if (child.Data is ConstantTreeNode firstConst) {288 // fold sibling constant nodes into the first constant279 if (child.Data is INumericTreeNode firstNum) { 280 // fold sibling number nodes into the first number 289 281 for (int k = j + 1; k < children.Length; ++k) { 290 282 var sibling = nodes[children[k]]; 291 if (sibling.Data is ConstantTreeNode otherConst) {283 if (sibling.Data is INumericTreeNode otherNum) { 292 284 sibling.Enabled = false; 293 285 node.Arity--; 294 first Const.Value *= otherConst.Value;286 firstNum.Value *= otherNum.Value; 295 287 } else { 296 288 break; … … 298 290 } 299 291 } else if (child.Data is VariableTreeNode variable) { 300 // fold sibling constantnodes into the variable weight292 // fold sibling number nodes into the variable weight 301 293 for (int k = j + 1; k < children.Length; ++k) { 302 294 var sibling = nodes[children[k]]; 303 if (sibling.Data is ConstantTreeNode constantNode) {295 if (sibling.Data is INumericTreeNode numNode) { 304 296 sibling.Enabled = false; 305 297 node.Arity--; 306 variable.Weight *= constantNode.Value;298 variable.Weight *= numNode.Value; 307 299 } else { 308 300 break; … … 328 320 } 329 321 330 if (node.Arity == 0) { // if everything is simplified this node becomes constant331 var constantTreeNode = constant.CreateTreeNode<ConstantTreeNode>();332 constantTreeNode.Value = 1;333 nodes[i] = constantTreeNode.ToHashNode();322 if (node.Arity == 0) { // if everything is simplified this node becomes a number 323 var numNode = number.CreateTreeNode<NumberTreeNode>(); 324 numNode.Value = 1; 325 nodes[i] = numNode.ToHashNode(); 334 326 } else if (node.Arity == 1) { // when i have only 1 arg left i can skip this node 335 327 node.Enabled = false; … … 344 336 var tmp = nodes; 345 337 346 if (children.All(x => tmp[x].Data.Symbol is Constant)) {347 var v = (( ConstantTreeNode)nodes[children.First()].Data).Value;338 if (children.All(x => tmp[x].Data.Symbol is INumericSymbol)) { 339 var v = ((INumericTreeNode)nodes[children.First()].Data).Value; 348 340 if (node.Arity == 1) { 349 341 v = 1 / v; 350 342 } else if (node.Arity > 1) { 351 343 foreach (var j in children.Skip(1)) { 352 v /= (( ConstantTreeNode)nodes[j].Data).Value;353 } 354 } 355 var constantTreeNode = constant.CreateTreeNode<ConstantTreeNode>();356 constantTreeNode.Value = v;357 nodes[i] = constantTreeNode.ToHashNode();344 v /= ((INumericTreeNode)nodes[j].Data).Value; 345 } 346 } 347 var numNode = number.CreateTreeNode<NumberTreeNode>(); 348 numNode.Value = v; 349 nodes[i] = numNode.ToHashNode(); 358 350 return; 359 351 } … … 368 360 } 369 361 if (node.Arity == 0) { 370 var constantTreeNode = constant.CreateTreeNode<ConstantTreeNode>();371 constantTreeNode.Value = 1; // x / x = 1372 nodes[i] = constantTreeNode.ToHashNode();362 var numNode = number.CreateTreeNode<NumberTreeNode>(); 363 numNode.Value = 1; // x / x = 1 364 nodes[i] = numNode.ToHashNode(); 373 365 } 374 366 } … … 376 368 377 369 public static void SimplifyUnaryNode(ref HashNode<ISymbolicExpressionTreeNode>[] nodes, int i) { 378 // check if the child of the unary node is a constant, then the whole node can be simplified370 // check if the child of the unary node is a number, then the whole node can be simplified 379 371 var parent = nodes[i]; 380 372 var child = nodes[i - 1]; … … 383 375 var childSymbol = child.Data.Symbol; 384 376 385 if (childSymbol is Constant) {377 if (childSymbol is INumericSymbol) { 386 378 nodes[i].Enabled = false; 387 379 } else if ((parentSymbol is Exponential && childSymbol is Logarithm) || (parentSymbol is Logarithm && childSymbol is Exponential)) { … … 393 385 var children = nodes.IterateChildren(i); 394 386 var tmp = nodes; 395 if (children.All(x => tmp[x].Data.Symbol is Constant)) {387 if (children.All(x => tmp[x].Data.Symbol is INumericSymbol)) { 396 388 foreach (var j in children) { 397 389 nodes[j].Enabled = false; 398 390 } 399 nodes[i] = constant.CreateTreeNode().ToHashNode();391 nodes[i] = number.CreateTreeNode().ToHashNode(); 400 392 } 401 393 }
Note: See TracChangeset
for help on using the changeset viewer.