Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added problem instance provider to import csv files for ConditionActionClassificationProblemData
  • adapted LCSAdaptedGeneticAlgorithm to use the crossover probability
  • fixed a bug in XCSModelView
File size: 8.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    #endregion
67
68    public Dataset Dataset {
69      get { return DatasetParameter.Value; }
70    }
71
72    public ICheckedItemList<StringValue> ConditionVariables {
73      get { return ConditionVariablesParameter.Value; }
74    }
75
76    public ICheckedItemList<StringValue> ActionVariables {
77      get { return ActionVariablesParameter.Value; }
78    }
79
80    public IEnumerable<string> AllowedConditionVariables {
81      get { return ConditionVariables.CheckedItems.Select(x => x.Value.Value); }
82    }
83    public IEnumerable<string> AllowedActionVariables {
84      get { return ActionVariables.CheckedItems.Select(x => x.Value.Value); }
85    }
86
87    private IDictionary<int, IClassifier> fetchClassifiersCache = new Dictionary<int, IClassifier>();
88
89
90    [StorableConstructor]
91    protected ConditionActionClassificationProblemData(bool deserializing) : base(deserializing) { }
92    protected ConditionActionClassificationProblemData(ConditionActionClassificationProblemData original, Cloner cloner)
93      : base(original, cloner) {
94    }
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new ConditionActionClassificationProblemData(this, cloner);
97    }
98
99    public ConditionActionClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
100      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
101      if (allowedActionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
102      if (allowedConditionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
103
104      if (allowedActionVariables.Except(dataset.DoubleVariables).Any())
105        throw new ArgumentException("All allowed action variables must be present in the dataset and of type double.");
106      if (allowedConditionVariables.Except(dataset.DoubleVariables).Any())
107        throw new ArgumentException("All allowed condition variables must be present in the dataset and of type double.");
108
109      var actionVariables = new CheckedItemList<StringValue>(dataset.DoubleVariables.Select(x => new StringValue(x)));
110      var conditionVariables = new CheckedItemList<StringValue>(actionVariables);
111      foreach (StringValue x in actionVariables) {
112        actionVariables.SetItemCheckedState(x, allowedActionVariables.Contains(x.Value));
113        conditionVariables.SetItemCheckedState(x, allowedConditionVariables.Contains(x.Value));
114      }
115
116      Parameters.Add(new FixedValueParameter<Dataset>("Dataset", "", dataset));
117      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ActionVariables", "", actionVariables.AsReadOnly()));
118      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>("ConditionVariables", "", conditionVariables.AsReadOnly()));
119      Parameters.Add(new FixedValueParameter<IntValue>("Length", "", new IntValue(allowedConditionVariables.Count() + allowedActionVariables.Count())));
120      Parameters.Add(new FixedValueParameter<IntValue>("ActionLength", "", new IntValue(allowedActionVariables.Count())));
121      Parameters.Add(new FixedValueParameter<IntMatrix>("Bounds", "", GetBoundsMatrix(dataset, allowedConditionVariables, allowedActionVariables)));
122
123      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
124    }
125
126    private IntMatrix GetBoundsMatrix(Dataset dataset, IEnumerable<string> conditionVariables, IEnumerable<string> actionVariables) {
127      IntMatrix bounds = new IntMatrix(conditionVariables.Count() + actionVariables.Count(), 2);
128      int index = 0;
129      foreach (var variable in conditionVariables) {
130        var values = dataset.GetDoubleValues(variable);
131        bounds[index, 0] = (int)values.Min();
132        bounds[index, 1] = (int)values.Max() + 2;
133        index++;
134      }
135      foreach (var variable in actionVariables) {
136        var values = dataset.GetDoubleValues(variable);
137        bounds[index, 0] = (int)values.Min();
138        bounds[index, 1] = (int)values.Max() + 1;
139        index++;
140      }
141      return bounds;
142    }
143
144    public event EventHandler Changed;
145    protected virtual void OnChanged() {
146      var listeners = Changed;
147      if (listeners != null) listeners(this, EventArgs.Empty);
148    }
149
150    public IntValue Length {
151      get { return LengthParameter.Value; }
152    }
153
154    public IntValue ActionLength {
155      get { return ActionLengthParameter.Value; }
156    }
157
158    public IntMatrix Bounds {
159      get { return BoundsParameter.Value; }
160    }
161
162    public IClassifier FetchClassifier(int rowNumber) {
163      if (!fetchClassifiersCache.ContainsKey(rowNumber)) {
164        int[] elements = new int[Length.Value];
165        var variableNamesList = Dataset.VariableNames.ToList();
166        int elementIndex = 0;
167        for (int i = 0; i < variableNamesList.Count; i++) {
168          if (AllowedConditionVariables.Contains(variableNamesList[i])) {
169            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
170            elementIndex++;
171          }
172        }
173        for (int i = 0; i < variableNamesList.Count; i++) {
174          if (AllowedActionVariables.Contains(variableNamesList[i])) {
175            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
176            elementIndex++;
177          }
178        }
179        if (elementIndex != Length.Value) {
180          throw new ArgumentException("Length of classifier is not equal to the number of allowed condition + action variables.");
181        }
182        fetchClassifiersCache.Add(rowNumber, new CombinedIntegerVector(elements, ActionLengthParameter.Value.Value, BoundsParameter.Value));
183      }
184      return fetchClassifiersCache[rowNumber];
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.