Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/27/12 11:02:09 (11 years ago)
Author:
mkommend
Message:

#1763: Merged remaining changes from the TreeSimplifier branch in the trunk and refactored impact values calculators.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicClassificationSolutionSimplifierViewBase.cs

    r8727 r8946  
    2929namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views {
    3030  public abstract partial class InteractiveSymbolicClassificationSolutionSimplifierViewBase : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
    31     private readonly ConstantTreeNode constantNode;
    32     private readonly SymbolicExpressionTree tempTree;
     31    private readonly SymbolicClassificationSolutionImpactValuesCalculator calculator;
    3332
    3433    public new ISymbolicClassificationSolution Content {
     
    4241      this.Caption = "Interactive Classification Solution Simplifier";
    4342
    44       constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
    45       ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
    46       ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
    47       root.AddSubtree(start);
    48       tempTree = new SymbolicExpressionTree(root);
     43      calculator = new SymbolicClassificationSolutionImpactValuesCalculator();
    4944    }
    5045
     
    6661
    6762    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
    68       Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
    69       foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
    70         replacementValues[node] = CalculateReplacementValue(node, tree);
    71       }
    72       return replacementValues;
     63      return tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToDictionary(
     64        n => n,
     65        n => calculator.CalculateReplacementValue(Content.Model, n, Content.ProblemData, Content.ProblemData.TrainingIndices)
     66        );
    7367    }
    7468
    7569    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
    76       var model = Content.Model;
    77       var dataset = Content.ProblemData.Dataset;
    78       var rows = Content.ProblemData.TrainingIndices;
    79       string targetVariable = Content.ProblemData.TargetVariable;
    80       Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
    81       List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
    82 
    83       var targetClassValues = dataset.GetDoubleValues(targetVariable, rows);
    84       var originalClassValues = model.GetEstimatedClassValues(dataset, rows);
    85       OnlineCalculatorError errorState;
    86       double originalAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
    87       if (errorState != OnlineCalculatorError.None) originalAccuracy = 0.0;
    88 
    89       foreach (ISymbolicExpressionTreeNode node in nodes) {
    90         var parent = node.Parent;
    91         constantNode.Value = CalculateReplacementValue(node, tree);
    92         ISymbolicExpressionTreeNode replacementNode = constantNode;
    93         SwitchNode(parent, node, replacementNode);
    94         var newModel = CreateModel(tree);
    95         var newClassValues = newModel.GetEstimatedClassValues(dataset, rows);
    96         double newAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, newClassValues, out errorState);
    97         if (errorState != OnlineCalculatorError.None) newAccuracy = 0.0;
    98 
    99         // impact = 0 if no change
    100         // impact < 0 if new solution is better
    101         // impact > 0 if new solution is worse
    102         impactValues[node] = originalAccuracy - newAccuracy;
    103         SwitchNode(parent, replacementNode, node);
    104       }
    105       return impactValues;
    106     }
    107 
    108     private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
    109       // remove old ADFs
    110       while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
    111       // clone ADFs of source tree
    112       for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
    113         tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
    114       }
    115       var start = tempTree.Root.GetSubtree(0);
    116       while (start.SubtreeCount > 0) start.RemoveSubtree(0);
    117       start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
    118       var interpreter = Content.Model.Interpreter;
    119       var rows = Content.ProblemData.TrainingIndices;
    120       return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows)
    121              .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit).Median();
    122     }
    123 
    124 
    125     private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
    126       for (int i = 0; i < root.SubtreeCount; i++) {
    127         if (root.GetSubtree(i) == oldBranch) {
    128           root.RemoveSubtree(i);
    129           root.InsertSubtree(i, newBranch);
    130           return;
    131         }
    132       }
     70      return tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToDictionary(
     71        n => n,
     72        n => calculator.CalculateImpactValue(Content.Model, n, Content.ProblemData, Content.ProblemData.TrainingIndices, Content.TrainingAccuracy)
     73        );
    13374    }
    13475
Note: See TracChangeset for help on using the changeset viewer.