Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.GP.StructureIdentification/3.2/StructIdProblemInjectorView.cs @ 3513

Last change on this file since 3513 was 1215, checked in by gkronber, 16 years ago

removed property VariableNames and added Get- and SetVariableName(index) methods instead. #419 (Refactor CEDMA plugins)

File size: 6.1 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.Operators;
32using HeuristicLab.DataAnalysis;
33
34namespace HeuristicLab.GP.StructureIdentification {
35  public partial class StructIdProblemInjectorView : ViewBase {
36    public StructIdProblemInjector StructIdProblemInjector {
37      get { return (StructIdProblemInjector)Item; }
38      set { base.Item = value; }
39    }
40
41    public StructIdProblemInjectorView()
42      : base() {
43      InitializeComponent();
44    }
45    public StructIdProblemInjectorView(StructIdProblemInjector structIdProblemInjector)
46      : this() {
47      StructIdProblemInjector = structIdProblemInjector;
48      variablesView.Operator = structIdProblemInjector;
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();
59      operatorBaseVariableInfosView.Operator = StructIdProblemInjector;
60      operatorBaseDescriptionView.Operator = StructIdProblemInjector;
61    }
62
63    protected override void UpdateControls() {
64      base.UpdateControls();
65      if (StructIdProblemInjector == null) {
66        importInstanceButton.Enabled = false;
67      } else {
68        Dataset dataset = (Dataset)StructIdProblemInjector.GetVariable("Dataset").Value;
69        datasetView.Dataset = dataset;
70        importInstanceButton.Enabled = true;
71      }
72    }
73
74    private void importInstanceButton_Click(object sender, EventArgs e) {
75      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
76        DatasetParser parser = new DatasetParser();
77        bool success = false;
78        try {
79          try {
80            parser.Import(openFileDialog.FileName, true);
81            success = true;
82          }
83          catch (DataFormatException ex) {
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          }
90        }
91        catch (DataFormatException ex) {
92          // if the non-strict parsing also failed then show the exception
93          ShowErrorMessageBox(ex);
94        }
95        if (success) {
96          Dataset dataset = (Dataset)StructIdProblemInjector.GetVariable("Dataset").Value;
97          dataset.Rows = parser.Rows;
98          dataset.Columns = parser.Columns;
99          for (int i = 0; i < parser.VariableNames.Length; i++) {
100            dataset.SetVariableName(i, parser.VariableNames[i]);
101          }
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
114          IVariable var = StructIdProblemInjector.GetVariable("AllowedFeatures");
115          if (var != null) {
116            ItemList<IntData> allowedFeatures = (ItemList<IntData>)var.Value;
117            allowedFeatures.Clear();
118            List<int> nonInputVariables = parser.NonInputVariables;
119            for (int i = 0; i < dataset.Columns; i++) {
120              if (!nonInputVariables.Contains(i)) allowedFeatures.Add(new IntData(i));
121            }
122          }
123          Refresh();
124        }
125      }
126    }
127
128    private void TrySetVariable(string name, int value) {
129      IVariable var = StructIdProblemInjector.GetVariable(name);
130      if (var != null) {
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
151      while (ex.InnerException != null) {
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}
Note: See TracBrowser for help on using the repository browser.