Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • fixed several bugs (crossover, subsumption, serialization etc.)
  • added ModuloOperator
  • CombinedIntegerVectorClassificationProblem\Data and VariableVectorClassificationProblem\Data inherit from ConditionActionClassificationProblem\Data
  • it can now be set how often the analyzers have to be executed
  • VariableVectorAction, VariableVectorCondition and VariableVectorInput now inherit from Item
File size: 5.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.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 IFixedValueParameter<IntMatrix> BoundsParameter {
48      get { return (IFixedValueParameter<IntMatrix>)Parameters["Bounds"]; }
49    }
50    #endregion
51
52    #region properties
53    public IntValue Length {
54      get { return LengthParameter.Value; }
55    }
56
57    public IntValue ActionLength {
58      get { return ActionLengthParameter.Value; }
59    }
60
61    public IntMatrix Bounds {
62      get { return BoundsParameter.Value; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    protected CombinedIntegerVectorClassificationProblemData(bool deserializing) : base(deserializing) { }
68    protected CombinedIntegerVectorClassificationProblemData(CombinedIntegerVectorClassificationProblemData original, Cloner cloner)
69      : base(original, cloner) {
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new CombinedIntegerVectorClassificationProblemData(this, cloner);
73    }
74
75    public CombinedIntegerVectorClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) :
76      base(dataset, allowedConditionVariables, allowedActionVariables) {
77      Parameters.Add(new FixedValueParameter<IntValue>("Length", "", new IntValue(allowedConditionVariables.Count() + allowedActionVariables.Count())));
78      Parameters.Add(new FixedValueParameter<IntValue>("ActionLength", "", new IntValue(allowedActionVariables.Count())));
79      Parameters.Add(new FixedValueParameter<IntMatrix>("Bounds", "", GetBoundsMatrix(dataset, allowedConditionVariables, allowedActionVariables)));
80    }
81
82    private IntMatrix GetBoundsMatrix(Dataset dataset, IEnumerable<string> conditionVariables, IEnumerable<string> actionVariables) {
83      IntMatrix bounds = new IntMatrix(conditionVariables.Count() + actionVariables.Count(), 2);
84      int index = 0;
85      foreach (var variable in conditionVariables) {
86        var values = dataset.GetDoubleValues(variable);
87        bounds[index, 0] = (int)values.Min();
88        bounds[index, 1] = (int)values.Max() + 2;
89        index++;
90      }
91      foreach (var variable in actionVariables) {
92        var values = dataset.GetDoubleValues(variable);
93        bounds[index, 0] = (int)values.Min();
94        bounds[index, 1] = (int)values.Max() + 1;
95        index++;
96      }
97      return bounds;
98    }
99
100    public override IInput FetchInput(int rowNumber) {
101      if (!fetchInputCache.ContainsKey(rowNumber)) {
102        int[] elements = new int[Length.Value];
103        var variableNamesList = Dataset.VariableNames.ToList();
104        int elementIndex = 0;
105        for (int i = 0; i < variableNamesList.Count; i++) {
106          if (AllowedConditionVariables.Contains(variableNamesList[i])) {
107            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
108            elementIndex++;
109          }
110        }
111        for (int i = 0; i < variableNamesList.Count; i++) {
112          if (AllowedActionVariables.Contains(variableNamesList[i])) {
113            elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i));
114            elementIndex++;
115          }
116        }
117        if (elementIndex != Length.Value) {
118          throw new ArgumentException("Length of classifier is not equal to the number of allowed condition + action variables.");
119        }
120        fetchInputCache.Add(rowNumber, new CombinedIntegerVector(elements, ActionLengthParameter.Value.Value, BoundsParameter.Value));
121      }
122      return fetchInputCache[rowNumber];
123    }
124
125    public override IAction FetchAction(int rowNumber) {
126      return (FetchInput(rowNumber) as IClassifier).Action;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.