Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15321


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.
Location:
branches/SimplifierViewsProgress
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicClassificationSolutionSimplifierViewBase.cs

    r14826 r15321  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    2523using HeuristicLab.Common;
    2624using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    3432      get { return (ISymbolicClassificationSolution)base.Content; }
    3533      set { base.Content = value; }
     34    }
     35
     36    protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator {
     37      get { return calculator; }
    3638    }
    3739
     
    6062    }
    6163
    62     protected override Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
    63       var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
    64       foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
    65         double impactValue, replacementValue, newQualityForImpactsCalculation;
    66         calculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
    67         impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
    68       }
    69       return impactAndReplacementValues;
    70     }
    71 
    7264    protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
    7365
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs

    r14826 r15321  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
    2524using HeuristicLab.Core;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2826
     
    4038    protected SymbolicClassificationSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { }
    4139
    42     public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model,
    43       ISymbolicExpressionTreeNode node,
    44       IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue,
    45       out double newQualityForImpactsCalculation,
    46       double qualityForImpactsCalculation = Double.NaN) {
     40    protected override double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
    4741      var classificationModel = (ISymbolicClassificationModel)model;
    4842      var classificationProblemData = (IClassificationProblemData)problemData;
    49 
    50       if (double.IsNaN(qualityForImpactsCalculation))
    51         qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);
    52 
    53 
    54       var cloner = new Cloner();
    55       var tempModel = cloner.Clone(classificationModel);
    56       var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
    57 
    58       var tempModelParentNode = tempModelNode.Parent;
    59       int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
    60       double bestReplacementValue = 0.0;
    61       double bestImpactValue = double.PositiveInfinity;
    62       newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize
    63       // try the potentially reasonable replacement values and use the best one
    64       foreach (var repValue in CalculateReplacementValues(node, classificationModel.SymbolicExpressionTree, classificationModel.Interpreter, classificationProblemData.Dataset, classificationProblemData.TrainingIndices)) {
    65         tempModelParentNode.RemoveSubtree(i);
    66 
    67         var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
    68         tempModelParentNode.InsertSubtree(i, constantNode);
    69 
    70         var dataset = classificationProblemData.Dataset;
    71         var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
    72         var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
    73         OnlineCalculatorError errorState;
    74         newQualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues,
    75           out errorState);
    76         if (errorState != OnlineCalculatorError.None) newQualityForImpactsCalculation = 0.0;
    77 
    78         impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
    79 
    80         if (impactValue < bestImpactValue) {
    81           bestImpactValue = impactValue;
    82           bestReplacementValue = repValue;
    83         }
    84       }
    85       replacementValue = bestReplacementValue;
    86       impactValue = bestImpactValue;
    87     }
    88 
    89     public static double CalculateQualityForImpacts(ISymbolicClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {
    9043      OnlineCalculatorError errorState;
    9144      var dataset = problemData.Dataset;
    92       var targetClassValues = dataset.GetDoubleValues(problemData.TargetVariable, rows);
    93       var originalClassValues = model.GetEstimatedClassValues(dataset, rows);
     45      var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
     46      var originalClassValues = classificationModel.GetEstimatedClassValues(dataset, rows);
    9447      var qualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
    9548      if (errorState != OnlineCalculatorError.None) qualityForImpactsCalculation = 0.0;
    96 
    9749      return qualityForImpactsCalculation;
    9850    }
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs

    r15319 r15321  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Threading.Tasks;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    3534    }
    3635
     36    protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator {
     37      get { return calculator; }
     38    }
     39
    3740    public InteractiveSymbolicRegressionSolutionSimplifierView()
    3841      : base() {
     
    4649      model.Scale(Content.ProblemData);
    4750      Content.Model = model;
    48     }
    49 
    50 
    51     protected override Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
    52       var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
    53       foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
    54         double impactValue, replacementValue, newQualityForImpactsCalculation;
    55         calculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
    56         Progress.ProgressValue += 1.0 / (tree.Length - 2);
    57         impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
    58       }
    59       return impactAndReplacementValues;
    6051    }
    6152
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs

    r14826 r15321  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
    2524using HeuristicLab.Core;
    26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2826
     
    3230  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
    3331    public SymbolicRegressionSolutionImpactValuesCalculator() { }
    34 
    3532    protected SymbolicRegressionSolutionImpactValuesCalculator(SymbolicRegressionSolutionImpactValuesCalculator original, Cloner cloner)
    3633      : base(original, cloner) { }
     
    3835      return new SymbolicRegressionSolutionImpactValuesCalculator(this, cloner);
    3936    }
    40 
    4137    [StorableConstructor]
    4238    protected SymbolicRegressionSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { }
    4339
    44     public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
    45       IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
    46       double qualityForImpactsCalculation = double.NaN) {
     40    protected override double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
    4741      var regressionModel = (ISymbolicRegressionModel)model;
    4842      var regressionProblemData = (IRegressionProblemData)problemData;
    49 
    50       var dataset = regressionProblemData.Dataset;
    51       var targetValues = dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows);
    52 
    53       if (double.IsNaN(qualityForImpactsCalculation))
    54         qualityForImpactsCalculation = CalculateQualityForImpacts(regressionModel, regressionProblemData, rows);
    55 
    56       var cloner = new Cloner();
    57       var tempModel = cloner.Clone(regressionModel);
    58       var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
    59 
    60       var tempModelParentNode = tempModelNode.Parent;
    61       int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
    62 
    63       double bestReplacementValue = 0.0;
    64       double bestImpactValue = double.PositiveInfinity;
    65       newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize
    66       // try the potentially reasonable replacement values and use the best one
    67       foreach (var repValue in CalculateReplacementValues(node, regressionModel.SymbolicExpressionTree, regressionModel.Interpreter, regressionProblemData.Dataset, regressionProblemData.TrainingIndices)) {
    68 
    69         tempModelParentNode.RemoveSubtree(i);
    70 
    71         var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
    72 
    73         tempModelParentNode.InsertSubtree(i, constantNode);
    74 
    75         var estimatedValues = tempModel.GetEstimatedValues(dataset, rows);
    76         OnlineCalculatorError errorState;
    77         double r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out errorState);
    78         if (errorState != OnlineCalculatorError.None) r = 0.0;
    79         newQualityForImpactsCalculation = r * r;
    80 
    81         impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
    82         if (impactValue < bestImpactValue) {
    83           bestImpactValue = impactValue;
    84           bestReplacementValue = repValue;
    85         }
    86       }
    87       replacementValue = bestReplacementValue;
    88       impactValue = bestImpactValue;
    89     }
    90 
    91     public static double CalculateQualityForImpacts(ISymbolicRegressionModel model, IRegressionProblemData problemData, IEnumerable<int> rows) {
    92       var estimatedValues = model.GetEstimatedValues(problemData.Dataset, rows); // also bounds the values
    93       var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     43      var estimatedValues = regressionModel.GetEstimatedValues(problemData.Dataset, rows); // also bounds the values
     44      var targetValues = problemData.Dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows);
    9445      OnlineCalculatorError errorState;
    9546      var r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out errorState);
  • 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  }
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r15319 r15321  
    191191    }
    192192
    193     protected abstract Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree);
     193    protected virtual Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
     194      var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
     195      foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
     196        double impactValue, replacementValue, newQualityForImpactsCalculation;
     197        ImpactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
     198        Progress.ProgressValue += 1.0 / (tree.Length - 2);
     199        impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
     200      }
     201      return impactAndReplacementValues;
     202    }
     203
    194204    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
     205
     206    protected abstract ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator { get; }
    195207
    196208    private static ConstantTreeNode MakeConstantTreeNode(double value) {
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs

    r14826 r15321  
    3232  public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : Item, ISymbolicDataAnalysisSolutionImpactValuesCalculator {
    3333    protected SymbolicDataAnalysisSolutionImpactValuesCalculator() { }
    34 
    3534    protected SymbolicDataAnalysisSolutionImpactValuesCalculator(SymbolicDataAnalysisSolutionImpactValuesCalculator original, Cloner cloner)
    3635      : base(original, cloner) { }
    3736    [StorableConstructor]
    3837    protected SymbolicDataAnalysisSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { }
    39     public abstract void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, double qualityForImpactsCalculation = double.NaN);
     38
     39    public virtual void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows,
     40        out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
     41        double qualityForImpactsCalculation = double.NaN) {
     42      if (double.IsNaN(qualityForImpactsCalculation))
     43        qualityForImpactsCalculation = CalculateQualityForImpacts(model, problemData, rows);
     44
     45      var cloner = new Cloner();
     46      var tempModel = cloner.Clone(model);
     47      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
     48
     49      var tempModelParentNode = tempModelNode.Parent;
     50      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
     51
     52      double bestReplacementValue = 0.0;
     53      double bestImpactValue = double.PositiveInfinity;
     54      newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize
     55      // try the potentially reasonable replacement values and use the best one
     56      foreach (var repValue in CalculateReplacementValues(node, model.SymbolicExpressionTree, model.Interpreter, problemData.Dataset, problemData.TrainingIndices)) {
     57        tempModelParentNode.RemoveSubtree(i);
     58
     59        var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
     60        tempModelParentNode.InsertSubtree(i, constantNode);
     61
     62        newQualityForImpactsCalculation = CalculateQualityForImpacts(tempModel, problemData, rows);
     63
     64        impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
     65        if (impactValue < bestImpactValue) {
     66          bestImpactValue = impactValue;
     67          bestReplacementValue = repValue;
     68        }
     69      }
     70
     71      replacementValue = bestReplacementValue;
     72      impactValue = bestImpactValue;
     73    }
     74
     75    protected abstract double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows);
    4076
    4177    protected IEnumerable<double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
Note: See TracChangeset for help on using the changeset viewer.