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