[5540] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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";
|
---|
[5586] | 39 |
|
---|
| 40 | #region parameter properites
|
---|
[5601] | 41 | public IFixedValueParameter<Dataset> DatasetParameter {
|
---|
| 42 | get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
|
---|
[5586] | 43 | }
|
---|
[5847] | 44 | public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> InputVariablesParameter {
|
---|
| 45 | get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
|
---|
[5586] | 46 | }
|
---|
[5759] | 47 | public IFixedValueParameter<IntRange> TrainingPartitionParameter {
|
---|
| 48 | get { return (IFixedValueParameter<IntRange>)Parameters[TrainingPartitionParameterName]; }
|
---|
[5586] | 49 | }
|
---|
[5759] | 50 | public IFixedValueParameter<IntRange> TestPartitionParameter {
|
---|
| 51 | get { return (IFixedValueParameter<IntRange>)Parameters[TestPartitionParameterName]; }
|
---|
[5586] | 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
[6666] | 55 | #region properties
|
---|
| 56 | protected bool isEmpty = false;
|
---|
| 57 | public bool IsEmpty {
|
---|
| 58 | get { return isEmpty; }
|
---|
| 59 | }
|
---|
[5540] | 60 | public Dataset Dataset {
|
---|
[5586] | 61 | get { return DatasetParameter.Value; }
|
---|
[5540] | 62 | }
|
---|
[5649] | 63 | public ICheckedItemList<StringValue> InputVariables {
|
---|
[5586] | 64 | get { return InputVariablesParameter.Value; }
|
---|
| 65 | }
|
---|
[5554] | 66 | public IEnumerable<string> AllowedInputVariables {
|
---|
[5649] | 67 | get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
[5540] | 68 | }
|
---|
| 69 |
|
---|
[5759] | 70 | public IntRange TrainingPartition {
|
---|
| 71 | get { return TrainingPartitionParameter.Value; }
|
---|
[5540] | 72 | }
|
---|
[5759] | 73 | public IntRange TestPartition {
|
---|
| 74 | get { return TestPartitionParameter.Value; }
|
---|
[5540] | 75 | }
|
---|
[5554] | 76 |
|
---|
[8508] | 77 | public virtual IEnumerable<int> TrainingIndices {
|
---|
[5554] | 78 | get {
|
---|
[7265] | 79 | return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
|
---|
[6672] | 80 | .Where(IsTrainingSample);
|
---|
[5540] | 81 | }
|
---|
| 82 | }
|
---|
[8508] | 83 | public virtual IEnumerable<int> TestIndices {
|
---|
[5554] | 84 | get {
|
---|
[7265] | 85 | return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
|
---|
[6672] | 86 | .Where(IsTestSample);
|
---|
[5554] | 87 | }
|
---|
| 88 | }
|
---|
[6672] | 89 |
|
---|
| 90 | public virtual bool IsTrainingSample(int index) {
|
---|
| 91 | return index >= 0 && index < Dataset.Rows &&
|
---|
| 92 | TrainingPartition.Start <= index && index < TrainingPartition.End &&
|
---|
| 93 | (index < TestPartition.Start || TestPartition.End <= index);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public virtual bool IsTestSample(int index) {
|
---|
| 97 | return index >= 0 && index < Dataset.Rows &&
|
---|
| 98 | TestPartition.Start <= index && index < TestPartition.End;
|
---|
| 99 | }
|
---|
[5540] | 100 | #endregion
|
---|
| 101 |
|
---|
[6236] | 102 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
|
---|
| 103 | : base(original, cloner) {
|
---|
[6666] | 104 | isEmpty = original.isEmpty;
|
---|
[6236] | 105 | RegisterEventHandlers();
|
---|
| 106 | }
|
---|
[5540] | 107 | [StorableConstructor]
|
---|
| 108 | protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
|
---|
[8811] | 109 |
|
---|
[6581] | 110 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 111 | private void AfterDeserialization() {
|
---|
| 112 | RegisterEventHandlers();
|
---|
| 113 | }
|
---|
[5559] | 114 |
|
---|
[5554] | 115 | protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
|
---|
[5559] | 116 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
| 117 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
|
---|
| 118 |
|
---|
[6740] | 119 | if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
|
---|
| 120 | throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
|
---|
[5554] | 121 |
|
---|
[6740] | 122 | var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
|
---|
[5586] | 123 | foreach (StringValue x in inputVariables)
|
---|
| 124 | inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
|
---|
[5540] | 125 |
|
---|
[5586] | 126 | int trainingPartitionStart = 0;
|
---|
| 127 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
| 128 | int testPartitionStart = dataset.Rows / 2;
|
---|
| 129 | int testPartitionEnd = dataset.Rows;
|
---|
[5540] | 130 |
|
---|
[5601] | 131 | Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
|
---|
[5847] | 132 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
|
---|
[5759] | 133 | Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
|
---|
| 134 | Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
|
---|
[5586] | 135 |
|
---|
[5601] | 136 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
[5586] | 137 | RegisterEventHandlers();
|
---|
[5540] | 138 | }
|
---|
[5542] | 139 |
|
---|
[5586] | 140 | private void RegisterEventHandlers() {
|
---|
| 141 | DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5649] | 142 | InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
|
---|
[5759] | 143 | TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
| 144 | TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5540] | 145 | }
|
---|
| 146 |
|
---|
[5649] | 147 | private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
[5586] | 148 | OnChanged();
|
---|
| 149 | }
|
---|
[5649] | 150 |
|
---|
[5586] | 151 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 152 | OnChanged();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[5554] | 155 | public event EventHandler Changed;
|
---|
| 156 | protected virtual void OnChanged() {
|
---|
[5540] | 157 | var listeners = Changed;
|
---|
| 158 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|