Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/MultiVariateDataAnalysisProblemData.cs @ 4057

Last change on this file since 4057 was 4056, checked in by gkronber, 14 years ago

Added new plugins for multi-variate regression. #1089

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