Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Core/ProblemView.cs @ 1510

Last change on this file since 1510 was 1287, checked in by gkronber, 16 years ago

Merged change sets from CEDMA branch to trunk:

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