Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs @ 8409

Last change on this file since 8409 was 8409, checked in by bburlacu, 12 years ago

#1763: Fixed bug when pasting subtrees. Moved the InteractiveSymbolicExpressionTreeChart and dialogs in the HeuristicLab.Problems.DataAnalysis.Symbolic.Views namespace and renamed the impact values calculators to a more sensible name.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25
26namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
27  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
28    public override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree,
29                                                                                               ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
30                                                                                               IDataAnalysisProblemData problemData) {
31      var replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
32      foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
33        replacementValues[node] = CalculateReplacementValue(node, tree, interpreter, problemData);
34      }
35      return replacementValues;
36    }
37
38    public override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData regressionProblemData, double lowerEstimationLimit, double upperEstimationLimit) {
39      var problemData = (IRegressionProblemData)regressionProblemData;
40      var dataset = problemData.Dataset;
41      var rows = problemData.TrainingIndices.ToList();
42      string targetVariable = problemData.TargetVariable;
43      var impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
44      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
45      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToArray();
46      var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
47      OnlineCalculatorError errorState;
48      double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
49      if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
50
51      foreach (ISymbolicExpressionTreeNode node in nodes) {
52        var parent = node.Parent;
53        var constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
54        constantNode.Value = CalculateReplacementValue(node, tree, interpreter, problemData);
55        ISymbolicExpressionTreeNode replacementNode = constantNode;
56        SwitchNode(parent, node, replacementNode);
57        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
58        double newR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
59        if (errorState != OnlineCalculatorError.None) newR2 = 0.0;
60
61        // impact = 0 if no change
62        // impact < 0 if new solution is better
63        // impact > 0 if new solution is worse
64        impactValues[node] = originalR2 - newR2;
65        SwitchNode(parent, replacementNode, node);
66      }
67      return impactValues;
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.