Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16189 was 9475, checked in by sforsten, 12 years ago

#1980: several small bug fixes

File size: 8.1 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      manipulator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
135      return new List<IManipulator>() { manipulator };
136    }
137
138    protected override void SetPossibleActions() {
139      var sampleAction = ProblemData.SampleVariableVector.Action;
140      var actionKeys = sampleAction.Order;
141      var actionsEnumeratorList = sampleAction.VariableDictionary.ToDictionary(x => x.Key, y => y.Value.GetAllPossibleActions().ToList());
142      int[] curPos = new int[actionsEnumeratorList.Count];
143      //initialize curPos
144      for (int i = 0; i < curPos.Length; i++) {
145        curPos[i] = 0;
146      }
147      bool done = false;
148      PossibleActions.Clear();
149      PossibleActionsConcreteClass.Clear();
150      VariableVectorAction curAction;
151      int cur;
152      while (!done) {
153        curAction = sampleAction.GetEmptyCopy();
154        cur = 0;
155        foreach (var key in actionKeys) {
156          curAction.VariableDictionary[key].SetTo(actionsEnumeratorList[key][curPos[cur]]);
157          cur++;
158        }
159        PossibleActions.Add(curAction);
160        PossibleActionsConcreteClass.Add(curAction);
161        done = MoveNext(actionsEnumeratorList, curPos, actionKeys);
162      }
163      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
164    }
165
166    private bool MoveNext(Dictionary<string, List<string>> actionsEnumeratorList, int[] curPos, IEnumerable<string> order) {
167      int cur = 0;
168      bool done = true;
169      foreach (var key in order) {
170        if (curPos[cur] + 1 < actionsEnumeratorList[key].Count) {
171          curPos[cur]++;
172          done = false;
173          break;
174        } else {
175          curPos[cur] = 0;
176        }
177        cur++;
178      }
179      return done;
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.