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