Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • several small bug fixes
  • added windowing technique ILAS to GAssist
  • GAssist and XCS work now with real-valued features
  • severely improved the performance of XCS
File size: 8.9 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    #endregion
111
112    [StorableHook(HookType.AfterDeserialization)]
113    private void AfterDeserialization() {
114      RegisterParameterEvents();
115    }
116    [StorableConstructor]
117    protected ConditionActionClassificationProblemData(bool deserializing) : base(deserializing) { }
118    protected ConditionActionClassificationProblemData(ConditionActionClassificationProblemData original, Cloner cloner)
119      : base(original, cloner) {
120      RegisterParameterEvents();
121    }
122
123    public ConditionActionClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
124      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
125      if (allowedActionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
126      if (allowedConditionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
127
128      var actionVariables = new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x)));
129      var conditionVariables = new CheckedItemList<StringValue>(actionVariables);
130      foreach (StringValue x in actionVariables) {
131        actionVariables.SetItemCheckedState(x, allowedActionVariables.Contains(x.Value));
132        conditionVariables.SetItemCheckedState(x, allowedConditionVariables.Contains(x.Value));
133      }
134
135      int trainingPartitionStart = 0;
136      int trainingPartitionEnd = dataset.Rows / 2;
137      int testPartitionStart = dataset.Rows / 2;
138      int testPartitionEnd = dataset.Rows;
139
140      Parameters.Add(new FixedValueParameter<Dataset>("Dataset", "", dataset));
141      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ActionVariables", "", actionVariables.AsReadOnly()));
142      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ConditionVariables", "", conditionVariables.AsReadOnly()));
143      Parameters.Add(new FixedValueParameter<IntRange>("TrainingPartition", "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
144      Parameters.Add(new FixedValueParameter<IntRange>("TestPartition", "", new IntRange(testPartitionStart, testPartitionEnd)));
145
146      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
147
148      RegisterParameterEvents();
149    }
150
151    public event EventHandler Changed;
152    protected virtual void OnChanged() {
153      var listeners = Changed;
154      if (listeners != null) listeners(this, EventArgs.Empty);
155    }
156
157    public IEnumerable<IInput> FetchInput(IEnumerable<int> rows) {
158      foreach (var row in rows) {
159        yield return FetchInput(row);
160      }
161    }
162    public abstract IInput FetchInput(int rowNumber);
163
164    public IEnumerable<IAction> FetchAction(IEnumerable<int> rows) {
165      foreach (var row in rows) {
166        yield return FetchAction(row);
167      }
168    }
169    public abstract IAction FetchAction(int rowNumber);
170
171    protected IDictionary<int, IInput> fetchInputCache = new Dictionary<int, IInput>();
172
173    #region events
174    private void RegisterParameterEvents() {
175      ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
176      ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
177      ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
178      ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
179    }
180    private void DeregisterParameterEvents() {
181      ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
182      ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
183      ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(VariablesChanged);
184      ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
185    }
186    private void Value_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
187      ActionConditionVariablesChanged();
188    }
189    private void VariablesChanged(object sender, EventArgs e) {
190      ActionConditionVariablesChanged();
191    }
192
193    protected abstract void ActionConditionVariablesChanged();
194    #endregion
195  }
196}
Note: See TracBrowser for help on using the repository browser.