Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisProblemData.cs @ 4250

Last change on this file since 4250 was 4250, checked in by mkommend, 12 years ago

Adapted SymbolicRegression classes to new grammars (ticket #1028).

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