1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
33 | [StorableClass]
|
---|
34 | public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
|
---|
35 | protected const string DatasetParameterName = "Dataset";
|
---|
36 | protected const string InputVariablesParameterName = "InputVariables";
|
---|
37 | protected const string TrainingPartitionParameterName = "TrainingPartition";
|
---|
38 | protected const string TestPartitionParameterName = "TestPartition";
|
---|
39 | protected const string TransformationsParameterName = "Transformations";
|
---|
40 |
|
---|
41 | #region parameter properites
|
---|
42 | public IFixedValueParameter<Dataset> DatasetParameter {
|
---|
43 | get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
|
---|
44 | }
|
---|
45 | public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
|
---|
46 | get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
|
---|
47 | }
|
---|
48 | public IFixedValueParameter<IntRange> TrainingPartitionParameter {
|
---|
49 | get { return (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
|
---|
50 | }
|
---|
51 | public IFixedValueParameter<IntRange> TestPartitionParameter {
|
---|
52 | get { return (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName]; }
|
---|
53 | }
|
---|
54 | public IFixedValueParameter<ReadOnlyItemCollection<ITransformation>> TransformationsParameter {
|
---|
55 | get { return (IFixedValueParameter<ReadOnlyItemCollection<ITransformation>>)Parameters[TransformationsParameterName]; }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | #region properties
|
---|
60 | protected bool isEmpty = false;
|
---|
61 | public bool IsEmpty {
|
---|
62 | get { return isEmpty; }
|
---|
63 | }
|
---|
64 | public Dataset Dataset {
|
---|
65 | get { return DatasetParameter.Value; }
|
---|
66 | }
|
---|
67 | public ICheckedItemList<StringValue> InputVariables {
|
---|
68 | get { return InputVariablesParameter.Value; }
|
---|
69 | }
|
---|
70 | public IEnumerable<string> AllowedInputVariables {
|
---|
71 | get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public IntRange TrainingPartition {
|
---|
75 | get { return TrainingPartitionParameter.Value; }
|
---|
76 | }
|
---|
77 | public IntRange TestPartition {
|
---|
78 | get { return TestPartitionParameter.Value; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public virtual IEnumerable<int> TrainingIndices {
|
---|
82 | get {
|
---|
83 | return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
|
---|
84 | .Where(IsTrainingSample);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | public virtual IEnumerable<int> TestIndices {
|
---|
88 | get {
|
---|
89 | return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
|
---|
90 | .Where(IsTestSample);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IEnumerable<ITransformation> Transformations {
|
---|
95 | get { return TransformationsParameter.Value; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public virtual bool IsTrainingSample(int index) {
|
---|
99 | return index >= 0 && index < Dataset.Rows &&
|
---|
100 | TrainingPartition.Start <= index && index < TrainingPartition.End &&
|
---|
101 | (index < TestPartition.Start || TestPartition.End <= index);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public virtual bool IsTestSample(int index) {
|
---|
105 | return index >= 0 && index < Dataset.Rows &&
|
---|
106 | TestPartition.Start <= index && index < TestPartition.End;
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
|
---|
111 | : base(original, cloner) {
|
---|
112 | isEmpty = original.isEmpty;
|
---|
113 | RegisterEventHandlers();
|
---|
114 | }
|
---|
115 | [StorableConstructor]
|
---|
116 | protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
|
---|
117 |
|
---|
118 | [StorableHook(HookType.AfterDeserialization)]
|
---|
119 | private void AfterDeserialization() {
|
---|
120 | RegisterEventHandlers();
|
---|
121 |
|
---|
122 | if (!Parameters.ContainsKey(TransformationsParameterName)) {
|
---|
123 | Parameters.Add(new FixedValueParameter<ReadOnlyItemCollection<ITransformation>>(TransformationsParameterName, "", new ItemCollection<ITransformation>().AsReadOnly()));
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<ITransformation> transformations) {
|
---|
128 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
129 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
|
---|
130 |
|
---|
131 | if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
|
---|
132 | throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
|
---|
133 |
|
---|
134 | if (transformations == null) throw new ArgumentNullException("The transformations must not be null.");
|
---|
135 |
|
---|
136 | var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
|
---|
137 | foreach (StringValue x in inputVariables)
|
---|
138 | inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
|
---|
139 |
|
---|
140 | int trainingPartitionStart = 0;
|
---|
141 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
142 | int testPartitionStart = dataset.Rows / 2;
|
---|
143 | int testPartitionEnd = dataset.Rows;
|
---|
144 |
|
---|
145 | var transformationsCollection = new ItemCollection<ITransformation>(transformations);
|
---|
146 |
|
---|
147 | Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
|
---|
148 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
|
---|
149 | Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
|
---|
150 | Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
|
---|
151 | Parameters.Add(new FixedValueParameter<ReadOnlyItemCollection<ITransformation>>(TransformationsParameterName, "", transformationsCollection.AsReadOnly()));
|
---|
152 |
|
---|
153 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
154 | RegisterEventHandlers();
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void RegisterEventHandlers() {
|
---|
158 | DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
159 | InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
|
---|
160 | TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
161 | TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
165 | OnChanged();
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
169 | OnChanged();
|
---|
170 | }
|
---|
171 |
|
---|
172 | public event EventHandler Changed;
|
---|
173 | protected virtual void OnChanged() {
|
---|
174 | var listeners = Changed;
|
---|
175 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|