Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs @ 5729

Last change on this file since 5729 was 5729, checked in by gkronber, 13 years ago

#1418 moved liner scaling method into symbolic regression model and fixed bug in interactive solution simplifier

File size: 5.3 KB
RevLine 
[3915]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3915]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;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
[5699]28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
[3915]30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31
[5699]32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views {
33  public partial class InteractiveSymbolicRegressionSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
34    private readonly ConstantTreeNode constantNode;
35    private readonly SymbolicExpressionTree tempTree;
[5717]36
37    public new SymbolicRegressionSolution Content {
38      get { return (SymbolicRegressionSolution)base.Content; }
39      set { base.Content = value; }
40    }
41
[5699]42    public InteractiveSymbolicRegressionSolutionSimplifierView()
43      : base() {
44      InitializeComponent();
45      this.Caption = "Interactive Regression Solution Simplifier";
[3915]46
[5699]47      constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
48      ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
49      ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
50      root.AddSubTree(start);
51      tempTree = new SymbolicExpressionTree(root);
[3915]52    }
53
[5717]54    protected override void UpdateModel(ISymbolicExpressionTree tree) {
55      Content.Model = new SymbolicRegressionModel(tree, Content.Model.Interpreter);
[5729]56      Content.ScaleModel();
[3915]57    }
58
[5717]59    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
[5699]60      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
61      foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {
62        if (!(node.Symbol is ProgramRootSymbol || node.Symbol is StartSymbol)) {
[5717]63          replacementValues[node] = CalculateReplacementValue(node);
[5699]64        }
65      }
66      return replacementValues;
[3915]67    }
68
[5717]69    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
70      var interpreter = Content.Model.Interpreter;
71      var dataset = Content.ProblemData.Dataset;
72      var rows = Content.ProblemData.TrainingIndizes;
73      string targetVariable = Content.ProblemData.TargetVariable;
[5699]74      Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
75      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubTree(0).GetSubTree(0).IterateNodesPostfix().ToList();
76      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
77        .ToArray();
[5717]78      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
[3915]79
[5717]80      double originalR2 = OnlinePearsonsRSquaredEvaluator.Calculate(targetValues, originalOutput);
81
[5699]82      foreach (ISymbolicExpressionTreeNode node in nodes) {
83        var parent = node.Parent;
[5717]84        constantNode.Value = CalculateReplacementValue(node);
[5699]85        ISymbolicExpressionTreeNode replacementNode = constantNode;
86        SwitchNode(parent, node, replacementNode);
[5717]87        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
88        double newR2 = OnlinePearsonsRSquaredEvaluator.Calculate(targetValues, newOutput);
[3915]89
[5717]90        // impact = 0 if no change
91        // impact < 0 if new solution is better
92        // impact > 0 if new solution is worse
93        impactValues[node] = originalR2 - newR2;
[5699]94        SwitchNode(parent, replacementNode, node);
[3915]95      }
[5699]96      return impactValues;
[3915]97    }
98
[5717]99    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node) {
[5699]100      var start = tempTree.Root.GetSubTree(0);
101      while (start.SubTrees.Count() > 0) start.RemoveSubTree(0);
102      start.AddSubTree((ISymbolicExpressionTreeNode)node.Clone());
[5717]103      var interpreter = Content.Model.Interpreter;
104      var rows = Content.ProblemData.TrainingIndizes;
[5699]105      return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows).Median();
[3915]106    }
107
108
[5699]109    private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
110      for (int i = 0; i < root.SubTrees.Count(); i++) {
111        if (root.GetSubTree(i) == oldBranch) {
[3915]112          root.RemoveSubTree(i);
113          root.InsertSubTree(i, newBranch);
114          return;
115        }
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.