Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9333 was 9226, checked in by sforsten, 12 years ago

#1980:

  • made classes in Problems.ConditionActionClassification abstract
  • added Problems.VariableVectorClassification and Problems.CombinedIntegerVectorClassification
  • LCS works now with arbitrary problems, which implement ConditionActionClassificationProblem
File size: 7.7 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.ConditionActionEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Problems.ConditionActionClassification {
34  [StorableClass]
35  [Item("ConditionActionClassificationProblemData", "A problem data for LCS.")]
36  public abstract class ConditionActionClassificationProblemData : ParameterizedNamedItem, IConditionActionProblemData {
37
38    #region default data
39    public static string[] defaultVariableNames = new string[] { "a", "b", "c", "d", "e", "f", "g" };
40    public static double[,] defaultData = new double[,]{
41      {0,0,1,1,0,0,0},
42      {0,1,1,1,0,0,0},
43      {0,0,1,0,0,0,1},
44      {1,0,1,0,1,1,0}
45    };
46    #endregion
47
48    #region parameter properites
49    public IFixedValueParameter<Dataset> DatasetParameter {
50      get { return (IFixedValueParameter<Dataset>)Parameters["Dataset"]; }
51    }
52    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> ConditionVariablesParameter {
53      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters["ConditionVariables"]; }
54    }
55    public IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>> ActionVariablesParameter {
56      get { return (IFixedValueParameter<ReadOnlyCheckedItemList<StringValue>>)Parameters["ActionVariables"]; }
57    }
58    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
59      get { return (IFixedValueParameter<IntRange>)Parameters["TrainingPartition"]; }
60    }
61    public IFixedValueParameter<IntRange> TestPartitionParameter {
62      get { return (IFixedValueParameter<IntRange>)Parameters["TestPartition"]; }
63    }
64    #endregion
65
66    #region properties
67    public Dataset Dataset {
68      get { return DatasetParameter.Value; }
69    }
70    public ICheckedItemList<StringValue> ConditionVariables {
71      get { return ConditionVariablesParameter.Value; }
72    }
73    public ICheckedItemList<StringValue> ActionVariables {
74      get { return ActionVariablesParameter.Value; }
75    }
76    public IEnumerable<string> AllowedConditionVariables {
77      get { return ConditionVariables.CheckedItems.Select(x => x.Value.Value); }
78    }
79    public IEnumerable<string> AllowedActionVariables {
80      get { return ActionVariables.CheckedItems.Select(x => x.Value.Value); }
81    }
82    public IntRange TrainingPartition {
83      get { return TrainingPartitionParameter.Value; }
84    }
85    public IntRange TestPartition {
86      get { return TestPartitionParameter.Value; }
87    }
88    public IEnumerable<int> TrainingIndices {
89      get {
90        return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
91                         .Where(IsTrainingSample);
92      }
93    }
94    public IEnumerable<int> TestIndices {
95      get {
96        return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
97           .Where(IsTestSample);
98      }
99    }
100    public bool IsTrainingSample(int index) {
101      return index >= 0 && index < Dataset.Rows &&
102        TrainingPartition.Start <= index && index < TrainingPartition.End &&
103        (index < TestPartition.Start || TestPartition.End <= index);
104    }
105    public bool IsTestSample(int index) {
106      return index >= 0 && index < Dataset.Rows &&
107             TestPartition.Start <= index && index < TestPartition.End;
108    }
109    #endregion
110
111    [StorableConstructor]
112    protected ConditionActionClassificationProblemData(bool deserializing) : base(deserializing) { }
113    protected ConditionActionClassificationProblemData(ConditionActionClassificationProblemData original, Cloner cloner)
114      : base(original, cloner) {
115    }
116
117    public ConditionActionClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
118      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
119      if (allowedActionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
120      if (allowedConditionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
121
122      if (allowedActionVariables.Except(dataset.DoubleVariables).Any())
123        throw new ArgumentException("All allowed action variables must be present in the dataset and of type double.");
124      if (allowedConditionVariables.Except(dataset.DoubleVariables).Any())
125        throw new ArgumentException("All allowed condition variables must be present in the dataset and of type double.");
126
127      var actionVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
128      var conditionVariables = new CheckedItemList<StringValue>(actionVariables);
129      foreach (StringValue x in actionVariables) {
130        actionVariables.SetItemCheckedState(x, allowedActionVariables.Contains(x.Value));
131        conditionVariables.SetItemCheckedState(x, allowedConditionVariables.Contains(x.Value));
132      }
133
134      int trainingPartitionStart = 0;
135      int trainingPartitionEnd = dataset.Rows / 2;
136      int testPartitionStart = dataset.Rows / 2;
137      int testPartitionEnd = dataset.Rows;
138
139      Parameters.Add(new FixedValueParameter<Dataset>("Dataset", "", dataset));
140      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ActionVariables", "", actionVariables.AsReadOnly()));
141      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ConditionVariables", "", conditionVariables.AsReadOnly()));
142      Parameters.Add(new FixedValueParameter<IntRange>("TrainingPartition", "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
143      Parameters.Add(new FixedValueParameter<IntRange>("TestPartition", "", new IntRange(testPartitionStart, testPartitionEnd)));
144
145      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
146    }
147
148    public event EventHandler Changed;
149    protected virtual void OnChanged() {
150      var listeners = Changed;
151      if (listeners != null) listeners(this, EventArgs.Empty);
152    }
153
154    public IEnumerable<IInput> FetchInput(IEnumerable<int> rows) {
155      foreach (var row in rows) {
156        yield return FetchInput(row);
157      }
158    }
159    public abstract IInput FetchInput(int rowNumber);
160
161    public IEnumerable<IAction> FetchAction(IEnumerable<int> rows) {
162      foreach (var row in rows) {
163        yield return FetchAction(row);
164      }
165    }
166    public abstract IAction FetchAction(int rowNumber);
167
168    protected IDictionary<int, IInput> fetchInputCache = new Dictionary<int, IInput>();
169  }
170}
Note: See TracBrowser for help on using the repository browser.