[992] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using System.Text;
|
---|
| 29 | using System.Windows.Forms;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
[1287] | 31 | using HeuristicLab.DataAnalysis;
|
---|
| 32 | using System.Diagnostics;
|
---|
[992] | 33 |
|
---|
| 34 | namespace 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();
|
---|
[1287] | 41 | UpdateControls();
|
---|
[992] | 42 | }
|
---|
[1287] | 43 |
|
---|
| 44 | protected override void UpdateControls() {
|
---|
| 45 | base.UpdateControls();
|
---|
[2000] | 46 | datasetView.Dataset = problem.Dataset;
|
---|
[1287] | 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();
|
---|
[2000] | 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));
|
---|
[1287] | 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) {
|
---|
[2000] | 91 | Dataset dataset = (Dataset)problem.Dataset;
|
---|
[1287] | 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);
|
---|
[2000] | 100 | datasetView.Dataset = problem.Dataset;
|
---|
[1287] | 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 | private void targetsListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
|
---|
| 121 | if (e.NewValue == CheckState.Checked && !problem.AllowedTargetVariables.Contains(e.Index))
|
---|
| 122 | problem.AllowedTargetVariables.Add(e.Index);
|
---|
| 123 | else if (e.NewValue == CheckState.Unchecked && problem.AllowedTargetVariables.Contains(e.Index))
|
---|
| 124 | problem.AllowedTargetVariables.Remove(e.Index);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void inputsListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
|
---|
| 128 | if (e.NewValue == CheckState.Checked && !problem.AllowedInputVariables.Contains(e.Index))
|
---|
| 129 | problem.AllowedInputVariables.Add(e.Index);
|
---|
| 130 | else if (e.NewValue == CheckState.Unchecked && problem.AllowedInputVariables.Contains(e.Index))
|
---|
| 131 | problem.AllowedInputVariables.Remove(e.Index);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private void autoregressiveCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 135 | problem.AutoRegressive = autoregressiveCheckBox.Checked;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private void samplesTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 139 | try {
|
---|
| 140 | int trainingStart = int.Parse(trainingSamplesStartTextBox.Text);
|
---|
| 141 | int trainingEnd = int.Parse(trainingSamplesEndTextBox.Text);
|
---|
| 142 | int validationStart = int.Parse(validationSamplesStartTextBox.Text);
|
---|
| 143 | int validationEnd = int.Parse(validationSamplesEndTextBox.Text);
|
---|
| 144 | int testStart = int.Parse(testSamplesStartTextBox.Text);
|
---|
| 145 | int testEnd = int.Parse(testSamplesEndTextBox.Text);
|
---|
| 146 | if (trainingStart < 0 || validationStart < 0 || testStart < 0 ||
|
---|
[2000] | 147 | trainingEnd >= problem.Dataset.Rows || validationEnd >= problem.Dataset.Rows || testEnd >= problem.Dataset.Rows ||
|
---|
[1287] | 148 | trainingStart >= trainingEnd ||
|
---|
| 149 | validationStart >= validationEnd ||
|
---|
| 150 | testStart >= testEnd ||
|
---|
| 151 | IsOverlapping(trainingStart, trainingEnd, validationStart, validationEnd) ||
|
---|
| 152 | IsOverlapping(trainingStart, trainingEnd, testStart, testEnd) ||
|
---|
| 153 | IsOverlapping(validationStart, validationEnd, testStart, testEnd))
|
---|
| 154 | ColorSamplesTextBoxes(Color.Red);
|
---|
| 155 | else
|
---|
| 156 | ColorSamplesTextBoxes(Color.White);
|
---|
| 157 | }
|
---|
| 158 | catch (FormatException ex) {
|
---|
| 159 | ColorSamplesTextBoxes(Color.Red);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private void samplesTextBox_Validated(object sender, EventArgs e) {
|
---|
| 164 | problem.TrainingSamplesStart = int.Parse(trainingSamplesStartTextBox.Text);
|
---|
| 165 | problem.TrainingSamplesEnd = int.Parse(trainingSamplesEndTextBox.Text);
|
---|
| 166 | problem.ValidationSamplesStart = int.Parse(validationSamplesStartTextBox.Text);
|
---|
| 167 | problem.ValidationSamplesEnd = int.Parse(validationSamplesEndTextBox.Text);
|
---|
| 168 | problem.TestSamplesStart = int.Parse(testSamplesStartTextBox.Text);
|
---|
| 169 | problem.TestSamplesEnd = int.Parse(testSamplesEndTextBox.Text);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | private void ColorSamplesTextBoxes(Color color) {
|
---|
| 173 | trainingSamplesStartTextBox.BackColor = color;
|
---|
| 174 | trainingSamplesEndTextBox.BackColor = color;
|
---|
| 175 | validationSamplesStartTextBox.BackColor = color;
|
---|
| 176 | validationSamplesEndTextBox.BackColor = color;
|
---|
| 177 | testSamplesStartTextBox.BackColor = color;
|
---|
| 178 | testSamplesEndTextBox.BackColor = color;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | private bool IsOverlapping(int x0, int y0, int x1, int y1) {
|
---|
| 182 | Trace.Assert(x0 <= y0 && x1 <= y1);
|
---|
| 183 | int tmp;
|
---|
| 184 | // make sure that x0,y0 is the left interval
|
---|
| 185 | if (x1 < x0) {
|
---|
| 186 | tmp = x1;
|
---|
| 187 | x1 = x0;
|
---|
| 188 | x0 = tmp;
|
---|
| 189 | tmp = y1;
|
---|
| 190 | y1 = y0;
|
---|
| 191 | y0 = tmp;
|
---|
| 192 | }
|
---|
| 193 | return y0 > x1;
|
---|
| 194 | }
|
---|
| 195 | private void ShowWarningMessageBox(Exception ex) {
|
---|
| 196 | MessageBox.Show(ex.Message,
|
---|
| 197 | "Warning - " + ex.GetType().Name,
|
---|
| 198 | MessageBoxButtons.OK,
|
---|
| 199 | MessageBoxIcon.Warning);
|
---|
| 200 | }
|
---|
| 201 | private void ShowErrorMessageBox(Exception ex) {
|
---|
| 202 | MessageBox.Show(BuildErrorMessage(ex),
|
---|
| 203 | "Error - " + ex.GetType().Name,
|
---|
| 204 | MessageBoxButtons.OK,
|
---|
| 205 | MessageBoxIcon.Error);
|
---|
| 206 | }
|
---|
| 207 | private string BuildErrorMessage(Exception ex) {
|
---|
| 208 | StringBuilder sb = new StringBuilder();
|
---|
| 209 | sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
|
---|
| 210 |
|
---|
| 211 | while (ex.InnerException != null) {
|
---|
| 212 | ex = ex.InnerException;
|
---|
| 213 | sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
|
---|
| 214 | }
|
---|
| 215 | return sb.ToString();
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | #region gui events
|
---|
| 219 | private void radioButton_CheckedChanged(object sender, EventArgs e) {
|
---|
| 220 | minTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
|
---|
| 221 | minTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
|
---|
| 222 | maxTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
|
---|
| 223 | maxTimeOffsetTextBox.Enabled = timeSeriesRadioButton.Checked;
|
---|
| 224 | autoregressiveCheckBox.Enabled = timeSeriesRadioButton.Checked;
|
---|
| 225 | autoregressiveLabel.Enabled = timeSeriesRadioButton.Checked;
|
---|
| 226 | if (timeSeriesRadioButton.Checked) problem.LearningTask = LearningTask.TimeSeries;
|
---|
| 227 | else if (classificationRadioButton.Checked) problem.LearningTask = LearningTask.Classification;
|
---|
| 228 | else if (regressionRadioButton.Checked) problem.LearningTask = LearningTask.Regression;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | private void timeOffsetTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 232 | int min, max;
|
---|
| 233 | e.Cancel = !int.TryParse(minTimeOffsetTextBox.Text, out min);
|
---|
| 234 | e.Cancel = !int.TryParse(maxTimeOffsetTextBox.Text, out max);
|
---|
| 235 | e.Cancel = min > max;
|
---|
| 236 | }
|
---|
| 237 | private void timeOffsetTextBox_Validated(object sender, EventArgs e) {
|
---|
| 238 | problem.MinTimeOffset = int.Parse(minTimeOffsetTextBox.Text);
|
---|
| 239 | problem.MaxTimeOffset = int.Parse(maxTimeOffsetTextBox.Text);
|
---|
| 240 | }
|
---|
| 241 | #endregion
|
---|
[992] | 242 | }
|
---|
| 243 | }
|
---|