Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added problem instance provider to import csv files for ConditionActionClassificationProblemData
  • adapted LCSAdaptedGeneticAlgorithm to use the crossover probability
  • fixed a bug in XCSModelView
File size: 15.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<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    private XCSSolutionAnalyzer XCSSolutionAnalyzer {
152      get { return Operators.OfType<XCSSolutionAnalyzer>().FirstOrDefault(); }
153    }
154    #endregion
155
156    [StorableConstructor]
157    protected ConditionActionClassificationProblem(bool deserializing) : base(deserializing) { }
158    protected ConditionActionClassificationProblem(ConditionActionClassificationProblem original, Cloner cloner)
159      : base(original, cloner) {
160    }
161    public override IDeepCloneable Clone(Cloner cloner) {
162      return new ConditionActionClassificationProblem(this, cloner);
163    }
164
165    public ConditionActionClassificationProblem() :
166      this(new ConditionActionClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
167        ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
168        new XCSEvaluator(), new UniformRandomCombinedIntegerVectorCreator(), new CombinedIntegerVectorCoveringCreator()) {
169    }
170
171    public ConditionActionClassificationProblem(ConditionActionClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomCombinedIntegerVectorCreator solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
172      : base(evaluator, solutionCreator) {
173      Parameters.Add(new ValueParameter<ConditionActionClassificationProblemData>("ProblemData", "", problemData));
174      Parameters.Add(new FixedValueParameter<DoubleValue>("PositiveReward", "", new DoubleValue(1000)));
175      Parameters.Add(new FixedValueParameter<DoubleValue>("NegativeReward", "", new DoubleValue(0)));
176      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialPrediction", "Initial Presiction", new DoubleValue(0)));
177      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialError", "Initial Error", new DoubleValue(0)));
178      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialFitness", "Initial Fitness", new DoubleValue(0)));
179
180      Parameters.Add(new ValueParameter<IActionExecuter>(ActionExecuterParameterName, "", new ActionExecuter()));
181      Parameters.Add(new ValueParameter<ClassifierFetcher>(ClassifierFetcherParameterName, "", new ClassifierFetcher()));
182      Parameters.Add(new FixedValueParameter<ItemSet<IClassifier>>("PossibleActions"));
183      Parameters.Add(new FixedValueParameter<ItemSet<CombinedIntegerVector>>("PossibleActionsConcreteClass"));
184      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)));
185
186      Parameters.Add(new ValueParameter<ICoveringSolutionCreator>("CoveringSolutionCreator", "", coveringSolutionCreator));
187      Parameters.Add(new FixedValueParameter<PercentValue>("ChangeSymbolProbabilityInCovering", "", new PercentValue(0.5)));
188
189      Parameters.Add(new ValueParameter<ActionSetSubsumptionOperator>(ActionSetSubsumptionOperatorParameterName, "", new ActionSetSubsumptionOperator()));
190
191      Evaluator.InitialErrorParameter.ActualName = "InitialError";
192      Evaluator.InitialFitnessParameter.ActualName = "InitialFitness";
193      Evaluator.InitialPredictionParameter.ActualName = "InitialPrediction";
194
195      coveringSolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
196      coveringSolutionCreator.CoverClassifierParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
197      coveringSolutionCreator.CreatedClassifierParameter.ActualName = "CombinedIntegerVector";
198
199      ClassifierFetcher.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
200
201      ActionExecuter.CurrentClassifierToMatchParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
202      ActionExecuter.NegativeRewardParameter.ActualName = NegativeRewardParameter.Name;
203      ActionExecuter.PositiveRewardParameter.ActualName = PositiveRewardParameter.Name;
204
205      ActionSetSubsumptionOperator.ClassifiersParameter.ActualName = "CombinedIntegerVector";
206
207      SetProblemDataSpecificParameters();
208      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
209
210      InitializeOperators();
211
212      problemData.Changed += new System.EventHandler(problemData_Changed);
213    }
214
215    private void problemData_Changed(object sender, System.EventArgs e) {
216      SetProblemDataSpecificParameters();
217    }
218
219    private void SetProblemDataSpecificParameters() {
220      SolutionCreator.ActionPartLengthParameter.ActualName = ProblemData.ActionLengthParameter.Name;
221      SolutionCreator.LengthParameter.ActualName = ProblemData.LengthParameter.Name;
222      SolutionCreator.BoundsParameter.ActualName = ProblemData.BoundsParameter.Name;
223
224      SetPossibleActions();
225    }
226
227    private void InitializeOperators() {
228      Operators.Add(new XCSSolutionAnalyzer());
229
230      ParameterizeAnalyzers();
231    }
232
233    private void ParameterizeAnalyzers() {
234      if (XCSSolutionAnalyzer != null) {
235        XCSSolutionAnalyzer.ClassifierParameter.ActualName = SolutionCreator.CombinedIntegerVectorParameter.ActualName;
236        XCSSolutionAnalyzer.PredictionParameter.ActualName = Evaluator.PredictionParameter.ActualName;
237        XCSSolutionAnalyzer.ErrorParameter.ActualName = Evaluator.ErrorParameter.ActualName;
238        XCSSolutionAnalyzer.FitnessParameter.ActualName = Evaluator.FitnessParameter.ActualName;
239        XCSSolutionAnalyzer.ExperienceParameter.ActualName = Evaluator.ExperienceParameter.ActualName;
240        XCSSolutionAnalyzer.AverageActionSetSizeParameter.ActualName = Evaluator.AverageActionSetSizeParameter.ActualName;
241        XCSSolutionAnalyzer.NumerosityParameter.ActualName = Evaluator.NumerosityParameter.ActualName;
242        XCSSolutionAnalyzer.TimestampParameter.ActualName = Evaluator.TimestampParameter.ActualName;
243        XCSSolutionAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
244        XCSSolutionAnalyzer.ResultsParameter.ActualName = "Results";
245      }
246    }
247
248    private void SetPossibleActions() {
249      //get bounds of action
250      IntMatrix actionBounds = GetElementsOfBoundsForAction(ProblemData.Bounds, ProblemData.Length.Value, ProblemData.ActionLength.Value);
251      int actionLength = ProblemData.ActionLength.Value;
252      int start = ProblemData.Length.Value - actionLength;
253      int[] elements = new int[actionLength];
254      int[] curPos = new int[actionLength];
255      bool done = false;
256      //initialize curPos
257      for (int i = 0; i < actionBounds.Rows; i++) {
258        curPos[i] = actionBounds[i, 0];
259      }
260      PossibleActions.Clear();
261      PossibleActionsConcreteClass.Clear();
262      while (!done) {
263        PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
264        PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
265        curPos = GetNextAction(curPos, actionBounds, out done);
266      }
267
268    }
269
270    private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) {
271      int cur = 0;
272      while (cur < curPos.Length) {
273        curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2];
274        if (curPos[cur] >= actionBounds[cur, 1]) {
275          curPos[cur] = actionBounds[cur, 0];
276          cur++;
277        } else {
278          break;
279        }
280      }
281      done = cur >= curPos.Length;
282      return curPos;
283    }
284
285    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
286      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
287      int start = length - actionPartLength;
288      for (int i = start; i < length; i++) {
289        int pos = i % bounds.Rows;
290        for (int j = 0; j < bounds.Columns; j++) {
291          actionBounds[i - start, j] = bounds[pos, j];
292        }
293      }
294      return actionBounds;
295    }
296
297    public void Load(ConditionActionClassificationProblemData data) {
298      Name = data.Name;
299      Description = data.Description;
300      ProblemData = data;
301    }
302  }
303}
Note: See TracBrowser for help on using the repository browser.