Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • made classes in Problems.ConditionActionClassification abstract
  • added Problems.VariableVectorClassification and Problems.CombinedIntegerVectorClassification
  • LCS works now with arbitrary problems, which implement ConditionActionClassificationProblem
File size: 8.0 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> {
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          SetProblemDataSpecificParameters();
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", new CombinedIntegerVectorComparer()));
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      SetProblemDataSpecificParameters();
94
95      InitializeOperators();
96
97      problemData.Changed += new System.EventHandler(problemData_Changed);
98    }
99
100    private void problemData_Changed(object sender, System.EventArgs e) {
101      SetProblemDataSpecificParameters();
102    }
103
104    private void SetProblemDataSpecificParameters() {
105      SolutionCreator.ActionPartLengthParameter.Value = ProblemData.ActionLengthParameter.Value;
106      SolutionCreator.LengthParameter.Value = ProblemData.LengthParameter.Value;
107      SolutionCreator.BoundsParameter.Value = ProblemData.BoundsParameter.Value;
108
109      SetPossibleActions();
110    }
111
112    private void InitializeOperators() {
113      Operators.AddRange(ApplicationManager.Manager.GetInstances<ICombinedIntegerVectorCrossover>());
114      Operators.AddRange(AddManipulators());
115
116      ParameterizeOperators();
117    }
118
119    private IEnumerable<ICombinedIntegerVectorManipulator> AddManipulators() {
120      var manipulator = new UniformSomePositionManipulator();
121      manipulator.ChildParameter.ActualName = ChildName;
122      manipulator.FetchedInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
123      manipulator.PossibleActionsParameter.ActualName = PossibleActionsConcreteClassParameter.Name;
124      return new List<ICombinedIntegerVectorManipulator>() { manipulator };
125    }
126
127    protected override void SetPossibleActions() {
128      //get bounds of action
129      IntMatrix actionBounds = GetElementsOfBoundsForAction(ProblemData.Bounds, ProblemData.Length.Value, ProblemData.ActionLength.Value);
130      int actionLength = ProblemData.ActionLength.Value;
131      int start = ProblemData.Length.Value - actionLength;
132      int[] elements = new int[actionLength];
133      int[] curPos = new int[actionLength];
134      bool done = false;
135      //initialize curPos
136      for (int i = 0; i < actionBounds.Rows; i++) {
137        curPos[i] = actionBounds[i, 0];
138      }
139      PossibleActions.Clear();
140      PossibleActionsConcreteClass.Clear();
141      while (!done) {
142        PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
143        PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
144        curPos = GetNextAction(curPos, actionBounds, out done);
145      }
146      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
147    }
148
149    private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) {
150      int cur = 0;
151      while (cur < curPos.Length) {
152        curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2];
153        if (curPos[cur] >= actionBounds[cur, 1]) {
154          curPos[cur] = actionBounds[cur, 0];
155          cur++;
156        } else {
157          break;
158        }
159      }
160      done = cur >= curPos.Length;
161      return curPos;
162    }
163
164    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
165      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
166      int start = length - actionPartLength;
167      for (int i = start; i < length; i++) {
168        int pos = i % bounds.Rows;
169        for (int j = 0; j < bounds.Columns; j++) {
170          actionBounds[i - start, j] = bounds[pos, j];
171        }
172      }
173      return actionBounds;
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.