Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Problems.CombinedIntegerVectorClassification/3.3/CombinedIntegerVectorClassificationProblemData.cs @ 11571

Last change on this file since 11571 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: 6.1 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.ConditionActionClassification;
33using HeuristicLab.Problems.DataAnalysis;
34
35namespace HeuristicLab.Problems.CombinedIntegerVectorClassification {
36  [StorableClass]
37  [Item("CombinedIntegerVectorClassificationProblemData", "A problem data for LCS.")]
38  public class CombinedIntegerVectorClassificationProblemData : ConditionActionClassificationProblemData, ICombinedIntegerVectorClassificationProblemData {
39
40    #region parameter properites
41    public IFixedValueParameter<IntValue> LengthParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
43    }
44    public IFixedValueParameter<IntValue> ActionLengthParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters["ActionLength"]; }
46    }
47    public IValueParameter<IntMatrix> BoundsParameter {
48      get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; }
49    }
50    #endregion
51
52    #region properties
53    public IntValue Length {
54      get { return LengthParameter.Value; }
55    }
56    public IntValue ActionLength {
57      get { return ActionLengthParameter.Value; }
58    }
59    public IntMatrix Bounds {
60      get { return BoundsParameter.Value; }
61    }
62    public CombinedIntegerVectorComparer ConcreteClassifierComparer {
63      get { return new CombinedIntegerVectorComparer(); }
64    }
65    public override IClassifierComparer ClassifierComparer {
66      get { return ConcreteClassifierComparer; }
67    }
68    #endregion
69
70    [StorableConstructor]
71    protected CombinedIntegerVectorClassificationProblemData(bool deserializing) : base(deserializing) { }
72    protected CombinedIntegerVectorClassificationProblemData(CombinedIntegerVectorClassificationProblemData original, Cloner cloner)
73      : base(original, cloner) {
74    }
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new CombinedIntegerVectorClassificationProblemData(this, cloner);
77    }
78
79    public CombinedIntegerVectorClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) :
80      base(dataset, allowedConditionVariables, allowedActionVariables) {
81      Parameters.Add(new FixedValueParameter<IntValue>("Length", "", new IntValue(allowedConditionVariables.Count() + allowedActionVariables.Count())));
82      Parameters.Add(new FixedValueParameter<IntValue>("ActionLength", "", new IntValue(allowedActionVariables.Count())));
83      Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "", GetBoundsMatrix(dataset, allowedConditionVariables, allowedActionVariables)));
84    }
85
86    private IntMatrix GetBoundsMatrix(Dataset dataset, IEnumerable<string> conditionVariables, IEnumerable<string> actionVariables) {
87      IntMatrix bounds = new IntMatrix(conditionVariables.Count() + actionVariables.Count(), 2);
88      int index = 0;
89      foreach (var variable in conditionVariables) {
90        var values = dataset.GetDoubleValues(variable);
91        bounds[index, 0] = (int)values.Min();
92        bounds[index, 1] = (int)values.Max() + 2;
93        index++;
94      }
95      foreach (var variable in actionVariables) {
96        var values = dataset.GetDoubleValues(variable);
97        bounds[index, 0] = (int)values.Min();
98        bounds[index, 1] = (int)values.Max() + 1;
99        index++;
100      }
101      return bounds;
102    }
103
104    public override IInput FetchInput(int rowNumber) {
105      if (!fetchInputCache.ContainsKey(rowNumber)) {
106        int[] elements = new int[Length.Value];
107        var variableNamesList = Dataset.VariableNames.ToList();
108        int elementIndex = 0;
109        for (int i = 0; i < variableNamesList.Count; i++) {
110          if (AllowedConditionVariables.Contains(variableNamesList[i])) {
111            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
112            elementIndex++;
113          }
114        }
115        for (int i = 0; i < variableNamesList.Count; i++) {
116          if (AllowedActionVariables.Contains(variableNamesList[i])) {
117            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
118            elementIndex++;
119          }
120        }
121        if (elementIndex != Length.Value) {
122          throw new ArgumentException("Length of classifier is not equal to the number of allowed condition + action variables.");
123        }
124        fetchInputCache.Add(rowNumber, new CombinedIntegerVector(elements, ActionLengthParameter.Value.Value, BoundsParameter.Value));
125      }
126      return fetchInputCache[rowNumber];
127    }
128
129    public override IAction FetchAction(int rowNumber) {
130      return (FetchInput(rowNumber) as IClassifier).Action;
131    }
132
133    protected override void ActionConditionVariablesChanged() {
134      ActionLength.Value = AllowedActionVariables.Count();
135      Length.Value = AllowedConditionVariables.Count() + ActionLength.Value;
136      BoundsParameter.Value = GetBoundsMatrix(Dataset, AllowedConditionVariables, AllowedActionVariables);
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.