Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/10/17 17:16:07 (7 years ago)
Author:
pfleck
Message:

#1666

  • Moved CalculateImpactAndReplacementValues into base-class.
  • Use SymRegImpactCalculator for SymTimeSeriesPrognosis.
  • Unified CalculateImpactAndReplacementValues for all SimplifierViews.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4/InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView.cs

    r14826 r15321  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using HeuristicLab.Common;
    2623using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     24using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    2725using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
    2826
    2927namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views {
    3028  public partial class InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
    31     private readonly ConstantTreeNode constantNode;
    32     private readonly SymbolicExpressionTree tempTree;
     29    private readonly SymbolicRegressionSolutionImpactValuesCalculator calculator;
    3330
    3431    public new SymbolicTimeSeriesPrognosisSolution Content {
     
    3734    }
    3835
     36    protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator {
     37      get { return calculator; }
     38    }
     39
    3940    public InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView()
    4041      : base() {
    4142      InitializeComponent();
    4243      this.Caption = "Interactive Time-Series Prognosis Solution Simplifier";
    43 
    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);
    49     }
    50 
    51     protected override Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
    52       var interpreter = Content.Model.Interpreter;
    53       var rows = Content.ProblemData.TrainingIndices;
    54       var dataset = Content.ProblemData.Dataset;
    55       var targetVariable = Content.ProblemData.TargetVariable;
    56       var targetValues = dataset.GetDoubleValues(targetVariable, rows);
    57       var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToArray();
    58 
    59       var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
    60       List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
    61       OnlineCalculatorError errorState;
    62       double originalR = OnlinePearsonsRCalculator.Calculate(targetValues, originalOutput, out errorState);
    63       if (errorState != OnlineCalculatorError.None) originalR = 0.0;
    64 
    65       foreach (ISymbolicExpressionTreeNode node in nodes) {
    66         var parent = node.Parent;
    67         constantNode.Value = CalculateReplacementValue(node, tree);
    68         ISymbolicExpressionTreeNode replacementNode = constantNode;
    69         SwitchNode(parent, node, replacementNode);
    70         var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
    71         double newR = OnlinePearsonsRCalculator.Calculate(targetValues, newOutput, out errorState);
    72         if (errorState != OnlineCalculatorError.None) newR = 0.0;
    73 
    74         // impact = 0 if no change
    75         // impact < 0 if new solution is better
    76         // impact > 0 if new solution is worse
    77         double impact = (originalR * originalR) - (newR * newR);
    78         impactAndReplacementValues[node] = new Tuple<double, double>(impact, constantNode.Value);
    79         SwitchNode(parent, replacementNode, node);
    80       }
    81       return impactAndReplacementValues;
     44      calculator = new SymbolicRegressionSolutionImpactValuesCalculator();
    8245    }
    8346
     
    8851    }
    8952
    90     private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
    91       // remove old ADFs
    92       while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
    93       // clone ADFs of source tree
    94       for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
    95         tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
    96       }
    97       var start = tempTree.Root.GetSubtree(0);
    98       while (start.SubtreeCount > 0) start.RemoveSubtree(0);
    99       start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
    100       var interpreter = Content.Model.Interpreter;
    101       var rows = Content.ProblemData.TrainingIndices;
    102       var allPrognosedValues = interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows);
     53    protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
    10354
    104       return allPrognosedValues.Median();
    105     }
    106 
    107 
    108     private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
    109       for (int i = 0; i < root.SubtreeCount; i++) {
    110         if (root.GetSubtree(i) == oldBranch) {
    111           root.RemoveSubtree(i);
    112           root.InsertSubtree(i, newBranch);
    113           return;
    114         }
    115       }
    116     }
    117 
    118     protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
    119       throw new NotImplementedException();
    12055    }
    12156  }
Note: See TracChangeset for help on using the changeset viewer.