Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Core/ProblemView.cs @ 1075

Last change on this file since 1075 was 1043, checked in by gkronber, 16 years ago

worked on view for CEDMA problems and persistence of CEDMA problems to RDF store. #419 (Refactor CEDMA plugins)

File size: 9.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;
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.VariableNames[i], problem.AllowedTargetVariables.Contains(i));
67        inputsListBox.Items.Add(problem.DataSet.VariableNames[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          dataset.VariableNames = parser.VariableNames;
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    private void modelType_SelectedValueChanged(object sender, EventArgs e) {
220      if ((LearningTask)modelType.SelectedItem == LearningTask.TimeSeries) autoregressiveCheckBox.Enabled = true;
221      else autoregressiveCheckBox.Enabled = false;
222      problem.LearningTask = (LearningTask)modelType.SelectedItem;
223    }
224  }
225}
Note: See TracBrowser for help on using the repository browser.