Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added GA subsumption
  • simplified deletion before covering
  • simplified XCSDeletionOperator
File size: 15.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.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;
32
33namespace HeuristicLab.Problems.ConditionActionClassification {
34  [StorableClass]
35  public class ConditionActionClassificationProblem : HeuristicOptimizationProblem<XCSEvaluator, UniformRandomCombinedIntegerVectorCreator>, IConditionActionProblem {
36    private const string ClassifierFetcherParameterName = "ClassifierFetcher";
37    private const string ActionExecuterParameterName = "ActionExecuter";
38    private const string ActionSetSubsumptionOperatorParameterName = "ActionSetSubsumption";
39
40    IXCSEvaluator IConditionActionProblem.Evaluator {
41      get { return Evaluator; }
42    }
43
44    #region parameter properties
45    public IValueParameter<ConditionActionClassificationProblemData> ProblemDataParameter {
46      get { return (IValueParameter<ConditionActionClassificationProblemData>)Parameters["ProblemData"]; }
47    }
48    public IValueParameter<ICoveringSolutionCreator> CoveringSolutionCreatorParameter {
49      get { return (IValueParameter<ICoveringSolutionCreator>)Parameters["CoveringSolutionCreator"]; }
50    }
51    public IFixedValueParameter<PercentValue> ChangeSymbolProbabilityInCoveringParameter {
52      get { return (IFixedValueParameter<PercentValue>)Parameters["ChangeSymbolProbabilityInCovering"]; }
53    }
54    public IFixedValueParameter<DoubleValue> PositiveRewardParameter {
55      get { return (IFixedValueParameter<DoubleValue>)Parameters["PositiveReward"]; }
56    }
57    public IFixedValueParameter<DoubleValue> NegativeRewardParameter {
58      get { return (IFixedValueParameter<DoubleValue>)Parameters["NegativeReward"]; }
59    }
60    public IFixedValueParameter<DoubleValue> InitialPredictionParameter {
61      get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialPrediction"]; }
62    }
63    public IFixedValueParameter<DoubleValue> InitialErrorParameter {
64      get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialError"]; }
65    }
66    public IFixedValueParameter<DoubleValue> InitialFitnessParameter {
67      get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialFitness"]; }
68    }
69    public IFixedValueParameter<ItemSet<IClassifier>> PossibleActionsParameter {
70      get { return (IFixedValueParameter<ItemSet<IClassifier>>)Parameters["PossibleActions"]; }
71    }
72    public IFixedValueParameter<ItemSet<CombinedIntegerVector>> PossibleActionsConcreteClassParameter {
73      get { return (IFixedValueParameter<ItemSet<CombinedIntegerVector>>)Parameters["PossibleActionsConcreteClass"]; }
74    }
75    public IFixedValueParameter<IntValue> ThetaMinimalNumberOfActionsParameter {
76      get { return (IFixedValueParameter<IntValue>)Parameters["ThetaMinimalNumberOfActions"]; }
77    }
78    //for test purposes
79    public IFixedValueParameter<IntValue> LengthParameter {
80      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
81    }
82    public IFixedValueParameter<IntValue> ActionPartLengthParameter {
83      get { return (IFixedValueParameter<IntValue>)Parameters["ActionPartLength"]; }
84    }
85    public IFixedValueParameter<IntMatrix> BoundsParameter {
86      get { return (IFixedValueParameter<IntMatrix>)Parameters["Bounds"]; }
87    }
88    #endregion
89
90    #region properties
91    IParameter IConditionActionProblem.ProblemDataParameter {
92      get { return ProblemDataParameter; }
93    }
94    IConditionActionProblemData IConditionActionProblem.ProblemData {
95      get { return ProblemData; }
96    }
97    public ConditionActionClassificationProblemData ProblemData {
98      get { return ProblemDataParameter.Value; }
99    }
100    IParameter IConditionActionProblem.PossibleActionsConcreteClassParameter {
101      get { return PossibleActionsConcreteClassParameter; }
102    }
103    public ItemSet<CombinedIntegerVector> PossibleActionsConcreteClass {
104      get { return PossibleActionsConcreteClassParameter.Value; }
105    }
106    IParameter IConditionActionProblem.PossibleActionsParameter {
107      get { return PossibleActionsParameter; }
108    }
109    //IItemSet<IClassifier> IConditionActionProblem.PossibleActions {
110    //  get { return PossibleActions; }
111    //}
112    public ItemSet<IClassifier> PossibleActions {
113      get { return PossibleActionsParameter.Value; }
114    }
115    public IActionExecuter ActionExecuter {
116      get { return ActionExecuterParameter.Value; }
117    }
118    public ValueParameter<IActionExecuter> ActionExecuterParameter {
119      get { return (ValueParameter<IActionExecuter>)Parameters[ActionExecuterParameterName]; }
120    }
121    IParameter IConditionActionProblem.ActionExecuterParameter { get { return ActionExecuterParameter; } }
122    public CombinedIntegerVectorClassifierFetcher ClassifierFetcher {
123      get { return ClassifierFetcherParameter.Value; }
124    }
125    public ValueParameter<CombinedIntegerVectorClassifierFetcher> ClassifierFetcherParameter {
126      get { return (ValueParameter<CombinedIntegerVectorClassifierFetcher>)Parameters[ClassifierFetcherParameterName]; }
127    }
128    IClassifierFetcher IConditionActionProblem.ClassifierFetcher { get { return ClassifierFetcher; } }
129    IParameter IConditionActionProblem.ClassifierFetcherParameter { get { return ClassifierFetcherParameter; } }
130
131
132    public ActionSetSubsumptionOperator ActionSetSubsumptionOperator {
133      get { return ActionSetSubsumptionOperatorParameter.Value; }
134    }
135    public ValueParameter<ActionSetSubsumptionOperator> ActionSetSubsumptionOperatorParameter {
136      get { return (ValueParameter<ActionSetSubsumptionOperator>)Parameters[ActionSetSubsumptionOperatorParameterName]; }
137    }
138    IActionSetSubsumption IConditionActionProblem.ActionSetSubsumptionOperator { get { return ActionSetSubsumptionOperator; } }
139    IParameter IConditionActionProblem.ActionSetSubsumptionOperatorParameter { get { return ActionSetSubsumptionOperatorParameter; } }
140
141    private IntValue ThetaMinimalNumberOfActions {
142      get { return ThetaMinimalNumberOfActionsParameter.Value; }
143    }
144    IParameter IConditionActionProblem.ThetaMinimalNumberOfActionsParameter {
145      get { return ThetaMinimalNumberOfActionsParameter; }
146    }
147    public ICoveringSolutionCreator CoveringSolutionCreator {
148      get { return CoveringSolutionCreatorParameter.Value; }
149    }
150    IParameter IConditionActionProblem.CoveringSolutionCreatorParameter {
151      get { return CoveringSolutionCreatorParameter; }
152    }
153    #endregion
154
155    [StorableConstructor]
156    protected ConditionActionClassificationProblem(bool deserializing) : base(deserializing) { }
157    protected ConditionActionClassificationProblem(ConditionActionClassificationProblem original, Cloner cloner)
158      : base(original, cloner) {
159    }
160    public override IDeepCloneable Clone(Cloner cloner) {
161      return new ConditionActionClassificationProblem(this, cloner);
162    }
163
164    public ConditionActionClassificationProblem() :
165      this(new ConditionActionClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
166        ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
167        new XCSEvaluator(), new UniformRandomCombinedIntegerVectorCreator(), new CombinedIntegerVectorCoveringCreator()) {
168    }
169
170    public ConditionActionClassificationProblem(ConditionActionClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomCombinedIntegerVectorCreator solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
171      : base(evaluator, solutionCreator) {
172      Parameters.Add(new FixedValueParameter<IntValue>("Length", "The operator to create a solution.", new IntValue(7)));
173      Parameters.Add(new FixedValueParameter<IntValue>("ActionPartLength", "The operator to create a solution.", new IntValue(1)));
174      int[,] elements = new int[,] { { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 2 } };
175      Parameters.Add(new FixedValueParameter<IntMatrix>("Bounds", "The operator to create a solution.", new IntMatrix(elements)));
176      Parameters.Add(new ValueParameter<ConditionActionClassificationProblemData>("ProblemData", "", problemData));
177      Parameters.Add(new FixedValueParameter<DoubleValue>("PositiveReward", "", new DoubleValue(1000)));
178      Parameters.Add(new FixedValueParameter<DoubleValue>("NegativeReward", "", new DoubleValue(0)));
179      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialPrediction", "Initial Presiction", new DoubleValue(0)));
180      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialError", "Initial Error", new DoubleValue(0)));
181      Parameters.Add(new FixedValueParameter<DoubleValue>("InitialFitness", "Initial Fitness", new DoubleValue(0)));
182
183      Parameters.Add(new ValueParameter<IActionExecuter>(ActionExecuterParameterName, "", new ActionExecuter()));
184      Parameters.Add(new ValueParameter<CombinedIntegerVectorClassifierFetcher>(ClassifierFetcherParameterName, "", new CombinedIntegerVectorClassifierFetcher()));
185      Parameters.Add(new FixedValueParameter<ItemSet<IClassifier>>("PossibleActions"));
186      Parameters.Add(new FixedValueParameter<ItemSet<CombinedIntegerVector>>("PossibleActionsConcreteClass"));
187      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)));
188
189      Parameters.Add(new ValueParameter<ICoveringSolutionCreator>("CoveringSolutionCreator", "", coveringSolutionCreator));
190      Parameters.Add(new FixedValueParameter<PercentValue>("ChangeSymbolProbabilityInCovering", "", new PercentValue(0.5)));
191
192      Parameters.Add(new ValueParameter<ActionSetSubsumptionOperator>(ActionSetSubsumptionOperatorParameterName, "", new ActionSetSubsumptionOperator()));
193
194      Evaluator.InitialErrorParameter.ActualName = "InitialError";
195      Evaluator.InitialFitnessParameter.ActualName = "InitialFitness";
196      Evaluator.InitialPredictionParameter.ActualName = "InitialPrediction";
197
198      SolutionCreator.ActionPartLengthParameter.ActualName = ActionPartLengthParameter.Name;
199      SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;
200      SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
201
202      coveringSolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
203      coveringSolutionCreator.CoverClassifierParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
204      coveringSolutionCreator.CreatedClassifierParameter.ActualName = "CombinedIntegerVector";
205
206      ClassifierFetcher.ActionPartLengthParameter.ActualName = ActionPartLengthParameter.Name;
207      ClassifierFetcher.BoundsParameter.ActualName = BoundsParameter.Name;
208      ClassifierFetcher.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
209
210      ActionExecuter.CurrentClassifierToMatchParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
211      ActionExecuter.NegativeRewardParameter.ActualName = NegativeRewardParameter.Name;
212      ActionExecuter.PositiveRewardParameter.ActualName = PositiveRewardParameter.Name;
213
214      ActionSetSubsumptionOperator.ClassifiersParameter.ActualName = "CombinedIntegerVector";
215
216      SetPossibleActions();
217      ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
218
219      BoundsParameter.ValueChanged += new System.EventHandler(BoundsParameter_ValueChanged);
220      LengthParameter.ValueChanged += new System.EventHandler(LengthParameter_ValueChanged);
221      ActionPartLengthParameter.ValueChanged += new System.EventHandler(ActionPartLengthParameter_ValueChanged);
222    }
223
224    #region event handler
225    private void ActionPartLengthParameter_ValueChanged(object sender, System.EventArgs e) {
226      SetPossibleActions();
227    }
228    private void LengthParameter_ValueChanged(object sender, System.EventArgs e) {
229      SetPossibleActions();
230    }
231    private void BoundsParameter_ValueChanged(object sender, System.EventArgs e) {
232      SetPossibleActions();
233    }
234    #endregion
235
236    private void SetPossibleActions() {
237      //get bounds of action
238      IntMatrix actionBounds = GetElementsOfBoundsForAction(BoundsParameter.Value, LengthParameter.Value.Value, ActionPartLengthParameter.Value.Value);
239      int actionLength = ActionPartLengthParameter.Value.Value;
240      int start = LengthParameter.Value.Value - actionLength;
241      int[] elements = new int[actionLength];
242      int[] curPos = new int[actionLength];
243      bool done = false;
244      //initialize curPos
245      for (int i = 0; i < actionBounds.Rows; i++) {
246        curPos[i] = actionBounds[i, 0];
247      }
248      PossibleActions.Clear();
249      PossibleActionsConcreteClass.Clear();
250      while (!done) {
251        PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
252        PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
253        curPos = GetNextAction(curPos, actionBounds, out done);
254      }
255
256    }
257
258    private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) {
259      int cur = 0;
260      while (cur < curPos.Length) {
261        curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2];
262        if (curPos[cur] >= actionBounds[cur, 1]) {
263          curPos[cur] = actionBounds[cur, 0];
264          cur++;
265        } else {
266          break;
267        }
268      }
269      done = cur >= curPos.Length;
270      return curPos;
271    }
272
273    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
274      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
275      int start = length - actionPartLength;
276      for (int i = start; i < length; i++) {
277        int pos = i % bounds.Rows;
278        for (int j = 0; j < bounds.Columns; j++) {
279          actionBounds[i - start, j] = bounds[pos, j];
280        }
281      }
282      return actionBounds;
283    }
284  }
285}
Note: See TracBrowser for help on using the repository browser.