source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4/SymbolicTimeSeriesPrognosisSolutionErrorCharacteristicsCurveView.cs @ 6807

Last change on this file since 6807 was 6807, checked in by gkronber, 10 years ago

#1081 added views for time series prognosis

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
23using System.Linq;
24using System.Windows.Forms;
25using System.Windows.Forms.DataVisualization.Charting;
26using HeuristicLab.Algorithms.DataAnalysis;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.MainForm;
29using HeuristicLab.Problems.DataAnalysis.Views;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views {
32  [View("Error Characteristics Curve")]
33  [Content(typeof(ISymbolicTimeSeriesPrognosisSolution))]
34  public partial class SymbolicTimeSeriesPrognosisSolutionErrorCharacteristicsCurveView : TimeSeriesPrognosisSolutionErrorCharacteristicsCurveView {
35    private ITimeSeriesPrognosisSolution linearTimeSeriesPrognosisSolution;
36    private ITimeSeriesPrognosisSolution naiveSolution;
37
38    public SymbolicTimeSeriesPrognosisSolutionErrorCharacteristicsCurveView() {
39      InitializeComponent();
40    }
41
42    public new ISymbolicTimeSeriesPrognosisSolution Content {
43      get { return (ISymbolicTimeSeriesPrognosisSolution)base.Content; }
44      set { base.Content = value; }
45    }
46
47    protected override void OnContentChanged() {
48      if (Content != null) {
49        linearTimeSeriesPrognosisSolution = CreateLinearTimeSeriesPrognosisSolution();
50        naiveSolution = CreateNaiveSolution();
51      } else {
52        linearTimeSeriesPrognosisSolution = null;
53        naiveSolution = null;
54      }
55
56      base.OnContentChanged();
57    }
58
59    protected override void UpdateChart() {
60      base.UpdateChart();
61      if (Content == null || linearTimeSeriesPrognosisSolution == null) return;
62      AddTimeSeriesPrognosisSolution(linearTimeSeriesPrognosisSolution);
63      AddTimeSeriesPrognosisSolution(naiveSolution);
64    }
65
66    private ITimeSeriesPrognosisSolution CreateLinearTimeSeriesPrognosisSolution() {
67      if (Content == null) throw new InvalidOperationException();
68      double rmse, cvRmsError;
69      var problemData = (ITimeSeriesPrognosisProblemData)ProblemData.Clone();
70
71      //clear checked inputVariables
72      foreach (var inputVariable in problemData.InputVariables.CheckedItems) {
73        problemData.InputVariables.SetItemCheckedState(inputVariable.Value, false);
74      }
75
76      //check inputVariables used in the symbolic time series prognosis model
77      var usedVariables =
78        Content.Model.SymbolicExpressionTree.IterateNodesPostfix().OfType<VariableTreeNode>().Select(
79          node => node.VariableName).Distinct();
80      foreach (var variable in usedVariables) {
81        problemData.InputVariables.SetItemCheckedState(
82          problemData.InputVariables.Where(x => x.Value == variable).First(), true);
83      }
84
85      int maxLag = Content.Model.SymbolicExpressionTree.IterateNodesPostfix()
86        .OfType<LaggedVariableTreeNode>()
87        .Select(n => -n.Lag)
88        .Max();
89
90      var solution = LinearTimeSeriesPrognosis.CreateLinearTimeSeriesPrognosisSolution(problemData, maxLag, out rmse, out cvRmsError);
91      solution.Name = "Linear Model";
92      return solution;
93    }
94    private ITimeSeriesPrognosisSolution CreateNaiveSolution() {
95      if (Content == null) throw new InvalidOperationException();
96      double rmse, cvRmsError;
97      var problemData = (ITimeSeriesPrognosisProblemData)ProblemData.Clone();
98
99      //clear checked inputVariables
100      foreach (var inputVariable in problemData.InputVariables.CheckedItems) {
101        problemData.InputVariables.SetItemCheckedState(inputVariable.Value, false);
102      }
103
104      foreach (var variable in problemData.InputVariables) {
105        if (variable.Value == problemData.TargetVariable) {
106          problemData.InputVariables.SetItemCheckedState(variable, true);
107        }
108      }
109
110      int maxLag = 1;
111
112      var solution = LinearTimeSeriesPrognosis.CreateLinearTimeSeriesPrognosisSolution(problemData, maxLag, out rmse, out cvRmsError);
113      solution.Name = "AR(1) Model";
114      return solution;
115    }
116
117    protected override void Content_ModelChanged(object sender, EventArgs e) {
118      linearTimeSeriesPrognosisSolution = CreateLinearTimeSeriesPrognosisSolution();
119      naiveSolution = CreateNaiveSolution();
120      base.Content_ModelChanged(sender, e);
121    }
122
123    protected override void Content_ProblemDataChanged(object sender, EventArgs e) {
124      linearTimeSeriesPrognosisSolution = CreateLinearTimeSeriesPrognosisSolution();
125      naiveSolution = CreateNaiveSolution();
126      base.Content_ProblemDataChanged(sender, e);
127    }
128
129    private void chart_MouseDown(object sender, MouseEventArgs e) {
130      if (e.Clicks < 2) return;
131      HitTestResult result = chart.HitTest(e.X, e.Y);
132      if (result.ChartElementType != ChartElementType.LegendItem) return;
133      if (result.Series.Name == linearTimeSeriesPrognosisSolution.Name && result.Series.Name != naiveSolution.Name) return;
134
135      MainFormManager.MainForm.ShowContent((ITimeSeriesPrognosisSolution)result.Series.Tag);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.