1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Encodings.ConditionActionEncoding;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.ConditionActionClassification {
|
---|
35 | [StorableClass]
|
---|
36 | [Item("ConditionActionClassificationProblemData", "A problem data for LCS.")]
|
---|
37 | public abstract class ConditionActionClassificationProblemData : ParameterizedNamedItem, IConditionActionProblemData {
|
---|
38 |
|
---|
39 | #region default data
|
---|
40 | public static string[] defaultVariableNames = new string[] { "a", "b", "c", "d", "e", "f", "g" };
|
---|
41 | public static double[,] defaultData = new double[,]{
|
---|
42 | {0,0,1,1,0,0,0},
|
---|
43 | {0,1,1,1,0,0,0},
|
---|
44 | {0,0,1,0,0,0,1},
|
---|
45 | {1,0,1,0,1,1,0}
|
---|
46 | };
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region parameter properites
|
---|
50 | public IFixedValueParameter<Dataset> DatasetParameter {
|
---|
51 | get { return (IFixedValueParameter<Dataset>)Parameters["Dataset"]; }
|
---|
52 | }
|
---|
53 | public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> ConditionVariablesParameter {
|
---|
54 | get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters["ConditionVariables"]; }
|
---|
55 | }
|
---|
56 | public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> ActionVariablesParameter {
|
---|
57 | get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters["ActionVariables"]; }
|
---|
58 | }
|
---|
59 | public IFixedValueParameter<IntRange> TrainingPartitionParameter {
|
---|
60 | get { return (IFixedValueParameter<IntRange>)Parameters["TrainingPartition"]; }
|
---|
61 | }
|
---|
62 | public IFixedValueParameter<IntRange> TestPartitionParameter {
|
---|
63 | get { return (IFixedValueParameter<IntRange>)Parameters["TestPartition"]; }
|
---|
64 | }
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | #region properties
|
---|
68 | public Dataset Dataset {
|
---|
69 | get { return DatasetParameter.Value; }
|
---|
70 | }
|
---|
71 | public ICheckedItemList<StringValue> ConditionVariables {
|
---|
72 | get { return ConditionVariablesParameter.Value; }
|
---|
73 | }
|
---|
74 | public ICheckedItemList<StringValue> ActionVariables {
|
---|
75 | get { return ActionVariablesParameter.Value; }
|
---|
76 | }
|
---|
77 | public IEnumerable<string> AllowedConditionVariables {
|
---|
78 | get { return ConditionVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
79 | }
|
---|
80 | public IEnumerable<string> AllowedActionVariables {
|
---|
81 | get { return ActionVariables.CheckedItems.Select(x => x.Value.Value); }
|
---|
82 | }
|
---|
83 | public IntRange TrainingPartition {
|
---|
84 | get { return TrainingPartitionParameter.Value; }
|
---|
85 | }
|
---|
86 | public IntRange TestPartition {
|
---|
87 | get { return TestPartitionParameter.Value; }
|
---|
88 | }
|
---|
89 | public IEnumerable<int> TrainingIndices {
|
---|
90 | get {
|
---|
91 | return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
|
---|
92 | .Where(IsTrainingSample);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | public IEnumerable<int> TestIndices {
|
---|
96 | get {
|
---|
97 | return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
|
---|
98 | .Where(IsTestSample);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | public bool IsTrainingSample(int index) {
|
---|
102 | return index >= 0 && index < Dataset.Rows &&
|
---|
103 | TrainingPartition.Start <= index && index < TrainingPartition.End &&
|
---|
104 | (index < TestPartition.Start || TestPartition.End <= index);
|
---|
105 | }
|
---|
106 | public bool IsTestSample(int index) {
|
---|
107 | return index >= 0 && index < Dataset.Rows &&
|
---|
108 | TestPartition.Start <= index && index < TestPartition.End;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public abstract IClassifierComparer ClassifierComparer { get; }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | [StorableHook(HookType.AfterDeserialization)]
|
---|
115 | private void AfterDeserialization() {
|
---|
116 | RegisterParameterEvents();
|
---|
117 | }
|
---|
118 | [StorableConstructor]
|
---|
119 | protected ConditionActionClassificationProblemData(bool deserializing) : base(deserializing) { }
|
---|
120 | protected ConditionActionClassificationProblemData(ConditionActionClassificationProblemData original, Cloner cloner)
|
---|
121 | : base(original, cloner) {
|
---|
122 | RegisterParameterEvents();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public ConditionActionClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
|
---|
126 | if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
|
---|
127 | if (allowedActionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
|
---|
128 | if (allowedConditionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
|
---|
129 |
|
---|
130 | var actionVariables = CheckVariablesForPossibleTargetVariables(dataset);
|
---|
131 | foreach (StringValue x in actionVariables) {
|
---|
132 | actionVariables.SetItemCheckedState(x, allowedActionVariables.Contains(x.Value));
|
---|
133 | }
|
---|
134 | var conditionVariables = new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x)));
|
---|
135 | foreach (StringValue x in conditionVariables) {
|
---|
136 | conditionVariables.SetItemCheckedState(x, allowedConditionVariables.Contains(x.Value));
|
---|
137 | }
|
---|
138 |
|
---|
139 | int trainingPartitionStart = 0;
|
---|
140 | int trainingPartitionEnd = dataset.Rows / 2;
|
---|
141 | int testPartitionStart = dataset.Rows / 2;
|
---|
142 | int testPartitionEnd = dataset.Rows;
|
---|
143 |
|
---|
144 | Parameters.Add(new FixedValueParameter<Dataset>("Dataset", "", dataset));
|
---|
145 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ActionVariables", "", actionVariables.AsReadOnly()));
|
---|
146 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ConditionVariables", "", conditionVariables.AsReadOnly()));
|
---|
147 | Parameters.Add(new FixedValueParameter<IntRange>("TrainingPartition", "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
|
---|
148 | Parameters.Add(new FixedValueParameter<IntRange>("TestPartition", "", new IntRange(testPartitionStart, testPartitionEnd)));
|
---|
149 |
|
---|
150 | ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
151 |
|
---|
152 | RegisterParameterEvents();
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected virtual CheckedItemList<StringValue> CheckVariablesForPossibleTargetVariables(DataAnalysis.Dataset dataset) {
|
---|
156 | return new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x)));
|
---|
157 | }
|
---|
158 |
|
---|
159 | public event EventHandler Changed;
|
---|
160 | protected virtual void OnChanged() {
|
---|
161 | var listeners = Changed;
|
---|
162 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
163 | }
|
---|
164 |
|
---|
165 | public IEnumerable<IInput> FetchInput(IEnumerable<int> rows) {
|
---|
166 | foreach (var row in rows) {
|
---|
167 | yield return FetchInput(row);
|
---|
168 | }
|
---|
169 | }
|
---|
170 | public abstract IInput FetchInput(int rowNumber);
|
---|
171 |
|
---|
172 | public IEnumerable<IAction> FetchAction(IEnumerable<int> rows) {
|
---|
173 | foreach (var row in rows) {
|
---|
174 | yield return FetchAction(row);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | public abstract IAction FetchAction(int rowNumber);
|
---|
178 |
|
---|
179 | protected IDictionary<int, IInput> fetchInputCache = new Dictionary<int, IInput>();
|
---|
180 |
|
---|
181 | #region events
|
---|
182 | private void RegisterParameterEvents() {
|
---|
183 | ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
|
---|
184 | ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
|
---|
185 | ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
|
---|
186 | ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
|
---|
187 | }
|
---|
188 | private void DeregisterParameterEvents() {
|
---|
189 | ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
|
---|
190 | ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
|
---|
191 | ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
|
---|
192 | ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
|
---|
193 | }
|
---|
194 | private void Value_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
|
---|
195 | ActionConditionVariablesChanged();
|
---|
196 | }
|
---|
197 | private void VariablesChanged(object sender, EventArgs e) {
|
---|
198 | ActionConditionVariablesChanged();
|
---|
199 | }
|
---|
200 |
|
---|
201 | protected abstract void ActionConditionVariablesChanged();
|
---|
202 | #endregion
|
---|
203 |
|
---|
204 | #region IDataAnalysisProblemData Members
|
---|
205 | public bool IsEmpty {
|
---|
206 | get { return true; }
|
---|
207 | }
|
---|
208 | public ICheckedItemList<StringValue> InputVariables {
|
---|
209 | get { return ConditionVariables; }
|
---|
210 | }
|
---|
211 | public IEnumerable<string> AllowedInputVariables {
|
---|
212 | get { return AllowedConditionVariables; }
|
---|
213 | }
|
---|
214 | #endregion
|
---|
215 | }
|
---|
216 | }
|
---|