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.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
33 | [StorableClass]
|
---|
34 | public abstract class DataAnalysisProblemData : ParameterizedNamedItem, IDataAnalysisProblemData {
|
---|
35 | private const string DatasetParameterName = "Dataset";
|
---|
36 | private const string InputVariablesParameterName = "InputVariables";
|
---|
37 | private const string TrainingPartitionStartParameterName = "TrainingPartitionStart";
|
---|
38 | private const string TrainingPartitionEndParameterName = "TrainingPartitionEnd";
|
---|
39 | private const string TestPartitionStartParameterName = "TestPartitionStart";
|
---|
40 | private const string TestPartitionEndParameterName = "TestPartitionEnd";
|
---|
41 |
|
---|
42 | #region parameter properites
|
---|
43 | public IFixedValueParameter<Dataset> DatasetParameter {
|
---|
44 | get { return (IFixedValueParameter<Dataset>)Parameters[DatasetParameterName]; }
|
---|
45 | }
|
---|
46 | public IFixedValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
|
---|
47 | get { return (IFixedValueParameter<ICheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
|
---|
48 | }
|
---|
49 | public IFixedValueParameter<IntValue> TrainingPartitionStartParameter {
|
---|
50 | get { return (IFixedValueParameter<IntValue>)Parameters[TrainingPartitionStartParameterName]; }
|
---|
51 | }
|
---|
52 | public IFixedValueParameter<IntValue> TrainingPartitionEndParameter {
|
---|
53 | get { return (IFixedValueParameter<IntValue>)Parameters[TrainingPartitionEndParameterName]; }
|
---|
54 | }
|
---|
55 | public IFixedValueParameter<IntValue> TestPartitionStartParameter {
|
---|
56 | get { return (IFixedValueParameter<IntValue>)Parameters[TestPartitionStartParameterName]; }
|
---|
57 | }
|
---|
58 | public IFixedValueParameter<IntValue> TestPartitionEndParameter {
|
---|
59 | get { return (IFixedValueParameter<IntValue>)Parameters[TestPartitionEndParameterName]; }
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region propeties
|
---|
64 | public Dataset Dataset {
|
---|
65 | get { return DatasetParameter.Value; }
|
---|
66 | }
|
---|
67 | public ICheckedItemList<StringValue> InputVariables {
|
---|
68 | get { return InputVariablesParameter.Value; }
|
---|
69 | }
|
---|
70 | public IEnumerable<string> AllowedInputVariables {
|
---|
71 | get { return InputVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public IntValue TrainingPartitionStart {
|
---|
75 | get { return TrainingPartitionStartParameter.Value; }
|
---|
76 | }
|
---|
77 | public IntValue TrainingPartitionEnd {
|
---|
78 | get { return TrainingPartitionEndParameter.Value; }
|
---|
79 | }
|
---|
80 | public IntValue TestPartitionStart {
|
---|
81 | get { return TestPartitionStartParameter.Value; }
|
---|
82 | }
|
---|
83 | public IntValue TestPartitionEnd {
|
---|
84 | get { return TestPartitionEndParameter.Value; }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public IEnumerable<int> TrainingIndizes {
|
---|
88 | get {
|
---|
89 | return Enumerable.Range(TrainingPartitionStart.Value, TrainingPartitionEnd.Value - TrainingPartitionStart.Value)
|
---|
90 | .Where(i => i >= 0 && i < Dataset.Rows && (i < TestPartitionStart.Value || TestPartitionEnd.Value <= i));
|
---|
91 | }
|
---|
92 | }
|
---|
93 | public IEnumerable<int> TestIndizes {
|
---|
94 | get {
|
---|
95 | return Enumerable.Range(TestPartitionStart.Value, TestPartitionEnd.Value - TestPartitionStart.Value)
|
---|
96 | .Where(i => i >= 0 && i < Dataset.Rows);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner) : base(original, cloner) { }
|
---|
102 | [StorableConstructor]
|
---|
103 | protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
|
---|
104 |
|
---|
105 | protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
|
---|
106 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
107 | if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
|
---|
108 |
|
---|
109 | if (allowedInputVariables.Except(dataset.VariableNames).Any())
|
---|
110 | throw new ArgumentException("All allowed input variables must be present in the dataset.");
|
---|
111 |
|
---|
112 | var inputVariables = new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x)));
|
---|
113 | foreach (StringValue x in inputVariables)
|
---|
114 | inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
|
---|
115 |
|
---|
116 | int trainingPartitionStart = 0;
|
---|
117 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
118 | int testPartitionStart = dataset.Rows / 2;
|
---|
119 | int testPartitionEnd = dataset.Rows;
|
---|
120 |
|
---|
121 | Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", dataset));
|
---|
122 | Parameters.Add(new FixedValueParameter<ICheckedItemList<StringValue>>(InputVariablesParameterName, "", inputVariables.AsReadOnly()));
|
---|
123 | Parameters.Add(new FixedValueParameter<IntValue>(TrainingPartitionStartParameterName, "", new IntValue(trainingPartitionStart)));
|
---|
124 | Parameters.Add(new FixedValueParameter<IntValue>(TrainingPartitionEndParameterName, "", new IntValue(trainingPartitionEnd)));
|
---|
125 | Parameters.Add(new FixedValueParameter<IntValue>(TestPartitionStartParameterName, "", new IntValue(testPartitionStart)));
|
---|
126 | Parameters.Add(new FixedValueParameter<IntValue>(TestPartitionEndParameterName, "", new IntValue(testPartitionEnd)));
|
---|
127 |
|
---|
128 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
129 | RegisterEventHandlers();
|
---|
130 | }
|
---|
131 |
|
---|
132 | private void RegisterEventHandlers() {
|
---|
133 | DatasetParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
134 | InputVariables.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
|
---|
135 | TrainingPartitionStart.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
136 | TrainingPartitionEnd.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
137 | TestPartitionStart.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
138 | TestPartitionEnd.ValueChanged += new EventHandler(Parameter_ValueChanged);
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void InputVariables_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
142 | OnChanged();
|
---|
143 | }
|
---|
144 |
|
---|
145 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
146 | OnChanged();
|
---|
147 | }
|
---|
148 |
|
---|
149 | public event EventHandler Changed;
|
---|
150 | protected virtual void OnChanged() {
|
---|
151 | var listeners = Changed;
|
---|
152 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|