Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.LearningClassifierSystems/3.3/LCSAdaptedGeneticAlgorithm.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: 14.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
22
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.ConditionActionEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Selection;
32namespace HeuristicLab.Algorithms.LearningClassifierSystems {
33  /// <summary>
34  /// An operator which represents the main loop of a genetic algorithm.
35  /// </summary>
36  [Item("LCSAdaptedGeneticAlgorithm", "An operator which represents the main loop of a genetic algorithm, which has been adapdet for learning classifier systems.")]
37  [StorableClass]
38  public sealed class LCSAdaptedGeneticAlgorithm : AlgorithmOperator {
39    private const string TEMPID = "TempID";
40    private const string SUBSUMEDBY = "SubsumedBy";
41    private const string SUBSUMED = "Subsumed";
42
43    #region Parameter properties
44    public ValueLookupParameter<IRandom> RandomParameter {
45      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
46    }
47    public ValueLookupParameter<BoolValue> MaximizationParameter {
48      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
49    }
50    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
51      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
52    }
53    public ValueLookupParameter<IOperator> SelectorParameter {
54      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
55    }
56    public ValueLookupParameter<IOperator> CrossoverParameter {
57      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
58    }
59    public ValueLookupParameter<IOperator> AfterCrossoverParameter {
60      get { return (ValueLookupParameter<IOperator>)Parameters["AfterCrossover"]; }
61    }
62    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
63      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
64    }
65    public ValueLookupParameter<IOperator> MutatorParameter {
66      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
67    }
68    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
69      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
70    }
71    public ValueLookupParameter<VariableCollection> ResultsParameter {
72      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
73    }
74    public ValueLookupParameter<IOperator> AnalyzerParameter {
75      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
76    }
77    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
78      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
79    }
80    public ValueLookupParameter<IntValue> PopulationSizeParameter {
81      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
82    }
83    public ValueLookupParameter<BoolValue> DoGASubsumptionParameter {
84      get { return (ValueLookupParameter<BoolValue>)Parameters["DoGASubsumption"]; }
85    }
86    private ScopeParameter CurrentScopeParameter {
87      get { return (ScopeParameter)Parameters["CurrentScope"]; }
88    }
89
90    public IScope CurrentScope {
91      get { return CurrentScopeParameter.ActualValue; }
92    }
93    #endregion
94
95    [StorableConstructor]
96    private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
97    private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
98      : base(original, cloner) {
99    }
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new LCSAdaptedGeneticAlgorithm(this, cloner);
102    }
103    public LCSAdaptedGeneticAlgorithm()
104      : base() {
105      Initialize();
106    }
107
108    private void Initialize() {
109      #region Create parameters
110      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
111      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
112      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
113      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
114      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
115      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCrossover", "The operator executed after crossing the solutions."));
116      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
117      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
118      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
119      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
120      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
122      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
123      Parameters.Add(new ValueLookupParameter<BoolValue>("DoGASubsumption", "Sets if GA subsumption is executed."));
124      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
125      #endregion
126
127      #region Create operators
128      VariableCreator variableCreator = new VariableCreator();
129      ResultsCollector resultsCollector1 = new ResultsCollector();
130      Placeholder analyzer1 = new Placeholder();
131      Placeholder selector = new Placeholder();
132      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
133      ChildrenCreator childrenCreator = new ChildrenCreator();
134      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
135      Placeholder crossover = new Placeholder();
136      Placeholder afterCrossover = new Placeholder();
137      Placeholder mutator = new Placeholder();
138      SubScopesRemover subScopesRemover = new SubScopesRemover();
139      SubScopesCounter subScopesCounter = new SubScopesCounter();
140      MergingReducer mergingReducer = new MergingReducer();
141      IntCounter intCounter = new IntCounter();
142      Comparator comparator = new Comparator();
143      Placeholder analyzer2 = new Placeholder();
144      ConditionalBranch conditionalBranch = new ConditionalBranch();
145
146      TempSubScopeIDAssigner tempIdAssigner = new TempSubScopeIDAssigner();
147      ConditionalBranch doGASubsumptionBranch1 = new ConditionalBranch();
148      UniformSubScopesProcessor setSubsumptionFalseSubScopesProcessor = new UniformSubScopesProcessor();
149      Assigner setSubsumpByAssigner = new Assigner();
150      Assigner setSubsumptionFalseAssigner = new Assigner();
151      CheckGASubsumptionOperator checkGASubsumptionOperator = new CheckGASubsumptionOperator();
152      ConditionalBranch doGASubsumptionBranch2 = new ConditionalBranch();
153      ExecuteGASubsumptionOperator executeGAsubsumptionOperator = new ExecuteGASubsumptionOperator();
154      ConditionalSelector subsumptionSelector = new ConditionalSelector();
155      LeftReducer subsumptionLeftReducer = new LeftReducer();
156
157      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
158
159      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
160      resultsCollector1.ResultsParameter.ActualName = "Results";
161
162      analyzer1.Name = "Analyzer";
163      analyzer1.OperatorParameter.ActualName = "Analyzer";
164
165      tempIdAssigner.LeftSideParameter.ActualName = TEMPID;
166
167      setSubsumpByAssigner.LeftSideParameter.ActualName = SUBSUMEDBY;
168      setSubsumpByAssigner.RightSideParameter.Value = new IntValue(-1);
169
170      setSubsumptionFalseAssigner.LeftSideParameter.ActualName = SUBSUMED;
171      setSubsumptionFalseAssigner.RightSideParameter.Value = new BoolValue(false);
172
173      selector.Name = "Selector";
174      selector.OperatorParameter.ActualName = "Selector";
175
176      childrenCreator.ParentsPerChild = new IntValue(2);
177
178      crossover.Name = "Crossover";
179      crossover.OperatorParameter.ActualName = "Crossover";
180
181      afterCrossover.Name = "AfterCrossover";
182      afterCrossover.OperatorParameter.ActualName = "AfterCrossover";
183
184      mutator.Name = "Mutator";
185      mutator.OperatorParameter.ActualName = "Mutator";
186
187      doGASubsumptionBranch1.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
188
189      checkGASubsumptionOperator.ChildClassifiersParameter.ActualName = "CombinedIntegerVector";
190      checkGASubsumptionOperator.ParentsClassifiersParameter.ActualName = "CombinedIntegerVector";
191      checkGASubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
192      checkGASubsumptionOperator.ExperiencesParameter.ActualName = "Experience";
193      checkGASubsumptionOperator.ErrorsParameter.ActualName = "Error";
194      checkGASubsumptionOperator.TempIDParameter.ActualName = TEMPID;
195      checkGASubsumptionOperator.ErrorZeroParameter.ActualName = "ErrorZero";
196      checkGASubsumptionOperator.ThetaSubsumptionParameter.ActualName = "ThetaSubsumption";
197      checkGASubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
198      checkGASubsumptionOperator.SubsumedParameter.ActualName = SUBSUMED;
199
200      subScopesRemover.RemoveAllSubScopes = true;
201
202      doGASubsumptionBranch2.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
203
204      executeGAsubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
205      executeGAsubsumptionOperator.TempIDParameter.ActualName = TEMPID;
206      executeGAsubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
207
208      subsumptionSelector.ConditionParameter.ActualName = SUBSUMED;
209      subsumptionSelector.CopySelected = new BoolValue(false);
210
211      subScopesCounter.Name = "Increment EvaluatedSolutions";
212      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
213
214      intCounter.Increment = new IntValue(1);
215      intCounter.ValueParameter.ActualName = "Generations";
216
217      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
218      comparator.LeftSideParameter.ActualName = "Generations";
219      comparator.ResultParameter.ActualName = "Terminate";
220      comparator.RightSideParameter.ActualName = "MaximumGenerations";
221
222      analyzer2.Name = "Analyzer";
223      analyzer2.OperatorParameter.ActualName = "Analyzer";
224
225      conditionalBranch.ConditionParameter.ActualName = "Terminate";
226      #endregion
227
228      #region Create operator graph
229      OperatorGraph.InitialOperator = variableCreator;
230      variableCreator.Successor = resultsCollector1;
231      resultsCollector1.Successor = analyzer1;
232      analyzer1.Successor = tempIdAssigner;
233      tempIdAssigner.Successor = setSubsumptionFalseSubScopesProcessor;
234      setSubsumptionFalseSubScopesProcessor.Operator = setSubsumpByAssigner;
235      setSubsumpByAssigner.Successor = setSubsumptionFalseAssigner;
236      setSubsumptionFalseAssigner.Successor = null;
237      setSubsumptionFalseSubScopesProcessor.Successor = selector;
238      selector.Successor = subScopesProcessor1;
239      subScopesProcessor1.Operators.Add(new EmptyOperator());
240      subScopesProcessor1.Operators.Add(childrenCreator);
241      subScopesProcessor1.Successor = mergingReducer;
242      childrenCreator.Successor = uniformSubScopesProcessor1;
243      uniformSubScopesProcessor1.Operator = crossover;
244      uniformSubScopesProcessor1.Successor = subScopesCounter;
245      crossover.Successor = afterCrossover;
246      afterCrossover.Successor = mutator;
247      mutator.Successor = doGASubsumptionBranch1;
248      doGASubsumptionBranch1.TrueBranch = checkGASubsumptionOperator;
249      doGASubsumptionBranch1.FalseBranch = new EmptyOperator();
250      doGASubsumptionBranch1.Successor = subScopesRemover;
251      subScopesRemover.Successor = null;
252      subScopesCounter.Successor = null;
253      mergingReducer.Successor = doGASubsumptionBranch2;
254      doGASubsumptionBranch2.TrueBranch = executeGAsubsumptionOperator;
255      doGASubsumptionBranch2.FalseBranch = new EmptyOperator();
256      executeGAsubsumptionOperator.Successor = subsumptionSelector;
257      subsumptionSelector.Successor = subsumptionLeftReducer;
258      subsumptionLeftReducer.Successor = null;
259      doGASubsumptionBranch2.Successor = intCounter;
260      intCounter.Successor = comparator;
261      comparator.Successor = analyzer2;
262      analyzer2.Successor = conditionalBranch;
263      conditionalBranch.FalseBranch = selector;
264      conditionalBranch.TrueBranch = null;
265      conditionalBranch.Successor = null;
266      #endregion
267    }
268
269    public override IOperation Apply() {
270      if (CrossoverParameter.ActualValue == null)
271        return null;
272      return base.Apply();
273    }
274  }
275}
Note: See TracBrowser for help on using the repository browser.