Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/16/21 14:23:48 (2 years ago)
Author:
mkommend
Message:

#3136: Merged trunk changes into branch.

Location:
branches/3136_Structural_GP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP

  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs

    r17180 r18146  
    2828namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2929  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();
    3931
    4032    private static ISymbolicExpressionTreeNode ActualRoot(this ISymbolicExpressionTree tree) => tree.Root.GetSubtree(0).GetSubtree(0);
     
    6658      var symbol = node.Symbol;
    6759      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";
    7062      } else if (node is VariableTreeNode variableNode) {
    7163        name = strict ? variableNode.Weight.ToString() + variableNode.VariableName : variableNode.VariableName;
     
    222214            variableTreeNode.VariableName = variable.VariableName;
    223215            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 = (NumberTreeNode)treeNodes[i];
     218            newNumNode.Value = existingNumNode.Value;
    227219          }
    228220          continue;
     
    272264    }
    273265
    274     // simplify multiplications by reducing constants and div terms
     266    // simplify multiplications by reducing numbers and div terms
    275267    public static void SimplifyMultiplication(ref HashNode<ISymbolicExpressionTreeNode>[] nodes, int i) {
    276268      var node = nodes[i];
     
    285277
    286278        var symbol = child.Data.Symbol;
    287         if (child.Data is ConstantTreeNode firstConst) {
    288           // fold sibling constant nodes into the first constant
     279        if (child.Data is NumberTreeNode firstNum) {
     280          // fold sibling number nodes into the first number
    289281          for (int k = j + 1; k < children.Length; ++k) {
    290282            var sibling = nodes[children[k]];
    291             if (sibling.Data is ConstantTreeNode otherConst) {
     283            if (sibling.Data is INumericTreeNode otherNum) {
    292284              sibling.Enabled = false;
    293285              node.Arity--;
    294               firstConst.Value *= otherConst.Value;
     286              firstNum.Value *= otherNum.Value;
    295287            } else {
    296288              break;
     
    298290          }
    299291        } else if (child.Data is VariableTreeNode variable) {
    300           // fold sibling constant nodes into the variable weight
     292          // fold sibling number nodes into the variable weight
    301293          for (int k = j + 1; k < children.Length; ++k) {
    302294            var sibling = nodes[children[k]];
    303             if (sibling.Data is ConstantTreeNode constantNode) {
     295            if (sibling.Data is INumericTreeNode numNode) {
    304296              sibling.Enabled = false;
    305297              node.Arity--;
    306               variable.Weight *= constantNode.Value;
     298              variable.Weight *= numNode.Value;
    307299            } else {
    308300              break;
     
    328320        }
    329321
    330         if (node.Arity == 0) { // if everything is simplified this node becomes constant
    331           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();
    334326        } else if (node.Arity == 1) { // when i have only 1 arg left i can skip this node
    335327          node.Enabled = false;
     
    344336      var tmp = nodes;
    345337
    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;
    348340        if (node.Arity == 1) {
    349341          v = 1 / v;
    350342        } else if (node.Arity > 1) {
    351343          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();
    358350        return;
    359351      }
     
    368360        }
    369361        if (node.Arity == 0) {
    370           var constantTreeNode = constant.CreateTreeNode<ConstantTreeNode>();
    371           constantTreeNode.Value = 1; // x / x = 1
    372           nodes[i] = constantTreeNode.ToHashNode();
     362          var numNode = number.CreateTreeNode<NumberTreeNode>();
     363          numNode.Value = 1; // x / x = 1
     364          nodes[i] = numNode.ToHashNode();
    373365        }
    374366      }
     
    376368
    377369    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 simplified
     370      // check if the child of the unary node is a number, then the whole node can be simplified
    379371      var parent = nodes[i];
    380372      var child = nodes[i - 1];
     
    383375      var childSymbol = child.Data.Symbol;
    384376
    385       if (childSymbol is Constant) {
     377      if (childSymbol is INumericSymbol) {
    386378        nodes[i].Enabled = false;
    387379      } else if ((parentSymbol is Exponential && childSymbol is Logarithm) || (parentSymbol is Logarithm && childSymbol is Exponential)) {
     
    393385      var children = nodes.IterateChildren(i);
    394386      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)) {
    396388        foreach (var j in children) {
    397389          nodes[j].Enabled = false;
    398390        }
    399         nodes[i] = constant.CreateTreeNode().ToHashNode();
     391        nodes[i] = number.CreateTreeNode().ToHashNode();
    400392      }
    401393    }
Note: See TracChangeset for help on using the changeset viewer.