source: branches/LearningClassifierSystems/HeuristicLab.Problems.VariableVectorClassification/3.3/VariableVectorClassificationProblemData.cs @ 9411

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

#1980:

  • added multiple discretizer to GAssist
  • created ensembles for LCS problems and edited CrossValidation to use them
File size: 6.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.Encodings.VariableVector;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.ConditionActionClassification;
33using HeuristicLab.Problems.DataAnalysis;
34
35namespace HeuristicLab.Problems.VariableVectorClassification {
36  [StorableClass]
37  [Item("CombinedIntegerVectorClassificationProblemData", "A problem data for LCS.")]
38  public class VariableVectorClassificationProblemData : ConditionActionClassificationProblemData, IVariableVectorClassificationProblemData {
39
40    #region parameter properites
41    public IValueParameter<VariableVector> SampleVariableVectorParameter {
42      get { return (IValueParameter<VariableVector>)Parameters["SampleVariableVector"]; }
43    }
44    public IFixedValueParameter<PercentValue> SpreadPercentageParameter {
45      get { return (IFixedValueParameter<PercentValue>)Parameters["SpreadPercentage"]; }
46    }
47    #endregion
48
49    #region properties
50    public VariableVector SampleVariableVector {
51      get { return SampleVariableVectorParameter.Value; }
52    }
53    public PercentValue SpreadPercentage {
54      get { return SpreadPercentageParameter.Value; }
55    }
56
57    public VariableVectorActionComparer ConcreteClassifierComparer {
58      get { return new VariableVectorActionComparer(); }
59    }
60    public override IClassifierComparer ClassifierComparer {
61      get { return ConcreteClassifierComparer; }
62    }
63    #endregion
64
65    [StorableConstructor]
66    protected VariableVectorClassificationProblemData(bool deserializing) : base(deserializing) { }
67    protected VariableVectorClassificationProblemData(VariableVectorClassificationProblemData original, Cloner cloner)
68      : base(original, cloner) {
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new VariableVectorClassificationProblemData(this, cloner);
72    }
73
74    public VariableVectorClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) :
75      base(dataset, allowedConditionVariables, allowedActionVariables) {
76      Parameters.Add(new ValueParameter<VariableVector>("SampleVariableVector", "", GenerateSampleVariableVector(dataset, allowedConditionVariables, allowedActionVariables)));
77      Parameters.Add(new FixedValueParameter<PercentValue>("SpreadPercentage", "", new PercentValue(0.5)));
78    }
79
80    private VariableVector GenerateSampleVariableVector(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
81      var conditionVariables = GetVariablesOfDataSet(dataset, allowedConditionVariables);
82      var actionVariables = GetVariablesOfDataSet(dataset, allowedActionVariables);
83      if (actionVariables.Count() == 0 || !actionVariables.All(x => x is IActionVariable)) {
84        throw new ArgumentException("Action variable can not be empty and all action variables have to be of type int or string.");
85      }
86      return new VariableVector(conditionVariables, actionVariables);
87    }
88
89    private IEnumerable<Encodings.VariableVector.IVariable> GetVariablesOfDataSet(DataAnalysis.Dataset dataset, IEnumerable<string> allowedVariables) {
90      var variables = new List<HeuristicLab.Encodings.VariableVector.IVariable>();
91      foreach (var variableName in allowedVariables) {
92        var variableValues = dataset.GetValues(variableName);
93        HeuristicLab.Encodings.VariableVector.IVariable variable;
94        if (variableValues is List<string>) {
95          variable = new StringVariable(variableName, (variableValues as List<string>).Distinct());
96        } else if (variableValues is List<double>) {
97          var doubleValues = (variableValues as List<double>).Distinct();
98          if (doubleValues.All(x => x % 1 == 0 || Double.IsNaN(x))) {
99            // ToList call is necessary, because otherwise it wouldn't be possible to serialize it
100            variable = new IntVariable(variableName, doubleValues.Select(x => Convert.ToInt32(x)).ToList());
101          } else {
102            variable = new DoubleVariable(variableName, doubleValues);
103          }
104        } else {
105          throw new ArgumentException("There is no matching variable type for the values in the dataset");
106        }
107        variables.Add(variable);
108      }
109      return variables;
110    }
111
112    public override IInput FetchInput(int rowNumber) {
113      if (!fetchInputCache.ContainsKey(rowNumber)) {
114        VariableVectorInput input = new VariableVectorInput();
115        IEnumerable<string> variableNames = SampleVariableVector.Condition.VariableDictionary.Keys.Union(SampleVariableVector.Action.VariableDictionary.Keys);
116        foreach (var variableName in variableNames) {
117          input.InputDictionary.Add(variableName, Dataset.GetValue(rowNumber, variableName));
118        }
119        fetchInputCache.Add(rowNumber, input);
120      }
121      return fetchInputCache[rowNumber];
122    }
123
124    protected IDictionary<int, IAction> fetchActionCache = new Dictionary<int, IAction>();
125    public override IAction FetchAction(int rowNumber) {
126      if (!fetchActionCache.ContainsKey(rowNumber)) {
127        var action = SampleVariableVector.Action.GetEmptyCopy();
128        foreach (var variableName in action.VariableDictionary.Keys) {
129          action.VariableDictionary[variableName].SetTo(Dataset.GetValue(rowNumber, variableName));
130        }
131        fetchActionCache.Add(rowNumber, action);
132      }
133      return fetchActionCache[rowNumber];
134    }
135
136    protected override void ActionConditionVariablesChanged() {
137      SampleVariableVectorParameter.Value = GenerateSampleVariableVector(Dataset, AllowedConditionVariables, AllowedActionVariables);
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.