Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5138 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
RevLine 
[3294]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;
[4068]24using System.IO;
[3294]25using System.Linq;
[3376]26using HeuristicLab.Common;
[3294]27using HeuristicLab.Core;
[4068]28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
[3294]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[3373]32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("DataAnalysisProblemData", "Represents an item containing all data defining a data analysis problem.")]
[3294]34  [StorableClass]
[4457]35  public class DataAnalysisProblemData : ParameterizedNamedItem, IStorableContent {
[3545]36    private bool suppressEvents = false;
[4457]37
38    #region IStorableContent Members
39    public string Filename { get; set; }
40    #endregion
41
[3723]42    #region default data
43    // y = x^4 + x^3 + x^2 + x
[4022]44    private static double[,] kozaF1 = new double[,] {
[3723]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
[3545]72    #region parameter properties
73    public IValueParameter<Dataset> DatasetParameter {
74      get { return (IValueParameter<Dataset>)Parameters["Dataset"]; }
[3442]75    }
[3545]76    public IValueParameter<StringValue> TargetVariableParameter {
77      get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; }
[3442]78    }
[3628]79    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
80      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
[3442]81    }
[3545]82    public IValueParameter<IntValue> TrainingSamplesStartParameter {
83      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; }
[3442]84    }
[3545]85    public IValueParameter<IntValue> TrainingSamplesEndParameter {
86      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesEnd"]; }
[3442]87    }
[3545]88    public IValueParameter<IntValue> TestSamplesStartParameter {
89      get { return (IValueParameter<IntValue>)Parameters["TestSamplesStart"]; }
[3442]90    }
[3545]91    public IValueParameter<IntValue> TestSamplesEndParameter {
92      get { return (IValueParameter<IntValue>)Parameters["TestSamplesEnd"]; }
[3442]93    }
94    #endregion
95
[3294]96    #region properties
97    public Dataset Dataset {
[3545]98      get { return (Dataset)DatasetParameter.Value; }
[3309]99      set {
[3442]100        if (value != Dataset) {
[3309]101          if (value == null) throw new ArgumentNullException();
[3545]102          DatasetParameter.Value = value;
[3309]103        }
104      }
[3294]105    }
106    public StringValue TargetVariable {
[3545]107      get { return (StringValue)TargetVariableParameter.Value; }
[3442]108      set {
[3545]109        if (value != TargetVariableParameter.Value) {
[3442]110          if (value == null) throw new ArgumentNullException();
111          if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
[3545]112          TargetVariableParameter.Value = value;
[3442]113        }
114      }
[3294]115    }
[3628]116    public ICheckedItemList<StringValue> InputVariables {
117      get { return (ICheckedItemList<StringValue>)InputVariablesParameter.Value; }
[3294]118      set {
[3442]119        if (value != InputVariables) {
[3294]120          if (value == null) throw new ArgumentNullException();
[3442]121          if (InputVariables != null) DeregisterInputVariablesEventHandlers();
[3545]122          InputVariablesParameter.Value = value;
[3294]123        }
124      }
125    }
126    public IntValue TrainingSamplesStart {
[3545]127      get { return (IntValue)TrainingSamplesStartParameter.Value; }
[3442]128      set {
129        if (value != TrainingSamplesStart) {
130          if (value == null) throw new ArgumentNullException();
131          if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
[3545]132          TrainingSamplesStartParameter.Value = value;
[3442]133        }
134      }
[3294]135    }
136    public IntValue TrainingSamplesEnd {
[3545]137      get { return (IntValue)TrainingSamplesEndParameter.Value; }
[3442]138      set {
139        if (value != TrainingSamplesEnd) {
140          if (value == null) throw new ArgumentNullException();
141          if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
[3545]142          TrainingSamplesEndParameter.Value = value;
[3442]143        }
144      }
[3294]145    }
146    public IntValue TestSamplesStart {
[3545]147      get { return (IntValue)TestSamplesStartParameter.Value; }
[3442]148      set {
149        if (value != TestSamplesStart) {
150          if (value == null) throw new ArgumentNullException();
151          if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
[3545]152          TestSamplesStartParameter.Value = value;
[3442]153        }
154      }
[3294]155    }
156    public IntValue TestSamplesEnd {
[3545]157      get { return (IntValue)TestSamplesEndParameter.Value; }
[3442]158      set {
159        if (value != TestSamplesEnd) {
160          if (value == null) throw new ArgumentNullException();
161          if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd);
[3545]162          TestSamplesEndParameter.Value = value;
[3442]163        }
164      }
[3294]165    }
166    #endregion
167
[3373]168    public DataAnalysisProblemData()
[3294]169      : base() {
[3723]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)));
[3545]183      RegisterParameterEventHandlers();
184      RegisterParameterValueEventHandlers();
[3294]185    }
186
[4022]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    }
[3545]206
[3294]207    [StorableConstructor]
[4341]208    private DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
[3294]209
[3442]210    [StorableHook(HookType.AfterDeserialization)]
211    private void AfterDeserializationHook() {
[3545]212      RegisterParameterEventHandlers();
213      RegisterParameterValueEventHandlers();
[3442]214    }
215
[3294]216    #region events
[3545]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() {
[3442]236      RegisterInputVariablesEventHandlers();
[3545]237      if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable);
[3442]238      RegisterValueTypeEventHandlers(TrainingSamplesStart);
239      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
240      RegisterValueTypeEventHandlers(TestSamplesStart);
241      RegisterValueTypeEventHandlers(TestSamplesEnd);
242    }
243
[3309]244
[3545]245    #region parameter value changed event handlers
[4341]246    private void DatasetParameter_ValueChanged(object sender, EventArgs e) {
[3545]247      OnProblemDataChanged(EventArgs.Empty);
[3309]248    }
[4341]249    private void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
[3545]250      RegisterInputVariablesEventHandlers();
251      OnProblemDataChanged(EventArgs.Empty);
[3442]252    }
[4341]253    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
[3545]254      if (TargetVariable != null) {
255        RegisterStringValueEventHandlers(TargetVariable);
256        OnProblemDataChanged(EventArgs.Empty);
[3651]257      }
[3442]258    }
[4341]259    private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
[3545]260      RegisterValueTypeEventHandlers(TrainingSamplesStart);
261      OnProblemDataChanged(EventArgs.Empty);
[3442]262    }
[4341]263    private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
[3545]264      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
265      OnProblemDataChanged(EventArgs.Empty);
[3442]266    }
[4341]267    private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
[3545]268      RegisterValueTypeEventHandlers(TestSamplesStart);
269      OnProblemDataChanged(EventArgs.Empty);
270    }
[4341]271    private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
[3545]272      RegisterValueTypeEventHandlers(TestSamplesEnd);
273      OnProblemDataChanged(EventArgs.Empty);
274    }
275    #endregion
[3442]276
277    private void RegisterInputVariablesEventHandlers() {
[3651]278      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
[3599]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);
[4341]282      foreach (var item in InputVariables) {
[3545]283        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
[4341]284      }
[3442]285    }
286
[3599]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    }
[3651]296
[3599]297    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
[3559]298      OnProblemDataChanged(e);
299    }
[3599]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);
[3545]303      OnProblemDataChanged(e);
[3442]304    }
[3599]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);
[3442]308      OnProblemDataChanged(e);
309    }
[3599]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);
[3442]313      OnProblemDataChanged(e);
314    }
[4341]315    private void InputVariable_ValueChanged(object sender, EventArgs e) {
[3442]316      OnProblemDataChanged(e);
317    }
[4341]318
[3545]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);
[3442]325    }
[3545]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
[4341]333    private void value_ValueChanged(object sender, EventArgs e) {
334      OnProblemDataChanged(e);
335    }
[3294]336    #endregion
[3545]337    #endregion
[3294]338
339    public virtual void ImportFromFile(string fileName) {
340      var csvFileParser = new CsvFileParser();
341      csvFileParser.Parse(fileName);
[3545]342      suppressEvents = true;
[3373]343      Name = "Data imported from " + Path.GetFileName(fileName);
[3294]344      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
345      Dataset.Name = Path.GetFileName(fileName);
[3545]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();
[3628]351      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
[3651]352      InputVariables.SetItemCheckedState(variableNames.First(), false);
[3442]353      int middle = (int)(csvFileParser.Rows * 0.5);
[3294]354      TrainingSamplesStart = new IntValue(0);
[3442]355      TrainingSamplesEnd = new IntValue(middle);
356      TestSamplesStart = new IntValue(middle);
[3294]357      TestSamplesEnd = new IntValue(csvFileParser.Rows);
[3545]358      suppressEvents = false;
359      OnProblemDataChanged(EventArgs.Empty);
[3294]360    }
[3442]361
362    public override IDeepCloneable Clone(Cloner cloner) {
363      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
[3545]364      clone.RegisterParameterEventHandlers();
365      clone.RegisterParameterValueEventHandlers();
[3442]366      return clone;
367    }
[3294]368  }
369}
Note: See TracBrowser for help on using the repository browser.