Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Problems.CombinedIntegerVectorClassification/3.3/CombinedIntegerVectorClassificationProblem.cs @ 11414

Last change on this file since 11414 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: 7.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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.CombinedIntegerVectorEncoding;
28using HeuristicLab.Encodings.ConditionActionEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.ConditionActionClassification;
33using HeuristicLab.Problems.DataAnalysis;
34
35namespace HeuristicLab.Problems.CombinedIntegerVectorClassification {
36  [StorableClass]
37  public class CombinedIntegerVectorClassificationProblem : ConditionActionClassificationProblem<UniformRandomCombinedIntegerVectorCreator,
38                                                            CombinedIntegerVectorComparer,
39                                                            CombinedIntegerVectorClassificationProblemData>, ICombinedIntegerVectorClassificationProblem {
40    public override string ChildName {
41      get { return "CombinedIntegerVector"; }
42    }
43
44    #region parameter properties
45    private IFixedValueParameter<ItemSet<CombinedIntegerVector>> PossibleActionsConcreteClassParameter {
46      get { return (IFixedValueParameter<ItemSet<CombinedIntegerVector>>)Parameters["PossibleActionsConcreteClass"]; }
47    }
48    public override IFixedValueParameter<CombinedIntegerVectorComparer> ClassifierComparerParameter {
49      get { return (IFixedValueParameter<CombinedIntegerVectorComparer>)Parameters["ClassifierComparer"]; }
50    }
51    #endregion
52
53    #region properties
54    public new CombinedIntegerVectorClassificationProblemData ProblemData {
55      get { return ProblemDataParameter.Value; }
56      protected set {
57        ProblemDataParameter.Value = value;
58        if (value != null) {
59          SetProblemDataSettings();
60        }
61      }
62    }
63
64    public ItemSet<CombinedIntegerVector> PossibleActionsConcreteClass {
65      get { return PossibleActionsConcreteClassParameter.Value; }
66    }
67    public IClassifierComparer ClassifierComparer {
68      get { return ClassifierComparerParameter.Value; }
69    }
70    #endregion
71
72    [StorableConstructor]
73    protected CombinedIntegerVectorClassificationProblem(bool deserializing) : base(deserializing) { }
74    protected CombinedIntegerVectorClassificationProblem(CombinedIntegerVectorClassificationProblem original, Cloner cloner)
75      : base(original, cloner) {
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new CombinedIntegerVectorClassificationProblem(this, cloner);
79    }
80
81    public CombinedIntegerVectorClassificationProblem() :
82      this(new CombinedIntegerVectorClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
83        ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
84        new XCSEvaluator(), new UniformRandomCombinedIntegerVectorCreator(), new CombinedIntegerVectorCoveringCreator()) {
85    }
86
87    public CombinedIntegerVectorClassificationProblem(CombinedIntegerVectorClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomCombinedIntegerVectorCreator solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
88      : base(problemData, evaluator, solutionCreator, coveringSolutionCreator) {
89      Parameters.Add(new FixedValueParameter<CombinedIntegerVectorComparer>("ClassifierComparer", problemData.ConcreteClassifierComparer));
90      Parameters.Add(new FixedValueParameter<ItemSet<IAction>>("PossibleActions", new ItemSet<IAction>(ClassifierComparer)));
91      Parameters.Add(new FixedValueParameter<ItemSet<CombinedIntegerVector>>("PossibleActionsConcreteClass", new ItemSet<CombinedIntegerVector>(ClassifierComparer)));
92
93      SetProblemDataSettings();
94
95      InitializeOperators();
96    }
97
98    protected override void SetProblemDataSettings() {
99      SolutionCreator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
100
101      SetPossibleActions();
102    }
103
104    private void InitializeOperators() {
105      Operators.AddRange(ApplicationManager.Manager.GetInstances<ICombinedIntegerVectorCrossover>());
106      Operators.AddRange(AddManipulators());
107
108      ParameterizeOperators();
109    }
110
111    private IEnumerable<ICombinedIntegerVectorManipulator> AddManipulators() {
112      var manipulator = new UniformSomePositionManipulator();
113      manipulator.ChildParameter.ActualName = ChildName;
114      manipulator.FetchedInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
115      manipulator.PossibleActionsParameter.ActualName = PossibleActionsConcreteClassParameter.Name;
116      return new List<ICombinedIntegerVectorManipulator>() { manipulator };
117    }
118
119    protected override void SetPossibleActions() {
120      //get bounds of action
121      IntMatrix actionBounds = GetElementsOfBoundsForAction(ProblemData.Bounds, ProblemData.Length.Value, ProblemData.ActionLength.Value);
122      int actionLength = ProblemData.ActionLength.Value;
123      int start = ProblemData.Length.Value - actionLength;
124      int[] elements = new int[actionLength];
125      int[] curPos = new int[actionLength];
126      bool done = false;
127      //initialize curPos
128      for (int i = 0; i < actionBounds.Rows; i++) {
129        curPos[i] = actionBounds[i, 0];
130      }
131      PossibleActions.Clear();
132      PossibleActionsConcreteClass.Clear();
133      while (!done) {
134        PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
135        PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
136        curPos = GetNextAction(curPos, actionBounds, out done);
137      }
138      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
139    }
140
141    private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) {
142      int cur = 0;
143      while (cur < curPos.Length) {
144        curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2];
145        if (curPos[cur] >= actionBounds[cur, 1]) {
146          curPos[cur] = actionBounds[cur, 0];
147          cur++;
148        } else {
149          break;
150        }
151      }
152      done = cur >= curPos.Length;
153      return curPos;
154    }
155
156    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
157      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
158      int start = length - actionPartLength;
159      for (int i = start; i < length; i++) {
160        int pos = i % bounds.Rows;
161        for (int j = 0; j < bounds.Columns; j++) {
162          actionBounds[i - start, j] = bounds[pos, j];
163        }
164      }
165      return actionBounds;
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.