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