Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.LearningClassifierSystems/3.3/LearningClassifierSystem.cs @ 9651

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

#1980:

  • renamed algorithm Learning Classifier System to XCS
  • DecisionListSolution and XCSSolution show more information
  • VariableVectorClassificationProblemData can now also import datasets where the last variable is not the target variable
File size: 21.4 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;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.ConditionActionEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Algorithms.LearningClassifierSystems {
37  /// <summary>
38  /// A learning classifier system.
39  /// </summary>
40  [Item("XCS", "A learning classifier system")]
41  [Creatable("Algorithms")]
42  [StorableClass]
43  public sealed class LearningClassifierSystem : HeuristicOptimizationEngineAlgorithm, IStorableContent {
44    public string Filename { get; set; }
45
46    #region Problem Properties
47    public override Type ProblemType {
48      get { return typeof(IConditionActionProblem); }
49    }
50    public new IConditionActionProblem Problem {
51      get { return (IConditionActionProblem)base.Problem; }
52      set { base.Problem = value; }
53    }
54    #endregion
55
56    #region Parameter Properties
57    private ValueParameter<IntValue> SeedParameter {
58      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
61      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
63    private ValueParameter<BoolValue> CreateInitialPopulationParameter {
64      get { return (ValueParameter<BoolValue>)Parameters["CreateInitialPopulation"]; }
65    }
66    private ValueParameter<IntValue> PopulationSizeParameter {
67      get { return (ValueParameter<IntValue>)Parameters["N"]; }
68    }
69    private ValueParameter<PercentValue> BetaParameter {
70      get { return (ValueParameter<PercentValue>)Parameters["Beta"]; }
71    }
72    private ValueParameter<PercentValue> AlphaParameter {
73      get { return (ValueParameter<PercentValue>)Parameters["Alpha"]; }
74    }
75    private ValueParameter<DoubleValue> ErrorZeroParameter {
76      get { return (ValueParameter<DoubleValue>)Parameters["ErrorZero"]; }
77    }
78    private ValueParameter<DoubleValue> PowerParameter {
79      get { return (ValueParameter<DoubleValue>)Parameters["v"]; }
80    }
81    private ValueParameter<PercentValue> GammaParameter {
82      get { return (ValueParameter<PercentValue>)Parameters["Gamma"]; }
83    }
84    private ValueParameter<PercentValue> CrossoverProbabilityParameter {
85      get { return (ValueParameter<PercentValue>)Parameters["CrossoverProbability"]; }
86    }
87    private ValueParameter<PercentValue> MutationProbabilityParameter {
88      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
89    }
90    private ValueParameter<IntValue> ThetaGAParameter {
91      get { return (ValueParameter<IntValue>)Parameters["ThetaGA"]; }
92    }
93    private ValueParameter<IntValue> ThetaDeletionParameter {
94      get { return (ValueParameter<IntValue>)Parameters["ThetaDeletion"]; }
95    }
96    private ValueParameter<IntValue> ThetaSubsumptionParameter {
97      get { return (ValueParameter<IntValue>)Parameters["ThetaSubsumption"]; }
98    }
99    private ValueParameter<PercentValue> DeltaParameter {
100      get { return (ValueParameter<PercentValue>)Parameters["Delta"]; }
101    }
102    private ValueParameter<PercentValue> ExplorationProbabilityParameter {
103      get { return (ValueParameter<PercentValue>)Parameters["ExplorationProbability"]; }
104    }
105    private ValueParameter<BoolValue> DoGASubsumptionParameter {
106      get { return (ValueParameter<BoolValue>)Parameters["DoGASubsumption"]; }
107    }
108    private ValueParameter<BoolValue> DoActionSetSubsumptionParameter {
109      get { return (ValueParameter<BoolValue>)Parameters["DoActionSetSubsumption"]; }
110    }
111    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
112      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
113    }
114    private ValueParameter<MultiAnalyzer> FinalAnalyzerParameter {
115      get { return (ValueParameter<MultiAnalyzer>)Parameters["FinalAnalyzer"]; }
116    }
117    private ValueParameter<IntValue> MaxIterationsParameter {
118      get { return (ValueParameter<IntValue>)Parameters["MaxIterations"]; }
119    }
120    public IConstrainedValueParameter<ISelector> SelectorParameter {
121      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
122    }
123    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
124      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
125    }
126    public IConstrainedValueParameter<IManipulator> MutatorParameter {
127      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
128    }
129    public ValueParameter<IntValue> AnalyzeInIterationParameter {
130      get { return (ValueParameter<IntValue>)Parameters["AnalyzeInIteration"]; }
131    }
132    #endregion
133
134    #region Properties
135    public IntValue Seed {
136      get { return SeedParameter.Value; }
137      set { SeedParameter.Value = value; }
138    }
139    public BoolValue SetSeedRandomly {
140      get { return SetSeedRandomlyParameter.Value; }
141      set { SetSeedRandomlyParameter.Value = value; }
142    }
143    public BoolValue CreateInitialPopulation {
144      get { return CreateInitialPopulationParameter.Value; }
145      set { CreateInitialPopulationParameter.Value = value; }
146    }
147    public IntValue PopulationSize {
148      get { return PopulationSizeParameter.Value; }
149      set { PopulationSizeParameter.Value = value; }
150    }
151    public PercentValue Beta {
152      get { return BetaParameter.Value; }
153      set { BetaParameter.Value = value; }
154    }
155    public PercentValue Alpha {
156      get { return AlphaParameter.Value; }
157      set { AlphaParameter.Value = value; }
158    }
159    public DoubleValue ErrorZero {
160      get { return ErrorZeroParameter.Value; }
161      set { ErrorZeroParameter.Value = value; }
162    }
163    public DoubleValue Power {
164      get { return PowerParameter.Value; }
165      set { PowerParameter.Value = value; }
166    }
167    public PercentValue Gamma {
168      get { return GammaParameter.Value; }
169      set { GammaParameter.Value = value; }
170    }
171    public PercentValue CrossoverProbability {
172      get { return CrossoverProbabilityParameter.Value; }
173      set { CrossoverProbabilityParameter.Value = value; }
174    }
175    public PercentValue MutationProbability {
176      get { return MutationProbabilityParameter.Value; }
177      set { MutationProbabilityParameter.Value = value; }
178    }
179    public IntValue ThetaGA {
180      get { return ThetaGAParameter.Value; }
181      set { ThetaGAParameter.Value = value; }
182    }
183    public IntValue ThetaDeletion {
184      get { return ThetaDeletionParameter.Value; }
185      set { ThetaDeletionParameter.Value = value; }
186    }
187    public IntValue ThetaSubsumption {
188      get { return ThetaSubsumptionParameter.Value; }
189      set { ThetaSubsumptionParameter.Value = value; }
190    }
191    public PercentValue Delta {
192      get { return DeltaParameter.Value; }
193      set { DeltaParameter.Value = value; }
194    }
195    public PercentValue ExplorationProbability {
196      get { return ExplorationProbabilityParameter.Value; }
197      set { ExplorationProbabilityParameter.Value = value; }
198    }
199    public BoolValue DoGASubsumption {
200      get { return DoGASubsumptionParameter.Value; }
201      set { DoGASubsumptionParameter.Value = value; }
202    }
203    public BoolValue DoActionSetSubsumption {
204      get { return DoActionSetSubsumptionParameter.Value; }
205      set { DoActionSetSubsumptionParameter.Value = value; }
206    }
207    public IntValue MaxIterations {
208      get { return MaxIterationsParameter.Value; }
209      set { MaxIterationsParameter.Value = value; }
210    }
211    public MultiAnalyzer Analyzer {
212      get { return AnalyzerParameter.Value; }
213      set { AnalyzerParameter.Value = value; }
214    }
215    public MultiAnalyzer FinalAnalyzer {
216      get { return FinalAnalyzerParameter.Value; }
217      set { FinalAnalyzerParameter.Value = value; }
218    }
219    public ISelector Selector {
220      get { return SelectorParameter.Value; }
221      set { SelectorParameter.Value = value; }
222    }
223    public ICrossover Crossover {
224      get { return CrossoverParameter.Value; }
225      set { CrossoverParameter.Value = value; }
226    }
227    public IManipulator Mutator {
228      get { return MutatorParameter.Value; }
229      set { MutatorParameter.Value = value; }
230    }
231    private RandomCreator RandomCreator {
232      get { return (RandomCreator)OperatorGraph.InitialOperator; }
233    }
234    public LearningClassifierSystemMainLoop MainLoop {
235      get { return FindMainLoop(RandomCreator.Successor); }
236    }
237    #endregion
238
239    public LearningClassifierSystem()
240      : base() {
241      #region Create parameters
242      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
243      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
244      Parameters.Add(new ValueParameter<BoolValue>("CreateInitialPopulation", "Specifies if a population should be created at the beginning of the algorithm.", new BoolValue(false)));
245      Parameters.Add(new ValueParameter<IntValue>("N", "Max size of the population of solutions.", new IntValue(100)));
246      Parameters.Add(new ValueParameter<PercentValue>("Beta", "Learning rate", new PercentValue(0.1)));
247      Parameters.Add(new ValueParameter<PercentValue>("Alpha", "", new PercentValue(0.1)));
248      Parameters.Add(new ValueParameter<DoubleValue>("ErrorZero", "The error below which classifiers are considered to have equal accuracy", new DoubleValue(10)));
249      Parameters.Add(new ValueParameter<DoubleValue>("v", "Power parameter", new DoubleValue(5)));
250      Parameters.Add(new ValueParameter<PercentValue>("Gamma", "Discount factor", new PercentValue(0.71)));
251      Parameters.Add(new ValueParameter<PercentValue>("CrossoverProbability", "Probability of crossover", new PercentValue(0.9)));
252      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "Probability of mutation", new PercentValue(0.05)));
253      Parameters.Add(new ValueParameter<IntValue>("ThetaGA", "GA threshold. GA is applied in a set when the average time since the last GA is greater than ThetaGA.", new IntValue(25)));
254      Parameters.Add(new ValueParameter<IntValue>("ThetaDeletion", "Deletion threshold. If the experience of a classifier is greater than ThetaDeletion, its fitness may be considered in its probability of deletion.", new IntValue(20)));
255      Parameters.Add(new ValueParameter<IntValue>("ThetaSubsumption", "Subsumption threshold. The experience of a classifier must be greater than TheatSubsumption to be able to subsume another classifier.", new IntValue(20)));
256      Parameters.Add(new ValueParameter<PercentValue>("Delta", "Delta specifies the fraction of mean fitness in [P] below which the fitness of a classifier may be considered in its probability of deletion", new PercentValue(0.1)));
257      Parameters.Add(new ValueParameter<PercentValue>("ExplorationProbability", "Probability of selecting the action uniform randomly", new PercentValue(0.5)));
258      Parameters.Add(new ValueParameter<BoolValue>("DoGASubsumption", "Specifies if offsprings are tested for possible logical subsumption by parents.", new BoolValue(true)));
259      Parameters.Add(new ValueParameter<BoolValue>("DoActionSetSubsumption", "Specifies if action set is tested for subsuming classifiers.", new BoolValue(true)));
260      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
261      Parameters.Add(new ValueParameter<MultiAnalyzer>("FinalAnalyzer", "The operator used to analyze the last generation.", new MultiAnalyzer()));
262      Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "The maximum number of iterations.", new IntValue(1000)));
263      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions."));
264      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
265      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
266      Parameters.Add(new ValueParameter<IntValue>("AnalyzeInIteration", "", new IntValue(50)));
267      #endregion
268
269      #region Create operators
270      RandomCreator randomCreator = new RandomCreator();
271
272      ResultsCollector resultsCollector = new ResultsCollector();
273      LearningClassifierSystemMainLoop mainLoop = new LearningClassifierSystemMainLoop();
274
275      randomCreator.RandomParameter.ActualName = "Random";
276      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
277      randomCreator.SeedParameter.Value = null;
278      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
279      randomCreator.SetSeedRandomlyParameter.Value = null;
280
281      resultsCollector.ResultsParameter.ActualName = "Results";
282
283      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
284      mainLoop.FinalAnalyzerParameter.ActualName = FinalAnalyzerParameter.Name;
285      mainLoop.MaxIterationsParameter.ActualName = MaxIterationsParameter.Name;
286      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
287      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
288      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
289      mainLoop.CrossoverProbabilityParameter.ActualName = CrossoverProbabilityParameter.Name;
290      mainLoop.AnalyzeInIterationParameter.ActualName = AnalyzeInIterationParameter.Name;
291      #endregion
292
293      #region Create operator graph
294      OperatorGraph.InitialOperator = randomCreator;
295      randomCreator.Successor = resultsCollector;
296      resultsCollector.Successor = mainLoop;
297      #endregion
298
299      UpdateAnalyzers();
300    }
301    private LearningClassifierSystem(LearningClassifierSystem original, Cloner cloner)
302      : base(original, cloner) {
303    }
304    public override IDeepCloneable Clone(Cloner cloner) {
305      return new LearningClassifierSystem(this, cloner);
306    }
307    [StorableConstructor]
308    private LearningClassifierSystem(bool deserializing) : base(deserializing) { }
309
310    protected override void OnProblemChanged() {
311      if (Problem != null) {
312        ParameterizeEvaluator(Problem.Evaluator);
313        MainLoop.SetCurrentProblem(Problem);
314        UpdateSelectors();
315        UpdateCrossovers();
316        UpdateMutators();
317        UpdateAnalyzers();
318        ParameterizeSelectors();
319        ParameterizeManipulator();
320      }
321      base.OnProblemChanged();
322    }
323
324    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
325      ParameterizeEvaluator(Problem.Evaluator);
326      MainLoop.SetCurrentProblem(Problem);
327      base.Problem_EvaluatorChanged(sender, e);
328    }
329    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
330      MainLoop.SetCurrentProblem(Problem);
331      base.Problem_SolutionCreatorChanged(sender, e);
332    }
333    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
334      UpdateSelectors();
335      UpdateCrossovers();
336      UpdateMutators();
337      UpdateAnalyzers();
338      ParameterizeSelectors();
339      ParameterizeManipulator();
340      base.Problem_OperatorsChanged(sender, e);
341    }
342
343    private void ParameterizeSelectors() {
344      foreach (ISelector selector in SelectorParameter.ValidValues) {
345        selector.CopySelected = new BoolValue(true);
346        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(4);
347        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
348        ParameterizeStochasticOperator(selector);
349      }
350      if (Problem != null) {
351        foreach (IXCSSelector selector in SelectorParameter.ValidValues.OfType<IXCSSelector>()) {
352          selector.NumerosityParameter.ActualName = Problem.Evaluator.NumerosityParameter.ActualName;
353          selector.NumerosityParameter.Hidden = true;
354        }
355        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
356          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
357          selector.MaximizationParameter.Hidden = true;
358          selector.QualityParameter.ActualName = Problem.Evaluator.FitnessParameter.ActualName;
359          selector.QualityParameter.Hidden = true;
360        }
361      }
362    }
363    private void ParameterizeManipulator() {
364      foreach (var op in Problem.Operators.OfType<IProbabilityMutatorOperator>()) {
365        op.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
366      }
367    }
368    private void ParameterizeEvaluator(IXCSEvaluator evaluator) {
369      evaluator.ActualTimeParameter.ActualName = "Iteration";
370      evaluator.BetaParameter.ActualName = BetaParameter.Name;
371      evaluator.AlphaParameter.ActualName = AlphaParameter.Name;
372      evaluator.PowerParameter.ActualName = PowerParameter.Name;
373      evaluator.ErrorZeroParameter.ActualName = ErrorZeroParameter.Name;
374    }
375    private void ParameterizeStochasticOperator(IOperator op) {
376      IStochasticOperator stochasticOp = op as IStochasticOperator;
377      if (stochasticOp != null) {
378        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
379        stochasticOp.RandomParameter.Hidden = true;
380      }
381    }
382
383    private void UpdateSelectors() {
384      ISelector oldSelector = SelectorParameter.Value;
385      SelectorParameter.ValidValues.Clear();
386      ISelector defaultSelector = Problem.Operators.OfType<IXCSSelector>().FirstOrDefault();
387      if (defaultSelector == null) {
388        defaultSelector = Problem.Operators.OfType<ISelector>().FirstOrDefault();
389      }
390
391      foreach (ISelector selector in Problem.Operators.OfType<ISelector>().OrderBy(x => x.Name))
392        SelectorParameter.ValidValues.Add(selector);
393
394      if (oldSelector != null) {
395        ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
396        if (selector != null) SelectorParameter.Value = selector;
397        else oldSelector = null;
398      }
399      if (oldSelector == null && defaultSelector != null)
400        SelectorParameter.Value = defaultSelector;
401    }
402
403    private void UpdateCrossovers() {
404      ICrossover oldCrossover = CrossoverParameter.Value;
405      CrossoverParameter.ValidValues.Clear();
406      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
407
408      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
409        CrossoverParameter.ValidValues.Add(crossover);
410
411      if (oldCrossover != null) {
412        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
413        if (crossover != null) CrossoverParameter.Value = crossover;
414        else oldCrossover = null;
415      }
416      if (oldCrossover == null && defaultCrossover != null)
417        CrossoverParameter.Value = defaultCrossover;
418    }
419    private void UpdateMutators() {
420      IManipulator oldMutator = MutatorParameter.Value;
421      MutatorParameter.ValidValues.Clear();
422      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
423
424      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
425        MutatorParameter.ValidValues.Add(mutator);
426      if (oldMutator != null) {
427        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
428        if (mutator != null) MutatorParameter.Value = mutator;
429        else oldMutator = null;
430      }
431      if (oldMutator == null && defaultMutator != null)
432        MutatorParameter.Value = defaultMutator;
433    }
434    private void UpdateAnalyzers() {
435      Analyzer.Operators.Clear();
436      FinalAnalyzer.Operators.Clear();
437      if (Problem != null) {
438        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
439          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
440          FinalAnalyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
441        }
442      }
443    }
444
445    private LearningClassifierSystemMainLoop FindMainLoop(IOperator start) {
446      IOperator mainLoop = start;
447      while (mainLoop != null && !(mainLoop is LearningClassifierSystemMainLoop))
448        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
449      if (mainLoop == null) return null;
450      else return (LearningClassifierSystemMainLoop)mainLoop;
451    }
452  }
453}
Note: See TracBrowser for help on using the repository browser.