Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added BestTrainingXCSSolutionAnalyzer and CurrentXCSSolutionAnalyzer
  • fixed bug: Equals method was not correct in CombinedIntegerVector
File size: 15.2 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<IClassifier>> PossibleActionsParameter {
72      get { return (IFixedValueParameter<ItemSet<IClassifier>>)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    #endregion
81
82    #region properties
83    IParameter IConditionActionProblem.ProblemDataParameter {
84      get { return ProblemDataParameter; }
85    }
86    IConditionActionProblemData IConditionActionProblem.ProblemData {
87      get { return ProblemData; }
88    }
89    public ConditionActionClassificationProblemData ProblemData {
90      get { return ProblemDataParameter.Value; }
91      protected set {
92        ProblemDataParameter.Value = value;
93        if (value != null) {
94          SetProblemDataSpecificParameters();
95        }
96      }
97    }
98    IParameter IConditionActionProblem.PossibleActionsConcreteClassParameter {
99      get { return PossibleActionsConcreteClassParameter; }
100    }
101    public ItemSet<CombinedIntegerVector> PossibleActionsConcreteClass {
102      get { return PossibleActionsConcreteClassParameter.Value; }
103    }
104    IParameter IConditionActionProblem.PossibleActionsParameter {
105      get { return PossibleActionsParameter; }
106    }
107    //IItemSet<IClassifier> IConditionActionProblem.PossibleActions {
108    //  get { return PossibleActions; }
109    //}
110    public ItemSet<IClassifier> PossibleActions {
111      get { return PossibleActionsParameter.Value; }
112    }
113    public IActionExecuter ActionExecuter {
114      get { return ActionExecuterParameter.Value; }
115    }
116    public ValueParameter<IActionExecuter> ActionExecuterParameter {
117      get { return (ValueParameter<IActionExecuter>)Parameters[ActionExecuterParameterName]; }
118    }
119    IParameter IConditionActionProblem.ActionExecuterParameter { get { return ActionExecuterParameter; } }
120    public ClassifierFetcher ClassifierFetcher {
121      get { return ClassifierFetcherParameter.Value; }
122    }
123    public ValueParameter<ClassifierFetcher> ClassifierFetcherParameter {
124      get { return (ValueParameter<ClassifierFetcher>)Parameters[ClassifierFetcherParameterName]; }
125    }
126    IClassifierFetcher IConditionActionProblem.ClassifierFetcher { get { return ClassifierFetcher; } }
127    IParameter IConditionActionProblem.ClassifierFetcherParameter { get { return ClassifierFetcherParameter; } }
128
129
130    public ActionSetSubsumptionOperator ActionSetSubsumptionOperator {
131      get { return ActionSetSubsumptionOperatorParameter.Value; }
132    }
133    public ValueParameter<ActionSetSubsumptionOperator> ActionSetSubsumptionOperatorParameter {
134      get { return (ValueParameter<ActionSetSubsumptionOperator>)Parameters[ActionSetSubsumptionOperatorParameterName]; }
135    }
136    IActionSetSubsumption IConditionActionProblem.ActionSetSubsumptionOperator { get { return ActionSetSubsumptionOperator; } }
137    IParameter IConditionActionProblem.ActionSetSubsumptionOperatorParameter { get { return ActionSetSubsumptionOperatorParameter; } }
138
139    private IntValue ThetaMinimalNumberOfActions {
140      get { return ThetaMinimalNumberOfActionsParameter.Value; }
141    }
142    IParameter IConditionActionProblem.ThetaMinimalNumberOfActionsParameter {
143      get { return ThetaMinimalNumberOfActionsParameter; }
144    }
145    public ICoveringSolutionCreator CoveringSolutionCreator {
146      get { return CoveringSolutionCreatorParameter.Value; }
147    }
148    IParameter IConditionActionProblem.CoveringSolutionCreatorParameter {
149      get { return CoveringSolutionCreatorParameter; }
150    }
151    #endregion
152
153    [StorableConstructor]
154    protected ConditionActionClassificationProblem(bool deserializing) : base(deserializing) { }
155    protected ConditionActionClassificationProblem(ConditionActionClassificationProblem original, Cloner cloner)
156      : base(original, cloner) {
157    }
158    public override IDeepCloneable Clone(Cloner cloner) {
159      return new ConditionActionClassificationProblem(this, cloner);
160    }
161
162    public ConditionActionClassificationProblem() :
163      this(new ConditionActionClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
164        ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
165        new XCSEvaluator(), new UniformRandomCombinedIntegerVectorCreator(), new CombinedIntegerVectorCoveringCreator()) {
166    }
167
168    public ConditionActionClassificationProblem(ConditionActionClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomCombinedIntegerVectorCreator solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
169      : base(evaluator, solutionCreator) {
170      Parameters.Add(new ValueParameter<ConditionActionClassificationProblemData>("ProblemData", "", problemData));
171      Parameters.Add(new FixedValueParameter<DoubleValue>("PositiveReward", "", new DoubleValue(1000)));
172      Parameters.Add(new FixedValueParameter<DoubleValue>("NegativeReward", "", new DoubleValue(0)));
173      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialPrediction", "Initial Presiction", new DoubleValue(0)));
174      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialError", "Initial Error", new DoubleValue(0)));
175      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialFitness", "Initial Fitness", new DoubleValue(0)));
176
177      Parameters.Add(new ValueParameter<IActionExecuter>(ActionExecuterParameterName, "", new ActionExecuter()));
178      Parameters.Add(new ValueParameter<ClassifierFetcher>(ClassifierFetcherParameterName, "", new ClassifierFetcher()));
179      Parameters.Add(new FixedValueParameter<ItemSet<IClassifier>>("PossibleActions"));
180      Parameters.Add(new FixedValueParameter<ItemSet<CombinedIntegerVector>>("PossibleActionsConcreteClass"));
181      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)));
182
183      Parameters.Add(new ValueParameter<ICoveringSolutionCreator>("CoveringSolutionCreator", "", coveringSolutionCreator));
184      Parameters.Add(new FixedValueParameter<PercentValue>("ChangeSymbolProbabilityInCovering", "", new PercentValue(0.33)));
185
186      Parameters.Add(new ValueParameter<ActionSetSubsumptionOperator>(ActionSetSubsumptionOperatorParameterName, "", new ActionSetSubsumptionOperator()));
187
188      Evaluator.InitialErrorParameter.ActualName = "InitialError";
189      Evaluator.InitialFitnessParameter.ActualName = "InitialFitness";
190      Evaluator.InitialPredictionParameter.ActualName = "InitialPrediction";
191
192      coveringSolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
193      coveringSolutionCreator.CoverClassifierParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
194      coveringSolutionCreator.CreatedClassifierParameter.ActualName = "CombinedIntegerVector";
195
196      ClassifierFetcher.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
197
198      ActionExecuter.CurrentClassifierToMatchParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
199      ActionExecuter.NegativeRewardParameter.ActualName = NegativeRewardParameter.Name;
200      ActionExecuter.PositiveRewardParameter.ActualName = PositiveRewardParameter.Name;
201
202      ActionSetSubsumptionOperator.ClassifiersParameter.ActualName = "CombinedIntegerVector";
203
204      SetProblemDataSpecificParameters();
205      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
206
207      InitializeOperators();
208
209      problemData.Changed += new System.EventHandler(problemData_Changed);
210    }
211
212    private void problemData_Changed(object sender, System.EventArgs e) {
213      SetProblemDataSpecificParameters();
214    }
215
216    private void SetProblemDataSpecificParameters() {
217      SolutionCreator.ActionPartLengthParameter.ActualName = ProblemData.ActionLengthParameter.Name;
218      SolutionCreator.LengthParameter.ActualName = ProblemData.LengthParameter.Name;
219      SolutionCreator.BoundsParameter.ActualName = ProblemData.BoundsParameter.Name;
220
221      SetPossibleActions();
222    }
223
224    private void InitializeOperators() {
225      Operators.Add(new BestTrainingXCSSolutionAnalyzer());
226      Operators.Add(new CurrentXCSSolutionAnalyzer());
227
228      ParameterizeAnalyzers();
229    }
230
231    private void ParameterizeAnalyzers() {
232      foreach (XCSSolutionAnalyzer xcsAnalyzer in Operators.Where(x => x is XCSSolutionAnalyzer)) {
233        xcsAnalyzer.ClassifierParameter.ActualName = SolutionCreator.CombinedIntegerVectorParameter.ActualName;
234        xcsAnalyzer.PredictionParameter.ActualName = Evaluator.PredictionParameter.ActualName;
235        xcsAnalyzer.ErrorParameter.ActualName = Evaluator.ErrorParameter.ActualName;
236        xcsAnalyzer.FitnessParameter.ActualName = Evaluator.FitnessParameter.ActualName;
237        xcsAnalyzer.ExperienceParameter.ActualName = Evaluator.ExperienceParameter.ActualName;
238        xcsAnalyzer.AverageActionSetSizeParameter.ActualName = Evaluator.AverageActionSetSizeParameter.ActualName;
239        xcsAnalyzer.NumerosityParameter.ActualName = Evaluator.NumerosityParameter.ActualName;
240        xcsAnalyzer.TimestampParameter.ActualName = Evaluator.TimestampParameter.ActualName;
241        xcsAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
242        xcsAnalyzer.ResultsParameter.ActualName = "Results";
243      }
244    }
245
246    private void SetPossibleActions() {
247      //get bounds of action
248      IntMatrix actionBounds = GetElementsOfBoundsForAction(ProblemData.Bounds, ProblemData.Length.Value, ProblemData.ActionLength.Value);
249      int actionLength = ProblemData.ActionLength.Value;
250      int start = ProblemData.Length.Value - actionLength;
251      int[] elements = new int[actionLength];
252      int[] curPos = new int[actionLength];
253      bool done = false;
254      //initialize curPos
255      for (int i = 0; i < actionBounds.Rows; i++) {
256        curPos[i] = actionBounds[i, 0];
257      }
258      PossibleActions.Clear();
259      PossibleActionsConcreteClass.Clear();
260      while (!done) {
261        PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
262        PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
263        curPos = GetNextAction(curPos, actionBounds, out done);
264      }
265
266    }
267
268    private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) {
269      int cur = 0;
270      while (cur < curPos.Length) {
271        curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2];
272        if (curPos[cur] >= actionBounds[cur, 1]) {
273          curPos[cur] = actionBounds[cur, 0];
274          cur++;
275        } else {
276          break;
277        }
278      }
279      done = cur >= curPos.Length;
280      return curPos;
281    }
282
283    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
284      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
285      int start = length - actionPartLength;
286      for (int i = start; i < length; i++) {
287        int pos = i % bounds.Rows;
288        for (int j = 0; j < bounds.Columns; j++) {
289          actionBounds[i - start, j] = bounds[pos, j];
290        }
291      }
292      return actionBounds;
293    }
294
295    public void Load(ConditionActionClassificationProblemData data) {
296      Name = data.Name;
297      Description = data.Description;
298      ProblemData = data;
299    }
300  }
301}
Note: See TracBrowser for help on using the repository browser.