[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;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 30 | [StorableClass]
|
---|
[5559] | 31 | public abstract class DataAnalysisProblemData : NamedItem, IDataAnalysisProblemData {
|
---|
[5540] | 32 | #region propeties
|
---|
[5559] | 33 | [Storable]
|
---|
| 34 | private Dataset dataset;
|
---|
[5540] | 35 | public Dataset Dataset {
|
---|
[5559] | 36 | get { return dataset; }
|
---|
[5540] | 37 | }
|
---|
[5559] | 38 |
|
---|
| 39 | [Storable]
|
---|
| 40 | private HashSet<string> allowedInputVariables;
|
---|
[5554] | 41 | public IEnumerable<string> AllowedInputVariables {
|
---|
[5559] | 42 | get { return allowedInputVariables; }
|
---|
[5540] | 43 | }
|
---|
| 44 |
|
---|
[5559] | 45 | [Storable]
|
---|
| 46 | private int trainingPartitionStart;
|
---|
| 47 | public int TrainingPartitionStart {
|
---|
| 48 | get { return trainingPartitionStart; }
|
---|
| 49 | set {
|
---|
| 50 | if (0 < value || value > dataset.Rows)
|
---|
| 51 | throw new ArgumentException(string.Format("The training partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
|
---|
| 52 | if (trainingPartitionStart != value) {
|
---|
| 53 | trainingPartitionStart = value;
|
---|
| 54 | OnChanged();
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
[5540] | 57 | }
|
---|
[5559] | 58 | [Storable]
|
---|
| 59 | private int trainingPartitionEnd;
|
---|
| 60 | public int TrainingPartitionEnd {
|
---|
| 61 | get { return trainingPartitionEnd; }
|
---|
| 62 | set {
|
---|
| 63 | if (0 < value || value > dataset.Rows)
|
---|
| 64 | throw new ArgumentException(string.Format("The training partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
|
---|
| 65 | if (trainingPartitionEnd != value) {
|
---|
| 66 | trainingPartitionEnd = value;
|
---|
| 67 | OnChanged();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
[5540] | 70 | }
|
---|
| 71 |
|
---|
[5559] | 72 | [Storable]
|
---|
| 73 | private int testPartitionStart;
|
---|
| 74 | public int TestPartitionStart {
|
---|
| 75 | get { return testPartitionStart; }
|
---|
| 76 | set {
|
---|
| 77 | if (0 < value || value > dataset.Rows)
|
---|
| 78 | throw new ArgumentException(string.Format("The test partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
|
---|
| 79 | if (testPartitionStart != value) {
|
---|
| 80 | testPartitionStart = value;
|
---|
| 81 | OnChanged();
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[5540] | 84 | }
|
---|
[5559] | 85 | [Storable]
|
---|
| 86 | private int testPartitionEnd;
|
---|
| 87 | public int TestPartitionEnd {
|
---|
| 88 | get { return testPartitionEnd; }
|
---|
| 89 | set {
|
---|
| 90 | if (0 < value || value > dataset.Rows)
|
---|
| 91 | throw new ArgumentException(string.Format("The test partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
|
---|
| 92 | if (testPartitionEnd != value) {
|
---|
| 93 | testPartitionEnd = value;
|
---|
| 94 | OnChanged();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[5554] | 97 | }
|
---|
| 98 |
|
---|
| 99 | public IEnumerable<int> TrainingIndizes {
|
---|
| 100 | get {
|
---|
[5559] | 101 | return Enumerable.Range(TrainingPartitionStart, TrainingPartitionEnd - TrainingPartitionStart)
|
---|
| 102 | .Where(i => i >= 0 && i < Dataset.Rows && (i < TestPartitionStart || TestPartitionEnd <= i));
|
---|
[5540] | 103 | }
|
---|
| 104 | }
|
---|
[5554] | 105 | public IEnumerable<int> TestIndizes {
|
---|
| 106 | get {
|
---|
[5559] | 107 | return Enumerable.Range(TestPartitionStart, TestPartitionEnd - TestPartitionStart)
|
---|
[5554] | 108 | .Where(i => i >= 0 && i < Dataset.Rows);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
[5540] | 111 | #endregion
|
---|
| 112 |
|
---|
[5559] | 113 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner) : base(original, cloner) { }
|
---|
[5540] | 114 | [StorableConstructor]
|
---|
| 115 | protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
|
---|
[5559] | 116 |
|
---|
[5554] | 117 | protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
|
---|
[5559] | 118 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
| 119 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
|
---|
| 120 |
|
---|
[5554] | 121 | if (allowedInputVariables.Except(dataset.VariableNames).Any())
|
---|
| 122 | throw new ArgumentException("All allowed input variables must be present in the dataset.");
|
---|
| 123 |
|
---|
[5559] | 124 | this.dataset = dataset;
|
---|
| 125 | allowedInputVariables = new HashSet<string>(allowedInputVariables);
|
---|
| 126 | trainingPartitionStart = 0;
|
---|
| 127 | trainingPartitionEnd = dataset.Rows / 2;
|
---|
| 128 | testPartitionStart = dataset.Rows / 2;
|
---|
| 129 | testPartitionEnd = dataset.Rows;
|
---|
[5540] | 130 | }
|
---|
| 131 |
|
---|
[5559] | 132 | public bool AddAllowedInputVariable(string inputVariable) {
|
---|
| 133 | if (!Dataset.VariableNames.Contains(inputVariable))
|
---|
| 134 | throw new ArgumentException("The allowed input variable must be present in the dataset.");
|
---|
| 135 | if (allowedInputVariables.Contains(inputVariable)) return false;
|
---|
[5540] | 136 |
|
---|
[5559] | 137 | allowedInputVariables.Add(inputVariable);
|
---|
| 138 | return true;
|
---|
[5540] | 139 | }
|
---|
[5559] | 140 | public bool RemoveAllowedInputVariable(string inputVariable) {
|
---|
| 141 | if (!Dataset.VariableNames.Contains(inputVariable))
|
---|
| 142 | throw new ArgumentException("The allowed input variable must be present in the dataset.");
|
---|
| 143 | if (!allowedInputVariables.Contains(inputVariable)) return false;
|
---|
[5542] | 144 |
|
---|
[5559] | 145 | allowedInputVariables.Remove(inputVariable);
|
---|
| 146 | return true;
|
---|
[5540] | 147 | }
|
---|
| 148 |
|
---|
[5554] | 149 | public event EventHandler Changed;
|
---|
| 150 | protected virtual void OnChanged() {
|
---|
[5540] | 151 | var listeners = Changed;
|
---|
| 152 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|