Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/3.3/ProblemView.cs @ 2223

Last change on this file since 2223 was 2223, checked in by mkommend, 15 years ago

reintegrated branch new heuristic.modeling database backend (ticket #712)

File size: 10.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31using HeuristicLab.DataAnalysis;
32using System.Diagnostics;
33using HeuristicLab.CEDMA.Core;
34
35namespace HeuristicLab.CEDMA.Server {
36  public partial class ProblemView : ViewBase {
37    private Problem problem;
38
39    public ProblemView(Problem problem) {
40      this.problem = problem;
41      problem.Changed += (sender, args) => UpdateControls();
42      InitializeComponent();
43      UpdateControls();
44    }
45
46    protected override void UpdateControls() {
47      base.UpdateControls();
48      trainingSamplesStartTextBox.Text = problem.TrainingSamplesStart.ToString();
49      trainingSamplesEndTextBox.Text = problem.TrainingSamplesEnd.ToString();
50      validationSamplesStartTextBox.Text = problem.ValidationSamplesStart.ToString();
51      validationSamplesEndTextBox.Text = problem.ValidationSamplesEnd.ToString();
52      testSamplesStartTextBox.Text = problem.TestSamplesStart.ToString();
53      testSamplesEndTextBox.Text = problem.TestSamplesEnd.ToString();
54      minTimeOffsetTextBox.Text = problem.MinTimeOffset.ToString();
55      maxTimeOffsetTextBox.Text = problem.MaxTimeOffset.ToString();
56      autoregressiveCheckBox.Checked = problem.AutoRegressive;
57      switch (problem.LearningTask) {
58        case LearningTask.Classification: classificationRadioButton.Checked = true; break;
59        case LearningTask.Regression: regressionRadioButton.Checked = true; break;
60        case LearningTask.TimeSeries: timeSeriesRadioButton.Checked = true; break;
61      }
62    }
63
64    private void importButton_Click(object sender, EventArgs e) {
65      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
66        DatasetParser parser = new DatasetParser();
67        bool success = false;
68        try {
69          try {
70            parser.Import(openFileDialog.FileName, true);
71            success = true;
72          }
73          catch (DataFormatException ex) {
74            ShowWarningMessageBox(ex);
75            // not possible to parse strictly => clear and try to parse non-strict
76            parser.Reset();
77            parser.Import(openFileDialog.FileName, false);
78            success = true;
79          }
80        }
81        catch (DataFormatException ex) {
82          // if the non-strict parsing also failed then show the exception
83          ShowErrorMessageBox(ex);
84        }
85        if (success) {
86          Dataset dataset = (Dataset)problem.Dataset;
87          dataset.Rows = parser.Rows;
88          dataset.Columns = parser.Columns;
89          for (int i = 0; i < parser.VariableNames.Length; i++) {
90            dataset.SetVariableName(i, parser.VariableNames[i]);
91          }
92          dataset.Name = parser.ProblemName;
93          dataset.Samples = new double[dataset.Rows * dataset.Columns];
94          Array.Copy(parser.Samples, dataset.Samples, dataset.Columns * dataset.Rows);
95          datasetView1.Dataset = problem.Dataset;
96
97          problem.TrainingSamplesStart = parser.TrainingSamplesStart;
98          problem.ValidationSamplesEnd = parser.TrainingSamplesStart;
99          problem.TrainingSamplesEnd = parser.TrainingSamplesEnd;
100          problem.ValidationSamplesStart = parser.ValidationSamplesStart;
101          problem.ValidationSamplesEnd = parser.ValidationSamplesEnd;
102          problem.TestSamplesStart = parser.TestSamplesStart;
103          problem.TestSamplesEnd = parser.TestSamplesEnd;
104          //problem.AllowedTargetVariables.Add(parser.TargetVariable);
105          problem.FireChanged();
106          //List<int> nonInputVariables = parser.NonInputVariables;
107          //for (int i = 0; i < dataset.Columns; i++) {
108          //  if (!nonInputVariables.Contains(i)) problem.AllowedInputVariables.Add(i);
109          //}
110          Refresh();
111        }
112      }
113    }
114
115    //private void targetsListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
116    //  if (e.NewValue == CheckState.Checked && !problem.AllowedTargetVariables.Contains(e.Index))
117    //    problem.AllowedTargetVariables.Add(e.Index);
118    //  else if (e.NewValue == CheckState.Unchecked && problem.AllowedTargetVariables.Contains(e.Index))
119    //    problem.AllowedTargetVariables.Remove(e.Index);
120    //}
121
122    //private void inputsListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
123    //  if (e.NewValue == CheckState.Checked && !problem.AllowedInputVariables.Contains(e.Index))
124    //    problem.AllowedInputVariables.Add(e.Index);
125    //  else if (e.NewValue == CheckState.Unchecked && problem.AllowedInputVariables.Contains(e.Index))
126    //    problem.AllowedInputVariables.Remove(e.Index);
127    //}
128
129    private void autoregressiveCheckBox_CheckedChanged(object sender, EventArgs e) {
130      problem.AutoRegressive = autoregressiveCheckBox.Checked;
131    }
132
133    private void samplesTextBox_Validating(object sender, CancelEventArgs e) {
134      try {
135        int trainingStart = int.Parse(trainingSamplesStartTextBox.Text);
136        int trainingEnd = int.Parse(trainingSamplesEndTextBox.Text);
137        int validationStart = int.Parse(validationSamplesStartTextBox.Text);
138        int validationEnd = int.Parse(validationSamplesEndTextBox.Text);
139        int testStart = int.Parse(testSamplesStartTextBox.Text);
140        int testEnd = int.Parse(testSamplesEndTextBox.Text);
141        if (trainingStart < 0 || validationStart < 0 || testStart < 0 ||
142          trainingEnd >= problem.Dataset.Rows || validationEnd >= problem.Dataset.Rows || testEnd >= problem.Dataset.Rows ||
143          trainingStart >= trainingEnd ||
144          validationStart >= validationEnd ||
145          testStart >= testEnd ||
146          IsOverlapping(trainingStart, trainingEnd, validationStart, validationEnd) ||
147          IsOverlapping(trainingStart, trainingEnd, testStart, testEnd) ||
148          IsOverlapping(validationStart, validationEnd, testStart, testEnd))
149          ColorSamplesTextBoxes(Color.Red);
150        else
151          ColorSamplesTextBoxes(Color.White);
152      }
153      catch (FormatException ex) {
154        ColorSamplesTextBoxes(Color.Red);
155      }
156    }
157
158    private void samplesTextBox_Validated(object sender, EventArgs e) {
159      problem.TrainingSamplesStart = int.Parse(trainingSamplesStartTextBox.Text);
160      problem.TrainingSamplesEnd = int.Parse(trainingSamplesEndTextBox.Text);
161      problem.ValidationSamplesStart = int.Parse(validationSamplesStartTextBox.Text);
162      problem.ValidationSamplesEnd = int.Parse(validationSamplesEndTextBox.Text);
163      problem.TestSamplesStart = int.Parse(testSamplesStartTextBox.Text);
164      problem.TestSamplesEnd = int.Parse(testSamplesEndTextBox.Text);
165    }
166
167    private void ColorSamplesTextBoxes(Color color) {
168      trainingSamplesStartTextBox.BackColor = color;
169      trainingSamplesEndTextBox.BackColor = color;
170      validationSamplesStartTextBox.BackColor = color;
171      validationSamplesEndTextBox.BackColor = color;
172      testSamplesStartTextBox.BackColor = color;
173      testSamplesEndTextBox.BackColor = color;
174    }
175
176    private bool IsOverlapping(int x0, int y0, int x1, int y1) {
177      Trace.Assert(x0 <= y0 && x1 <= y1);
178      int tmp;
179      // make sure that x0,y0 is the left interval
180      if (x1 < x0) {
181        tmp = x1;
182        x1 = x0;
183        x0 = tmp;
184        tmp = y1;
185        y1 = y0;
186        y0 = tmp;
187      }
188      return y0 > x1;
189    }
190    private void ShowWarningMessageBox(Exception ex) {
191      MessageBox.Show(ex.Message,
192                      "Warning - " + ex.GetType().Name,
193                      MessageBoxButtons.OK,
194                      MessageBoxIcon.Warning);
195    }
196    private void ShowErrorMessageBox(Exception ex) {
197      MessageBox.Show(BuildErrorMessage(ex),
198                      "Error - " + ex.GetType().Name,
199                      MessageBoxButtons.OK,
200                      MessageBoxIcon.Error);
201    }
202    private string BuildErrorMessage(Exception ex) {
203      StringBuilder sb = new StringBuilder();
204      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
205
206      while (ex.InnerException != null) {
207        ex = ex.InnerException;
208        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
209      }
210      return sb.ToString();
211    }
212
213    #region gui events
214    private void radioButton_CheckedChanged(object sender, EventArgs e) {
215      minTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
216      minTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
217      maxTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
218      maxTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
219      autoregressiveCheckBox.Enabled = timeSeriesRadioButton.Checked;
220      autoregressiveLabel.Enabled = timeSeriesRadioButton.Checked;
221      if (timeSeriesRadioButton.Checked) problem.LearningTask = LearningTask.TimeSeries;
222      else if (classificationRadioButton.Checked) problem.LearningTask = LearningTask.Classification;
223      else if (regressionRadioButton.Checked) problem.LearningTask = LearningTask.Regression;
224    }
225
226    private void timeOffsetTextBox_Validating(object sender, CancelEventArgs e) {
227      int min, max;
228      e.Cancel = !int.TryParse(minTimeOffsetTextBox.Text, out min);
229      e.Cancel = !int.TryParse(maxTimeOffsetTextBox.Text, out max);
230      e.Cancel = min > max;
231    }
232    private void timeOffsetTextBox_Validated(object sender, EventArgs e) {
233      problem.MinTimeOffset = int.Parse(minTimeOffsetTextBox.Text);
234      problem.MaxTimeOffset = int.Parse(maxTimeOffsetTextBox.Text);
235    }
236    #endregion
237  }
238}
Note: See TracBrowser for help on using the repository browser.