1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
33 | [StorableType("85AE1542-D563-434F-A760-1D181EFC2101")]
|
---|
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 | protected const string VariableRangesParameterName = "VariableRanges";
|
---|
41 |
|
---|
42 | #region parameter properites
|
---|
43 | //mkommend: inserted parameter caching due to performance reasons
|
---|
44 | private IFixedValueParameter<Dataset> datasetParameter;
|
---|
45 | public IFixedValueParameter<Dataset> DatasetParameter {
|
---|
46 | get {
|
---|
47 | if (datasetParameter == null) datasetParameter = (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName];
|
---|
48 | return datasetParameter;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public IFixedValueParameter<IntervalCollection> VariableRangesParameter => (IFixedValueParameter<IntervalCollection>)Parameters[VariableRangesParameterName];
|
---|
53 | public IntervalCollection VariableRanges => VariableRangesParameter.Value;
|
---|
54 |
|
---|
55 | private IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> inputVariablesParameter;
|
---|
56 | public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
|
---|
57 | get {
|
---|
58 | if (inputVariablesParameter == null) inputVariablesParameter = (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName];
|
---|
59 | return inputVariablesParameter;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private IFixedValueParameter<IntRange> trainingPartitionParameter;
|
---|
64 | public IFixedValueParameter<IntRange> TrainingPartitionParameter {
|
---|
65 | get {
|
---|
66 | if (trainingPartitionParameter == null) trainingPartitionParameter = (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName];
|
---|
67 | return trainingPartitionParameter;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | private IFixedValueParameter<IntRange> testPartitionParameter;
|
---|
72 | public IFixedValueParameter<IntRange> TestPartitionParameter {
|
---|
73 | get {
|
---|
74 | if (testPartitionParameter == null) testPartitionParameter = (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName];
|
---|
75 | return testPartitionParameter;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | public IFixedValueParameter<ReadOnlyItemList<ITransformation>> TransformationsParameter {
|
---|
80 | get { return (IFixedValueParameter<ReadOnlyItemList<ITransformation>>)Parameters[TransformationsParameterName]; }
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | #region properties
|
---|
85 | protected bool isEmpty = false;
|
---|
86 | public bool IsEmpty {
|
---|
87 | get { return isEmpty; }
|
---|
88 | }
|
---|
89 | public IDataset Dataset {
|
---|
90 | get { return DatasetParameter.Value; }
|
---|
91 | }
|
---|
92 | public ICheckedItemList<StringValue> InputVariables {
|
---|
93 | get { return InputVariablesParameter.Value; }
|
---|
94 | }
|
---|
95 | public IEnumerable<string> AllowedInputVariables {
|
---|
96 | get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public double[,] AllowedInputsTrainingValues {
|
---|
100 | get { return Dataset.ToArray(AllowedInputVariables, TrainingIndices); }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public double[,] AllowedInputsTestValues { get { return Dataset.ToArray(AllowedInputVariables, TestIndices); } }
|
---|
104 | public IntRange TrainingPartition {
|
---|
105 | get { return TrainingPartitionParameter.Value; }
|
---|
106 | }
|
---|
107 | public IntRange TestPartition {
|
---|
108 | get { return TestPartitionParameter.Value; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public virtual IEnumerable<int> AllIndices {
|
---|
112 | get { return Enumerable.Range(0, Dataset.Rows); }
|
---|
113 | }
|
---|
114 | public virtual IEnumerable<int> TrainingIndices {
|
---|
115 | get {
|
---|
116 | return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
|
---|
117 | .Where(IsTrainingSample);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | public virtual IEnumerable<int> TestIndices {
|
---|
121 | get {
|
---|
122 | return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
|
---|
123 | .Where(IsTestSample);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public IEnumerable<ITransformation> Transformations {
|
---|
128 | get { return TransformationsParameter.Value; }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public virtual bool IsTrainingSample(int index) {
|
---|
132 | return index >= 0 && index < Dataset.Rows &&
|
---|
133 | TrainingPartition.Start <= index && index < TrainingPartition.End &&
|
---|
134 | (index < TestPartition.Start || TestPartition.End <= index);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public virtual bool IsTestSample(int index) {
|
---|
138 | return index >= 0 && index < Dataset.Rows &&
|
---|
139 | TestPartition.Start <= index && index < TestPartition.End;
|
---|
140 | }
|
---|
141 | #endregion
|
---|
142 |
|
---|
143 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
|
---|
144 | : base(original, cloner) {
|
---|
145 | isEmpty = original.isEmpty;
|
---|
146 | RegisterEventHandlers();
|
---|
147 | }
|
---|
148 | [StorableConstructor]
|
---|
149 | protected DataAnalysisProblemData(StorableConstructorFlag _) : base(_) { }
|
---|
150 |
|
---|
151 | [StorableHook(HookType.AfterDeserialization)]
|
---|
152 | private void AfterDeserialization() {
|
---|
153 | if (!Parameters.ContainsKey(TransformationsParameterName)) {
|
---|
154 | Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", new ItemList<ITransformation>().AsReadOnly()));
|
---|
155 | TransformationsParameter.Hidden = true;
|
---|
156 | }
|
---|
157 | if (!Parameters.ContainsKey(VariableRangesParameterName)) {
|
---|
158 | var variableRanges = Dataset.GetVariableRanges();
|
---|
159 | Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, variableRanges));
|
---|
160 | }
|
---|
161 | RegisterEventHandlers();
|
---|
162 | }
|
---|
163 |
|
---|
164 | protected DataAnalysisProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<ITransformation> transformations = null, IntervalCollection variableRanges = null) {
|
---|
165 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
166 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowed input variables must not be null.");
|
---|
167 |
|
---|
168 | if (allowedInputVariables.Except(dataset.DoubleVariables).Except(dataset.StringVariables).Any())
|
---|
169 | throw new ArgumentException("All allowed input variables must be present in the dataset and of type double or string.");
|
---|
170 |
|
---|
171 | var variables = dataset.VariableNames.Where(variable => dataset.VariableHasType<double>(variable) || dataset.VariableHasType<string>(variable));
|
---|
172 | var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x).AsReadOnly()));
|
---|
173 | foreach (StringValue x in inputVariables)
|
---|
174 | inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
|
---|
175 |
|
---|
176 | int trainingPartitionStart = 0;
|
---|
177 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
178 | int testPartitionStart = dataset.Rows / 2;
|
---|
179 | int testPartitionEnd = dataset.Rows;
|
---|
180 |
|
---|
181 | var transformationsList = new ItemList<ITransformation>(transformations ?? Enumerable.Empty<ITransformation>());
|
---|
182 |
|
---|
183 | Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", (Dataset)dataset));
|
---|
184 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
|
---|
185 | Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
|
---|
186 | Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
|
---|
187 | Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", transformationsList.AsReadOnly()));
|
---|
188 |
|
---|
189 | if (variableRanges == null) {
|
---|
190 | variableRanges = Dataset.GetVariableRanges();
|
---|
191 | }
|
---|
192 | Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, variableRanges));
|
---|
193 |
|
---|
194 | TransformationsParameter.Hidden = true;
|
---|
195 |
|
---|
196 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
197 | RegisterEventHandlers();
|
---|
198 | }
|
---|
199 |
|
---|
200 | private void RegisterEventHandlers() {
|
---|
201 | DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
202 | InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
|
---|
203 | TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
204 | TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
205 | TransformationsParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
206 | }
|
---|
207 |
|
---|
208 | private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
209 | OnChanged();
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
213 | OnChanged();
|
---|
214 | }
|
---|
215 |
|
---|
216 | public event EventHandler Changed;
|
---|
217 | protected virtual void OnChanged() {
|
---|
218 | var listeners = Changed;
|
---|
219 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|