Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9175 was 9175, checked in by sforsten, 11 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;
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("Learning Classifier System", "A genetic algorithm.")]
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    #endregion
121
122    #region Properties
123    public IntValue Seed {
124      get { return SeedParameter.Value; }
125      set { SeedParameter.Value = value; }
126    }
127    public BoolValue SetSeedRandomly {
128      get { return SetSeedRandomlyParameter.Value; }
129      set { SetSeedRandomlyParameter.Value = value; }
130    }
131    public BoolValue CreateInitialPopulation {
132      get { return CreateInitialPopulationParameter.Value; }
133      set { CreateInitialPopulationParameter.Value = value; }
134    }
135    public IntValue PopulationSize {
136      get { return PopulationSizeParameter.Value; }
137      set { PopulationSizeParameter.Value = value; }
138    }
139    public PercentValue Beta {
140      get { return BetaParameter.Value; }
141      set { BetaParameter.Value = value; }
142    }
143    public PercentValue Alpha {
144      get { return AlphaParameter.Value; }
145      set { AlphaParameter.Value = value; }
146    }
147    public DoubleValue ErrorZero {
148      get { return ErrorZeroParameter.Value; }
149      set { ErrorZeroParameter.Value = value; }
150    }
151    public DoubleValue Power {
152      get { return PowerParameter.Value; }
153      set { PowerParameter.Value = value; }
154    }
155    public PercentValue Gamma {
156      get { return GammaParameter.Value; }
157      set { GammaParameter.Value = value; }
158    }
159    public PercentValue CrossoverProbability {
160      get { return CrossoverProbabilityParameter.Value; }
161      set { CrossoverProbabilityParameter.Value = value; }
162    }
163    public PercentValue MutationProbability {
164      get { return MutationProbabilityParameter.Value; }
165      set { MutationProbabilityParameter.Value = value; }
166    }
167    public IntValue ThetaGA {
168      get { return ThetaGAParameter.Value; }
169      set { ThetaGAParameter.Value = value; }
170    }
171    public IntValue ThetaDeletion {
172      get { return ThetaDeletionParameter.Value; }
173      set { ThetaDeletionParameter.Value = value; }
174    }
175    public IntValue ThetaSubsumption {
176      get { return ThetaSubsumptionParameter.Value; }
177      set { ThetaSubsumptionParameter.Value = value; }
178    }
179    public PercentValue Delta {
180      get { return DeltaParameter.Value; }
181      set { DeltaParameter.Value = value; }
182    }
183    public PercentValue ExplorationProbability {
184      get { return ExplorationProbabilityParameter.Value; }
185      set { ExplorationProbabilityParameter.Value = value; }
186    }
187    public BoolValue DoGASubsumption {
188      get { return DoGASubsumptionParameter.Value; }
189      set { DoGASubsumptionParameter.Value = value; }
190    }
191    public BoolValue DoActionSetSubsumption {
192      get { return DoActionSetSubsumptionParameter.Value; }
193      set { DoActionSetSubsumptionParameter.Value = value; }
194    }
195    public IntValue MaxIterations {
196      get { return MaxIterationsParameter.Value; }
197      set { MaxIterationsParameter.Value = value; }
198    }
199    public MultiAnalyzer Analyzer {
200      get { return AnalyzerParameter.Value; }
201      set { AnalyzerParameter.Value = value; }
202    }
203    public MultiAnalyzer FinalAnalyzer {
204      get { return FinalAnalyzerParameter.Value; }
205      set { FinalAnalyzerParameter.Value = value; }
206    }
207    private RandomCreator RandomCreator {
208      get { return (RandomCreator)OperatorGraph.InitialOperator; }
209    }
210    public LearningClassifierSystemMainLoop MainLoop {
211      get { return FindMainLoop(RandomCreator.Successor); }
212    }
213    #endregion
214
215    public LearningClassifierSystem()
216      : base() {
217      #region Create parameters
218      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
219      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
220      Parameters.Add(new ValueParameter<BoolValue>("CreateInitialPopulation", "Specifies if a population should be created at the beginning of the algorithm.", new BoolValue(false)));
221      Parameters.Add(new ValueParameter<IntValue>("N", "Max size of the population of solutions.", new IntValue(100)));
222      Parameters.Add(new ValueParameter<PercentValue>("Beta", "Learning rate", new PercentValue(0.1)));
223      Parameters.Add(new ValueParameter<PercentValue>("Alpha", "", new PercentValue(0.1)));
224      Parameters.Add(new ValueParameter<DoubleValue>("ErrorZero", "The error below which classifiers are considered to have equal accuracy", new DoubleValue(10)));
225      Parameters.Add(new ValueParameter<DoubleValue>("v", "Power parameter", new DoubleValue(5)));
226      Parameters.Add(new ValueParameter<PercentValue>("Gamma", "Discount factor", new PercentValue(0.71)));
227      Parameters.Add(new ValueParameter<PercentValue>("CrossoverProbability", "Probability of crossover", new PercentValue(0.9)));
228      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "Probability of mutation", new PercentValue(0.05)));
229      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)));
230      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)));
231      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)));
232      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)));
233      Parameters.Add(new ValueParameter<PercentValue>("ExplorationProbability", "Probability of selecting the action uniform randomly", new PercentValue(0.5)));
234      Parameters.Add(new ValueParameter<BoolValue>("DoGASubsumption", "Specifies if offsprings are tested for possible logical subsumption by parents.", new BoolValue(true)));
235      Parameters.Add(new ValueParameter<BoolValue>("DoActionSetSubsumption", "Specifies if action set is tested for subsuming classifiers.", new BoolValue(true)));
236      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
237      Parameters.Add(new ValueParameter<MultiAnalyzer>("FinalAnalyzer", "The operator used to analyze the last generation.", new MultiAnalyzer()));
238      Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "The maximum number of iterations.", new IntValue(1000)));
239      #endregion
240
241      #region Create operators
242      RandomCreator randomCreator = new RandomCreator();
243
244      ResultsCollector resultsCollector = new ResultsCollector();
245      LearningClassifierSystemMainLoop mainLoop = new LearningClassifierSystemMainLoop();
246
247      randomCreator.RandomParameter.ActualName = "Random";
248      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
249      randomCreator.SeedParameter.Value = null;
250      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
251      randomCreator.SetSeedRandomlyParameter.Value = null;
252
253      resultsCollector.ResultsParameter.ActualName = "Results";
254
255      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
256      mainLoop.FinalAnalyzerParameter.ActualName = FinalAnalyzerParameter.Name;
257      mainLoop.MaxIterationsParameter.ActualName = MaxIterationsParameter.Name;
258      #endregion
259
260      #region Create operator graph
261      OperatorGraph.InitialOperator = randomCreator;
262      randomCreator.Successor = resultsCollector;
263      resultsCollector.Successor = mainLoop;
264      #endregion
265
266      UpdateAnalyzers();
267    }
268    private LearningClassifierSystem(LearningClassifierSystem original, Cloner cloner)
269      : base(original, cloner) {
270    }
271    public override IDeepCloneable Clone(Cloner cloner) {
272      return new LearningClassifierSystem(this, cloner);
273    }
274    [StorableConstructor]
275    private LearningClassifierSystem(bool deserializing) : base(deserializing) { }
276
277    protected override void OnProblemChanged() {
278      if (Problem != null) {
279        ParameterizeEvaluator(Problem.Evaluator);
280        MainLoop.SetCurrentProblem(Problem);
281        UpdateAnalyzers();
282      }
283      base.OnProblemChanged();
284    }
285    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
286      ParameterizeEvaluator(Problem.Evaluator);
287      MainLoop.SetCurrentProblem(Problem);
288      base.Problem_EvaluatorChanged(sender, e);
289    }
290    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
291      MainLoop.SetCurrentProblem(Problem);
292      base.Problem_SolutionCreatorChanged(sender, e);
293    }
294    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
295      UpdateAnalyzers();
296      base.Problem_OperatorsChanged(sender, e);
297    }
298
299    private void ParameterizeEvaluator(IXCSEvaluator evaluator) {
300      evaluator.ActualTimeParameter.ActualName = "Iteration";
301      evaluator.BetaParameter.ActualName = BetaParameter.Name;
302      evaluator.AlphaParameter.ActualName = AlphaParameter.Name;
303      evaluator.PowerParameter.ActualName = PowerParameter.Name;
304      evaluator.ErrorZeroParameter.ActualName = ErrorZeroParameter.Name;
305    }
306
307    private void UpdateAnalyzers() {
308      Analyzer.Operators.Clear();
309      FinalAnalyzer.Operators.Clear();
310      if (Problem != null) {
311        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
312          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
313          FinalAnalyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
314        }
315      }
316    }
317
318    private LearningClassifierSystemMainLoop FindMainLoop(IOperator start) {
319      IOperator mainLoop = start;
320      while (mainLoop != null && !(mainLoop is LearningClassifierSystemMainLoop))
321        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
322      if (mainLoop == null) return null;
323      else return (LearningClassifierSystemMainLoop)mainLoop;
324    }
325  }
326}
Note: See TracBrowser for help on using the repository browser.