[645] | 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.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Data;
|
---|
| 31 | using HeuristicLab.DataAnalysis;
|
---|
| 32 |
|
---|
[1856] | 33 | namespace HeuristicLab.Modeling {
|
---|
[1252] | 34 | public partial class ProblemInjectorView : ViewBase {
|
---|
| 35 | public ProblemInjector ProblemInjector {
|
---|
| 36 | get { return (ProblemInjector)Item; }
|
---|
[645] | 37 | set { base.Item = value; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[1252] | 40 | public ProblemInjectorView()
|
---|
[645] | 41 | : base() {
|
---|
| 42 | InitializeComponent();
|
---|
| 43 | }
|
---|
[1252] | 44 | public ProblemInjectorView(ProblemInjector problemInjector)
|
---|
[645] | 45 | : this() {
|
---|
[1252] | 46 | ProblemInjector = problemInjector;
|
---|
| 47 | variablesView.Operator = problemInjector;
|
---|
[645] | 48 | variablesView.Update();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | protected override void RemoveItemEvents() {
|
---|
| 52 | operatorBaseVariableInfosView.Operator = null;
|
---|
| 53 | operatorBaseDescriptionView.Operator = null;
|
---|
| 54 | base.RemoveItemEvents();
|
---|
| 55 | }
|
---|
| 56 | protected override void AddItemEvents() {
|
---|
| 57 | base.AddItemEvents();
|
---|
[1252] | 58 | operatorBaseVariableInfosView.Operator = ProblemInjector;
|
---|
| 59 | operatorBaseDescriptionView.Operator = ProblemInjector;
|
---|
[645] | 60 | }
|
---|
| 61 |
|
---|
| 62 | protected override void UpdateControls() {
|
---|
| 63 | base.UpdateControls();
|
---|
[1252] | 64 | if (ProblemInjector == null) {
|
---|
[645] | 65 | importInstanceButton.Enabled = false;
|
---|
| 66 | } else {
|
---|
[1252] | 67 | Dataset dataset = (Dataset)ProblemInjector.GetVariable("Dataset").Value;
|
---|
[645] | 68 | datasetView.Dataset = dataset;
|
---|
| 69 | importInstanceButton.Enabled = true;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | private void importInstanceButton_Click(object sender, EventArgs e) {
|
---|
[1215] | 74 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[645] | 75 | DatasetParser parser = new DatasetParser();
|
---|
| 76 | bool success = false;
|
---|
| 77 | try {
|
---|
| 78 | try {
|
---|
| 79 | parser.Import(openFileDialog.FileName, true);
|
---|
| 80 | success = true;
|
---|
[1215] | 81 | }
|
---|
| 82 | catch (DataFormatException ex) {
|
---|
[645] | 83 | ShowWarningMessageBox(ex);
|
---|
| 84 | // not possible to parse strictly => clear and try to parse non-strict
|
---|
| 85 | parser.Reset();
|
---|
| 86 | parser.Import(openFileDialog.FileName, false);
|
---|
| 87 | success = true;
|
---|
| 88 | }
|
---|
[1215] | 89 | }
|
---|
| 90 | catch (DataFormatException ex) {
|
---|
[645] | 91 | // if the non-strict parsing also failed then show the exception
|
---|
| 92 | ShowErrorMessageBox(ex);
|
---|
| 93 | }
|
---|
[1215] | 94 | if (success) {
|
---|
[1252] | 95 | Dataset dataset = (Dataset)ProblemInjector.GetVariable("Dataset").Value;
|
---|
[645] | 96 | dataset.Rows = parser.Rows;
|
---|
| 97 | dataset.Columns = parser.Columns;
|
---|
[1215] | 98 | for (int i = 0; i < parser.VariableNames.Length; i++) {
|
---|
| 99 | dataset.SetVariableName(i, parser.VariableNames[i]);
|
---|
| 100 | }
|
---|
[645] | 101 | dataset.Name = parser.ProblemName;
|
---|
| 102 | dataset.Samples = new double[dataset.Rows * dataset.Columns];
|
---|
| 103 | Array.Copy(parser.Samples, dataset.Samples, dataset.Columns * dataset.Rows);
|
---|
| 104 | TrySetVariable("TrainingSamplesStart", parser.TrainingSamplesStart);
|
---|
| 105 | TrySetVariable("TrainingSamplesStart", parser.TrainingSamplesStart);
|
---|
| 106 | TrySetVariable("TrainingSamplesEnd", parser.TrainingSamplesEnd);
|
---|
| 107 | TrySetVariable("ValidationSamplesStart", parser.ValidationSamplesStart);
|
---|
| 108 | TrySetVariable("ValidationSamplesEnd", parser.ValidationSamplesEnd);
|
---|
| 109 | TrySetVariable("TestSamplesStart", parser.TestSamplesStart);
|
---|
| 110 | TrySetVariable("TestSamplesEnd", parser.TestSamplesEnd);
|
---|
| 111 | TrySetVariable("TargetVariable", parser.TargetVariable);
|
---|
| 112 |
|
---|
[1252] | 113 | IVariable var = ProblemInjector.GetVariable("AllowedFeatures");
|
---|
[1215] | 114 | if (var != null) {
|
---|
[645] | 115 | ItemList<IntData> allowedFeatures = (ItemList<IntData>)var.Value;
|
---|
| 116 | allowedFeatures.Clear();
|
---|
| 117 | List<int> nonInputVariables = parser.NonInputVariables;
|
---|
[1215] | 118 | for (int i = 0; i < dataset.Columns; i++) {
|
---|
| 119 | if (!nonInputVariables.Contains(i)) allowedFeatures.Add(new IntData(i));
|
---|
[645] | 120 | }
|
---|
| 121 | }
|
---|
| 122 | Refresh();
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void TrySetVariable(string name, int value) {
|
---|
[1252] | 128 | IVariable var = ProblemInjector.GetVariable(name);
|
---|
[1215] | 129 | if (var != null) {
|
---|
[645] | 130 | ((IntData)var.Value).Data = value;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private void ShowWarningMessageBox(Exception ex) {
|
---|
| 135 | MessageBox.Show(ex.Message,
|
---|
| 136 | "Warning - " + ex.GetType().Name,
|
---|
| 137 | MessageBoxButtons.OK,
|
---|
| 138 | MessageBoxIcon.Warning);
|
---|
| 139 | }
|
---|
| 140 | private void ShowErrorMessageBox(Exception ex) {
|
---|
| 141 | MessageBox.Show(BuildErrorMessage(ex),
|
---|
| 142 | "Error - " + ex.GetType().Name,
|
---|
| 143 | MessageBoxButtons.OK,
|
---|
| 144 | MessageBoxIcon.Error);
|
---|
| 145 | }
|
---|
| 146 | private string BuildErrorMessage(Exception ex) {
|
---|
| 147 | StringBuilder sb = new StringBuilder();
|
---|
| 148 | sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
|
---|
| 149 |
|
---|
[1215] | 150 | while (ex.InnerException != null) {
|
---|
[645] | 151 | ex = ex.InnerException;
|
---|
| 152 | sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
|
---|
| 153 | }
|
---|
| 154 | return sb.ToString();
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|