Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2285 was 2285, checked in by gkronber, 15 years ago

Worked on #722 (IModel should provide a Predict() method to get predicted values for an input vector).
At the same time removed parameter PunishmentFactor from GP algorithms (this parameter is internal to TreeEvaluators now).

File size: 9.3 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          dataset.Name = parser.ProblemName;
90          dataset.Samples = new double[dataset.Rows * dataset.Columns];
91          Array.Copy(parser.Samples, dataset.Samples, dataset.Columns * dataset.Rows);
92          datasetView1.Dataset = problem.Dataset;
93
94          problem.TrainingSamplesStart = parser.TrainingSamplesStart;
95          problem.ValidationSamplesEnd = parser.TrainingSamplesStart;
96          problem.TrainingSamplesEnd = parser.TrainingSamplesEnd;
97          problem.ValidationSamplesStart = parser.ValidationSamplesStart;
98          problem.ValidationSamplesEnd = parser.ValidationSamplesEnd;
99          problem.TestSamplesStart = parser.TestSamplesStart;
100          problem.TestSamplesEnd = parser.TestSamplesEnd;
101          for (int i = 0; i < parser.VariableNames.Length; i++) {
102            dataset.SetVariableName(i, parser.VariableNames[i]);
103          }
104
105          problem.FireChanged();
106          Refresh();
107        }
108      }
109    }
110
111    private void autoregressiveCheckBox_CheckedChanged(object sender, EventArgs e) {
112      problem.AutoRegressive = autoregressiveCheckBox.Checked;
113    }
114
115    private void samplesTextBox_Validating(object sender, CancelEventArgs e) {
116      try {
117        int trainingStart = int.Parse(trainingSamplesStartTextBox.Text);
118        int trainingEnd = int.Parse(trainingSamplesEndTextBox.Text);
119        int validationStart = int.Parse(validationSamplesStartTextBox.Text);
120        int validationEnd = int.Parse(validationSamplesEndTextBox.Text);
121        int testStart = int.Parse(testSamplesStartTextBox.Text);
122        int testEnd = int.Parse(testSamplesEndTextBox.Text);
123        if (trainingStart < 0 || validationStart < 0 || testStart < 0 ||
124          trainingEnd >= problem.Dataset.Rows || validationEnd >= problem.Dataset.Rows || testEnd >= problem.Dataset.Rows ||
125          trainingStart >= trainingEnd ||
126          validationStart >= validationEnd ||
127          testStart >= testEnd ||
128          IsOverlapping(trainingStart, trainingEnd, validationStart, validationEnd) ||
129          IsOverlapping(trainingStart, trainingEnd, testStart, testEnd) ||
130          IsOverlapping(validationStart, validationEnd, testStart, testEnd))
131          ColorSamplesTextBoxes(Color.Red);
132        else
133          ColorSamplesTextBoxes(Color.White);
134      }
135      catch (FormatException ex) {
136        ColorSamplesTextBoxes(Color.Red);
137      }
138    }
139
140    private void samplesTextBox_Validated(object sender, EventArgs e) {
141      problem.TrainingSamplesStart = int.Parse(trainingSamplesStartTextBox.Text);
142      problem.TrainingSamplesEnd = int.Parse(trainingSamplesEndTextBox.Text);
143      problem.ValidationSamplesStart = int.Parse(validationSamplesStartTextBox.Text);
144      problem.ValidationSamplesEnd = int.Parse(validationSamplesEndTextBox.Text);
145      problem.TestSamplesStart = int.Parse(testSamplesStartTextBox.Text);
146      problem.TestSamplesEnd = int.Parse(testSamplesEndTextBox.Text);
147    }
148
149    private void ColorSamplesTextBoxes(Color color) {
150      trainingSamplesStartTextBox.BackColor = color;
151      trainingSamplesEndTextBox.BackColor = color;
152      validationSamplesStartTextBox.BackColor = color;
153      validationSamplesEndTextBox.BackColor = color;
154      testSamplesStartTextBox.BackColor = color;
155      testSamplesEndTextBox.BackColor = color;
156    }
157
158    private bool IsOverlapping(int x0, int y0, int x1, int y1) {
159      Trace.Assert(x0 <= y0 && x1 <= y1);
160      int tmp;
161      // make sure that x0,y0 is the left interval
162      if (x1 < x0) {
163        tmp = x1;
164        x1 = x0;
165        x0 = tmp;
166        tmp = y1;
167        y1 = y0;
168        y0 = tmp;
169      }
170      return y0 > x1;
171    }
172    private void ShowWarningMessageBox(Exception ex) {
173      MessageBox.Show(ex.Message,
174                      "Warning - " + ex.GetType().Name,
175                      MessageBoxButtons.OK,
176                      MessageBoxIcon.Warning);
177    }
178    private void ShowErrorMessageBox(Exception ex) {
179      MessageBox.Show(BuildErrorMessage(ex),
180                      "Error - " + ex.GetType().Name,
181                      MessageBoxButtons.OK,
182                      MessageBoxIcon.Error);
183    }
184    private string BuildErrorMessage(Exception ex) {
185      StringBuilder sb = new StringBuilder();
186      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
187
188      while (ex.InnerException != null) {
189        ex = ex.InnerException;
190        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
191      }
192      return sb.ToString();
193    }
194
195    #region gui events
196    private void radioButton_CheckedChanged(object sender, EventArgs e) {
197      minTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
198      minTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
199      maxTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
200      maxTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
201      autoregressiveCheckBox.Enabled = timeSeriesRadioButton.Checked;
202      autoregressiveLabel.Enabled = timeSeriesRadioButton.Checked;
203      if (timeSeriesRadioButton.Checked) problem.LearningTask = LearningTask.TimeSeries;
204      else if (classificationRadioButton.Checked) problem.LearningTask = LearningTask.Classification;
205      else if (regressionRadioButton.Checked) problem.LearningTask = LearningTask.Regression;
206    }
207
208    private void timeOffsetTextBox_Validating(object sender, CancelEventArgs e) {
209      int min, max;
210      e.Cancel = !int.TryParse(minTimeOffsetTextBox.Text, out min);
211      e.Cancel = !int.TryParse(maxTimeOffsetTextBox.Text, out max);
212      e.Cancel = min > max;
213    }
214    private void timeOffsetTextBox_Validated(object sender, EventArgs e) {
215      problem.MinTimeOffset = int.Parse(minTimeOffsetTextBox.Text);
216      problem.MaxTimeOffset = int.Parse(maxTimeOffsetTextBox.Text);
217    }
218    #endregion
219  }
220}
Note: See TracBrowser for help on using the repository browser.