Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.CEDMA.Core/3.2/ProblemView.cs @ 3494

Last change on this file since 3494 was 1215, checked in by gkronber, 16 years ago

removed property VariableNames and added Get- and SetVariableName(index) methods instead. #419 (Refactor CEDMA plugins)

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