Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9467 was 9411, checked in by sforsten, 12 years ago

#1980:

  • added multiple discretizer to GAssist
  • created ensembles for LCS problems and edited CrossValidation to use them
File size: 9.3 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.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.ConditionActionEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace 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 = new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x)));
131      var conditionVariables = new CheckedItemList<StringValue>(actionVariables);
132      foreach (StringValue x in actionVariables) {
133        actionVariables.SetItemCheckedState(x, allowedActionVariables.Contains(x.Value));
134        conditionVariables.SetItemCheckedState(x, allowedConditionVariables.Contains(x.Value));
135      }
136
137      int trainingPartitionStart = 0;
138      int trainingPartitionEnd = dataset.Rows / 2;
139      int testPartitionStart = dataset.Rows / 2;
140      int testPartitionEnd = dataset.Rows;
141
142      Parameters.Add(new FixedValueParameter<Dataset>("Dataset", "", dataset));
143      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ActionVariables", "", actionVariables.AsReadOnly()));
144      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ConditionVariables", "", conditionVariables.AsReadOnly()));
145      Parameters.Add(new FixedValueParameter<IntRange>("TrainingPartition", "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
146      Parameters.Add(new FixedValueParameter<IntRange>("TestPartition", "", new IntRange(testPartitionStart, testPartitionEnd)));
147
148      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
149
150      RegisterParameterEvents();
151    }
152
153    public event EventHandler Changed;
154    protected virtual void OnChanged() {
155      var listeners = Changed;
156      if (listeners != null) listeners(this, EventArgs.Empty);
157    }
158
159    public IEnumerable<IInput> FetchInput(IEnumerable<int> rows) {
160      foreach (var row in rows) {
161        yield return FetchInput(row);
162      }
163    }
164    public abstract IInput FetchInput(int rowNumber);
165
166    public IEnumerable<IAction> FetchAction(IEnumerable<int> rows) {
167      foreach (var row in rows) {
168        yield return FetchAction(row);
169      }
170    }
171    public abstract IAction FetchAction(int rowNumber);
172
173    protected IDictionary<int, IInput> fetchInputCache = new Dictionary<int, IInput>();
174
175    #region events
176    private void RegisterParameterEvents() {
177      ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
178      ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
179      ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
180      ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
181    }
182    private void DeregisterParameterEvents() {
183      ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
184      ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
185      ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
186      ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
187    }
188    private void Value_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
189      ActionConditionVariablesChanged();
190    }
191    private void VariablesChanged(object sender, EventArgs e) {
192      ActionConditionVariablesChanged();
193    }
194
195    protected abstract void ActionConditionVariablesChanged();
196    #endregion
197
198    #region IDataAnalysisProblemData Members
199    public bool IsEmpty {
200      get { return true; }
201    }
202    public ICheckedItemList<StringValue> InputVariables {
203      get { return ConditionVariables; }
204    }
205    public IEnumerable<string> AllowedInputVariables {
206      get { return AllowedConditionVariables; }
207    }
208    #endregion
209  }
210}
Note: See TracBrowser for help on using the repository browser.