private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree)
        {
            // remove old ADFs
            while (tempTree.Root.SubtreeCount > 1)
            {
                tempTree.Root.RemoveSubtree(1);
            }
            // clone ADFs of source tree
            for (int i = 1; i < sourceTree.Root.SubtreeCount; i++)
            {
                tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
            }
            var start = tempTree.Root.GetSubtree(0);

            while (start.SubtreeCount > 0)
            {
                start.RemoveSubtree(0);
            }
            start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
            var interpreter        = Content.Model.Interpreter;
            var rows               = Content.ProblemData.TrainingIndices;
            var allPrognosedValues = interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows);

            return(allPrognosedValues.Median());
        }
        protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
                                                          IDataset dataset, IEnumerable <int> rows)
        {
            //optimization: constant nodes return always the same value
            ConstantTreeNode constantNode = node as ConstantTreeNode;

            if (constantNode != null)
            {
                return(constantNode.Value);
            }

            var rootSymbol  = new ProgramRootSymbol().CreateTreeNode();
            var startSymbol = new StartSymbol().CreateTreeNode();

            rootSymbol.AddSubtree(startSymbol);
            startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());

            var tempTree = new SymbolicExpressionTree(rootSymbol);

            // clone ADFs of source tree
            for (int i = 1; i < sourceTree.Root.SubtreeCount; i++)
            {
                tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
            }
            return(interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median());
        }
        protected IEnumerable <double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
                                                                  IDataset dataset, IEnumerable <int> rows)
        {
            //optimization: constant nodes return always the same value
            ConstantTreeNode             constantNode     = node as ConstantTreeNode;
            BinaryFactorVariableTreeNode binaryFactorNode = node as BinaryFactorVariableTreeNode;
            FactorVariableTreeNode       factorNode       = node as FactorVariableTreeNode;

            if (constantNode != null)
            {
                yield return(constantNode.Value);
            }
            else if (binaryFactorNode != null)
            {
                // valid replacements are either all off or all on
                yield return(0);

                yield return(1);
            }
            else if (factorNode != null)
            {
                foreach (var w in factorNode.Weights)
                {
                    yield return(w);
                }
                yield return(0.0);
            }
            else
            {
                var rootSymbol  = new ProgramRootSymbol().CreateTreeNode();
                var startSymbol = new StartSymbol().CreateTreeNode();
                rootSymbol.AddSubtree(startSymbol);
                startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());

                var tempTree = new SymbolicExpressionTree(rootSymbol);
                // clone ADFs of source tree
                for (int i = 1; i < sourceTree.Root.SubtreeCount; i++)
                {
                    tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
                }
                yield return(interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median());

                yield return(interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Average()); // TODO perf
            }
        }
    protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
      IDataset dataset, IEnumerable<int> rows) {
      //optimization: constant nodes return always the same value
      ConstantTreeNode constantNode = node as ConstantTreeNode;
      if (constantNode != null) return constantNode.Value;

      var rootSymbol = new ProgramRootSymbol().CreateTreeNode();
      var startSymbol = new StartSymbol().CreateTreeNode();
      rootSymbol.AddSubtree(startSymbol);
      startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());

      var tempTree = new SymbolicExpressionTree(rootSymbol);
      // clone ADFs of source tree
      for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
        tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
      }
      return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median();
    }
        private void pasteToolStripMenuItem_Clicked(object sender, EventArgs e)
        {
            if (!(lastOp == EditOp.CopySubtree || lastOp == EditOp.CutSubtree))
            {
                return;
            }
            // check if the copied/cut node (stored in the tempNode) can be inserted as a child of the current selected node
            var node = currSelected.Content;

            if (node is ConstantTreeNode || node is VariableTreeNode)
            {
                return;
            }
            // check if the currently selected node can accept the copied node as a child
            // no need to check the grammar, an arity check will do just fine here
            if (node.Symbol.MaximumArity <= node.SubtreeCount)
            {
                return;
            }
            switch (lastOp)
            {
            case EditOp.CutSubtree: {
                if (tempNode.IterateNodesBreadth().Contains(node))
                {
                    throw new ArgumentException();                 // cannot cut/paste a node into itself
                }
                ModifyTree(Tree, tempNode.Parent, tempNode, null); //remove node from its original parent
                ModifyTree(Tree, node, null, tempNode);            //insert it as a child to the new parent
                break;
            }

            case EditOp.CopySubtree: {
                var clone = (SymbolicExpressionTreeNode)tempNode.Clone();
                ModifyTree(Tree, node, null, clone);
                break;
            }
            }
            currSelected = null; // because the tree will have changed
            tempNode     = null; // clear the clipboard after one paste
        }
Пример #6
0
        public static ISymbolicExpressionTreeNode Derive(ISymbolicExpressionTreeNode branch, string variableName)
        {
            if (branch.Symbol is Constant)
            {
                return(CreateConstant(0.0));
            }
            if (branch.Symbol is Variable)
            {
                var varNode = branch as VariableTreeNode;
                if (varNode.VariableName == variableName)
                {
                    return(CreateConstant(varNode.Weight));
                }
                else
                {
                    return(CreateConstant(0.0));
                }
            }
            if (branch.Symbol is Addition)
            {
                var sum = addSy.CreateTreeNode();
                foreach (var subTree in branch.Subtrees)
                {
                    sum.AddSubtree(Derive(subTree, variableName));
                }
                return(sum);
            }
            if (branch.Symbol is Subtraction)
            {
                var sum = subSy.CreateTreeNode();
                foreach (var subTree in branch.Subtrees)
                {
                    sum.AddSubtree(Derive(subTree, variableName));
                }
                return(sum);
            }
            if (branch.Symbol is Multiplication)
            {
                // (f * g)' = f'*g + f*g'
                // for multiple factors: (f * g * h)' = ((f*g) * h)' = (f*g)' * h + (f*g) * h'

                if (branch.SubtreeCount >= 2)
                {
                    var f      = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                    var fprime = Derive(f, variableName);
                    for (int i = 1; i < branch.SubtreeCount; i++)
                    {
                        var g       = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone();
                        var fg      = Product((ISymbolicExpressionTreeNode)f.Clone(), (ISymbolicExpressionTreeNode)g.Clone());
                        var gPrime  = Derive(g, variableName);
                        var fgPrime = Sum(Product(fprime, g), Product(gPrime, f));
                        // prepare for next iteration
                        f      = fg;
                        fprime = fgPrime;
                    }
                    return(fprime);
                }
                else
                {
                    // multiplication with only one argument has no effect -> derive the argument
                    return(Derive(branch.GetSubtree(0), variableName));
                }
            }
            if (branch.Symbol is Division)
            {
                // (f/g)' = (f'g - g'f) / g²
                if (branch.SubtreeCount == 1)
                {
                    var g       = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                    var gPrime  = Product(CreateConstant(-1.0), Derive(g, variableName));
                    var sqrNode = new Square().CreateTreeNode();
                    sqrNode.AddSubtree(g);
                    return(Div(gPrime, sqrNode));
                }
                else
                {
                    // for two subtrees:
                    // (f/g)' = (f'g - fg')/g²

                    // if there are more than 2 subtrees
                    // div(x,y,z) is interpretered as (x/y)/z
                    // which is the same as x / (y*z)

                    // --> make a product of all but the first subtree and differentiate as for the 2-argument case above
                    var f      = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                    var g      = Product(branch.Subtrees.Skip(1).Select(n => (ISymbolicExpressionTreeNode)n.Clone()));
                    var fprime = Derive(f, variableName);
                    var gprime = Derive(g, variableName);
                    var gSqr   = Square(g);
                    return(Div(Subtract(Product(fprime, g), Product(f, gprime)), gSqr));
                }
            }
            if (branch.Symbol is Logarithm)
            {
                var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                return(Product(Div(CreateConstant(1.0), f), Derive(f, variableName)));
            }
            if (branch.Symbol is Exponential)
            {
                var f = (ISymbolicExpressionTreeNode)branch.Clone();
                return(Product(f, Derive(branch.GetSubtree(0), variableName)));
            }
            if (branch.Symbol is Square)
            {
                var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                return(Product(Product(CreateConstant(2.0), f), Derive(f, variableName)));
            }
            if (branch.Symbol is SquareRoot)
            {
                var f = (ISymbolicExpressionTreeNode)branch.Clone();
                var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                return(Product(Div(CreateConstant(1.0), Product(CreateConstant(2.0), f)), Derive(u, variableName)));
            }
            if (branch.Symbol is CubeRoot)
            {
                var f = (ISymbolicExpressionTreeNode)branch.Clone();
                var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                return(Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName))); // 1/3 1/cbrt(f(x))^2 d/dx f(x)
            }
            if (branch.Symbol is Cube)
            {
                var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                return(Product(Product(CreateConstant(3.0), Square(f)), Derive(f, variableName)));
            }
            if (branch.Symbol is Absolute)
            {
                var f    = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                var absf = Abs((ISymbolicExpressionTreeNode)f.Clone());
                return(Product(Div(f, absf), Derive(f, variableName)));
            }
            if (branch.Symbol is AnalyticQuotient)
            {
                // aq(a(x), b(x)) = a(x) / sqrt(b(x)²+1)
                var a = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                var b = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();

                var definition = Div(a, SquareRoot(Sum(Square(b), CreateConstant(1.0))));
                return(Derive(definition, variableName));
            }
            if (branch.Symbol is Sine)
            {
                var u   = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                var cos = (new Cosine()).CreateTreeNode();
                cos.AddSubtree(u);
                return(Product(cos, Derive(u, variableName)));
            }
            if (branch.Symbol is Cosine)
            {
                var u   = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                var sin = (new Sine()).CreateTreeNode();
                sin.AddSubtree(u);
                return(Product(CreateConstant(-1.0), Product(sin, Derive(u, variableName))));
            }
            if (branch.Symbol is Tangent)
            {
                // tan(x)' = 1 / cos²(x)
                var fxp = Derive(branch.GetSubtree(0), variableName);
                var u   = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
                return(Div(fxp, Square(Cosine(u))));
            }
            if (branch.Symbol is HyperbolicTangent)
            {
                // tanh(f(x))' = f(x)'sech²(f(x)) = f(x)'(1 - tanh²(f(x)))
                var fxp  = Derive(branch.GetSubtree(0), variableName);
                var tanh = (ISymbolicExpressionTreeNode)branch.Clone();
                return(Product(fxp, Subtract(CreateConstant(1.0), Square(tanh))));
            }
            throw new NotSupportedException(string.Format("Symbol {0} is not supported.", branch.Symbol));
        }
 private ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {
   if (lag == 0) return subtree;
   else if (lag == -1 || lag == 1) {
     return MakeSum(subtree, AddLagToDynamicNodes((ISymbolicExpressionTreeNode)subtree.Clone(), lag));
   } else {
     var node = (LaggedTreeNode)integralSymbol.CreateTreeNode();
     node.Lag = lag;
     node.AddSubtree(subtree);
     return node;
   }
 }
 private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
   // can't simplify this function but simplify all subtrees 
   List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees);
   while (original.Subtrees.Count() > 0) original.RemoveSubtree(0);
   var clone = (SymbolicExpressionTreeNode)original.Clone();
   List<ISymbolicExpressionTreeNode> simplifiedSubtrees = new List<ISymbolicExpressionTreeNode>();
   foreach (var subtree in subtrees) {
     simplifiedSubtrees.Add(GetSimplifiedTree(subtree));
     original.AddSubtree(subtree);
   }
   foreach (var simplifiedSubtree in simplifiedSubtrees) {
     clone.AddSubtree(simplifiedSubtree);
   }
   if (simplifiedSubtrees.TrueForAll(t => IsConstant(t))) {
     SimplifyConstantExpression(clone);
   }
   return clone;
 }
 /// <summary>
 /// Creates a new simplified tree
 /// </summary>
 /// <param name="original"></param>
 /// <returns></returns>
 public ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
   if (IsConstant(original) || IsVariable(original)) {
     return (ISymbolicExpressionTreeNode)original.Clone();
   } else if (IsAddition(original)) {
     return SimplifyAddition(original);
   } else if (IsSubtraction(original)) {
     return SimplifySubtraction(original);
   } else if (IsMultiplication(original)) {
     return SimplifyMultiplication(original);
   } else if (IsDivision(original)) {
     return SimplifyDivision(original);
   } else if (IsAverage(original)) {
     return SimplifyAverage(original);
   } else if (IsLog(original)) {
     return SimplifyLog(original);
   } else if (IsExp(original)) {
     return SimplifyExp(original);
   } else if (IsSquare(original)) {
     return SimplifySquare(original);
   } else if (IsSquareRoot(original)) {
     return SimplifySquareRoot(original);
   } else if (IsPower(original)) {
     return SimplifyPower(original);
   } else if (IsRoot(original)) {
     return SimplifyRoot(original);
   } else if (IsSine(original)) {
     return SimplifySine(original);
   } else if (IsCosine(original)) {
     return SimplifyCosine(original);
   } else if (IsTangent(original)) {
     return SimplifyTangent(original);
   } else if (IsIfThenElse(original)) {
     return SimplifyIfThenElse(original);
   } else if (IsGreaterThan(original)) {
     return SimplifyGreaterThan(original);
   } else if (IsLessThan(original)) {
     return SimplifyLessThan(original);
   } else if (IsAnd(original)) {
     return SimplifyAnd(original);
   } else if (IsOr(original)) {
     return SimplifyOr(original);
   } else if (IsNot(original)) {
     return SimplifyNot(original);
   } else if (IsTimeLag(original)) {
     return SimplifyTimeLag(original);
   } else if (IsIntegral(original)) {
     return SimplifyIntegral(original);
   } else {
     return SimplifyAny(original);
   }
 }
    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
      // remove old ADFs
      while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
      // clone ADFs of source tree
      for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
        tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
      }
      var start = tempTree.Root.GetSubtree(0);
      while (start.SubtreeCount > 0) start.RemoveSubtree(0);
      start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
      var interpreter = Content.Model.Interpreter;
      var rows = Content.ProblemData.TrainingIndices;
      var allPrognosedValues = interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows);

      return allPrognosedValues.Median();
    }