Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/MultiVariateDataAnalysisProblemData.cs @ 4364

Last change on this file since 4364 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 20.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.IO;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.MultiVariate {
32  [Item("MultiVariateDataAnalysisProblemData", "Represents an item containing all data defining a multi-variate data analysis problem.")]
33  [StorableClass]
34  public class MultiVariateDataAnalysisProblemData : ParameterizedNamedItem {
35    private bool suppressEvents = false;
36    #region default data
37    // y0 = x^4 + x^3 + x^2 + x
38    // y1 = -(y0)
39    private static double[,] kozaF1 = new double[,] {
40{2.017885919,   -2.017885919, -1.449165046},
41{1.30060506,  -1.30060506,  -1.344523885},
42{1.147134798, -1.147134798, -1.317989331},
43{0.877182504, -0.877182504, -1.266142284},
44{0.852562452, -0.852562452, -1.261020794},
45{0.431095788, -0.431095788, -1.158793317},
46{0.112586002, -0.112586002, -1.050908405},
47{0.04594507,  -0.04594507,  -1.021989402},
48{0.042572879, -0.042572879, -1.020438113},
49{-0.074027291,  0.074027291,  -0.959859562},
50{-0.109178553,  0.109178553,  -0.938094706},
51{-0.259721109,  0.259721109,  -0.803635355},
52{-0.272991057,  0.272991057,  -0.387519561},
53{-0.161978191,  0.161978191,  -0.193611001},
54{-0.102489983,  0.102489983,  -0.114215349},
55{-0.01469968, 0.01469968, -0.014918985},
56{-0.008863365,  0.008863365,  -0.008942626},
57{0.026751057, -0.026751057, 0.026054094},
58{0.166922436, -0.166922436, 0.14309643},
59{0.176953808, -0.176953808, 0.1504144},
60{0.190233418, -0.190233418, 0.159916534},
61{0.199800708, -0.199800708, 0.166635331},
62{0.261502822, -0.261502822, 0.207600348},
63{0.30182879,  -0.30182879,  0.232370249},
64{0.83763905,  -0.83763905,  0.468046718}
65    };
66    #endregion
67    #region parameter properties
68    public IValueParameter<Dataset> DatasetParameter {
69      get { return (IValueParameter<Dataset>)Parameters["Dataset"]; }
70    }
71
72    public IValueParameter<ICheckedItemList<StringValue>> TargetVariablesParameter {
73      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["TargetVariables"]; }
74    }
75
76    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
77      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
78    }
79
80    public IValueParameter<IntValue> TrainingSamplesStartParameter {
81      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; }
82    }
83    public IValueParameter<IntValue> TrainingSamplesEndParameter {
84      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesEnd"]; }
85    }
86    public IValueParameter<IntValue> TestSamplesStartParameter {
87      get { return (IValueParameter<IntValue>)Parameters["TestSamplesStart"]; }
88    }
89    public IValueParameter<IntValue> TestSamplesEndParameter {
90      get { return (IValueParameter<IntValue>)Parameters["TestSamplesEnd"]; }
91    }
92    #endregion
93
94    #region properties
95    public Dataset Dataset {
96      get { return (Dataset)DatasetParameter.Value; }
97      set {
98        if (value != Dataset) {
99          if (value == null) throw new ArgumentNullException();
100          if (Dataset != null) DeregisterDatasetEventHandlers();
101          DatasetParameter.Value = value;
102        }
103      }
104    }
105    public ICheckedItemList<StringValue> TargetVariables {
106      get { return (ICheckedItemList<StringValue>)TargetVariablesParameter.Value; }
107      set {
108        if (value != TargetVariablesParameter.Value) {
109          if (value == null) throw new ArgumentNullException();
110          if (TargetVariables != null) DeregisterTargetVariablesEventHandlers();
111          TargetVariablesParameter.Value = value;
112        }
113      }
114    }
115    public ICheckedItemList<StringValue> InputVariables {
116      get { return (ICheckedItemList<StringValue>)InputVariablesParameter.Value; }
117      set {
118        if (value != InputVariables) {
119          if (value == null) throw new ArgumentNullException();
120          if (InputVariables != null) DeregisterInputVariablesEventHandlers();
121          InputVariablesParameter.Value = value;
122        }
123      }
124    }
125    public IntValue TrainingSamplesStart {
126      get { return (IntValue)TrainingSamplesStartParameter.Value; }
127      set {
128        if (value != TrainingSamplesStart) {
129          if (value == null) throw new ArgumentNullException();
130          if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
131          TrainingSamplesStartParameter.Value = value;
132        }
133      }
134    }
135    public IntValue TrainingSamplesEnd {
136      get { return (IntValue)TrainingSamplesEndParameter.Value; }
137      set {
138        if (value != TrainingSamplesEnd) {
139          if (value == null) throw new ArgumentNullException();
140          if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
141          TrainingSamplesEndParameter.Value = value;
142        }
143      }
144    }
145    public IntValue TestSamplesStart {
146      get { return (IntValue)TestSamplesStartParameter.Value; }
147      set {
148        if (value != TestSamplesStart) {
149          if (value == null) throw new ArgumentNullException();
150          if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
151          TestSamplesStartParameter.Value = value;
152        }
153      }
154    }
155    public IntValue TestSamplesEnd {
156      get { return (IntValue)TestSamplesEndParameter.Value; }
157      set {
158        if (value != TestSamplesEnd) {
159          if (value == null) throw new ArgumentNullException();
160          if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd);
161          TestSamplesEndParameter.Value = value;
162        }
163      }
164    }
165    #endregion
166
167    public MultiVariateDataAnalysisProblemData()
168      : base() {
169      var inputVariables = new CheckedItemList<StringValue>();
170      StringValue inputVariable = new StringValue("x");
171      inputVariables.Add(inputVariable);
172      StringValue targetVariable0 = new StringValue("y0");
173      StringValue targetVariable1 = new StringValue("y1");
174      var targetVariables = new CheckedItemList<StringValue>();
175      targetVariables.Add(targetVariable0);
176      targetVariables.Add(targetVariable1);
177      Parameters.Add(new ValueParameter<Dataset>("Dataset", new Dataset(new string[] { "y0", "y1", "x" }, kozaF1)));
178      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariables));
179      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("TargetVariables", targetVariables));
180      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(0)));
181      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(15)));
182      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(15)));
183      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(25)));
184      RegisterParameterEventHandlers();
185      RegisterParameterValueEventHandlers();
186    }
187
188
189    [StorableConstructor]
190    private MultiVariateDataAnalysisProblemData(bool deserializing) : base() { }
191
192    [StorableHook(HookType.AfterDeserialization)]
193    private void AfterDeserializationHook() {
194      RegisterParameterEventHandlers();
195      RegisterParameterValueEventHandlers();
196    }
197
198    #region events
199    public event EventHandler ProblemDataChanged;
200    protected virtual void OnProblemDataChanged(EventArgs e) {
201      if (!suppressEvents) {
202        var listeners = ProblemDataChanged;
203        if (listeners != null) listeners(this, e);
204      }
205    }
206
207    private void RegisterParameterEventHandlers() {
208      DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged);
209      InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
210      TargetVariablesParameter.ValueChanged += new EventHandler(TargetVariablesParameter_ValueChanged);
211      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
212      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
213      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
214      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
215    }
216
217    private void RegisterParameterValueEventHandlers() {
218      RegisterDatasetEventHandlers();
219      RegisterInputVariablesEventHandlers();
220      RegisterTargetVariablesEventHandlers();
221      RegisterValueTypeEventHandlers(TrainingSamplesStart);
222      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
223      RegisterValueTypeEventHandlers(TestSamplesStart);
224      RegisterValueTypeEventHandlers(TestSamplesEnd);
225    }
226
227
228    #region parameter value changed event handlers
229    void DatasetParameter_ValueChanged(object sender, EventArgs e) {
230      RegisterDatasetEventHandlers();
231      OnProblemDataChanged(EventArgs.Empty);
232    }
233    void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
234      RegisterInputVariablesEventHandlers();
235      OnProblemDataChanged(EventArgs.Empty);
236    }
237    void TargetVariablesParameter_ValueChanged(object sender, EventArgs e) {
238      RegisterTargetVariablesEventHandlers();
239      OnProblemDataChanged(EventArgs.Empty);
240    }
241    void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
242      RegisterValueTypeEventHandlers(TrainingSamplesStart);
243      OnProblemDataChanged(EventArgs.Empty);
244    }
245    void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
246      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
247      OnProblemDataChanged(EventArgs.Empty);
248    }
249    void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
250      RegisterValueTypeEventHandlers(TestSamplesStart);
251      OnProblemDataChanged(EventArgs.Empty);
252    }
253    void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
254      RegisterValueTypeEventHandlers(TestSamplesEnd);
255      OnProblemDataChanged(EventArgs.Empty);
256    }
257    #endregion
258
259
260    private void RegisterDatasetEventHandlers() {
261      Dataset.Reset += new EventHandler(Dataset_Reset);
262      Dataset.ColumnNamesChanged += new EventHandler(Dataset_ColumnNamesChanged);
263    }
264
265    private void DeregisterDatasetEventHandlers() {
266      Dataset.Reset -= new EventHandler(Dataset_Reset);
267      Dataset.ColumnNamesChanged -= new EventHandler(Dataset_ColumnNamesChanged);
268    }
269
270    void Dataset_ColumnNamesChanged(object sender, EventArgs e) {
271      OnProblemDataChanged(e);
272    }
273
274    void Dataset_Reset(object sender, EventArgs e) {
275      OnProblemDataChanged(e);
276    }
277
278    void Dataset_DataChanged(object sender, EventArgs<int, int> e) {
279      OnProblemDataChanged(e);
280    }
281
282    private void RegisterInputVariablesEventHandlers() {
283      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
284      InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
285      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
286      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
287      foreach (var item in InputVariables)
288        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
289    }
290
291    private void DeregisterInputVariablesEventHandlers() {
292      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
293      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
294      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
295      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
296      foreach (var item in InputVariables) {
297        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
298      }
299    }
300
301    private void RegisterTargetVariablesEventHandlers() {
302      TargetVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_CollectionReset);
303      TargetVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_ItemsAdded);
304      TargetVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_ItemsRemoved);
305      TargetVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_CheckedItemsChanged);
306      foreach (var item in TargetVariables)
307        item.ValueChanged += new EventHandler(TargetVariable_ValueChanged);
308    }
309
310    private void DeregisterTargetVariablesEventHandlers() {
311      TargetVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_CollectionReset);
312      TargetVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_ItemsAdded);
313      TargetVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_ItemsRemoved);
314      TargetVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_CheckedItemsChanged);
315      foreach (var item in TargetVariables) {
316        item.ValueChanged -= new EventHandler(TargetVariable_ValueChanged);
317      }
318    }
319
320    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
321      OnProblemDataChanged(e);
322    }
323
324    private void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
325      foreach (var indexedItem in e.Items)
326        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
327      OnProblemDataChanged(e);
328    }
329
330    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
331      foreach (var indexedItem in e.Items)
332        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
333      OnProblemDataChanged(e);
334    }
335
336    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
337      foreach (var indexedItem in e.OldItems)
338        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
339      OnProblemDataChanged(e);
340    }
341
342    void InputVariable_ValueChanged(object sender, EventArgs e) {
343      OnProblemDataChanged(e);
344    }
345    private void TargetVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
346      OnProblemDataChanged(e);
347    }
348
349    private void TargetVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
350      foreach (var indexedItem in e.Items)
351        indexedItem.Value.ValueChanged -= new EventHandler(TargetVariable_ValueChanged);
352      OnProblemDataChanged(e);
353    }
354
355    private void TargetVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
356      foreach (var indexedItem in e.Items)
357        indexedItem.Value.ValueChanged += new EventHandler(TargetVariable_ValueChanged);
358      OnProblemDataChanged(e);
359    }
360
361    private void TargetVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
362      foreach (var indexedItem in e.OldItems)
363        indexedItem.Value.ValueChanged -= new EventHandler(TargetVariable_ValueChanged);
364      OnProblemDataChanged(e);
365    }
366
367    void TargetVariable_ValueChanged(object sender, EventArgs e) {
368      OnProblemDataChanged(e);
369    }
370    #region helper
371
372    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
373      value.ValueChanged += new EventHandler(value_ValueChanged);
374    }
375
376    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
377      value.ValueChanged -= new EventHandler(value_ValueChanged);
378    }
379
380    void value_ValueChanged(object sender, EventArgs e) {
381      OnProblemDataChanged(e);
382    }
383
384    private void RegisterStringValueEventHandlers(StringValue value) {
385      value.ValueChanged += new EventHandler(value_ValueChanged);
386    }
387
388    private void DeregisterStringValueEventHandlers(StringValue value) {
389      value.ValueChanged -= new EventHandler(value_ValueChanged);
390    }
391
392    #endregion
393    #endregion
394
395    public virtual void ImportFromFile(string fileName) {
396      var csvFileParser = new CsvFileParser();
397      csvFileParser.Parse(fileName);
398      suppressEvents = true;
399      Name = "Data imported from " + Path.GetFileName(fileName);
400      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
401      Dataset.Name = Path.GetFileName(fileName);
402      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
403      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
404      TargetVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
405      int middle = (int)(csvFileParser.Rows * 0.5);
406      TrainingSamplesStart = new IntValue(0);
407      TrainingSamplesEnd = new IntValue(middle);
408      TestSamplesStart = new IntValue(middle);
409      TestSamplesEnd = new IntValue(csvFileParser.Rows);
410      suppressEvents = false;
411      OnProblemDataChanged(EventArgs.Empty);
412    }
413
414    public override IDeepCloneable Clone(Cloner cloner) {
415      MultiVariateDataAnalysisProblemData clone = (MultiVariateDataAnalysisProblemData)base.Clone(cloner);
416      clone.RegisterParameterEventHandlers();
417      clone.RegisterParameterValueEventHandlers();
418      return clone;
419    }
420
421    public DataAnalysisProblemData ConvertToDataAnalysisProblemData(string targetVariable) {
422      return new DataAnalysisProblemData((Dataset)Dataset.Clone(),
423        InputVariables.Select(x => x.Value),
424        targetVariable,
425        TrainingSamplesStart.Value,
426        TrainingSamplesEnd.Value,
427        TestSamplesStart.Value,
428        TestSamplesEnd.Value);
429    }
430  }
431}
Note: See TracBrowser for help on using the repository browser.