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

Last change on this file since 9467 was 9467, checked in by sforsten, 8 years ago

#1980:

  • added ProportionalTournamentSelector for XCS
  • fixed bug: if an initial population is created in XCS, the initial population also creates general classifier, not only specific ones
  • merged r9204:9466 HeuristicLab.Core from trunk to branch
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.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", problemData.ConcreteClassifierComparer));
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      SolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
98
99      SetProblemDataSettings();
100
101      InitializeOperators();
102    }
103
104    protected override void SetProblemDataSettings() {
105      SolutionCreator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
106
107      VariableVectorCoveringCreator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
108
109      SetPossibleActions();
110    }
111
112    private void InitializeOperators() {
113      Operators.AddRange(ApplicationManager.Manager.GetInstances<IVariableVectorCrossover>());
114      Operators.AddRange(AddManipulators());
115
116      ParameterizeOperators();
117    }
118
119    protected override void ParameterizeOperators() {
120      base.ParameterizeOperators();
121      foreach (IVariableVectorCrossover op in Operators.OfType<IVariableVectorCrossover>()) {
122        op.ParentsParameter.ActualName = SolutionCreator.VariableVectorParameter.ActualName;
123        op.ParentsParameter.Hidden = true;
124        op.ChildParameter.ActualName = SolutionCreator.VariableVectorParameter.ActualName;
125        op.ChildParameter.Hidden = true;
126      }
127    }
128
129    private IEnumerable<IManipulator> AddManipulators() {
130      var manipulator = new UniformSomePositionManipulator();
131      manipulator.ChildParameter.ActualName = ChildName;
132      manipulator.FetchedInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
133      manipulator.PossibleActionsParameter.ActualName = PossibleActionsConcreteClassParameter.Name;
134      return new List<IManipulator>() { manipulator };
135    }
136
137    protected override void SetPossibleActions() {
138      var sampleAction = ProblemData.SampleVariableVector.Action;
139      var actionKeys = sampleAction.Order;
140      var actionsEnumeratorList = sampleAction.VariableDictionary.ToDictionary(x => x.Key, y => y.Value.GetAllPossibleActions().ToList());
141      int[] curPos = new int[actionsEnumeratorList.Count];
142      //initialize curPos
143      for (int i = 0; i < curPos.Length; i++) {
144        curPos[i] = 0;
145      }
146      bool done = false;
147      PossibleActions.Clear();
148      PossibleActionsConcreteClass.Clear();
149      VariableVectorAction curAction;
150      int cur;
151      while (!done) {
152        curAction = sampleAction.GetEmptyCopy();
153        cur = 0;
154        foreach (var key in actionKeys) {
155          curAction.VariableDictionary[key].SetTo(actionsEnumeratorList[key][curPos[cur]]);
156          cur++;
157        }
158        PossibleActions.Add(curAction);
159        PossibleActionsConcreteClass.Add(curAction);
160        done = MoveNext(actionsEnumeratorList, curPos, actionKeys);
161      }
162      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
163    }
164
165    private bool MoveNext(Dictionary<string, List<string>> actionsEnumeratorList, int[] curPos, IEnumerable<string> order) {
166      int cur = 0;
167      bool done = true;
168      foreach (var key in order) {
169        if (curPos[cur] + 1 < actionsEnumeratorList[key].Count) {
170          curPos[cur]++;
171          done = false;
172          break;
173        } else {
174          curPos[cur] = 0;
175        }
176        cur++;
177      }
178      return done;
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.