source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/SolutionView.cs @ 16400

Last change on this file since 16400 was 16400, checked in by gkronber, 14 months ago

#2925 small gui improvement

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22
23
24using System;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Analysis.Views;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32
33namespace HeuristicLab.Problems.DynamicalSystemsModelling {
34  [View("Solution View")]
35  [Content(typeof(Solution), true)]
36  public sealed partial class SolutionView : ItemView {
37    public new Solution Content {
38      get { return (Solution)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public SolutionView() {
43      InitializeComponent();
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content == null) {
49        episodeStartTextbox.Text = string.Empty;
50        episodeEndTextbox.Text = string.Empty;
51      } else {
52        var episode = Content.TrainingEpisodes.First();
53        episodeStartTextbox.Text = episode.Start.ToString();
54        episodeEndTextbox.Text = episode.End.ToString();
55      }
56      UpdatePredictions();
57    }
58
59    private void UpdatePredictions() {
60      tableLayoutPanel.Controls.Clear();
61      if (Content == null) return;
62      tableLayoutPanel.RowCount = Content.Trees.Length;
63      var targetVars = Content.TargetVariables;
64      var calculatedVars = Content.TargetVariables.Concat(Content.LatentVariables).ToArray();
65      var ds = Content.ProblemData.Dataset;
66      var predictionEpisode = new IntRange(int.Parse(episodeStartTextbox.Text), int.Parse(episodeEndTextbox.Text));
67
68      trackBar.Minimum = 0;
69      trackBar.Maximum = ds.Rows;
70      var forecastHorizon = 0;
71      int.TryParse(forecastTextbox.Text, out forecastHorizon);
72      // trackBar.Value = predictionEpisode.Start;
73
74      double snmse;
75      var predictions = Content.Predict(predictionEpisode, forecastHorizon, out snmse).ToArray();
76      errorLabel.Text = $"SNMSE: {snmse:e4}";
77      var trainingPredictions = predictions.Take(predictionEpisode.Size).ToArray();
78      var forecastPredictions = predictions.Skip(predictionEpisode.Size).ToArray();
79      for (int i = 0; i < calculatedVars.Length; i++) {
80        var varName = calculatedVars[i];
81        var tree = Content.Trees[i];
82        var dt = new DataTable(varName);
83        var trainingPredValues =
84          Enumerable.Repeat(double.NaN, predictionEpisode.Start)
85          .Concat(trainingPredictions.Select(pi => pi[i]))
86          .Concat(Enumerable.Repeat(double.NaN, ds.Rows - predictionEpisode.Start - trainingPredictions.Length))
87          .ToArray();
88
89        var predRow = new DataRow(varName + " (training)", varName + " (training)", trainingPredValues);
90        dt.Rows.Add(predRow);
91
92        if (targetVars.Contains(varName)) {
93          var targetValues = ds.GetReadOnlyDoubleValues(varName);
94          var targetRow = new DataRow(varName + " (target)", varName + " (target)", targetValues);
95          dt.Rows.Add(targetRow);
96        }
97
98        var forecastPredValues =
99  Enumerable.Repeat(double.NaN, predictionEpisode.End)
100  .Concat(forecastPredictions.Select(pi => pi[i]))
101  .Concat(Enumerable.Repeat(double.NaN, ds.Rows - predictionEpisode.End - forecastPredictions.Length))
102  .ToArray();
103
104        var forecastRow = new DataRow(varName + " (forecast)", varName + " (forecast)", forecastPredValues);
105        dt.Rows.Add(forecastRow);
106
107        var vizProp = new DataTableVisualProperties();
108        dt.VisualProperties = vizProp;
109        var dtv = new DataTableView();
110        dtv.Content = dt;
111        dtv.ShowChartOnly = true;
112        dtv.Dock = System.Windows.Forms.DockStyle.Fill;
113        tableLayoutPanel.Controls.Add(dtv, 0, i);
114      }
115    }
116
117    protected override void OnLockedChanged() {
118      base.OnLockedChanged();
119    }
120
121    private void updateButton_Click(object sender, System.EventArgs e) {
122      UpdatePredictions();
123    }
124
125    private void trackBar_Validated(object sender, EventArgs e) {
126      var start = int.Parse(episodeStartTextbox.Text);
127      if (start == trackBar.Value) return;
128      var end = int.Parse(episodeEndTextbox.Text);
129      var delta = end - start;
130      episodeStartTextbox.Text = (trackBar.Value).ToString();
131      episodeEndTextbox.Text = (trackBar.Value + delta).ToString();
132    }
133
134    private void episodeStartTextbox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
135      int res;
136      e.Cancel = !int.TryParse(episodeStartTextbox.Text, out res);
137    }
138
139    private void episodeStartTextbox_Validated(object sender, EventArgs e) {
140      trackBar.Value = int.Parse(episodeStartTextbox.Text);
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.