Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4/InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView.cs @ 10377

Last change on this file since 10377 was 9462, checked in by swagner, 12 years ago

Updated copyright year and incremented version of plugins, applications and assembly files (#1889)

File size: 6.0 KB
RevLine 
[8798]1#region License Information
2/* HeuristicLab
[9462]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8798]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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views {
30  public partial class InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
31    private readonly ConstantTreeNode constantNode;
32    private readonly SymbolicExpressionTree tempTree;
33
34    public new SymbolicTimeSeriesPrognosisSolution Content {
35      get { return (SymbolicTimeSeriesPrognosisSolution)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView()
40      : base() {
41      InitializeComponent();
42      this.Caption = "Interactive Time-Series Prognosis 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);
49    }
50
51    protected override void UpdateModel(ISymbolicExpressionTree tree) {
52      var model = new SymbolicTimeSeriesPrognosisModel(tree, Content.Model.Interpreter);
[8972]53      model.Scale(Content.ProblemData);
[8798]54      Content.Model = model;
55    }
56
57    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
58      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
59      foreach (var componentBranch in tree.Root.GetSubtree(0).Subtrees)
60        foreach (ISymbolicExpressionTreeNode node in componentBranch.IterateNodesPrefix()) {
61          replacementValues[node] = CalculateReplacementValue(node, tree);
62        }
63      return replacementValues;
64    }
65
66    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
67      var interpreter = Content.Model.Interpreter;
68      var rows = Content.ProblemData.TrainingIndices;
69      var dataset = Content.ProblemData.Dataset;
70      var targetVariable = Content.ProblemData.TargetVariable;
71      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
72      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToArray();
73
74      Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
75      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
76      OnlineCalculatorError errorState;
77      double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
78      if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
79
80      foreach (ISymbolicExpressionTreeNode node in nodes) {
81        var parent = node.Parent;
82        constantNode.Value = CalculateReplacementValue(node, tree);
83        ISymbolicExpressionTreeNode replacementNode = constantNode;
84        SwitchNode(parent, node, replacementNode);
85        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
86        double newR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
87        if (errorState != OnlineCalculatorError.None) newR2 = 0.0;
88
89        // impact = 0 if no change
90        // impact < 0 if new solution is better
91        // impact > 0 if new solution is worse
92        impactValues[node] = originalR2 - newR2;
93        SwitchNode(parent, replacementNode, node);
94      }
95      return impactValues;
96    }
97
98    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
99      // remove old ADFs
100      while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
101      // clone ADFs of source tree
102      for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
103        tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
104      }
105      var start = tempTree.Root.GetSubtree(0);
106      while (start.SubtreeCount > 0) start.RemoveSubtree(0);
107      start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
108      var interpreter = Content.Model.Interpreter;
109      var rows = Content.ProblemData.TrainingIndices;
110      var allPrognosedValues = interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows);
111
112      return allPrognosedValues.Median();
113    }
114
115
116    private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
117      for (int i = 0; i < root.SubtreeCount; i++) {
118        if (root.GetSubtree(i) == oldBranch) {
119          root.RemoveSubtree(i);
120          root.InsertSubtree(i, newBranch);
121          return;
122        }
123      }
124    }
125
126    protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
127      throw new NotImplementedException();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.