[5540] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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;
|
---|
[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 {
|
---|
[6760] | 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 |
|
---|
[6760] | 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 |
|
---|
[6760] | 77 | public virtual IEnumerable<int> TrainingIndizes {
|
---|
[5554] | 78 | get {
|
---|
[5759] | 79 | return Enumerable.Range(TrainingPartition.Start, TrainingPartition.End - TrainingPartition.Start)
|
---|
[6760] | 80 | .Where(IsTrainingSample);
|
---|
[5540] | 81 | }
|
---|
| 82 | }
|
---|
[6760] | 83 | public virtual IEnumerable<int> TestIndizes {
|
---|
[5554] | 84 | get {
|
---|
[5759] | 85 | return Enumerable.Range(TestPartition.Start, TestPartition.End - TestPartition.Start)
|
---|
[6760] | 86 | .Where(IsTestSample);
|
---|
[5554] | 87 | }
|
---|
| 88 | }
|
---|
[6760] | 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 |
|
---|
[6760] | 102 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
|
---|
| 103 | : base(original, cloner) {
|
---|
| 104 | isEmpty = original.isEmpty;
|
---|
| 105 | RegisterEventHandlers();
|
---|
| 106 | }
|
---|
[5540] | 107 | [StorableConstructor]
|
---|
| 108 | protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
|
---|
[6760] | 109 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 110 | private void AfterDeserialization() {
|
---|
| 111 | RegisterEventHandlers();
|
---|
| 112 | }
|
---|
[5559] | 113 |
|
---|
[5554] | 114 | protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
|
---|
[5559] | 115 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
| 116 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
|
---|
| 117 |
|
---|
[6760] | 118 | if (allowedInputVariables.Except(dataset.DoubleVariables).Any())
|
---|
| 119 | throw new ArgumentException("All allowed input variables must be present in the dataset and of type double.");
|
---|
[5554] | 120 |
|
---|
[6760] | 121 | var inputVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
|
---|
[5586] | 122 | foreach (StringValue x in inputVariables)
|
---|
| 123 | inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
|
---|
[5540] | 124 |
|
---|
[5586] | 125 | int trainingPartitionStart = 0;
|
---|
| 126 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
| 127 | int testPartitionStart = dataset.Rows / 2;
|
---|
| 128 | int testPartitionEnd = dataset.Rows;
|
---|
[5540] | 129 |
|
---|
[5601] | 130 | Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
|
---|
[5847] | 131 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
|
---|
[5759] | 132 | Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
|
---|
| 133 | Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
|
---|
[5586] | 134 |
|
---|
[5601] | 135 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
[5586] | 136 | RegisterEventHandlers();
|
---|
[5540] | 137 | }
|
---|
[5542] | 138 |
|
---|
[5586] | 139 | private void RegisterEventHandlers() {
|
---|
| 140 | DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5649] | 141 | InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
|
---|
[5759] | 142 | TrainingPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
| 143 | TestPartition.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
[5540] | 144 | }
|
---|
| 145 |
|
---|
[5649] | 146 | private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
[5586] | 147 | OnChanged();
|
---|
| 148 | }
|
---|
[5649] | 149 |
|
---|
[5586] | 150 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 151 | OnChanged();
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[5554] | 154 | public event EventHandler Changed;
|
---|
| 155 | protected virtual void OnChanged() {
|
---|
[5540] | 156 | var listeners = Changed;
|
---|
| 157 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|