Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisProblemData.cs @ 4675

Last change on this file since 4675 was 4457, checked in by gkronber, 14 years ago

made DataAnalysisProblem and DataAnalysisProblemData and SymbolicTimeSeriesPrognosisSolution savable and added field for conditional evaluation to Symbolic/SymbolicTimeSeriesPrognosisSolution. #1142

File size: 17.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.IO;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("DataAnalysisProblemData", "Represents an item containing all data defining a data analysis problem.")]
34  [StorableClass]
35  public class DataAnalysisProblemData : ParameterizedNamedItem, IStorableContent {
36    private bool suppressEvents = false;
37
38    #region IStorableContent Members
39    public string Filename { get; set; }
40    #endregion
41
42    #region default data
43    // y = x^4 + x^3 + x^2 + x
44    private static double[,] kozaF1 = new double[,] {
45{2.017885919, -1.449165046},
46{1.30060506,  -1.344523885},
47{1.147134798, -1.317989331},
48{0.877182504, -1.266142284},
49{0.852562452, -1.261020794},
50{0.431095788, -1.158793317},
51{0.112586002, -1.050908405},
52{0.04594507,  -1.021989402},
53{0.042572879, -1.020438113},
54{-0.074027291,  -0.959859562},
55{-0.109178553,  -0.938094706},
56{-0.259721109,  -0.803635355},
57{-0.272991057,  -0.387519561},
58{-0.161978191,  -0.193611001},
59{-0.102489983,  -0.114215349},
60{-0.01469968, -0.014918985},
61{-0.008863365,  -0.008942626},
62{0.026751057, 0.026054094},
63{0.166922436, 0.14309643},
64{0.176953808, 0.1504144},
65{0.190233418, 0.159916534},
66{0.199800708, 0.166635331},
67{0.261502822, 0.207600348},
68{0.30182879,  0.232370249},
69{0.83763905,  0.468046718}
70    };
71    #endregion
72    #region parameter properties
73    public IValueParameter<Dataset> DatasetParameter {
74      get { return (IValueParameter<Dataset>)Parameters["Dataset"]; }
75    }
76    public IValueParameter<StringValue> TargetVariableParameter {
77      get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; }
78    }
79    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
80      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
81    }
82    public IValueParameter<IntValue> TrainingSamplesStartParameter {
83      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; }
84    }
85    public IValueParameter<IntValue> TrainingSamplesEndParameter {
86      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesEnd"]; }
87    }
88    public IValueParameter<IntValue> TestSamplesStartParameter {
89      get { return (IValueParameter<IntValue>)Parameters["TestSamplesStart"]; }
90    }
91    public IValueParameter<IntValue> TestSamplesEndParameter {
92      get { return (IValueParameter<IntValue>)Parameters["TestSamplesEnd"]; }
93    }
94    #endregion
95
96    #region properties
97    public Dataset Dataset {
98      get { return (Dataset)DatasetParameter.Value; }
99      set {
100        if (value != Dataset) {
101          if (value == null) throw new ArgumentNullException();
102          DatasetParameter.Value = value;
103        }
104      }
105    }
106    public StringValue TargetVariable {
107      get { return (StringValue)TargetVariableParameter.Value; }
108      set {
109        if (value != TargetVariableParameter.Value) {
110          if (value == null) throw new ArgumentNullException();
111          if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
112          TargetVariableParameter.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 DataAnalysisProblemData()
169      : base() {
170      var inputVariables = new CheckedItemList<StringValue>();
171      StringValue inputVariable = new StringValue("x");
172      inputVariables.Add(inputVariable);
173      StringValue targetVariable = new StringValue("y");
174      var validTargetVariables = new ItemSet<StringValue>();
175      validTargetVariables.Add(targetVariable);
176      Parameters.Add(new ValueParameter<Dataset>("Dataset", new Dataset(new string[] { "y", "x" }, kozaF1)));
177      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariables.AsReadOnly()));
178      Parameters.Add(new ConstrainedValueParameter<StringValue>("TargetVariable", validTargetVariables, targetVariable));
179      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(0)));
180      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(15)));
181      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(15)));
182      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(25)));
183      RegisterParameterEventHandlers();
184      RegisterParameterValueEventHandlers();
185    }
186
187    public DataAnalysisProblemData(Dataset dataset, IEnumerable<string> inputVariables, string targetVariable,
188      int trainingSamplesStart, int trainingSamplesEnd, int testSamplesStart, int testSamplesEnd) {
189      var inputVariablesList = new CheckedItemList<StringValue>(inputVariables.Select(x => new StringValue(x)));
190      StringValue targetVariableValue = new StringValue(targetVariable);
191      var validTargetVariables = new ItemSet<StringValue>();
192      foreach (var variable in dataset.VariableNames)
193        if (variable != targetVariable)
194          validTargetVariables.Add(new StringValue(variable));
195      validTargetVariables.Add(targetVariableValue);
196      Parameters.Add(new ValueParameter<Dataset>("Dataset", dataset));
197      Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>("InputVariables", inputVariablesList.AsReadOnly()));
198      Parameters.Add(new ConstrainedValueParameter<StringValue>("TargetVariable", validTargetVariables, targetVariableValue));
199      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesStart", new IntValue(trainingSamplesStart)));
200      Parameters.Add(new ValueParameter<IntValue>("TrainingSamplesEnd", new IntValue(trainingSamplesEnd)));
201      Parameters.Add(new ValueParameter<IntValue>("TestSamplesStart", new IntValue(testSamplesStart)));
202      Parameters.Add(new ValueParameter<IntValue>("TestSamplesEnd", new IntValue(testSamplesEnd)));
203      RegisterParameterEventHandlers();
204      RegisterParameterValueEventHandlers();
205    }
206
207    [StorableConstructor]
208    private DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
209
210    [StorableHook(HookType.AfterDeserialization)]
211    private void AfterDeserializationHook() {
212      RegisterParameterEventHandlers();
213      RegisterParameterValueEventHandlers();
214    }
215
216    #region events
217    public event EventHandler ProblemDataChanged;
218    protected virtual void OnProblemDataChanged(EventArgs e) {
219      if (!suppressEvents) {
220        var listeners = ProblemDataChanged;
221        if (listeners != null) listeners(this, e);
222      }
223    }
224
225    private void RegisterParameterEventHandlers() {
226      DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged);
227      InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
228      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
229      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
230      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
231      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
232      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
233    }
234
235    private void RegisterParameterValueEventHandlers() {
236      RegisterInputVariablesEventHandlers();
237      if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable);
238      RegisterValueTypeEventHandlers(TrainingSamplesStart);
239      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
240      RegisterValueTypeEventHandlers(TestSamplesStart);
241      RegisterValueTypeEventHandlers(TestSamplesEnd);
242    }
243
244
245    #region parameter value changed event handlers
246    private void DatasetParameter_ValueChanged(object sender, EventArgs e) {
247      OnProblemDataChanged(EventArgs.Empty);
248    }
249    private void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
250      RegisterInputVariablesEventHandlers();
251      OnProblemDataChanged(EventArgs.Empty);
252    }
253    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
254      if (TargetVariable != null) {
255        RegisterStringValueEventHandlers(TargetVariable);
256        OnProblemDataChanged(EventArgs.Empty);
257      }
258    }
259    private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
260      RegisterValueTypeEventHandlers(TrainingSamplesStart);
261      OnProblemDataChanged(EventArgs.Empty);
262    }
263    private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
264      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
265      OnProblemDataChanged(EventArgs.Empty);
266    }
267    private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
268      RegisterValueTypeEventHandlers(TestSamplesStart);
269      OnProblemDataChanged(EventArgs.Empty);
270    }
271    private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
272      RegisterValueTypeEventHandlers(TestSamplesEnd);
273      OnProblemDataChanged(EventArgs.Empty);
274    }
275    #endregion
276
277    private void RegisterInputVariablesEventHandlers() {
278      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
279      InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
280      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
281      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
282      foreach (var item in InputVariables) {
283        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
284      }
285    }
286
287    private void DeregisterInputVariablesEventHandlers() {
288      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
289      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
290      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
291      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
292      foreach (var item in InputVariables) {
293        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
294      }
295    }
296
297    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
298      OnProblemDataChanged(e);
299    }
300    private void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
301      foreach (var indexedItem in e.Items)
302        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
303      OnProblemDataChanged(e);
304    }
305    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
306      foreach (var indexedItem in e.Items)
307        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
308      OnProblemDataChanged(e);
309    }
310    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
311      foreach (var indexedItem in e.OldItems)
312        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
313      OnProblemDataChanged(e);
314    }
315    private void InputVariable_ValueChanged(object sender, EventArgs e) {
316      OnProblemDataChanged(e);
317    }
318
319    #region helper
320    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
321      value.ValueChanged += new EventHandler(value_ValueChanged);
322    }
323    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
324      value.ValueChanged -= new EventHandler(value_ValueChanged);
325    }
326    private void RegisterStringValueEventHandlers(StringValue value) {
327      value.ValueChanged += new EventHandler(value_ValueChanged);
328    }
329    private void DeregisterStringValueEventHandlers(StringValue value) {
330      value.ValueChanged -= new EventHandler(value_ValueChanged);
331    }
332
333    private void value_ValueChanged(object sender, EventArgs e) {
334      OnProblemDataChanged(e);
335    }
336    #endregion
337    #endregion
338
339    public virtual void ImportFromFile(string fileName) {
340      var csvFileParser = new CsvFileParser();
341      csvFileParser.Parse(fileName);
342      suppressEvents = true;
343      Name = "Data imported from " + Path.GetFileName(fileName);
344      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
345      Dataset.Name = Path.GetFileName(fileName);
346      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
347      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
348      foreach (var variableName in variableNames)
349        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
350      TargetVariable = variableNames.First();
351      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
352      InputVariables.SetItemCheckedState(variableNames.First(), false);
353      int middle = (int)(csvFileParser.Rows * 0.5);
354      TrainingSamplesStart = new IntValue(0);
355      TrainingSamplesEnd = new IntValue(middle);
356      TestSamplesStart = new IntValue(middle);
357      TestSamplesEnd = new IntValue(csvFileParser.Rows);
358      suppressEvents = false;
359      OnProblemDataChanged(EventArgs.Empty);
360    }
361
362    public override IDeepCloneable Clone(Cloner cloner) {
363      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
364      clone.RegisterParameterEventHandlers();
365      clone.RegisterParameterValueEventHandlers();
366      return clone;
367    }
368  }
369}
Note: See TracBrowser for help on using the repository browser.