Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11883 was 5275, checked in by gkronber, 14 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 20.6 KB
RevLine 
[4056]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;
[4068]23using System.IO;
[4056]24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
[4068]27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
[4056]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]
[4457]34  public class MultiVariateDataAnalysisProblemData : ParameterizedNamedItem, IStorableContent {
[4056]35    private bool suppressEvents = false;
[4461]36    public string Filename { get; set; }
[4056]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
[5275]168
169    [StorableConstructor]
170    private MultiVariateDataAnalysisProblemData(bool deserializing) : base() { }
171    protected MultiVariateDataAnalysisProblemData(MultiVariateDataAnalysisProblemData original, Cloner cloner)
172      : base(original, cloner) {
173        RegisterParameterEventHandlers();
174        RegisterParameterValueEventHandlers();
175    }
[4056]176    public MultiVariateDataAnalysisProblemData()
177      : base() {
178      var inputVariables = new CheckedItemList<StringValue>();
179      StringValue inputVariable = new StringValue("x");
180      inputVariables.Add(inputVariable);
181      StringValue targetVariable0 = new StringValue("y0");
182      StringValue targetVariable1 = new StringValue("y1");
183      var targetVariables = new CheckedItemList<StringValue>();
184      targetVariables.Add(targetVariable0);
185      targetVariables.Add(targetVariable1);
186      Parameters.Add(new ValueParameter<Dataset>("Dataset", new Dataset(new string[] { "y0", "y1", "x" }, kozaF1)));
187      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariables));
188      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("TargetVariables", targetVariables));
189      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(0)));
190      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(15)));
191      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(15)));
192      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(25)));
193      RegisterParameterEventHandlers();
194      RegisterParameterValueEventHandlers();
195    }
196
[5275]197    public override IDeepCloneable Clone(Cloner cloner) {
198      return new MultiVariateDataAnalysisProblemData(this, cloner);
199    }
[4056]200
201    [StorableHook(HookType.AfterDeserialization)]
[5275]202    private void AfterDeserialization() {
[4056]203      RegisterParameterEventHandlers();
204      RegisterParameterValueEventHandlers();
205    }
206
207    #region events
208    public event EventHandler ProblemDataChanged;
209    protected virtual void OnProblemDataChanged(EventArgs e) {
210      if (!suppressEvents) {
211        var listeners = ProblemDataChanged;
212        if (listeners != null) listeners(this, e);
213      }
214    }
215
216    private void RegisterParameterEventHandlers() {
217      DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged);
218      InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
219      TargetVariablesParameter.ValueChanged += new EventHandler(TargetVariablesParameter_ValueChanged);
220      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
221      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
222      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
223      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
224    }
225
226    private void RegisterParameterValueEventHandlers() {
227      RegisterDatasetEventHandlers();
228      RegisterInputVariablesEventHandlers();
229      RegisterTargetVariablesEventHandlers();
230      RegisterValueTypeEventHandlers(TrainingSamplesStart);
231      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
232      RegisterValueTypeEventHandlers(TestSamplesStart);
233      RegisterValueTypeEventHandlers(TestSamplesEnd);
234    }
235
236
237    #region parameter value changed event handlers
238    void DatasetParameter_ValueChanged(object sender, EventArgs e) {
239      RegisterDatasetEventHandlers();
240      OnProblemDataChanged(EventArgs.Empty);
241    }
242    void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
243      RegisterInputVariablesEventHandlers();
244      OnProblemDataChanged(EventArgs.Empty);
245    }
246    void TargetVariablesParameter_ValueChanged(object sender, EventArgs e) {
247      RegisterTargetVariablesEventHandlers();
248      OnProblemDataChanged(EventArgs.Empty);
249    }
250    void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
251      RegisterValueTypeEventHandlers(TrainingSamplesStart);
252      OnProblemDataChanged(EventArgs.Empty);
253    }
254    void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
255      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
256      OnProblemDataChanged(EventArgs.Empty);
257    }
258    void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
259      RegisterValueTypeEventHandlers(TestSamplesStart);
260      OnProblemDataChanged(EventArgs.Empty);
261    }
262    void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
263      RegisterValueTypeEventHandlers(TestSamplesEnd);
264      OnProblemDataChanged(EventArgs.Empty);
265    }
266    #endregion
267
268
269    private void RegisterDatasetEventHandlers() {
270      Dataset.Reset += new EventHandler(Dataset_Reset);
271      Dataset.ColumnNamesChanged += new EventHandler(Dataset_ColumnNamesChanged);
272    }
273
274    private void DeregisterDatasetEventHandlers() {
275      Dataset.Reset -= new EventHandler(Dataset_Reset);
276      Dataset.ColumnNamesChanged -= new EventHandler(Dataset_ColumnNamesChanged);
277    }
278
279    void Dataset_ColumnNamesChanged(object sender, EventArgs e) {
280      OnProblemDataChanged(e);
281    }
282
283    void Dataset_Reset(object sender, EventArgs e) {
284      OnProblemDataChanged(e);
285    }
286
287    void Dataset_DataChanged(object sender, EventArgs<int, int> e) {
288      OnProblemDataChanged(e);
289    }
290
291    private void RegisterInputVariablesEventHandlers() {
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    private void DeregisterInputVariablesEventHandlers() {
301      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
302      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
303      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
304      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
305      foreach (var item in InputVariables) {
306        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
307      }
308    }
309
310    private void RegisterTargetVariablesEventHandlers() {
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    private void DeregisterTargetVariablesEventHandlers() {
320      TargetVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_CollectionReset);
321      TargetVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_ItemsAdded);
322      TargetVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_ItemsRemoved);
323      TargetVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(TargetVariables_CheckedItemsChanged);
324      foreach (var item in TargetVariables) {
325        item.ValueChanged -= new EventHandler(TargetVariable_ValueChanged);
326      }
327    }
328
329    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
330      OnProblemDataChanged(e);
331    }
332
333    private void InputVariables_ItemsRemoved(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_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
340      foreach (var indexedItem in e.Items)
341        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
342      OnProblemDataChanged(e);
343    }
344
345    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
346      foreach (var indexedItem in e.OldItems)
347        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
348      OnProblemDataChanged(e);
349    }
350
351    void InputVariable_ValueChanged(object sender, EventArgs e) {
352      OnProblemDataChanged(e);
353    }
354    private void TargetVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
355      OnProblemDataChanged(e);
356    }
357
358    private void TargetVariables_ItemsRemoved(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_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
365      foreach (var indexedItem in e.Items)
366        indexedItem.Value.ValueChanged += new EventHandler(TargetVariable_ValueChanged);
367      OnProblemDataChanged(e);
368    }
369
370    private void TargetVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
371      foreach (var indexedItem in e.OldItems)
372        indexedItem.Value.ValueChanged -= new EventHandler(TargetVariable_ValueChanged);
373      OnProblemDataChanged(e);
374    }
375
376    void TargetVariable_ValueChanged(object sender, EventArgs e) {
377      OnProblemDataChanged(e);
378    }
379    #region helper
380
381    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
382      value.ValueChanged += new EventHandler(value_ValueChanged);
383    }
384
385    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
386      value.ValueChanged -= new EventHandler(value_ValueChanged);
387    }
388
389    void value_ValueChanged(object sender, EventArgs e) {
390      OnProblemDataChanged(e);
391    }
392
393    private void RegisterStringValueEventHandlers(StringValue value) {
394      value.ValueChanged += new EventHandler(value_ValueChanged);
395    }
396
397    private void DeregisterStringValueEventHandlers(StringValue value) {
398      value.ValueChanged -= new EventHandler(value_ValueChanged);
399    }
400
401    #endregion
402    #endregion
403
404    public virtual void ImportFromFile(string fileName) {
[5275]405      var tableFileParser = new TableFileParser();
406      tableFileParser.Parse(fileName);
[4056]407      suppressEvents = true;
408      Name = "Data imported from " + Path.GetFileName(fileName);
[5275]409      Dataset = new Dataset(tableFileParser.VariableNames, tableFileParser.Values);
[4056]410      Dataset.Name = Path.GetFileName(fileName);
411      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
412      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
413      TargetVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
[5275]414      int middle = (int)(tableFileParser.Rows * 0.5);
[4056]415      TrainingSamplesStart = new IntValue(0);
416      TrainingSamplesEnd = new IntValue(middle);
417      TestSamplesStart = new IntValue(middle);
[5275]418      TestSamplesEnd = new IntValue(tableFileParser.Rows);
[4056]419      suppressEvents = false;
420      OnProblemDataChanged(EventArgs.Empty);
421    }
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.