Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980: implemented covering and changed SinglePointCrossover for CombinedIntegerVectorEncoding

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