Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Problems.ConditionActionClassification/3.3/Implementation/ConditionActionClassificationProblem.cs @ 9194

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

#1980:

  • added necessary interface ICondition, IAction, IInput
  • removed not used class MatchSelector and interface IMatchSelector
  • added constructors to ItemDictionary
  • added new encoding
File size: 16.3 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.CombinedIntegerVectorEncoding;
27using HeuristicLab.Encodings.ConditionActionEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32using HeuristicLab.Problems.Instances;
33
34namespace HeuristicLab.Problems.ConditionActionClassification {
35  [StorableClass]
36  public class ConditionActionClassificationProblem : HeuristicOptimizationProblem<XCSEvaluator, UniformRandomCombinedIntegerVectorCreator>, IConditionActionProblem,
37  IProblemInstanceConsumer<ConditionActionClassificationProblemData> {
38    private const string ClassifierFetcherParameterName = "ClassifierFetcher";
39    private const string ActionExecuterParameterName = "ActionExecuter";
40    private const string ActionSetSubsumptionOperatorParameterName = "ActionSetSubsumption";
41
42    IXCSEvaluator IConditionActionProblem.Evaluator {
43      get { return Evaluator; }
44    }
45
46    #region parameter properties
47    public IValueParameter<ConditionActionClassificationProblemData> ProblemDataParameter {
48      get { return (IValueParameter<ConditionActionClassificationProblemData>)Parameters["ProblemData"]; }
49    }
50    public IValueParameter<ICoveringSolutionCreator> CoveringSolutionCreatorParameter {
51      get { return (IValueParameter<ICoveringSolutionCreator>)Parameters["CoveringSolutionCreator"]; }
52    }
53    public IFixedValueParameter<PercentValue> ChangeSymbolProbabilityInCoveringParameter {
54      get { return (IFixedValueParameter<PercentValue>)Parameters["ChangeSymbolProbabilityInCovering"]; }
55    }
56    public IFixedValueParameter<DoubleValue> PositiveRewardParameter {
57      get { return (IFixedValueParameter<DoubleValue>)Parameters["PositiveReward"]; }
58    }
59    public IFixedValueParameter<DoubleValue> NegativeRewardParameter {
60      get { return (IFixedValueParameter<DoubleValue>)Parameters["NegativeReward"]; }
61    }
62    public IFixedValueParameter<DoubleValue> InitialPredictionParameter {
63      get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialPrediction"]; }
64    }
65    public IFixedValueParameter<DoubleValue> InitialErrorParameter {
66      get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialError"]; }
67    }
68    public IFixedValueParameter<DoubleValue> InitialFitnessParameter {
69      get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialFitness"]; }
70    }
71    public IFixedValueParameter<ItemSet<IAction>> PossibleActionsParameter {
72      get { return (IFixedValueParameter<ItemSet<IAction>>)Parameters["PossibleActions"]; }
73    }
74    public IFixedValueParameter<ItemSet<CombinedIntegerVector>> PossibleActionsConcreteClassParameter {
75      get { return (IFixedValueParameter<ItemSet<CombinedIntegerVector>>)Parameters["PossibleActionsConcreteClass"]; }
76    }
77    public IFixedValueParameter<IntValue> ThetaMinimalNumberOfActionsParameter {
78      get { return (IFixedValueParameter<IntValue>)Parameters["ThetaMinimalNumberOfActions"]; }
79    }
80    public IFixedValueParameter<HeuristicLab.Encodings.CombinedIntegerVectorEncoding.CombinedIntegerVector.CombinedIntegerVectorComparer> ClassifierComparerParameter {
81      get { return (IFixedValueParameter<HeuristicLab.Encodings.CombinedIntegerVectorEncoding.CombinedIntegerVector.CombinedIntegerVectorComparer>)Parameters["ClassifierComparer"]; }
82    }
83    #endregion
84
85    #region properties
86    IParameter IConditionActionProblem.ProblemDataParameter {
87      get { return ProblemDataParameter; }
88    }
89    IConditionActionProblemData IConditionActionProblem.ProblemData {
90      get { return ProblemData; }
91    }
92    public ConditionActionClassificationProblemData ProblemData {
93      get { return ProblemDataParameter.Value; }
94      protected set {
95        ProblemDataParameter.Value = value;
96        if (value != null) {
97          SetProblemDataSpecificParameters();
98        }
99      }
100    }
101    IParameter IConditionActionProblem.PossibleActionsConcreteClassParameter {
102      get { return PossibleActionsConcreteClassParameter; }
103    }
104    public ItemSet<CombinedIntegerVector> PossibleActionsConcreteClass {
105      get { return PossibleActionsConcreteClassParameter.Value; }
106    }
107    IParameter IConditionActionProblem.PossibleActionsParameter {
108      get { return PossibleActionsParameter; }
109    }
110    //IItemSet<IClassifier> IConditionActionProblem.PossibleActions {
111    //  get { return PossibleActions; }
112    //}
113    public ItemSet<IAction> PossibleActions {
114      get { return PossibleActionsParameter.Value; }
115    }
116    public IActionExecuter ActionExecuter {
117      get { return ActionExecuterParameter.Value; }
118    }
119    public ValueParameter<IActionExecuter> ActionExecuterParameter {
120      get { return (ValueParameter<IActionExecuter>)Parameters[ActionExecuterParameterName]; }
121    }
122    IParameter IConditionActionProblem.ActionExecuterParameter { get { return ActionExecuterParameter; } }
123    public ClassifierFetcher ClassifierFetcher {
124      get { return ClassifierFetcherParameter.Value; }
125    }
126    public ValueParameter<ClassifierFetcher> ClassifierFetcherParameter {
127      get { return (ValueParameter<ClassifierFetcher>)Parameters[ClassifierFetcherParameterName]; }
128    }
129    IClassifierFetcher IConditionActionProblem.ClassifierFetcher { get { return ClassifierFetcher; } }
130    IParameter IConditionActionProblem.ClassifierFetcherParameter { get { return ClassifierFetcherParameter; } }
131
132
133    public ActionSetSubsumptionOperator ActionSetSubsumptionOperator {
134      get { return ActionSetSubsumptionOperatorParameter.Value; }
135    }
136    public ValueParameter<ActionSetSubsumptionOperator> ActionSetSubsumptionOperatorParameter {
137      get { return (ValueParameter<ActionSetSubsumptionOperator>)Parameters[ActionSetSubsumptionOperatorParameterName]; }
138    }
139    IActionSetSubsumption IConditionActionProblem.ActionSetSubsumptionOperator { get { return ActionSetSubsumptionOperator; } }
140    IParameter IConditionActionProblem.ActionSetSubsumptionOperatorParameter { get { return ActionSetSubsumptionOperatorParameter; } }
141
142    private IntValue ThetaMinimalNumberOfActions {
143      get { return ThetaMinimalNumberOfActionsParameter.Value; }
144    }
145    IParameter IConditionActionProblem.ThetaMinimalNumberOfActionsParameter {
146      get { return ThetaMinimalNumberOfActionsParameter; }
147    }
148    public ICoveringSolutionCreator CoveringSolutionCreator {
149      get { return CoveringSolutionCreatorParameter.Value; }
150    }
151    IParameter IConditionActionProblem.CoveringSolutionCreatorParameter {
152      get { return CoveringSolutionCreatorParameter; }
153    }
154    public IClassifierComparer ClassifierComparer {
155      get { return ClassifierComparerParameter.Value; }
156    }
157    IParameter IConditionActionProblem.ClassifierComparerParameter {
158      get { return ClassifierComparerParameter; }
159    }
160    #endregion
161
162    [StorableConstructor]
163    protected ConditionActionClassificationProblem(bool deserializing) : base(deserializing) { }
164    protected ConditionActionClassificationProblem(ConditionActionClassificationProblem original, Cloner cloner)
165      : base(original, cloner) {
166    }
167    public override IDeepCloneable Clone(Cloner cloner) {
168      return new ConditionActionClassificationProblem(this, cloner);
169    }
170
171    public ConditionActionClassificationProblem() :
172      this(new ConditionActionClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
173        ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
174        new XCSEvaluator(), new UniformRandomCombinedIntegerVectorCreator(), new CombinedIntegerVectorCoveringCreator()) {
175    }
176
177    public ConditionActionClassificationProblem(ConditionActionClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomCombinedIntegerVectorCreator solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
178      : base(evaluator, solutionCreator) {
179      Parameters.Add(new ValueParameter<ConditionActionClassificationProblemData>("ProblemData", "", problemData));
180      Parameters.Add(new FixedValueParameter<DoubleValue>("PositiveReward", "", new DoubleValue(1000)));
181      Parameters.Add(new FixedValueParameter<DoubleValue>("NegativeReward", "", new DoubleValue(0)));
182      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialPrediction", "Initial Presiction", new DoubleValue(0)));
183      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialError", "Initial Error", new DoubleValue(0)));
184      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialFitness", "Initial Fitness", new DoubleValue(0)));
185
186      Parameters.Add(new ValueParameter<IActionExecuter>(ActionExecuterParameterName, "", new ActionExecuter()));
187      Parameters.Add(new ValueParameter<ClassifierFetcher>(ClassifierFetcherParameterName, "", new ClassifierFetcher()));
188      Parameters.Add(new FixedValueParameter<HeuristicLab.Encodings.CombinedIntegerVectorEncoding.CombinedIntegerVector.CombinedIntegerVectorComparer>("ClassifierComparer", new HeuristicLab.Encodings.CombinedIntegerVectorEncoding.CombinedIntegerVector.CombinedIntegerVectorComparer()));
189      Parameters.Add(new FixedValueParameter<ItemSet<IAction>>("PossibleActions", new ItemSet<IAction>(ClassifierComparer)));
190      Parameters.Add(new FixedValueParameter<ItemSet<CombinedIntegerVector>>("PossibleActionsConcreteClass", new ItemSet<CombinedIntegerVector>(ClassifierComparer)));
191      Parameters.Add(new FixedValueParameter<IntValue>("ThetaMinimalNumberOfActions", "Minimal number of actions, which have to be present in the match set, or else covering will occure.", new IntValue(1)));
192
193      Parameters.Add(new ValueParameter<ICoveringSolutionCreator>("CoveringSolutionCreator", "", coveringSolutionCreator));
194      Parameters.Add(new FixedValueParameter<PercentValue>("ChangeSymbolProbabilityInCovering", "", new PercentValue(0.33)));
195
196      Parameters.Add(new ValueParameter<ActionSetSubsumptionOperator>(ActionSetSubsumptionOperatorParameterName, "", new ActionSetSubsumptionOperator()));
197
198      Evaluator.InitialErrorParameter.ActualName = "InitialError";
199      Evaluator.InitialFitnessParameter.ActualName = "InitialFitness";
200      Evaluator.InitialPredictionParameter.ActualName = "InitialPrediction";
201
202      coveringSolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
203      coveringSolutionCreator.CoverInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
204      coveringSolutionCreator.CreatedClassifierParameter.ActualName = "CombinedIntegerVector";
205
206      ClassifierFetcher.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
207
208      ActionExecuter.CurrentActionToMatchParameter.ActualName = ClassifierFetcher.CurrentActionToMatchParameter.ActualName;
209      ActionExecuter.NegativeRewardParameter.ActualName = NegativeRewardParameter.Name;
210      ActionExecuter.PositiveRewardParameter.ActualName = PositiveRewardParameter.Name;
211
212      ActionSetSubsumptionOperator.ClassifiersParameter.ActualName = "CombinedIntegerVector";
213
214      SetProblemDataSpecificParameters();
215      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
216
217      InitializeOperators();
218
219      problemData.Changed += new System.EventHandler(problemData_Changed);
220    }
221
222    private void problemData_Changed(object sender, System.EventArgs e) {
223      SetProblemDataSpecificParameters();
224    }
225
226    private void SetProblemDataSpecificParameters() {
227      SolutionCreator.ActionPartLengthParameter.ActualName = ProblemData.ActionLengthParameter.Name;
228      SolutionCreator.LengthParameter.ActualName = ProblemData.LengthParameter.Name;
229      SolutionCreator.BoundsParameter.ActualName = ProblemData.BoundsParameter.Name;
230
231      SetPossibleActions();
232    }
233
234    private void InitializeOperators() {
235      Operators.Add(new BestTrainingXCSSolutionAnalyzer());
236      Operators.Add(new CurrentXCSSolutionAnalyzer());
237
238      ParameterizeAnalyzers();
239    }
240
241    private void ParameterizeAnalyzers() {
242      foreach (XCSSolutionAnalyzer xcsAnalyzer in Operators.Where(x => x is XCSSolutionAnalyzer)) {
243        xcsAnalyzer.ClassifierParameter.ActualName = SolutionCreator.CombinedIntegerVectorParameter.ActualName;
244        xcsAnalyzer.PredictionParameter.ActualName = Evaluator.PredictionParameter.ActualName;
245        xcsAnalyzer.ErrorParameter.ActualName = Evaluator.ErrorParameter.ActualName;
246        xcsAnalyzer.FitnessParameter.ActualName = Evaluator.FitnessParameter.ActualName;
247        xcsAnalyzer.ExperienceParameter.ActualName = Evaluator.ExperienceParameter.ActualName;
248        xcsAnalyzer.AverageActionSetSizeParameter.ActualName = Evaluator.AverageActionSetSizeParameter.ActualName;
249        xcsAnalyzer.NumerosityParameter.ActualName = Evaluator.NumerosityParameter.ActualName;
250        xcsAnalyzer.TimestampParameter.ActualName = Evaluator.TimestampParameter.ActualName;
251        xcsAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
252        xcsAnalyzer.ResultsParameter.ActualName = "Results";
253        xcsAnalyzer.ClassifierComparerParameter.ActualName = ClassifierComparerParameter.Name;
254      }
255    }
256
257    private void SetPossibleActions() {
258      //get bounds of action
259      IntMatrix actionBounds = GetElementsOfBoundsForAction(ProblemData.Bounds, ProblemData.Length.Value, ProblemData.ActionLength.Value);
260      int actionLength = ProblemData.ActionLength.Value;
261      int start = ProblemData.Length.Value - actionLength;
262      int[] elements = new int[actionLength];
263      int[] curPos = new int[actionLength];
264      bool done = false;
265      //initialize curPos
266      for (int i = 0; i < actionBounds.Rows; i++) {
267        curPos[i] = actionBounds[i, 0];
268      }
269      PossibleActions.Clear();
270      PossibleActionsConcreteClass.Clear();
271      while (!done) {
272        PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
273        PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
274        curPos = GetNextAction(curPos, actionBounds, out done);
275      }
276
277    }
278
279    private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) {
280      int cur = 0;
281      while (cur < curPos.Length) {
282        curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2];
283        if (curPos[cur] >= actionBounds[cur, 1]) {
284          curPos[cur] = actionBounds[cur, 0];
285          cur++;
286        } else {
287          break;
288        }
289      }
290      done = cur >= curPos.Length;
291      return curPos;
292    }
293
294    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
295      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
296      int start = length - actionPartLength;
297      for (int i = start; i < length; i++) {
298        int pos = i % bounds.Rows;
299        for (int j = 0; j < bounds.Columns; j++) {
300          actionBounds[i - start, j] = bounds[pos, j];
301        }
302      }
303      return actionBounds;
304    }
305
306    public void Load(ConditionActionClassificationProblemData data) {
307      Name = data.Name;
308      Description = data.Description;
309      ProblemData = data;
310    }
311  }
312}
Note: See TracBrowser for help on using the repository browser.