Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs @ 5955

Last change on this file since 5955 was 5942, checked in by mkommend, 14 years ago

#1453: Renamed IOnlineEvaluator to IOnlineCalculator

File size: 5.6 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();
[5736]50      root.AddSubtree(start);
[5699]51      tempTree = new SymbolicExpressionTree(root);
[3915]52    }
53
[5717]54    protected override void UpdateModel(ISymbolicExpressionTree tree) {
[5818]55      var model = new SymbolicRegressionModel(tree, Content.Model.Interpreter);
56      SymbolicRegressionModel.Scale(model, Content.ProblemData);
57      Content.Model = model;
[3915]58    }
59
[5717]60    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
[5699]61      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
62      foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {
63        if (!(node.Symbol is ProgramRootSymbol || node.Symbol is StartSymbol)) {
[5717]64          replacementValues[node] = CalculateReplacementValue(node);
[5699]65        }
66      }
67      return replacementValues;
[3915]68    }
69
[5717]70    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
71      var interpreter = Content.Model.Interpreter;
72      var dataset = Content.ProblemData.Dataset;
73      var rows = Content.ProblemData.TrainingIndizes;
74      string targetVariable = Content.ProblemData.TargetVariable;
[5699]75      Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
[5736]76      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
[5699]77      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
78        .ToArray();
[5717]79      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
[5942]80      OnlineCalculatorError errorState;
81      double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
82      if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
[3915]83
[5699]84      foreach (ISymbolicExpressionTreeNode node in nodes) {
85        var parent = node.Parent;
[5717]86        constantNode.Value = CalculateReplacementValue(node);
[5699]87        ISymbolicExpressionTreeNode replacementNode = constantNode;
88        SwitchNode(parent, node, replacementNode);
[5717]89        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
[5942]90        double newR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
91        if (errorState != OnlineCalculatorError.None) newR2 = 0.0;
[3915]92
[5717]93        // impact = 0 if no change
94        // impact < 0 if new solution is better
95        // impact > 0 if new solution is worse
96        impactValues[node] = originalR2 - newR2;
[5699]97        SwitchNode(parent, replacementNode, node);
[3915]98      }
[5699]99      return impactValues;
[3915]100    }
101
[5717]102    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node) {
[5736]103      var start = tempTree.Root.GetSubtree(0);
104      while (start.SubtreesCount > 0) start.RemoveSubtree(0);
105      start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
[5717]106      var interpreter = Content.Model.Interpreter;
107      var rows = Content.ProblemData.TrainingIndizes;
[5699]108      return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows).Median();
[3915]109    }
110
111
[5699]112    private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
[5736]113      for (int i = 0; i < root.SubtreesCount; i++) {
114        if (root.GetSubtree(i) == oldBranch) {
115          root.RemoveSubtree(i);
116          root.InsertSubtree(i, newBranch);
[3915]117          return;
118        }
119      }
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.