Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17393 was 16976, checked in by gkronber, 6 years ago

#2925: Update data files to have equidistant time steps. Reactivated CVODES solver.

File size: 5.5 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).ToArray();
76      errorLabel.Text = $"SNMSE: {0.0: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        if (ds.Rows - predictionEpisode.End - forecastPredictions.Length > 0) {
99          var forecastPredValues =
100    Enumerable.Repeat(double.NaN, predictionEpisode.End)
101    .Concat(forecastPredictions.Select(pi => pi[i]))
102    .Concat(Enumerable.Repeat(double.NaN, ds.Rows - predictionEpisode.End - forecastPredictions.Length))
103    .ToArray();
104
105          var forecastRow = new DataRow(varName + " (forecast)", varName + " (forecast)", forecastPredValues);
106          dt.Rows.Add(forecastRow);
107        }
108
109        var vizProp = new DataTableVisualProperties();
110        dt.VisualProperties = vizProp;
111        var dtv = new DataTableView();
112        dtv.Content = dt;
113        dtv.ShowChartOnly = true;
114        dtv.Dock = System.Windows.Forms.DockStyle.Fill;
115        tableLayoutPanel.Controls.Add(dtv, 0, i);
116      }
117    }
118
119    protected override void OnLockedChanged() {
120      base.OnLockedChanged();
121    }
122
123    private void updateButton_Click(object sender, System.EventArgs e) {
124      UpdatePredictions();
125    }
126
127    private void trackBar_Validated(object sender, EventArgs e) {
128      var start = int.Parse(episodeStartTextbox.Text);
129      if (start == trackBar.Value) return;
130      var end = int.Parse(episodeEndTextbox.Text);
131      var delta = end - start;
132      episodeStartTextbox.Text = (trackBar.Value).ToString();
133      episodeEndTextbox.Text = (trackBar.Value + delta).ToString();
134    }
135
136    private void episodeStartTextbox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
137      int res;
138      e.Cancel = !int.TryParse(episodeStartTextbox.Text, out res);
139    }
140
141    private void episodeStartTextbox_Validated(object sender, EventArgs e) {
142      trackBar.Value = int.Parse(episodeStartTextbox.Text);
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.