Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Modeling/3.2/ProblemInjectorView.cs @ 3962

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

Fixed #784 (ProblemInjector should be changed to read variable names instead of indexes for input and target variables)

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