Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Problems.VariableVectorClassification/3.3/VariableVectorClassificationProblem.cs @ 9333

Last change on this file since 9333 was 9242, checked in by sforsten, 12 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: 7.4 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.Encodings.ConditionActionEncoding;
27using HeuristicLab.Encodings.VariableVector;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.ConditionActionClassification;
33using HeuristicLab.Problems.DataAnalysis;
34
35namespace HeuristicLab.Problems.VariableVectorClassification {
36  [StorableClass]
37  public class VariableVectorClassificationProblem : ConditionActionClassificationProblem<UniformRandomVariableVectorCreator,
38                                                            VariableVectorActionComparer,
39                                                            VariableVectorClassificationProblemData>, IVariableVectorClassificationProblem {
40    public override string ChildName {
41      get { return "VariableVector"; }
42    }
43
44    #region parameter properties
45    private IFixedValueParameter<ItemSet<VariableVectorAction>> PossibleActionsConcreteClassParameter {
46      get { return (IFixedValueParameter<ItemSet<VariableVectorAction>>)Parameters["PossibleActionsConcreteClass"]; }
47    }
48    public override IFixedValueParameter<VariableVectorActionComparer> ClassifierComparerParameter {
49      get { return (IFixedValueParameter<VariableVectorActionComparer>)Parameters["ClassifierComparer"]; }
50    }
51    public IVariableVectorCoveringCreator VariableVectorCoveringCreator {
52      get { return (IVariableVectorCoveringCreator)CoveringSolutionCreator; }
53    }
54    #endregion
55
56    #region properties
57    public new VariableVectorClassificationProblemData ProblemData {
58      get { return ProblemDataParameter.Value; }
59      protected set {
60        ProblemDataParameter.Value = value;
61        if (value != null) {
62          SetProblemDataSettings();
63        }
64      }
65    }
66
67    public ItemSet<VariableVectorAction> PossibleActionsConcreteClass {
68      get { return PossibleActionsConcreteClassParameter.Value; }
69    }
70    public IClassifierComparer ClassifierComparer {
71      get { return ClassifierComparerParameter.Value; }
72    }
73    #endregion
74
75    [StorableConstructor]
76    protected VariableVectorClassificationProblem(bool deserializing) : base(deserializing) { }
77    protected VariableVectorClassificationProblem(VariableVectorClassificationProblem original, Cloner cloner)
78      : base(original, cloner) {
79    }
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new VariableVectorClassificationProblem(this, cloner);
82    }
83
84    public VariableVectorClassificationProblem() :
85      this(new VariableVectorClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
86        ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
87        new XCSEvaluator(), new UniformRandomVariableVectorCreator(), new VariableVectorCoveringCreator()) {
88    }
89
90    public VariableVectorClassificationProblem(VariableVectorClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomVariableVectorCreator solutionCreator, IVariableVectorCoveringCreator coveringSolutionCreator)
91      : base(problemData, evaluator, solutionCreator, coveringSolutionCreator) {
92      Parameters.Add(new FixedValueParameter<VariableVectorActionComparer>("ClassifierComparer", new VariableVectorActionComparer()));
93      Parameters.Add(new FixedValueParameter<ItemSet<IAction>>("PossibleActions", new ItemSet<IAction>(ClassifierComparer)));
94      Parameters.Add(new FixedValueParameter<ItemSet<VariableVectorAction>>("PossibleActionsConcreteClass", new ItemSet<VariableVectorAction>(ClassifierComparer)));
95
96      SolutionCreator.VariableVectorParameter.ActualName = ChildName;
97
98      SetProblemDataSettings();
99
100      InitializeOperators();
101    }
102
103    protected override void SetProblemDataSettings() {
104      SolutionCreator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
105
106      VariableVectorCoveringCreator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
107
108      SetPossibleActions();
109    }
110
111    private void InitializeOperators() {
112      Operators.AddRange(ApplicationManager.Manager.GetInstances<IVariableVectorCrossover>());
113      Operators.AddRange(AddManipulators());
114
115      ParameterizeOperators();
116    }
117
118    private IEnumerable<IManipulator> AddManipulators() {
119      var manipulator = new UniformSomePositionManipulator();
120      manipulator.ChildParameter.ActualName = ChildName;
121      manipulator.FetchedInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
122      manipulator.PossibleActionsParameter.ActualName = PossibleActionsConcreteClassParameter.Name;
123      return new List<IManipulator>() { manipulator };
124    }
125
126    protected override void SetPossibleActions() {
127      var sampleAction = ProblemData.SampleVariableVector.Action;
128      var actionKeys = sampleAction.Order;
129      var actionsEnumeratorList = sampleAction.VariableDictionary.ToDictionary(x => x.Key, y => y.Value.GetAllPossibleActions().ToList());
130      int[] curPos = new int[actionsEnumeratorList.Count];
131      //initialize curPos
132      for (int i = 0; i < curPos.Length; i++) {
133        curPos[i] = 0;
134      }
135      bool done = false;
136      PossibleActions.Clear();
137      PossibleActionsConcreteClass.Clear();
138      VariableVectorAction curAction;
139      int cur;
140      while (!done) {
141        curAction = sampleAction.GetEmptyCopy();
142        cur = 0;
143        foreach (var key in actionKeys) {
144          curAction.VariableDictionary[key].SetTo(actionsEnumeratorList[key][curPos[cur]]);
145          cur++;
146        }
147        PossibleActions.Add(curAction);
148        PossibleActionsConcreteClass.Add(curAction);
149        done = MoveNext(actionsEnumeratorList, curPos, actionKeys);
150      }
151      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
152    }
153
154    private bool MoveNext(Dictionary<string, List<string>> actionsEnumeratorList, int[] curPos, IEnumerable<string> order) {
155      int cur = 0;
156      bool done = true;
157      foreach (var key in order) {
158        if (curPos[cur] + 1 < actionsEnumeratorList[key].Count) {
159          curPos[cur]++;
160          done = false;
161          break;
162        } else {
163          curPos[cur] = 0;
164        }
165        cur++;
166      }
167      return done;
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.