Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/24/15 11:17:08 (10 years ago)
Author:
mkommend
Message:

#2276: Merged trunk changes into dataset refactoring branch.

Location:
branches/HeuristicLab.DatasetRefactor/sources
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DatasetRefactor/sources

  • branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs

    r12031 r12247  
    109109      : base(original, cloner) { }
    110110
    111     protected SymbolicDataAnalysisExpressionPruningOperator() {
     111    protected SymbolicDataAnalysisExpressionPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator) {
    112112      #region add parameters
    113113      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
     
    122122      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
    123123      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName));
     124      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, impactValuesCalculator));
    124125      #endregion
    125126    }
    126127
    127     protected abstract ISymbolicDataAnalysisModel CreateModel();
     128    protected abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits);
    128129
    129130    protected abstract double Evaluate(IDataAnalysisModel model);
    130131
    131132    public override IOperation Apply() {
    132       var model = CreateModel();
     133      var model = CreateModel(SymbolicExpressionTree, Interpreter, ProblemData, EstimationLimits);
    133134      var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
    134135      var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
     
    169170    }
    170171
    171     private static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
     172    public ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
     173      var model = CreateModel((ISymbolicExpressionTree)tree.Clone(), Interpreter, ProblemData, EstimationLimits);
     174      var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
     175      var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
     176
     177      double quality = Evaluate(model);
     178
     179      for (int i = 0; i < nodes.Count; ++i) {
     180        var node = nodes[i];
     181        if (node is ConstantTreeNode) continue;
     182
     183        double impactValue, replacementValue;
     184        ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);
     185
     186        if (PruneOnlyZeroImpactNodes) {
     187          if (!impactValue.IsAlmost(0.0)) continue;
     188        } else if (NodeImpactThreshold < impactValue) {
     189          continue;
     190        }
     191
     192        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
     193        constantNode.Value = replacementValue;
     194
     195        ReplaceWithConstant(node, constantNode);
     196        i += node.GetLength() - 1; // skip subtrees under the node that was folded
     197
     198        quality -= impactValue;
     199      }
     200      return model.SymbolicExpressionTree;
     201    }
     202
     203    protected static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
    172204      var parent = original.Parent;
    173205      var i = parent.IndexOfSubtree(original);
  • branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs

    r12031 r12247  
    299299        // simplify expressions x0..xn
    300300        // make multiplication (x0 * 1/(x1 * x1 * .. * xn))
    301         var simplifiedTrees = original.Subtrees.Select(x => GetSimplifiedTree(x));
     301        var simplifiedTrees = original.Subtrees.Select(GetSimplifiedTree).ToArray();
    302302        return
    303           MakeProduct(simplifiedTrees.First(), Invert(simplifiedTrees.Skip(1).Aggregate((a, b) => MakeProduct(a, b))));
     303          MakeProduct(simplifiedTrees.First(), Invert(simplifiedTrees.Skip(1).Aggregate(MakeProduct)));
    304304      }
    305305    }
     
    310310      } else {
    311311        return original.Subtrees
    312           .Select(x => GetSimplifiedTree(x))
    313           .Aggregate((a, b) => MakeProduct(a, b));
     312          .Select(GetSimplifiedTree).ToArray()
     313          .Aggregate(MakeProduct);
    314314      }
    315315    }
     
    321321        // simplify expressions x0..xn
    322322        // make addition (x0,-x1..-xn)
    323         var simplifiedTrees = original.Subtrees.Select(x => GetSimplifiedTree(x));
     323        var simplifiedTrees = original.Subtrees.Select(GetSimplifiedTree).ToArray();
    324324        return simplifiedTrees.Take(1)
    325           .Concat(simplifiedTrees.Skip(1).Select(x => Negate(x)))
    326           .Aggregate((a, b) => MakeSum(a, b));
     325          .Concat(simplifiedTrees.Skip(1).Select(Negate))
     326          .Aggregate(MakeSum);
    327327      }
    328328    }
     
    335335        // make addition (x0..xn)
    336336        return original.Subtrees
    337           .Select(x => GetSimplifiedTree(x))
    338           .Aggregate((a, b) => MakeSum(a, b));
     337          .Select(GetSimplifiedTree).ToArray()
     338          .Aggregate(MakeSum);
    339339      }
    340340    }
Note: See TracChangeset for help on using the changeset viewer.