public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicClassificationModelCreator modelCreator,
                                                    SymbolicClassificationSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
                                                    IClassificationProblemData problemData, DoubleLimit estimationLimits, IEnumerable <int> rows,
                                                    double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false)
        {
            var clonedTree = (ISymbolicExpressionTree)tree.Clone();
            var model      = modelCreator.CreateSymbolicClassificationModel(clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);

            var    nodes = clonedTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
            double qualityForImpactsCalculation = double.NaN;

            for (int i = 0; i < nodes.Count; ++i)
            {
                var node = nodes[i];
                if (node is ConstantTreeNode)
                {
                    continue;
                }

                double impactValue, replacementValue, newQualityForImpactsCalculation;
                impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpactsCalculation, qualityForImpactsCalculation);

                if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0))
                {
                    continue;
                }
                if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold)
                {
                    continue;
                }

                var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
                constantNode.Value = replacementValue;

                ReplaceWithConstant(node, constantNode);
                i += node.GetLength() - 1; // skip subtrees under the node that was folded

                qualityForImpactsCalculation = newQualityForImpactsCalculation;
            }
            return(model.SymbolicExpressionTree);
        }
Exemple #2
0
 protected SymbolicClassificationSolutionImpactValuesCalculator(SymbolicClassificationSolutionImpactValuesCalculator original, Cloner cloner)
     : base(original, cloner)
 {
 }
 protected SymbolicClassificationSolutionImpactValuesCalculator(SymbolicClassificationSolutionImpactValuesCalculator original, Cloner cloner)
   : base(original, cloner) { }
    public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicClassificationModelCreator modelCreator,
      SymbolicClassificationSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
      IClassificationProblemData problemData, DoubleLimit estimationLimits, IEnumerable<int> rows,
      double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false) {
      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
      var model = modelCreator.CreateSymbolicClassificationModel(problemData.TargetVariable, clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);

      var nodes = clonedTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
      double qualityForImpactsCalculation = double.NaN;

      for (int i = 0; i < nodes.Count; ++i) {
        var node = nodes[i];
        if (node is ConstantTreeNode) continue;

        double impactValue, replacementValue, newQualityForImpactsCalculation;
        impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpactsCalculation, qualityForImpactsCalculation);

        if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
        if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold) continue;

        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
        constantNode.Value = replacementValue;

        ReplaceWithConstant(node, constantNode);
        i += node.GetLength() - 1; // skip subtrees under the node that was folded

        qualityForImpactsCalculation = newQualityForImpactsCalculation;
      }
      return model.SymbolicExpressionTree;
    }