Changeset 8946


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

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

Location:
trunk/sources
Files:
1 deleted
23 edited
9 copied

Legend:

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

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views

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

    r8636 r8946  
    3030    }
    3131
    32     public InteractiveSymbolicClassificationSolutionSimplifierView() : base() { }
     32    public InteractiveSymbolicClassificationSolutionSimplifierView()
     33      : base() {
     34      InitializeComponent();
     35      this.Caption = "Interactive Classification Solution Simplifier";
     36    }
    3337
    3438    protected override void UpdateModel(ISymbolicExpressionTree tree) {
  • 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
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView.cs

    r8636 r8946  
    2424namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views {
    2525  public partial class InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView : InteractiveSymbolicClassificationSolutionSimplifierViewBase {
    26 
    2726    public new SymbolicDiscriminantFunctionClassificationSolution Content {
    2827      get { return (SymbolicDiscriminantFunctionClassificationSolution)base.Content; }
     
    3029    }
    3130
    32     public InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView() : base() { }
     31    public InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView()
     32      : base() {
     33      InitializeComponent();
     34      this.Caption = "Interactive Classification Solution Simplifier";
     35    }
    3336
    3437    protected override void UpdateModel(ISymbolicExpressionTree tree) {
    3538      var model = CreateModel(tree);
    36       model.RecalculateModelParameters(Content.ProblemData, Content.ProblemData.TrainingIndices);
    3739      Content.Model = (ISymbolicDiscriminantFunctionClassificationModel)model;
    3840    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification-3.4.csproj

    r8606 r8946  
    120120    <Compile Include="ModelCreators\NormalDistributedThresholdsModelCreator.cs" />
    121121    <Compile Include="MultiObjective\SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs" />
     122    <Compile Include="SymbolicClassificationSolutionImpactValuesCalculator.cs" />
    122123    <Compile Include="SymbolicNearestNeighbourClassificationModel.cs" />
    123124    <Compile Include="Plugin.cs" />
     
    258259  -->
    259260  <PropertyGroup>
    260    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     261    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    261262set ProjectDir=$(ProjectDir)
    262263set SolutionDir=$(SolutionDir)
     
    265266call PreBuildEvent.cmd
    266267</PreBuildEvent>
    267 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     268    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
    268269export ProjectDir=$(ProjectDir)
    269270export SolutionDir=$(SolutionDir)
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/Properties

    • Property svn:ignore
      --- 
      +++ 
      
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs

    r8942 r8946  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using System.Linq;
    2523using HeuristicLab.Common;
    2624using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2725
    2826namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
    29   public class SymbolicDiscriminantFunctionClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
    30     public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateReplacementValues(ISymbolicExpressionTree tree,
    31                                                                                                        ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    32                                                                                                        IDataAnalysisProblemData problemData) {
    33       return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()
    34              select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateReplacementValue(node, tree, interpreter, problemData));
     27  public class SymbolicClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
     28    public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
     29      var classificationModel = (ISymbolicClassificationModel)model;
     30      var classificationProblemData = (IClassificationProblemData)problemData;
     31
     32      return CalculateReplacementValue(node, classificationModel.SymbolicExpressionTree, classificationModel.Interpreter, classificationProblemData.Dataset, rows);
    3533    }
    3634
    37     public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateImpactValues(ISymbolicExpressionTree tree,
    38                                                                                                   ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    39                                                                                                   IDataAnalysisProblemData classificationProblemData,
    40                                                                                                   double lowerEstimationLimit, double upperEstimationLimit) {
    41       var problemData = (IClassificationProblemData)classificationProblemData;
    42       var dataset = problemData.Dataset;
    43       var rows = problemData.TrainingIndices;
    44       string targetVariable = problemData.TargetVariable;
    45       var targetClassValues = dataset.GetDoubleValues(targetVariable, rows);
    46       var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit).ToArray();
     35    public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) {
     36      var classificationModel = (ISymbolicClassificationModel)model;
     37      var classificationProblemData = (IClassificationProblemData)problemData;
     38
     39      var dataset = classificationProblemData.Dataset;
     40      var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
     41
    4742      OnlineCalculatorError errorState;
    48       double originalGini = NormalizedGiniCalculator.Calculate(targetClassValues, originalOutput, out errorState);
    49       if (errorState != OnlineCalculatorError.None) originalGini = 0.0;
     43      if (double.IsNaN(originalQuality)) {
     44        var originalClassValues = classificationModel.GetEstimatedClassValues(dataset, rows);
     45        originalQuality = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
     46        if (errorState != OnlineCalculatorError.None) originalQuality = 0.0;
     47      }
    5048
    51       return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix()
    52              select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateImpact(tree, originalGini, node, interpreter, problemData, lowerEstimationLimit, upperEstimationLimit));
     49      var replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
     50      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
     51      var cloner = new Cloner();
     52      cloner.RegisterClonedObject(node, constantNode);
     53      var tempModel = cloner.Clone(classificationModel);
     54      tempModel.RecalculateModelParameters(classificationProblemData, rows);
     55
     56      var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
     57      double newQuality = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
     58      if (errorState != OnlineCalculatorError.None) newQuality = 0.0;
     59
     60      return originalQuality - newQuality;
    5361    }
    5462
    55     private static double CalculateImpact(ISymbolicExpressionTree tree, double originalQuality, ISymbolicExpressionTreeNode node,
    56                                           ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IClassificationProblemData problemData,
    57                                           double lowerEstimationLimit, double upperEstimationLimit) {
    58       var dataset = problemData.Dataset;
    59       var rows = problemData.TrainingIndices.ToList();
    60       string targetVariable = problemData.TargetVariable;
    61       var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
    62 
    63       var parent = node.Parent;
    64       var constantNode = (ConstantTreeNode)new Constant().CreateTreeNode();
    65       constantNode.Value = CalculateReplacementValue(node, tree, interpreter, problemData);
    66       SwitchNode(parent, node, constantNode);
    67       var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
    68                                  .LimitToRange(lowerEstimationLimit, upperEstimationLimit)
    69                                  .ToArray();
    70       OnlineCalculatorError errorState;
    71       double quality = NormalizedGiniCalculator.Calculate(targetValues, newOutput, out errorState);
    72       if (errorState != OnlineCalculatorError.None) quality = 0.0;
    73       SwitchNode(parent, constantNode, node);
    74       // impact = 0 if no change
    75       // impact < 0 if new solution is better
    76       // impact > 0 if new solution is worse
    77       return originalQuality - quality;
    78     }
    7963  }
    8064}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs

    r8736 r8946  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using HeuristicLab.Common;
    2625using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2726using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
     
    2928namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views {
    3029  public partial class InteractiveSymbolicRegressionSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
    31     private readonly ConstantTreeNode constantNode;
    32     private readonly SymbolicExpressionTree tempTree;
     30    private readonly SymbolicRegressionSolutionImpactValuesCalculator calculator;
    3331
    3432    public new SymbolicRegressionSolution Content {
     
    4139      InitializeComponent();
    4240      this.Caption = "Interactive Regression 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);
     41      calculator = new SymbolicRegressionSolutionImpactValuesCalculator();
    4942    }
    5043
     
    5649
    5750    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
    58       Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
    59       foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
    60         replacementValues[node] = CalculateReplacementValue(node, tree);
    61       }
    62       return replacementValues;
     51      return tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToDictionary(
     52        n => n,
     53        n => calculator.CalculateReplacementValue(Content.Model, n, Content.ProblemData, Content.ProblemData.TrainingIndices)
     54        );
    6355    }
    6456
    6557    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
    66       var interpreter = Content.Model.Interpreter;
    67       var dataset = Content.ProblemData.Dataset;
    68       var rows = Content.ProblemData.TrainingIndices;
    69       string targetVariable = Content.ProblemData.TargetVariable;
    70       Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
    71       List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
    72       var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit).ToArray();
    73       var targetValues = dataset.GetDoubleValues(targetVariable, rows);
    74       OnlineCalculatorError errorState;
    75       double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
    76       if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
    77 
    78       foreach (ISymbolicExpressionTreeNode node in nodes) {
    79         var parent = node.Parent;
    80         constantNode.Value = CalculateReplacementValue(node, tree);
    81         ISymbolicExpressionTreeNode replacementNode = constantNode;
    82         SwitchNode(parent, node, replacementNode);
    83         var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
    84         double newR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
    85         if (errorState != OnlineCalculatorError.None) newR2 = 0.0;
    86 
    87         // impact = 0 if no change
    88         // impact < 0 if new solution is better
    89         // impact > 0 if new solution is worse
    90         impactValues[node] = originalR2 - newR2;
    91         SwitchNode(parent, replacementNode, node);
    92       }
    93       return impactValues;
     58      return tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToDictionary(
     59        n => n,
     60        n => calculator.CalculateImpactValue(Content.Model, n, Content.ProblemData, Content.ProblemData.TrainingIndices, Content.TrainingRSquared)
     61        );
    9462    }
    95 
    96     private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
    97       // remove old ADFs
    98       while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
    99       // clone ADFs of source tree
    100       for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
    101         tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
    102       }
    103       var start = tempTree.Root.GetSubtree(0);
    104       while (start.SubtreeCount > 0) start.RemoveSubtree(0);
    105       start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
    106       var interpreter = Content.Model.Interpreter;
    107       var rows = Content.ProblemData.TrainingIndices;
    108       return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows)
    109                          .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
    110                          .Median();
    111     }
    112 
    113 
    114     private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
    115       for (int i = 0; i < root.SubtreeCount; i++) {
    116         if (root.GetSubtree(i) == oldBranch) {
    117           root.RemoveSubtree(i);
    118           root.InsertSubtree(i, newBranch);
    119           return;
    120         }
    121       }
    122     }
    123 
    124     protected override void OnModelChanged() {
    125       base.OnModelChanged();
    126       if (Content != null)
    127         btnOptimizeConstants.Enabled =
    128           SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(Content.Model.SymbolicExpressionTree);
    129       else
    130         btnOptimizeConstants.Enabled = false;
    131     }
    132     protected override void OnContentChanged() {
    133       base.OnContentChanged();
    134       base.OnModelChanged();
    135       if (Content != null)
    136         btnOptimizeConstants.Enabled =
    137           SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(Content.Model.SymbolicExpressionTree);
    138       else
    139         btnOptimizeConstants.Enabled = false;
    140     }
    141 
    14263
    14364    protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
    14465      var model = Content.Model;
    145       SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(Content.Model.Interpreter, Content.Model.SymbolicExpressionTree, Content.ProblemData, Content.ProblemData.TrainingIndices,
     66      SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(model.Interpreter, model.SymbolicExpressionTree, Content.ProblemData, Content.ProblemData.TrainingIndices,
    14667        applyLinearScaling: true, maxIterations: 50, upperEstimationLimit: model.UpperEstimationLimit, lowerEstimationLimit: model.LowerEstimationLimit);
    14768      UpdateModel(Content.Model.SymbolicExpressionTree);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4

    • Property svn:ignore
      •  

        old new  
         1*.user
         2Plugin.cs
        13bin
        2 *.user
        3 HeuristicLabProblemsDataAnalysisSymbolicRegressionPlugin.cs
        44obj
        5 *.vs10x
        6 Plugin.cs
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r8895 r8946  
    143143    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" />
    144144    <Compile Include="SymbolicRegressionSolution.cs" />
     145    <Compile Include="SymbolicRegressionSolutionImpactValuesCalculator.cs" />
    145146    <None Include="HeuristicLab.snk" />
    146147    <None Include="Plugin.cs.frame" />
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Properties

    • Property svn:ignore
      --- 
      +++ 
      
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs

    r8942 r8946  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using System.Linq;
    2523using HeuristicLab.Common;
    2624using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2826namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2927  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
    30     public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateReplacementValues(ISymbolicExpressionTree tree,
    31                                                                                                        ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    32                                                                                                        IDataAnalysisProblemData problemData) {
    33       return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()
    34              select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateReplacementValue(node, tree, interpreter, problemData));
     28    public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
     29      var regressionModel = (ISymbolicRegressionModel)model;
     30      var regressionProblemData = (IRegressionProblemData)problemData;
     31
     32      return CalculateReplacementValue(node, regressionModel.SymbolicExpressionTree, regressionModel.Interpreter, regressionProblemData.Dataset, rows);
    3533    }
    3634
    37     public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateImpactValues(ISymbolicExpressionTree tree,
    38                                                                                                   ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    39                                                                                                   IDataAnalysisProblemData regressionProblemData,
    40                                                                                                   double lowerEstimationLimit, double upperEstimationLimit) {
    41       var problemData = (IRegressionProblemData)regressionProblemData;
    42       var dataset = problemData.Dataset;
    43       var rows = problemData.TrainingIndices.ToList();
    44       string targetVariable = problemData.TargetVariable;
    45       List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
    46       var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit).ToArray();
    47       var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
     35    public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) {
     36      var regressionModel = (ISymbolicRegressionModel)model;
     37      var regressionProblemData = (IRegressionProblemData)problemData;
     38
     39      var dataset = regressionProblemData.Dataset;
     40      var targetValues = dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows);
     41
    4842      OnlineCalculatorError errorState;
    49       double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
    50       if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
     43      if (double.IsNaN(originalQuality)) {
     44        var originalClassValues = regressionModel.GetEstimatedValues(dataset, rows);
     45        originalQuality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalClassValues, out errorState);
     46        if (errorState != OnlineCalculatorError.None) originalQuality = 0.0;
     47      }
    5148
    52       return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList()
    53              select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateImpact(tree, originalR2, node, interpreter, problemData, lowerEstimationLimit, upperEstimationLimit));
     49      var replacementValue = CalculateReplacementValue(regressionModel, node, regressionProblemData, rows);
     50      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
     51      var cloner = new Cloner();
     52      cloner.RegisterClonedObject(node, constantNode);
     53      var tempModel = cloner.Clone(regressionModel);
     54      SymbolicDataAnalysisModel.Scale(tempModel, regressionProblemData, regressionProblemData.TargetVariable);
     55
     56      var estimatedValues = tempModel.GetEstimatedValues(dataset, rows);
     57      double newQuality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, estimatedValues, out errorState);
     58      if (errorState != OnlineCalculatorError.None) newQuality = 0.0;
     59
     60      return originalQuality - newQuality;
    5461    }
    5562
    56     private static double CalculateImpact(ISymbolicExpressionTree tree, double originalQuality, ISymbolicExpressionTreeNode node,
    57                                           ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IRegressionProblemData problemData,
    58                                           double lowerEstimationLimit, double upperEstimationLimit) {
    59       var dataset = problemData.Dataset;
    60       var rows = problemData.TrainingIndices.ToList();
    61       string targetVariable = problemData.TargetVariable;
    62       var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
    63 
    64       var parent = node.Parent;
    65       var constantNode = (ConstantTreeNode)new Constant().CreateTreeNode();
    66       constantNode.Value = CalculateReplacementValue(node, tree, interpreter, problemData);
    67       SwitchNode(parent, node, constantNode);
    68       var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
    69                                  .LimitToRange(lowerEstimationLimit, upperEstimationLimit)
    70                                  .ToArray();
    71       OnlineCalculatorError errorState;
    72       double quality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
    73       if (errorState != OnlineCalculatorError.None) quality = 0.0;
    74       SwitchNode(parent, constantNode, node);
    75       // impact = 0 if no change
    76       // impact < 0 if new solution is better
    77       // impact > 0 if new solution is worse
    78       return originalQuality - quality;
    79     }
    8063  }
    8164}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4

    • Property svn:ignore
      •  

        old new  
         1*.user
         2Plugin.cs
        13bin
        24obj
        3 *.user
        4 HeuristicLabProblemsDataAnalysisSymbolicViewsPlugin.cs
        5 *.vs10x
        6 Plugin.cs
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4.csproj

    r8600 r8946  
    114114  </ItemGroup>
    115115  <ItemGroup>
     116    <Compile Include="InteractiveSymbolicExpressionTreeChart.cs">
     117      <SubType>UserControl</SubType>
     118    </Compile>
     119    <Compile Include="InteractiveSymbolicExpressionTreeChart.Designer.cs">
     120      <DependentUpon>InteractiveSymbolicExpressionTreeChart.cs</DependentUpon>
     121    </Compile>
    116122    <Compile Include="MathSymbolicDataAnalysisModelView.cs">
    117123      <SubType>UserControl</SubType>
     
    121127    </Compile>
    122128    <Compile Include="Plugin.cs" />
     129    <Compile Include="SymbolicExpressionTreeNodeChangeValueDialog.cs">
     130      <SubType>Form</SubType>
     131    </Compile>
     132    <Compile Include="SymbolicExpressionTreeNodeChangeValueDialog.Designer.cs">
     133      <DependentUpon>SymbolicExpressionTreeNodeChangeValueDialog.cs</DependentUpon>
     134    </Compile>
    123135    <Compile Include="TextualSymbolicDataAnalysisModelView.cs">
    124136      <SubType>UserControl</SubType>
     
    168180    <Compile Include="Symbols\VariableView.Designer.cs">
    169181      <DependentUpon>VariableView.cs</DependentUpon>
     182    </Compile>
     183    <Compile Include="TreeEditDialogs\SymbolicExpressionTreeNodeChangeValueDialog.cs">
     184      <SubType>Form</SubType>
     185    </Compile>
     186    <Compile Include="TreeEditDialogs\SymbolicExpressionTreeNodeChangeValueDialog.Designer.cs">
     187      <DependentUpon>SymbolicExpressionTreeNodeChangeValueDialog.cs</DependentUpon>
     188    </Compile>
     189    <Compile Include="TreeEditDialogs\SymbolicExpressionTreeNodeInsertDialog.cs">
     190      <SubType>Form</SubType>
     191    </Compile>
     192    <Compile Include="TreeEditDialogs\SymbolicExpressionTreeNodeInsertDialog.Designer.cs">
     193      <DependentUpon>SymbolicExpressionTreeNodeInsertDialog.cs</DependentUpon>
    170194    </Compile>
    171195    <None Include="HeuristicLab.snk" />
     
    313337  -->
    314338  <PropertyGroup>
    315    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     339    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    316340set ProjectDir=$(ProjectDir)
    317341set SolutionDir=$(SolutionDir)
     
    320344call PreBuildEvent.cmd
    321345</PreBuildEvent>
    322 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     346    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
    323347export ProjectDir=$(ProjectDir)
    324348export SolutionDir=$(SolutionDir)
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.Designer.cs

    r8053 r8946  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    4545    /// </summary>
    4646    private void InitializeComponent() {
    47       this.treeChart = new HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views.SymbolicExpressionTreeChart();
     47      this.components = new System.ComponentModel.Container();
    4848      this.viewHost = new HeuristicLab.MainForm.WindowsForms.ViewHost();
    4949      this.splitContainer = new System.Windows.Forms.SplitContainer();
     
    5252      this.btnSimplify = new System.Windows.Forms.Button();
    5353      this.btnOptimizeConstants = new System.Windows.Forms.Button();
     54      this.treeChart = new HeuristicLab.Problems.DataAnalysis.Symbolic.Views.InteractiveSymbolicExpressionTreeChart();
    5455      this.grpViewHost = new System.Windows.Forms.GroupBox();
    5556      ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
     
    6162      this.grpViewHost.SuspendLayout();
    6263      this.SuspendLayout();
    63       //
    64       // treeChart
    65       //
    66       this.treeChart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    67             | System.Windows.Forms.AnchorStyles.Left)
    68             | System.Windows.Forms.AnchorStyles.Right)));
    69       this.treeChart.BackgroundColor = System.Drawing.Color.White;
    70       this.treeChart.LineColor = System.Drawing.Color.Black;
    71       this.treeChart.Location = new System.Drawing.Point(6, 16);
    72       this.treeChart.Name = "treeChart";
    73       this.treeChart.Size = new System.Drawing.Size(201, 291);
    74       this.treeChart.Spacing = 5;
    75       this.treeChart.TabIndex = 0;
    76       this.treeChart.TextFont = new System.Drawing.Font("Times New Roman", 8F);
    77       this.treeChart.Tree = null;
    78       this.treeChart.SymbolicExpressionTreeNodeDoubleClicked += new System.Windows.Forms.MouseEventHandler(this.treeChart_SymbolicExpressionTreeNodeDoubleClicked);
    7964      //
    8065      // viewHost
     
    11398      // grpSimplify
    11499      //
     100      this.grpSimplify.AutoSize = true;
    115101      this.grpSimplify.Controls.Add(this.flowLayoutPanel);
    116102      this.grpSimplify.Controls.Add(this.treeChart);
     
    157143      this.btnOptimizeConstants.Click += new System.EventHandler(this.btnOptimizeConstants_Click);
    158144      //
     145      // treeChart
     146      //
     147      this.treeChart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     148            | System.Windows.Forms.AnchorStyles.Left)
     149            | System.Windows.Forms.AnchorStyles.Right)));
     150      this.treeChart.BackgroundColor = System.Drawing.Color.White;
     151      this.treeChart.LineColor = System.Drawing.Color.Black;
     152      this.treeChart.Location = new System.Drawing.Point(6, 16);
     153      this.treeChart.Name = "treeChart";
     154      this.treeChart.Size = new System.Drawing.Size(201, 291);
     155      this.treeChart.Spacing = 5;
     156      this.treeChart.SuspendRepaint = false;
     157      this.treeChart.TabIndex = 0;
     158      this.treeChart.TextFont = new System.Drawing.Font("Times New Roman", 8F);
     159      this.treeChart.Tree = null;
     160      this.treeChart.SymbolicExpressionTreeChanged += new System.EventHandler(this.treeChart_SymbolicExpressionTreeChanged);
     161      this.treeChart.SymbolicExpressionTreeNodeChanged += new System.EventHandler(this.treeChart_SymbolicExpressionTreeNodeChanged);
     162      this.treeChart.SymbolicExpressionTreeNodeDoubleClicked += new System.Windows.Forms.MouseEventHandler(this.treeChart_SymbolicExpressionTreeNodeDoubleClicked);
     163      //
    159164      // grpViewHost
    160165      //
     
    171176      //
    172177      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    173       this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
     178      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    174179      this.Controls.Add(this.splitContainer);
     180      this.DoubleBuffered = true;
    175181      this.Name = "InteractiveSymbolicDataAnalysisSolutionSimplifierView";
    176182      this.Size = new System.Drawing.Size(564, 348);
    177183      this.splitContainer.Panel1.ResumeLayout(false);
     184      this.splitContainer.Panel1.PerformLayout();
    178185      this.splitContainer.Panel2.ResumeLayout(false);
    179186      ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).EndInit();
     
    188195    #endregion
    189196
    190     private HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views.SymbolicExpressionTreeChart treeChart;
     197    private InteractiveSymbolicExpressionTreeChart treeChart;
    191198    private System.Windows.Forms.SplitContainer splitContainer;
    192199    private HeuristicLab.MainForm.WindowsForms.ViewHost viewHost;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r7259 r8946  
    3434    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes;
    3535    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
    36     private bool updateInProgress = false;
     36    private Dictionary<ISymbolicExpressionTreeNode, double> originalValues;
     37    private Dictionary<ISymbolicExpressionTreeNode, string> originalVariableNames;
    3738
    3839    public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
    3940      InitializeComponent();
    40       this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
    41       this.nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
     41      replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
     42      nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
     43      originalValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
     44      originalVariableNames = new Dictionary<ISymbolicExpressionTreeNode, string>();
     45
    4246      this.Caption = "Interactive Solution Simplifier";
    4347    }
     
    5054    protected override void RegisterContentEvents() {
    5155      base.RegisterContentEvents();
    52       Content.ModelChanged += new EventHandler(Content_ModelChanged);
    53       Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
     56      Content.ModelChanged += Content_Changed;
     57      Content.ProblemDataChanged += Content_Changed;
    5458    }
    5559    protected override void DeregisterContentEvents() {
    5660      base.DeregisterContentEvents();
    57       Content.ModelChanged -= new EventHandler(Content_ModelChanged);
    58       Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
    59     }
    60 
    61     private void Content_ModelChanged(object sender, EventArgs e) {
    62       OnModelChanged();
    63     }
    64     private void Content_ProblemDataChanged(object sender, EventArgs e) {
    65       OnProblemDataChanged();
    66     }
    67 
    68     protected virtual void OnModelChanged() {
    69       this.CalculateReplacementNodesAndNodeImpacts();
    70     }
    71 
    72     protected virtual void OnProblemDataChanged() {
    73       this.CalculateReplacementNodesAndNodeImpacts();
     61      Content.ModelChanged -= Content_Changed;
     62      Content.ProblemDataChanged -= Content_Changed;
     63    }
     64
     65    private void Content_Changed(object sender, EventArgs e) {
     66      UpdateView();
    7467    }
    7568
    7669    protected override void OnContentChanged() {
    7770      base.OnContentChanged();
    78       this.CalculateReplacementNodesAndNodeImpacts();
    79       this.viewHost.Content = this.Content;
    80     }
    81 
    82     private void CalculateReplacementNodesAndNodeImpacts() {
    83       if (Content != null && Content.Model != null && Content.ProblemData != null) {
    84         var tree = Content.Model.SymbolicExpressionTree;
    85         var replacementValues = CalculateReplacementValues(tree);
    86         foreach (var pair in replacementValues) {
    87           if (!(pair.Key is ConstantTreeNode)) {
    88             replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
    89           }
    90         }
    91         nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree);
    92 
    93         if (!updateInProgress) {
    94           // automatically fold all branches with impact = 1
    95           List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSubtree(0).IterateNodesPrefix().ToList();
    96           foreach (var parent in nodeList) {
    97             for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) {
    98               var child = parent.GetSubtree(subTreeIndex);
    99               if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) {
    100                 SwitchNodeWithReplacementNode(parent, subTreeIndex);
    101               }
    102             }
    103           }
    104         }
    105 
    106         // show only interesting part of solution
    107         if (tree.Root.SubtreeCount > 1)
    108           this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs
    109         else
    110           this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB
    111         this.PaintNodeImpacts();
    112       }
     71      UpdateView();
     72      viewHost.Content = this.Content;
     73    }
     74
     75    private void UpdateView() {
     76      if (Content == null || Content.Model == null || Content.ProblemData == null) return;
     77      var tree = Content.Model.SymbolicExpressionTree;
     78
     79      var replacementValues = CalculateReplacementValues(tree);
     80      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
     81        replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
     82      }
     83
     84      nodeImpacts = CalculateImpactValues(tree);
     85
     86      var model = Content.Model.SymbolicExpressionTree;
     87      treeChart.Tree = model.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(model.Root) : new SymbolicExpressionTree(model.Root.GetSubtree(0).GetSubtree(0));
     88      PaintNodeImpacts();
    11389    }
    11490
     
    11793    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
    11894
    119     private ConstantTreeNode MakeConstantTreeNode(double value) {
    120       Constant constant = new Constant();
    121       constant.MinValue = value - 1;
    122       constant.MaxValue = value + 1;
    123       ConstantTreeNode constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
     95    private static ConstantTreeNode MakeConstantTreeNode(double value) {
     96      var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
     97      var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
    12498      constantTreeNode.Value = value;
    12599      return constantTreeNode;
     
    127101
    128102    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
    129       VisualSymbolicExpressionTreeNode visualTreeNode = (VisualSymbolicExpressionTreeNode)sender;
     103      if (!treeChart.TreeValid) return;
     104      var visualNode = (VisualSymbolicExpressionTreeNode)sender;
     105      var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.SymbolicExpressionTreeNode;
     106      if (symbExprTreeNode == null) return;
    130107      var tree = Content.Model.SymbolicExpressionTree;
    131       foreach (SymbolicExpressionTreeNode treeNode in tree.IterateNodesPostfix()) {
    132         for (int i = 0; i < treeNode.SubtreeCount; i++) {
    133           ISymbolicExpressionTreeNode subTree = treeNode.GetSubtree(i);
    134           // only allow to replace nodes for which a replacement value is known (replacement value for ADF nodes are not available)
    135           if (subTree == visualTreeNode.SymbolicExpressionTreeNode && replacementNodes.ContainsKey(subTree)) {
    136             SwitchNodeWithReplacementNode(treeNode, i);
    137 
    138             // show only interesting part of solution
    139             if (tree.Root.SubtreeCount > 1)
    140               this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs
    141             else
    142               this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB
    143 
    144             updateInProgress = true;
    145             UpdateModel(tree);
    146             updateInProgress = false;
    147             return; // break all loops
     108
     109      bool update = false;
     110      // check if the node value/weight has been altered
     111      // if so, the first double click will return the node to its original value/weight/variable name
     112      // the next double click will replace the ConstantNode with the original SymbolicExpressionTreeNode
     113      if (originalVariableNames.ContainsKey(symbExprTreeNode)) {
     114        var variable = (VariableTreeNode)symbExprTreeNode;
     115        variable.VariableName = originalVariableNames[symbExprTreeNode];
     116        originalVariableNames.Remove(variable);
     117        update = true;
     118      } else if (originalValues.ContainsKey(symbExprTreeNode)) {
     119        double value = originalValues[symbExprTreeNode];
     120        if (symbExprTreeNode.Symbol is Constant) {
     121          var constantTreeNode = (ConstantTreeNode)symbExprTreeNode;
     122          constantTreeNode.Value = value;
     123        } else if (symbExprTreeNode.Symbol is Variable) {
     124          var variable = (VariableTreeNode)symbExprTreeNode;
     125          variable.Weight = value;
     126        }
     127        originalValues.Remove(symbExprTreeNode);
     128        update = true;
     129      } else if (replacementNodes.ContainsKey(symbExprTreeNode)) {
     130        foreach (var treeNode in tree.IterateNodesPostfix()) {
     131          for (int i = 0; i < treeNode.SubtreeCount; i++) {
     132            var subtree = treeNode.GetSubtree(i);
     133            if (subtree == symbExprTreeNode) {
     134              SwitchNodeWithReplacementNode(treeNode, i);
     135              // show only interesting part of solution
     136              treeChart.Tree = tree.Root.SubtreeCount > 1
     137                                 ? new SymbolicExpressionTree(tree.Root)
     138                                 : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
     139              update = true;
     140            }
    148141          }
    149         }
     142          if (update) break;
     143        }
     144      }
     145      if (update) UpdateModel(tree);
     146    }
     147
     148    private void treeChart_SymbolicExpressionTreeChanged(object sender, EventArgs e) {
     149      UpdateModel(Content.Model.SymbolicExpressionTree);
     150      UpdateView();
     151    }
     152
     153    private void treeChart_SymbolicExpressionTreeNodeChanged(object sender, EventArgs e) {
     154      var dialog = (ValueChangeDialog)sender;
     155      bool flag1 = false, flag2 = false;
     156      var node = dialog.Content;
     157
     158      if (node is VariableTreeNode) {
     159        var variable = (VariableTreeNode)node;
     160        var weight = double.Parse(dialog.newValueTextBox.Text);
     161        var name = (string)dialog.variableNamesCombo.SelectedItem;
     162        if (!variable.Weight.Equals(weight)) {
     163          flag1 = true;
     164          originalValues[variable] = variable.Weight;
     165          variable.Weight = weight;
     166        }
     167        if (!variable.VariableName.Equals(name)) {
     168          flag2 = true;
     169          originalVariableNames[variable] = variable.VariableName;
     170          variable.VariableName = name;
     171        }
     172      } else if (node is ConstantTreeNode) {
     173        var constant = (ConstantTreeNode)node;
     174        var value = double.Parse(dialog.newValueTextBox.Text);
     175        if (!constant.Value.Equals(value)) {
     176          flag1 = true;
     177          originalValues[constant] = constant.Value;
     178          constant.Value = value;
     179        }
     180      }
     181      if (flag1 || flag2) {
     182        UpdateView();
    150183      }
    151184    }
     
    168201      double min = impacts.Min();
    169202      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
     203        VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
     204        bool flag1 = replacementNodes.ContainsKey(treeNode);
     205        bool flag2 = originalValues.ContainsKey(treeNode);
     206        bool flag3 = treeNode is ConstantTreeNode;
     207
     208        if (flag2) // constant or variable node was changed
     209          visualTree.ToolTip += Environment.NewLine + "Original value: " + originalValues[treeNode];
     210        else if (flag1 && flag3) // symbol node was folded to a constant
     211          visualTree.ToolTip += Environment.NewLine + "Original node: " + replacementNodes[treeNode];
     212
    170213        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
    171214          double impact = nodeImpacts[treeNode];
    172           VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
    173215
    174216          // impact = 0 if no change
     
    197239    private void PaintCollapsedNodes() {
    198240      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
    199         if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode))
    200           this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;
    201         else {
    202           VisualSymbolicExpressionTreeNode visNode = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
    203           if (visNode != null)
    204             visNode.LineColor = Color.Black;
     241        bool flag1 = replacementNodes.ContainsKey(treeNode);
     242        bool flag2 = originalValues.ContainsKey(treeNode);
     243        if (flag1 && treeNode is ConstantTreeNode) {
     244          this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = flag2 ? Color.DarkViolet : Color.DarkOrange;
     245        } else if (flag2) {
     246          this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DodgerBlue;
    205247        }
    206248      }
     
    208250
    209251    private void btnSimplify_Click(object sender, EventArgs e) {
    210       SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
     252      var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
    211253      var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
    212254      UpdateModel(simplifiedExpressionTree);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicExpressionTreeChart.cs

    r8942 r8946  
    2828
    2929namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
    30   public partial class InteractiveSymbolicExpressionTreeChart : SymbolicExpressionTreeChart {
     30  public sealed partial class InteractiveSymbolicExpressionTreeChart : SymbolicExpressionTreeChart {
    3131    private ISymbolicExpressionTreeNode tempNode;
    3232    private VisualSymbolicExpressionTreeNode lastSelected; // previously selected node
     
    163163      lastOp = EditOp.CutNode;
    164164      tempNode = currSelected.SymbolicExpressionTreeNode;
    165       var visualNode = visualTreeNodes[tempNode];
     165      var visualNode = GetVisualSymbolicExpressionTreeNode(tempNode);
    166166      visualNode.LineColor = Color.LightGray;
    167167      visualNode.TextColor = Color.LightGray;
     
    173173      tempNode = currSelected.SymbolicExpressionTreeNode; // should never be null
    174174      foreach (var node in tempNode.IterateNodesPostfix()) {
    175         var visualNode = visualTreeNodes[node];
     175        var visualNode = GetVisualSymbolicExpressionTreeNode(node);
    176176        visualNode.LineColor = Color.LightGray;
    177177        visualNode.TextColor = Color.LightGray;
    178178        if (node.SubtreeCount > 0) {
    179179          foreach (var subtree in node.Subtrees) {
    180             var visualLine = visualLines[new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree)];
     180            var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
    181181            visualLine.LineColor = Color.LightGray;
    182182          }
     
    198198      tempNode = currSelected.SymbolicExpressionTreeNode;
    199199      foreach (var node in tempNode.IterateNodesPostfix()) {
    200         var visualNode = visualTreeNodes[node];
     200        var visualNode = GetVisualSymbolicExpressionTreeNode(node);
    201201        visualNode.LineColor = Color.LightGray;
    202202        visualNode.TextColor = Color.LightGray;
    203203        if (node.SubtreeCount <= 0) continue;
    204204        foreach (var subtree in node.Subtrees) {
    205           var visualLine = visualLines[new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree)];
     205          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
    206206          visualLine.LineColor = Color.LightGray;
    207207        }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Properties

    • Property svn:ignore
      --- 
      +++ 
      
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4

    • Property svn:ignore
      •  

        old new  
         1*.user
         2Plugin.cs
        13bin
        2 *.user
        3 HeuristicLabProblemsDataAnalysisSymbolicPlugin.cs
        44obj
        5 *.vs10x
        6 Plugin.cs
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r8895 r8946  
    140140    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs" />
    141141    <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionCrossover.cs" />
     142    <Compile Include="Interfaces\ISymbolicDataAnalysisImpactValuesCalculator.cs" />
    142143    <Compile Include="Interpreter\InterpreterState.cs" />
    143144    <Compile Include="Interpreter\OpCodes.cs" />
     
    164165    <Compile Include="SymbolicDataAnalysisExpressionTreeSimplifier.cs" />
    165166    <Compile Include="SymbolicDataAnalysisProblem.cs" />
     167    <Compile Include="SymbolicDataAnalysisSolutionImpactValuesCalculator.cs" />
    166168    <Compile Include="Symbols\Addition.cs" />
    167169    <Compile Include="Symbols\And.cs" />
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisImpactValuesCalculator.cs

    r8942 r8946  
    44namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    55  public interface ISymbolicDataAnalysisSolutionImpactValuesCalculator {
    6     Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree,
    7                                                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    8                                                                                IDataAnalysisProblemData problemData);
    9     Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree,
    10                                                                           ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    11                                                                           IDataAnalysisProblemData problemData,
    12                                                                           double lowerEstimationLimit, double upperEstimationLimit);
     6    double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows);
     7    double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN);
    138  }
    149}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Properties

    • Property svn:ignore
      --- 
      +++ 
      
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs

    r8942 r8946  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
     
    2625
    2726namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    28   public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator {
    29     public abstract IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateReplacementValues(ISymbolicExpressionTree tree,
    30                                                                                                        ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    31                                                                                                        IDataAnalysisProblemData problemData);
    32     public abstract IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateImpactValues(ISymbolicExpressionTree tree,
    33                                                                                                   ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    34                                                                                                   IDataAnalysisProblemData problemData,
    35                                                                                                   double lowerEstimationLimit, double upperEstimationLimit);
     27  public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : ISymbolicDataAnalysisSolutionImpactValuesCalculator {
     28    public abstract double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows);
     29    public abstract double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN);
    3630
    37     protected static void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
    38       for (int i = 0; i < root.SubtreeCount; i++) {
    39         if (root.GetSubtree(i) == oldBranch) {
    40           root.RemoveSubtree(i);
    41           root.InsertSubtree(i, newBranch);
    42           return;
    43         }
    44       }
    45     }
     31    protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     32      Dataset dataset, IEnumerable<int> rows) {
     33      //optimization: constant nodes return always the same value
     34      ConstantTreeNode constantNode = node as ConstantTreeNode;
     35      if (constantNode != null) return constantNode.Value;
    4636
    47     protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree,
    48                                                       ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData) {
    4937      var rootSymbol = new ProgramRootSymbol().CreateTreeNode();
    5038      var startSymbol = new StartSymbol().CreateTreeNode();
    5139      rootSymbol.AddSubtree(startSymbol);
    5240      startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
    53       var rows = problemData.TrainingIndices;
     41
    5442      var tempTree = new SymbolicExpressionTree(rootSymbol);
    55       return interpreter.GetSymbolicExpressionTreeValues(tempTree, problemData.Dataset, rows).Median();
     43      // clone ADFs of source tree
     44      for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
     45        tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
     46      }
     47      return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median();
    5648    }
    5749  }
Note: See TracChangeset for help on using the changeset viewer.