Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3916 was 3723, checked in by gkronber, 15 years ago

Added default problem data for symbolic regression problems. #938 (Data types and operators for regression problems)

File size: 16.9 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 {
35  [Item("DataAnalysisProblemData", "Represents an item containing all data defining a data analysis problem.")]
36  [StorableClass]
37  public class DataAnalysisProblemData : ParameterizedNamedItem {
38    private bool suppressEvents = false;
39    #region default data
40    // y = x^4 + x^3 + x^2 + x
41    private readonly double[,] kozaF1 = new double[,] {
42{2.017885919, -1.449165046},
43{1.30060506,  -1.344523885},
44{1.147134798, -1.317989331},
45{0.877182504, -1.266142284},
46{0.852562452, -1.261020794},
47{0.431095788, -1.158793317},
48{0.112586002, -1.050908405},
49{0.04594507,  -1.021989402},
50{0.042572879, -1.020438113},
51{-0.074027291,  -0.959859562},
52{-0.109178553,  -0.938094706},
53{-0.259721109,  -0.803635355},
54{-0.272991057,  -0.387519561},
55{-0.161978191,  -0.193611001},
56{-0.102489983,  -0.114215349},
57{-0.01469968, -0.014918985},
58{-0.008863365,  -0.008942626},
59{0.026751057, 0.026054094},
60{0.166922436, 0.14309643},
61{0.176953808, 0.1504144},
62{0.190233418, 0.159916534},
63{0.199800708, 0.166635331},
64{0.261502822, 0.207600348},
65{0.30182879,  0.232370249},
66{0.83763905,  0.468046718}
67    };
68    #endregion
69    #region parameter properties
70    public IValueParameter<Dataset> DatasetParameter {
71      get { return (IValueParameter<Dataset>)Parameters["Dataset"]; }
72    }
73
74    public IValueParameter<StringValue> TargetVariableParameter {
75      get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; }
76    }
77
78    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
79      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
80    }
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          if (Dataset != null) DeregisterDatasetEventHandlers();
103          DatasetParameter.Value = value;
104        }
105      }
106    }
107    public StringValue TargetVariable {
108      get { return (StringValue)TargetVariableParameter.Value; }
109      set {
110        if (value != TargetVariableParameter.Value) {
111          if (value == null) throw new ArgumentNullException();
112          if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
113          TargetVariableParameter.Value = value;
114        }
115      }
116    }
117    public ICheckedItemList<StringValue> InputVariables {
118      get { return (ICheckedItemList<StringValue>)InputVariablesParameter.Value; }
119      set {
120        if (value != InputVariables) {
121          if (value == null) throw new ArgumentNullException();
122          if (InputVariables != null) DeregisterInputVariablesEventHandlers();
123          InputVariablesParameter.Value = value;
124        }
125      }
126    }
127    public IntValue TrainingSamplesStart {
128      get { return (IntValue)TrainingSamplesStartParameter.Value; }
129      set {
130        if (value != TrainingSamplesStart) {
131          if (value == null) throw new ArgumentNullException();
132          if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
133          TrainingSamplesStartParameter.Value = value;
134        }
135      }
136    }
137    public IntValue TrainingSamplesEnd {
138      get { return (IntValue)TrainingSamplesEndParameter.Value; }
139      set {
140        if (value != TrainingSamplesEnd) {
141          if (value == null) throw new ArgumentNullException();
142          if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
143          TrainingSamplesEndParameter.Value = value;
144        }
145      }
146    }
147    public IntValue TestSamplesStart {
148      get { return (IntValue)TestSamplesStartParameter.Value; }
149      set {
150        if (value != TestSamplesStart) {
151          if (value == null) throw new ArgumentNullException();
152          if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
153          TestSamplesStartParameter.Value = value;
154        }
155      }
156    }
157    public IntValue TestSamplesEnd {
158      get { return (IntValue)TestSamplesEndParameter.Value; }
159      set {
160        if (value != TestSamplesEnd) {
161          if (value == null) throw new ArgumentNullException();
162          if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd);
163          TestSamplesEndParameter.Value = value;
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
189    [StorableConstructor]
190    private DataAnalysisProblemData(bool deserializing) : base() { }
191
192    [StorableHook(HookType.AfterDeserialization)]
193    private void AfterDeserializationHook() {
194      RegisterParameterEventHandlers();
195      RegisterParameterValueEventHandlers();
196    }
197
198    #region events
199    public event EventHandler ProblemDataChanged;
200    protected virtual void OnProblemDataChanged(EventArgs e) {
201      if (!suppressEvents) {
202        var listeners = ProblemDataChanged;
203        if (listeners != null) listeners(this, e);
204      }
205    }
206
207    private void RegisterParameterEventHandlers() {
208      DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_ValueChanged);
209      InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
210      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
211      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
212      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
213      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
214      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
215    }
216
217    private void RegisterParameterValueEventHandlers() {
218      RegisterDatasetEventHandlers();
219      RegisterInputVariablesEventHandlers();
220      if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable);
221      RegisterValueTypeEventHandlers(TrainingSamplesStart);
222      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
223      RegisterValueTypeEventHandlers(TestSamplesStart);
224      RegisterValueTypeEventHandlers(TestSamplesEnd);
225    }
226
227
228    #region parameter value changed event handlers
229    void DatasetParameter_ValueChanged(object sender, EventArgs e) {
230      RegisterDatasetEventHandlers();
231      OnProblemDataChanged(EventArgs.Empty);
232    }
233    void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
234      RegisterInputVariablesEventHandlers();
235      OnProblemDataChanged(EventArgs.Empty);
236    }
237    void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
238      if (TargetVariable != null) {
239        RegisterStringValueEventHandlers(TargetVariable);
240        OnProblemDataChanged(EventArgs.Empty);
241      }
242    }
243    void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
244      RegisterValueTypeEventHandlers(TrainingSamplesStart);
245      OnProblemDataChanged(EventArgs.Empty);
246    }
247    void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
248      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
249      OnProblemDataChanged(EventArgs.Empty);
250    }
251    void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
252      RegisterValueTypeEventHandlers(TestSamplesStart);
253      OnProblemDataChanged(EventArgs.Empty);
254    }
255    void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
256      RegisterValueTypeEventHandlers(TestSamplesEnd);
257      OnProblemDataChanged(EventArgs.Empty);
258    }
259    #endregion
260
261
262    private void RegisterDatasetEventHandlers() {
263      Dataset.DataChanged += new EventHandler<EventArgs<int, int>>(Dataset_DataChanged);
264      Dataset.Reset += new EventHandler(Dataset_Reset);
265      Dataset.ColumnNamesChanged += new EventHandler(Dataset_ColumnNamesChanged);
266    }
267
268    private void DeregisterDatasetEventHandlers() {
269      Dataset.DataChanged -= new EventHandler<EventArgs<int, int>>(Dataset_DataChanged);
270      Dataset.Reset -= new EventHandler(Dataset_Reset);
271      Dataset.ColumnNamesChanged -= new EventHandler(Dataset_ColumnNamesChanged);
272    }
273
274    void Dataset_ColumnNamesChanged(object sender, EventArgs e) {
275      OnProblemDataChanged(e);
276    }
277
278    void Dataset_Reset(object sender, EventArgs e) {
279      OnProblemDataChanged(e);
280    }
281
282    void Dataset_DataChanged(object sender, EventArgs<int, int> e) {
283      OnProblemDataChanged(e);
284    }
285
286    private void RegisterInputVariablesEventHandlers() {
287      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
288      InputVariables.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
289      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
290      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
291      foreach (var item in InputVariables)
292        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
293    }
294
295    private void DeregisterInputVariablesEventHandlers() {
296      InputVariables.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
297      InputVariables.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
298      InputVariables.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
299      InputVariables.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
300      foreach (var item in InputVariables) {
301        item.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
302      }
303    }
304
305    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
306      OnProblemDataChanged(e);
307    }
308
309    private void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
310      foreach (var indexedItem in e.Items)
311        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
312      OnProblemDataChanged(e);
313    }
314
315    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
316      foreach (var indexedItem in e.Items)
317        indexedItem.Value.ValueChanged += new EventHandler(InputVariable_ValueChanged);
318      OnProblemDataChanged(e);
319    }
320
321    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
322      foreach (var indexedItem in e.OldItems)
323        indexedItem.Value.ValueChanged -= new EventHandler(InputVariable_ValueChanged);
324      OnProblemDataChanged(e);
325    }
326
327    void InputVariable_ValueChanged(object sender, EventArgs e) {
328      OnProblemDataChanged(e);
329    }
330    #region helper
331
332    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
333      value.ValueChanged += new EventHandler(value_ValueChanged);
334    }
335
336    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
337      value.ValueChanged -= new EventHandler(value_ValueChanged);
338    }
339
340    void value_ValueChanged(object sender, EventArgs e) {
341      OnProblemDataChanged(e);
342    }
343
344    private void RegisterStringValueEventHandlers(StringValue value) {
345      value.ValueChanged += new EventHandler(value_ValueChanged);
346    }
347
348    private void DeregisterStringValueEventHandlers(StringValue value) {
349      value.ValueChanged -= new EventHandler(value_ValueChanged);
350    }
351
352    #endregion
353    #endregion
354
355    public virtual void ImportFromFile(string fileName) {
356      var csvFileParser = new CsvFileParser();
357      csvFileParser.Parse(fileName);
358      suppressEvents = true;
359      Name = "Data imported from " + Path.GetFileName(fileName);
360      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
361      Dataset.Name = Path.GetFileName(fileName);
362      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
363      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
364      foreach (var variableName in variableNames)
365        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
366      TargetVariable = variableNames.First();
367      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
368      InputVariables.SetItemCheckedState(variableNames.First(), false);
369      int middle = (int)(csvFileParser.Rows * 0.5);
370      TrainingSamplesStart = new IntValue(0);
371      TrainingSamplesEnd = new IntValue(middle);
372      TestSamplesStart = new IntValue(middle);
373      TestSamplesEnd = new IntValue(csvFileParser.Rows);
374      suppressEvents = false;
375      OnProblemDataChanged(EventArgs.Empty);
376    }
377
378    public override IDeepCloneable Clone(Cloner cloner) {
379      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
380      clone.RegisterParameterEventHandlers();
381      clone.RegisterParameterValueEventHandlers();
382      return clone;
383    }
384  }
385}
Note: See TracBrowser for help on using the repository browser.