Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Problems.ConditionActionClassification/3.3/Implementation/ConditionActionClassificationProblemData.cs @ 9161

Last change on this file since 9161 was 9161, checked in by sforsten, 11 years ago

#1980:

  • added training and test partition to ConditionActionClassificationProblemData
  • ClassifierFetcher only uses training partition
File size: 10.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.CombinedIntegerVectorEncoding;
29using HeuristicLab.Encodings.ConditionActionEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.Problems.ConditionActionClassification {
35  public class ConditionActionClassificationProblemData : ParameterizedNamedItem, IConditionActionProblemData {
36
37    #region default data
38    public static string[] defaultVariableNames = new string[] { "a", "b", "c", "d", "e", "f", "g" };
39    public static double[,] defaultData = new double[,]{
40      {0,0,1,1,0,0,0},
41      {0,1,1,1,0,0,0},
42      {0,0,1,0,0,0,1},
43      {1,0,1,0,1,1,0}
44    };
45    #endregion
46
47    #region parameter properites
48    public IFixedValueParameter<Dataset> DatasetParameter {
49      get { return (IFixedValueParameter<Dataset>)Parameters["Dataset"]; }
50    }
51    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> ConditionVariablesParameter {
52      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters["ConditionVariables"]; }
53    }
54    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> ActionVariablesParameter {
55      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters["ActionVariables"]; }
56    }
57    public IFixedValueParameter<IntValue> LengthParameter {
58      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
59    }
60    public IFixedValueParameter<IntValue> ActionLengthParameter {
61      get { return (IFixedValueParameter<IntValue>)Parameters["ActionLength"]; }
62    }
63    public IFixedValueParameter<IntMatrix> BoundsParameter {
64      get { return (IFixedValueParameter<IntMatrix>)Parameters["Bounds"]; }
65    }
66    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
67      get { return (IFixedValueParameter<IntRange>)Parameters["TrainingPartition"]; }
68    }
69    public IFixedValueParameter<IntRange> TestPartitionParameter {
70      get { return (IFixedValueParameter<IntRange>)Parameters["TestPartition"]; }
71    }
72    #endregion
73
74    #region properties
75    public Dataset Dataset {
76      get { return DatasetParameter.Value; }
77    }
78    public ICheckedItemList<StringValue> ConditionVariables {
79      get { return ConditionVariablesParameter.Value; }
80    }
81    public ICheckedItemList<StringValue> ActionVariables {
82      get { return ActionVariablesParameter.Value; }
83    }
84    public IEnumerable<string> AllowedConditionVariables {
85      get { return ConditionVariables.CheckedItems.Select(x => x.Value.Value); }
86    }
87    public IEnumerable<string> AllowedActionVariables {
88      get { return ActionVariables.CheckedItems.Select(x => x.Value.Value); }
89    }
90    public IntRange TrainingPartition {
91      get { return TrainingPartitionParameter.Value; }
92    }
93    public IntRange TestPartition {
94      get { return TestPartitionParameter.Value; }
95    }
96    public IEnumerable<int> TrainingIndices {
97      get {
98        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
99                         .Where(IsTrainingSample);
100      }
101    }
102    public IEnumerable<int> TestIndices {
103      get {
104        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
105           .Where(IsTestSample);
106      }
107    }
108    public bool IsTrainingSample(int index) {
109      return index >= 0 && index < Dataset.Rows &&
110        TrainingPartition.Start <= index && index < TrainingPartition.End &&
111        (index < TestPartition.Start || TestPartition.End <= index);
112    }
113    public bool IsTestSample(int index) {
114      return index >= 0 && index < Dataset.Rows &&
115             TestPartition.Start <= index && index < TestPartition.End;
116    }
117    #endregion
118
119    private IDictionary<int, IClassifier> fetchClassifiersCache = new Dictionary<int, IClassifier>();
120
121
122    [StorableConstructor]
123    protected ConditionActionClassificationProblemData(bool deserializing) : base(deserializing) { }
124    protected ConditionActionClassificationProblemData(ConditionActionClassificationProblemData original, Cloner cloner)
125      : base(original, cloner) {
126    }
127    public override IDeepCloneable Clone(Cloner cloner) {
128      return new ConditionActionClassificationProblemData(this, cloner);
129    }
130
131    public ConditionActionClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
132      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
133      if (allowedActionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
134      if (allowedConditionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
135
136      if (allowedActionVariables.Except(dataset.DoubleVariables).Any())
137        throw new ArgumentException("All allowed action variables must be present in the dataset and of type double.");
138      if (allowedConditionVariables.Except(dataset.DoubleVariables).Any())
139        throw new ArgumentException("All allowed condition variables must be present in the dataset and of type double.");
140
141      var actionVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
142      var conditionVariables = new CheckedItemList<StringValue>(actionVariables);
143      foreach (StringValue x in actionVariables) {
144        actionVariables.SetItemCheckedState(x, allowedActionVariables.Contains(x.Value));
145        conditionVariables.SetItemCheckedState(x, allowedConditionVariables.Contains(x.Value));
146      }
147
148      int trainingPartitionStart = 0;
149      int trainingPartitionEnd = dataset.Rows / 2;
150      int testPartitionStart = dataset.Rows / 2;
151      int testPartitionEnd = dataset.Rows;
152
153      Parameters.Add(new FixedValueParameter<Dataset>("Dataset", "", dataset));
154      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ActionVariables", "", actionVariables.AsReadOnly()));
155      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ConditionVariables", "", conditionVariables.AsReadOnly()));
156      Parameters.Add(new FixedValueParameter<IntValue>("Length", "", new IntValue(allowedConditionVariables.Count() + allowedActionVariables.Count())));
157      Parameters.Add(new FixedValueParameter<IntValue>("ActionLength", "", new IntValue(allowedActionVariables.Count())));
158      Parameters.Add(new FixedValueParameter<IntMatrix>("Bounds", "", GetBoundsMatrix(dataset, allowedConditionVariables, allowedActionVariables)));
159      Parameters.Add(new FixedValueParameter<IntRange>("TrainingPartition", "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
160      Parameters.Add(new FixedValueParameter<IntRange>("TestPartition", "", new IntRange(testPartitionStart, testPartitionEnd)));
161
162      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
163    }
164
165    private IntMatrix GetBoundsMatrix(Dataset dataset, IEnumerable<string> conditionVariables, IEnumerable<string> actionVariables) {
166      IntMatrix bounds = new IntMatrix(conditionVariables.Count() + actionVariables.Count(), 2);
167      int index = 0;
168      foreach (var variable in conditionVariables) {
169        var values = dataset.GetDoubleValues(variable);
170        bounds[index, 0] = (int)values.Min();
171        bounds[index, 1] = (int)values.Max() + 2;
172        index++;
173      }
174      foreach (var variable in actionVariables) {
175        var values = dataset.GetDoubleValues(variable);
176        bounds[index, 0] = (int)values.Min();
177        bounds[index, 1] = (int)values.Max() + 1;
178        index++;
179      }
180      return bounds;
181    }
182
183    public event EventHandler Changed;
184    protected virtual void OnChanged() {
185      var listeners = Changed;
186      if (listeners != null) listeners(this, EventArgs.Empty);
187    }
188
189    public IntValue Length {
190      get { return LengthParameter.Value; }
191    }
192
193    public IntValue ActionLength {
194      get { return ActionLengthParameter.Value; }
195    }
196
197    public IntMatrix Bounds {
198      get { return BoundsParameter.Value; }
199    }
200
201    public IEnumerable<IClassifier> FetchClassifier(IEnumerable<int> rows) {
202      foreach (var row in rows) {
203        yield return FetchClassifier(row);
204      }
205    }
206
207    public IClassifier FetchClassifier(int rowNumber) {
208      if (!fetchClassifiersCache.ContainsKey(rowNumber)) {
209        int[] elements = new int[Length.Value];
210        var variableNamesList = Dataset.VariableNames.ToList();
211        int elementIndex = 0;
212        for (int i = 0; i < variableNamesList.Count; i++) {
213          if (AllowedConditionVariables.Contains(variableNamesList[i])) {
214            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
215            elementIndex++;
216          }
217        }
218        for (int i = 0; i < variableNamesList.Count; i++) {
219          if (AllowedActionVariables.Contains(variableNamesList[i])) {
220            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
221            elementIndex++;
222          }
223        }
224        if (elementIndex != Length.Value) {
225          throw new ArgumentException("Length of classifier is not equal to the number of allowed condition + action variables.");
226        }
227        fetchClassifiersCache.Add(rowNumber, new CombinedIntegerVector(elements, ActionLengthParameter.Value.Value, BoundsParameter.Value));
228      }
229      return fetchClassifiersCache[rowNumber];
230    }
231  }
232}
Note: See TracBrowser for help on using the repository browser.