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