Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/DataAnalysisProblemData.cs @ 5540

Last change on this file since 5540 was 5540, checked in by gkronber, 12 years ago

#1418 added implementations of DataAnalysisProblem and RegressionProblem

File size: 11.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [StorableClass]
34  public abstract class DataAnalysisProblemData : ParameterizedNamedItem,
35    IDataAnalysisProblemData {
36    private const string DatasetParameterName = "Dataset";
37    private const string InputVariablesParameterName = "Input variables";
38    private const string TrainingSamplesStartParameterName = "Training partition start";
39    private const string TrainingSamplesEndParameterName = "Training partition end";
40    private const string TestSamplesStartParameterName = "Test partition start";
41    private const string TestSamplesEndParameterName = "Test partition end";
42
43    #region default data
44    // y = x^4 + x^3 + x^2 + x
45    private static double[,] kozaF1 = new double[,] {
46{2.017885919, -1.449165046},
47{1.30060506,  -1.344523885},
48{1.147134798, -1.317989331},
49{0.877182504, -1.266142284},
50{0.852562452, -1.261020794},
51{0.431095788, -1.158793317},
52{0.112586002, -1.050908405},
53{0.04594507,  -1.021989402},
54{0.042572879, -1.020438113},
55{-0.074027291,  -0.959859562},
56{-0.109178553,  -0.938094706},
57{-0.259721109,  -0.803635355},
58{-0.272991057,  -0.387519561},
59{-0.161978191,  -0.193611001},
60{-0.102489983,  -0.114215349},
61{-0.01469968, -0.014918985},
62{-0.008863365,  -0.008942626},
63{0.026751057, 0.026054094},
64{0.166922436, 0.14309643},
65{0.176953808, 0.1504144},
66{0.190233418, 0.159916534},
67{0.199800708, 0.166635331},
68{0.261502822, 0.207600348},
69{0.30182879,  0.232370249},
70{0.83763905,  0.468046718}
71    };
72    #endregion
73
74    #region parameter properties
75    public IValueParameter<Dataset> DatasetParameter {
76      get { return (IValueParameter<Dataset>)Parameters[DatasetParameterName]; }
77    }
78    public IValueParameter<CheckedItemList<StringValue>> InputVariablesParameter {
79      get { return (IValueParameter<CheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
80    }
81    public IValueParameter<IntValue> TrainingSamplesStartParameter {
82      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesStartParameterName]; }
83    }
84    public IValueParameter<IntValue> TrainingSamplesEndParameter {
85      get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesEndParameterName]; }
86    }
87    public IValueParameter<IntValue> TestSamplesStartParameter {
88      get { return (IValueParameter<IntValue>)Parameters[TestSamplesStartParameterName]; }
89    }
90    public IValueParameter<IntValue> TestSamplesEndParameter {
91      get { return (IValueParameter<IntValue>)Parameters[TestSamplesEndParameterName]; }
92    }
93    #endregion
94    #region propeties
95    public Dataset Dataset {
96      get { return DatasetParameter.Value; }
97    }
98
99    public IEnumerable<string> InputVariables {
100      get { return InputVariablesParameter.Value.CheckedItems.Select(i => i.Value.Value); }
101    }
102
103    public int TrainingSamplesStart {
104      get { return TrainingSamplesStartParameter.Value.Value; }
105      set {
106        if (value != TrainingSamplesStart) {
107          TrainingSamplesStartParameter.Value.Value = value;
108        }
109      }
110    }
111
112    public int TrainingSamplesEnd {
113      get { return TrainingSamplesEndParameter.Value.Value; }
114      set {
115        if (value != TrainingSamplesEnd) {
116          TrainingSamplesEndParameter.Value.Value = value;
117        }
118      }
119    }
120
121    public int TestSamplesStart {
122      get { return TestSamplesStartParameter.Value.Value; }
123      set {
124        if (value != TestSamplesStart) {
125          TestSamplesStartParameter.Value.Value = value;
126        }
127      }
128    }
129
130    public int TestSamplesEnd {
131      get { return TestSamplesEndParameter.Value.Value; }
132      set {
133        if (value != TestSamplesEnd) {
134          TestSamplesEndParameter.Value.Value = value;
135        }
136      }
137    }
138
139    public event EventHandler Changed;
140    #endregion
141
142    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
143      : base(original, cloner) {
144      RegisterEventHandlers();
145    }
146    [StorableConstructor]
147    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
148    public DataAnalysisProblemData()
149      : base() {
150      List<string> variableNames = new List<string>() { "x", "f(x)" };
151      Dataset kozaF1Dataset = new Dataset(variableNames, kozaF1);
152      kozaF1Dataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
153      kozaF1Dataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
154      CheckedItemList<StringValue> inputVariablesList = new CheckedItemList<StringValue>();
155      StringValue x = new StringValue(variableNames[0]);
156      StringValue fx = new StringValue(variableNames[1]);
157      inputVariablesList.Add(x, true);
158      inputVariablesList.Add(fx, false);
159      Parameters.Add(new ValueParameter<Dataset>(DatasetParameterName, kozaF1Dataset));
160      Parameters.Add(new ValueParameter<CheckedItemList<StringValue>>(InputVariablesParameterName, inputVariablesList));
161      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesStartParameterName, new IntValue(0)));
162      Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, new IntValue(kozaF1Dataset.Rows)));
163      Parameters.Add(new ValueParameter<IntValue>(TestSamplesStartParameterName, new IntValue(kozaF1Dataset.Rows)));
164      Parameters.Add(new ValueParameter<IntValue>(TestSamplesEndParameterName, new IntValue(kozaF1Dataset.Rows)));
165
166      RegisterEventHandlers();
167    }
168
169    [StorableHook(HookType.AfterDeserialization)]
170    private void AfterDeserialization() {
171      RegisterEventHandlers();
172    }
173
174    #region changed event propagation
175    private void RegisterEventHandlers() {
176      DatasetParameter.Value.Reset += new EventHandler(DatasetChanged);
177      DatasetParameter.Value.ColumnsChanged += new EventHandler(DatasetChanged);
178      DatasetParameter.Value.RowsChanged += new EventHandler(DatasetChanged);
179      InputVariablesParameter.Value.CheckedItemsChanged += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
180      InputVariablesParameter.Value.CollectionReset += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset);
181      InputVariablesParameter.Value.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded);
182      InputVariablesParameter.Value.ItemsMoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsMoved);
183      InputVariablesParameter.Value.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
184      InputVariablesParameter.Value.ItemsReplaced += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsReplaced);
185
186      TrainingSamplesStartParameter.ValueChanged += new EventHandler(TrainingSamplesStartParameter_ValueChanged);
187      TrainingSamplesEndParameter.ValueChanged += new EventHandler(TrainingSamplesEndParameter_ValueChanged);
188      TestSamplesStartParameter.ValueChanged += new EventHandler(TestSamplesStartParameter_ValueChanged);
189      TestSamplesEndParameter.ValueChanged += new EventHandler(TestSamplesEndParameter_ValueChanged);
190
191      TrainingSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
192      TrainingSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
193      TestSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
194      TestSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
195    }
196
197    private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
198      TestSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
199    }
200
201    private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
202      TestSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
203    }
204
205    private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
206      TrainingSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
207    }
208
209    private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
210      TrainingSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
211    }
212
213    private void Partitions_ValueChanged(object sender, EventArgs e) {
214      OnProblemChanged();
215    }
216
217    private void InputVariables_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) {
218      OnProblemChanged();
219    }
220
221    private void InputVariables_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) {
222      OnProblemChanged();
223    }
224
225    private void InputVariables_ItemsMoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) {
226      OnProblemChanged();
227    }
228
229    private void InputVariables_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) {
230      OnProblemChanged();
231    }
232
233    private void InputVariables_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) {
234      OnProblemChanged();
235    }
236
237    private void InputVariables_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) {
238      OnProblemChanged();
239    }
240
241    private void DatasetChanged(object sender, EventArgs e) {
242      OnProblemChanged();
243    }
244
245    protected void OnProblemChanged() {
246      var listeners = Changed;
247      if (listeners != null) listeners(this, EventArgs.Empty);
248    }
249    #endregion
250  }
251}
Note: See TracBrowser for help on using the repository browser.