Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.LearningClassifierSystems/3.3/LCSAdaptedGeneticAlgorithm.cs @ 9154

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

#1980:

  • added XCSSolution, XCSModel, XCSClassifier to represent the xcs classifier
  • XCSSolution also shows the current accuracy (training and test partition has to be added)
  • added XCSSolutionAnalyzer to create a XCSSolution during the run of the algorithm
  • added XCSModelView to show the xcs model
  • fixed a bug in XCSDeletionOperator (sometimes it deleted less classifiers than it should)
  • moved some parameter from ConditionActionClassificationProblem to ConditionActionClassificationProblemData
File size: 13.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
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<IntValue> EvaluatedSolutionsParameter {
75      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
76    }
77    public ValueLookupParameter<IntValue> PopulationSizeParameter {
78      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
79    }
80    public ValueLookupParameter<BoolValue> DoGASubsumptionParameter {
81      get { return (ValueLookupParameter<BoolValue>)Parameters["DoGASubsumption"]; }
82    }
83    private ScopeParameter CurrentScopeParameter {
84      get { return (ScopeParameter)Parameters["CurrentScope"]; }
85    }
86
87    public IScope CurrentScope {
88      get { return CurrentScopeParameter.ActualValue; }
89    }
90    #endregion
91
92    [StorableConstructor]
93    private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
94    private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
95      : base(original, cloner) {
96    }
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new LCSAdaptedGeneticAlgorithm(this, cloner);
99    }
100    public LCSAdaptedGeneticAlgorithm()
101      : base() {
102      Initialize();
103    }
104
105    private void Initialize() {
106      #region Create parameters
107      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
108      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
109      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
110      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
111      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
112      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCrossover", "The operator executed after crossing the solutions."));
113      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
114      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
115      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
116      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
117      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
118      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
119      Parameters.Add(new ValueLookupParameter<BoolValue>("DoGASubsumption", "Sets if GA subsumption is executed."));
120      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
121      #endregion
122
123      #region Create operators
124      VariableCreator variableCreator = new VariableCreator();
125      ResultsCollector resultsCollector1 = new ResultsCollector();
126      Placeholder selector = new Placeholder();
127      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
128      ChildrenCreator childrenCreator = new ChildrenCreator();
129      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
130      Placeholder crossover = new Placeholder();
131      Placeholder afterCrossover = new Placeholder();
132      Placeholder mutator = new Placeholder();
133      SubScopesRemover subScopesRemover = new SubScopesRemover();
134      SubScopesCounter subScopesCounter = new SubScopesCounter();
135      MergingReducer mergingReducer = new MergingReducer();
136      IntCounter intCounter = new IntCounter();
137      Comparator comparator = new Comparator();
138      ConditionalBranch conditionalBranch = new ConditionalBranch();
139
140      TempSubScopeIDAssigner tempIdAssigner = new TempSubScopeIDAssigner();
141      ConditionalBranch doGASubsumptionBranch1 = new ConditionalBranch();
142      UniformSubScopesProcessor setSubsumptionFalseSubScopesProcessor = new UniformSubScopesProcessor();
143      Assigner setSubsumpByAssigner = new Assigner();
144      Assigner setSubsumptionFalseAssigner = new Assigner();
145      CheckGASubsumptionOperator checkGASubsumptionOperator = new CheckGASubsumptionOperator();
146      ConditionalBranch doGASubsumptionBranch2 = new ConditionalBranch();
147      ExecuteGASubsumptionOperator executeGAsubsumptionOperator = new ExecuteGASubsumptionOperator();
148      ConditionalSelector subsumptionSelector = new ConditionalSelector();
149      LeftReducer subsumptionLeftReducer = new LeftReducer();
150
151      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
152
153      //resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
154      resultsCollector1.ResultsParameter.ActualName = "Results";
155
156      tempIdAssigner.LeftSideParameter.ActualName = TEMPID;
157
158      setSubsumpByAssigner.LeftSideParameter.ActualName = SUBSUMEDBY;
159      setSubsumpByAssigner.RightSideParameter.Value = new IntValue(-1);
160
161      setSubsumptionFalseAssigner.LeftSideParameter.ActualName = SUBSUMED;
162      setSubsumptionFalseAssigner.RightSideParameter.Value = new BoolValue(false);
163
164      selector.Name = "Selector";
165      selector.OperatorParameter.ActualName = "Selector";
166
167      childrenCreator.ParentsPerChild = new IntValue(2);
168
169      crossover.Name = "Crossover";
170      crossover.OperatorParameter.ActualName = "Crossover";
171
172      afterCrossover.Name = "AfterCrossover";
173      afterCrossover.OperatorParameter.ActualName = "AfterCrossover";
174
175      mutator.Name = "Mutator";
176      mutator.OperatorParameter.ActualName = "Mutator";
177
178      doGASubsumptionBranch1.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
179
180      checkGASubsumptionOperator.ChildClassifiersParameter.ActualName = "CombinedIntegerVector";
181      checkGASubsumptionOperator.ParentsClassifiersParameter.ActualName = "CombinedIntegerVector";
182      checkGASubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
183      checkGASubsumptionOperator.ExperiencesParameter.ActualName = "Experience";
184      checkGASubsumptionOperator.ErrorsParameter.ActualName = "Error";
185      checkGASubsumptionOperator.TempIDParameter.ActualName = TEMPID;
186      checkGASubsumptionOperator.ErrorZeroParameter.ActualName = "ErrorZero";
187      checkGASubsumptionOperator.ThetaSubsumptionParameter.ActualName = "ThetaSubsumption";
188      checkGASubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
189      checkGASubsumptionOperator.SubsumedParameter.ActualName = SUBSUMED;
190
191      subScopesRemover.RemoveAllSubScopes = true;
192
193      doGASubsumptionBranch2.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
194
195      executeGAsubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
196      executeGAsubsumptionOperator.TempIDParameter.ActualName = TEMPID;
197      executeGAsubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
198
199      subsumptionSelector.ConditionParameter.ActualName = SUBSUMED;
200      subsumptionSelector.CopySelected = new BoolValue(false);
201
202      subScopesCounter.Name = "Increment EvaluatedSolutions";
203      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
204
205      intCounter.Increment = new IntValue(1);
206      intCounter.ValueParameter.ActualName = "Generations";
207
208      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
209      comparator.LeftSideParameter.ActualName = "Generations";
210      comparator.ResultParameter.ActualName = "Terminate";
211      comparator.RightSideParameter.ActualName = "MaximumGenerations";
212
213      conditionalBranch.ConditionParameter.ActualName = "Terminate";
214      #endregion
215
216      #region Create operator graph
217      OperatorGraph.InitialOperator = variableCreator;
218      variableCreator.Successor = resultsCollector1;
219      resultsCollector1.Successor = tempIdAssigner;
220      tempIdAssigner.Successor = setSubsumptionFalseSubScopesProcessor;
221      setSubsumptionFalseSubScopesProcessor.Operator = setSubsumpByAssigner;
222      setSubsumpByAssigner.Successor = setSubsumptionFalseAssigner;
223      setSubsumptionFalseAssigner.Successor = null;
224      setSubsumptionFalseSubScopesProcessor.Successor = selector;
225      selector.Successor = subScopesProcessor1;
226      subScopesProcessor1.Operators.Add(new EmptyOperator());
227      subScopesProcessor1.Operators.Add(childrenCreator);
228      subScopesProcessor1.Successor = mergingReducer;
229      childrenCreator.Successor = uniformSubScopesProcessor1;
230      uniformSubScopesProcessor1.Operator = crossover;
231      uniformSubScopesProcessor1.Successor = subScopesCounter;
232      crossover.Successor = afterCrossover;
233      afterCrossover.Successor = mutator;
234      mutator.Successor = doGASubsumptionBranch1;
235      doGASubsumptionBranch1.TrueBranch = checkGASubsumptionOperator;
236      doGASubsumptionBranch1.FalseBranch = new EmptyOperator();
237      doGASubsumptionBranch1.Successor = subScopesRemover;
238      subScopesRemover.Successor = null;
239      subScopesCounter.Successor = null;
240      mergingReducer.Successor = doGASubsumptionBranch2;
241      doGASubsumptionBranch2.TrueBranch = executeGAsubsumptionOperator;
242      doGASubsumptionBranch2.FalseBranch = new EmptyOperator();
243      executeGAsubsumptionOperator.Successor = subsumptionSelector;
244      subsumptionSelector.Successor = subsumptionLeftReducer;
245      subsumptionLeftReducer.Successor = null;
246      doGASubsumptionBranch2.Successor = intCounter;
247      intCounter.Successor = comparator;
248      comparator.Successor = conditionalBranch;
249      conditionalBranch.FalseBranch = selector;
250      conditionalBranch.TrueBranch = null;
251      conditionalBranch.Successor = null;
252      #endregion
253    }
254
255    public override IOperation Apply() {
256      if (CrossoverParameter.ActualValue == null)
257        return null;
258      return base.Apply();
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.