Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifierView/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionValuesCalculator.cs @ 7784

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

#1832: Moved replacement and impact values calculation code from view to separate files. Implemented simplifier actions: copy, cut, delete, insert node/subtree.

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 SymbolicRegressionSolutionValuesCalculator : SymbolicDataAnalysisSolutionValuesCalculator {
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,
39                                                                                          double lowerEstimationLimit, double upperEstimationLimit) {
40      var problemData = (IRegressionProblemData)regressionProblemData;
41      var dataset = problemData.Dataset;
42      var rows = problemData.TrainingIndizes.ToList();
43      string targetVariable = problemData.TargetVariable;
44      var impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
45      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
46      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToArray();
47      var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
48      OnlineCalculatorError errorState;
49      double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
50      if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
51
52      foreach (ISymbolicExpressionTreeNode node in nodes) {
53        var parent = node.Parent;
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.