Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4695 was 4461, checked in by gkronber, 14 years ago

made DataAnalysisProblem and DataAnalysisProblemData and SymbolicTimeSeriesPrognosisSolution savable and added field for conditional evaluation to Symbolic/SymbolicTimeSeriesPrognosisSolution (fix of changeset r4457). #1142

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