Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 17.4 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]
[3545]35  public class DataAnalysisProblemData : ParameterizedNamedItem {
36    private bool suppressEvents = false;
[3723]37    #region default data
38    // y = x^4 + x^3 + x^2 + x
[4022]39    private static double[,] kozaF1 = new double[,] {
[3723]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
[3545]67    #region parameter properties
68    public IValueParameter<Dataset> DatasetParameter {
69      get { return (IValueParameter<Dataset>)Parameters["Dataset"]; }
[3442]70    }
[3545]71    public IValueParameter<StringValue> TargetVariableParameter {
72      get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; }
[3442]73    }
[3628]74    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
75      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
[3442]76    }
[3545]77    public IValueParameter<IntValue> TrainingSamplesStartParameter {
78      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; }
[3442]79    }
[3545]80    public IValueParameter<IntValue> TrainingSamplesEndParameter {
81      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesEnd"]; }
[3442]82    }
[3545]83    public IValueParameter<IntValue> TestSamplesStartParameter {
84      get { return (IValueParameter<IntValue>)Parameters["TestSamplesStart"]; }
[3442]85    }
[3545]86    public IValueParameter<IntValue> TestSamplesEndParameter {
87      get { return (IValueParameter<IntValue>)Parameters["TestSamplesEnd"]; }
[3442]88    }
89    #endregion
90
[3294]91    #region properties
92    public Dataset Dataset {
[3545]93      get { return (Dataset)DatasetParameter.Value; }
[3309]94      set {
[3442]95        if (value != Dataset) {
[3309]96          if (value == null) throw new ArgumentNullException();
[3545]97          DatasetParameter.Value = value;
[3309]98        }
99      }
[3294]100    }
101    public StringValue TargetVariable {
[3545]102      get { return (StringValue)TargetVariableParameter.Value; }
[3442]103      set {
[3545]104        if (value != TargetVariableParameter.Value) {
[3442]105          if (value == null) throw new ArgumentNullException();
106          if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
[3545]107          TargetVariableParameter.Value = value;
[4250]108          RegisterStringValueEventHandlers(TargetVariable);
[3442]109        }
110      }
[3294]111    }
[3628]112    public ICheckedItemList<StringValue> InputVariables {
113      get { return (ICheckedItemList<StringValue>)InputVariablesParameter.Value; }
[3294]114      set {
[3442]115        if (value != InputVariables) {
[3294]116          if (value == null) throw new ArgumentNullException();
[3442]117          if (InputVariables != null) DeregisterInputVariablesEventHandlers();
[3545]118          InputVariablesParameter.Value = value;
[4250]119          RegisterInputVariablesEventHandlers();
[3294]120        }
121      }
122    }
123    public IntValue TrainingSamplesStart {
[3545]124      get { return (IntValue)TrainingSamplesStartParameter.Value; }
[3442]125      set {
126        if (value != TrainingSamplesStart) {
127          if (value == null) throw new ArgumentNullException();
128          if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
[3545]129          TrainingSamplesStartParameter.Value = value;
[4250]130          RegisterValueTypeEventHandlers(TrainingSamplesStart);
[3442]131        }
132      }
[3294]133    }
134    public IntValue TrainingSamplesEnd {
[3545]135      get { return (IntValue)TrainingSamplesEndParameter.Value; }
[3442]136      set {
137        if (value != TrainingSamplesEnd) {
138          if (value == null) throw new ArgumentNullException();
139          if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
[3545]140          TrainingSamplesEndParameter.Value = value;
[4250]141          RegisterValueTypeEventHandlers(TrainingSamplesEnd);
[3442]142        }
143      }
[3294]144    }
145    public IntValue TestSamplesStart {
[3545]146      get { return (IntValue)TestSamplesStartParameter.Value; }
[3442]147      set {
148        if (value != TestSamplesStart) {
149          if (value == null) throw new ArgumentNullException();
150          if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
[3545]151          TestSamplesStartParameter.Value = value;
[4250]152          RegisterValueTypeEventHandlers(TestSamplesStart);
[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;
[4250]163          RegisterValueTypeEventHandlers(TestSamplesEnd);
[3442]164        }
165      }
[3294]166    }
167    #endregion
168
[3373]169    public DataAnalysisProblemData()
[3294]170      : base() {
[3723]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)));
[3545]184      RegisterParameterEventHandlers();
185      RegisterParameterValueEventHandlers();
[3294]186    }
187
[4022]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    }
[3545]207
[3294]208    [StorableConstructor]
[3373]209    private DataAnalysisProblemData(bool deserializing) : base() { }
[3294]210
[3442]211    [StorableHook(HookType.AfterDeserialization)]
212    private void AfterDeserializationHook() {
[3545]213      RegisterParameterEventHandlers();
214      RegisterParameterValueEventHandlers();
[3442]215    }
216
[3294]217    #region events
[3545]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() {
[3442]237      RegisterInputVariablesEventHandlers();
[3545]238      if (TargetVariable != null) RegisterStringValueEventHandlers(TargetVariable);
[3442]239      RegisterValueTypeEventHandlers(TrainingSamplesStart);
240      RegisterValueTypeEventHandlers(TrainingSamplesEnd);
241      RegisterValueTypeEventHandlers(TestSamplesStart);
242      RegisterValueTypeEventHandlers(TestSamplesEnd);
243    }
244
[3309]245
[3545]246    #region parameter value changed event handlers
[4250]247    private void DatasetParameter_ValueChanged(object sender, EventArgs e) {
[3545]248      OnProblemDataChanged(EventArgs.Empty);
[3309]249    }
[4250]250    private void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
[3545]251      OnProblemDataChanged(EventArgs.Empty);
[3442]252    }
[4250]253    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
254      if (TargetVariable != null) OnProblemDataChanged(EventArgs.Empty);
[3442]255    }
[4250]256    private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
[3545]257      OnProblemDataChanged(EventArgs.Empty);
[3442]258    }
[4250]259    private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
[3545]260      OnProblemDataChanged(EventArgs.Empty);
[3442]261    }
[4250]262    private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
[3545]263      OnProblemDataChanged(EventArgs.Empty);
264    }
[4250]265    private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
[3545]266      OnProblemDataChanged(EventArgs.Empty);
267    }
268    #endregion
[3442]269
270    private void RegisterInputVariablesEventHandlers() {
[3651]271      InputVariables.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
[3599]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);
[4250]275      foreach (var item in InputVariables) {
[3545]276        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
[4250]277      }
[3442]278    }
279
[3599]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    }
[3651]289
[3599]290    private void InputVariables_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
[3559]291      OnProblemDataChanged(e);
292    }
[3599]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);
[3545]296      OnProblemDataChanged(e);
[3442]297    }
[3599]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);
[3442]301      OnProblemDataChanged(e);
302    }
[3599]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);
[3442]306      OnProblemDataChanged(e);
307    }
[4250]308    private void InputVariable_ValueChanged(object sender, EventArgs e) {
[3442]309      OnProblemDataChanged(e);
310    }
[4250]311
[3545]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);
[3442]318    }
[3545]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
[4250]326    private void value_ValueChanged(object sender, EventArgs e) {
327      OnProblemDataChanged(e);
328    }
[3294]329    #endregion
[3545]330    #endregion
[3294]331
332    public virtual void ImportFromFile(string fileName) {
333      var csvFileParser = new CsvFileParser();
334      csvFileParser.Parse(fileName);
[3545]335      suppressEvents = true;
[3373]336      Name = "Data imported from " + Path.GetFileName(fileName);
[3294]337      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
338      Dataset.Name = Path.GetFileName(fileName);
[3545]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();
[3628]344      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
[3651]345      InputVariables.SetItemCheckedState(variableNames.First(), false);
[3442]346      int middle = (int)(csvFileParser.Rows * 0.5);
[3294]347      TrainingSamplesStart = new IntValue(0);
[3442]348      TrainingSamplesEnd = new IntValue(middle);
349      TestSamplesStart = new IntValue(middle);
[3294]350      TestSamplesEnd = new IntValue(csvFileParser.Rows);
[3545]351      suppressEvents = false;
352      OnProblemDataChanged(EventArgs.Empty);
[3294]353    }
[3442]354
355    public override IDeepCloneable Clone(Cloner cloner) {
356      DataAnalysisProblemData clone = (DataAnalysisProblemData)base.Clone(cloner);
[3545]357      clone.RegisterParameterEventHandlers();
358      clone.RegisterParameterValueEventHandlers();
[3442]359      return clone;
360    }
[3294]361  }
362}
Note: See TracBrowser for help on using the repository browser.