Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • made classes in Problems.ConditionActionClassification abstract
  • added Problems.VariableVectorClassification and Problems.CombinedIntegerVectorClassification
  • LCS works now with arbitrary problems, which implement ConditionActionClassificationProblem
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
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> AfterCopyingParentsParameter {
57      get { return (ValueLookupParameter<IOperator>)Parameters["AfterCopyingParents"]; }
58    }
59    public ValueLookupParameter<IOperator> CrossoverParameter {
60      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
61    }
62    public ValueLookupParameter<IOperator> AfterCrossoverParameter {
63      get { return (ValueLookupParameter<IOperator>)Parameters["AfterCrossover"]; }
64    }
65    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
66      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
67    }
68    public ValueLookupParameter<PercentValue> CrossoverProbabilityParameter {
69      get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
70    }
71    public ValueLookupParameter<IOperator> MutatorParameter {
72      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
73    }
74    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
75      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
76    }
77    public ValueLookupParameter<VariableCollection> ResultsParameter {
78      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
79    }
80    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
81      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
82    }
83    public ValueLookupParameter<IntValue> PopulationSizeParameter {
84      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
85    }
86    public ValueLookupParameter<BoolValue> DoGASubsumptionParameter {
87      get { return (ValueLookupParameter<BoolValue>)Parameters["DoGASubsumption"]; }
88    }
89    private ScopeParameter CurrentScopeParameter {
90      get { return (ScopeParameter)Parameters["CurrentScope"]; }
91    }
92
93    public IScope CurrentScope {
94      get { return CurrentScopeParameter.ActualValue; }
95    }
96    #endregion
97
98    private CheckGASubsumptionOperator checkGASubsumptionOperator;
99
100    [StorableConstructor]
101    private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
102    private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
103      : base(original, cloner) {
104    }
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new LCSAdaptedGeneticAlgorithm(this, cloner);
107    }
108    public LCSAdaptedGeneticAlgorithm()
109      : base() {
110      Initialize();
111    }
112
113    private void Initialize() {
114      #region Create parameters
115      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
116      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
117      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
118      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
119      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCopyingParents", "The operator executed after copying a parent instead of using crossover."));
120      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
121      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCrossover", "The operator executed after crossing the solutions."));
122      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
123      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that druing the mutation operator a mutation takes place."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
125      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
126      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
127      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
128      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
129      Parameters.Add(new ValueLookupParameter<BoolValue>("DoGASubsumption", "Sets if GA subsumption is executed."));
130      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
131      #endregion
132
133      #region Create operators
134      VariableCreator variableCreator = new VariableCreator();
135      ResultsCollector resultsCollector1 = new ResultsCollector();
136      Placeholder selector = new Placeholder();
137      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
138      ChildrenCreator childrenCreator = new ChildrenCreator();
139      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
140      StochasticBranch crossoverStochasticBranch = new StochasticBranch();
141      RandomSelector randomSelector = new RandomSelector();
142      PreservingRightReducer preservingRightReducer = new PreservingRightReducer();
143      Placeholder afterCopyingParents = new Placeholder();
144      Placeholder crossover = new Placeholder();
145      Placeholder afterCrossover = new Placeholder();
146      Placeholder mutator = new Placeholder();
147      SubScopesRemover subScopesRemover = new SubScopesRemover();
148      SubScopesCounter subScopesCounter = new SubScopesCounter();
149      MergingReducer mergingReducer = new MergingReducer();
150      IntCounter intCounter = new IntCounter();
151      Comparator comparator = new Comparator();
152      ConditionalBranch conditionalBranch = new ConditionalBranch();
153
154      TempSubScopeIDAssigner tempIdAssigner = new TempSubScopeIDAssigner();
155      ConditionalBranch doGASubsumptionBranch1 = new ConditionalBranch();
156      UniformSubScopesProcessor setSubsumptionFalseSubScopesProcessor = new UniformSubScopesProcessor();
157      Assigner setSubsumpByAssigner = new Assigner();
158      Assigner setSubsumptionFalseAssigner = new Assigner();
159      checkGASubsumptionOperator = new CheckGASubsumptionOperator();
160      ConditionalBranch doGASubsumptionBranch2 = new ConditionalBranch();
161      ExecuteGASubsumptionOperator executeGAsubsumptionOperator = new ExecuteGASubsumptionOperator();
162      ConditionalSelector subsumptionSelector = new ConditionalSelector();
163      LeftReducer subsumptionLeftReducer = new LeftReducer();
164
165      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
166
167      //resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
168      resultsCollector1.ResultsParameter.ActualName = "Results";
169
170      tempIdAssigner.LeftSideParameter.ActualName = TEMPID;
171
172      setSubsumpByAssigner.LeftSideParameter.ActualName = SUBSUMEDBY;
173      setSubsumpByAssigner.RightSideParameter.Value = new IntValue(-1);
174
175      setSubsumptionFalseAssigner.LeftSideParameter.ActualName = SUBSUMED;
176      setSubsumptionFalseAssigner.RightSideParameter.Value = new BoolValue(false);
177
178      selector.Name = "Selector";
179      selector.OperatorParameter.ActualName = "Selector";
180
181      childrenCreator.ParentsPerChild = new IntValue(2);
182
183      crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.ActualName;
184      crossoverStochasticBranch.RandomParameter.ActualName = "Random";
185
186      randomSelector.CopySelected.Value = true;
187      randomSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
188
189      afterCopyingParents.Name = "AfterCopyingParents";
190      afterCopyingParents.OperatorParameter.ActualName = "AfterCopyingParents";
191
192      crossover.Name = "Crossover";
193      crossover.OperatorParameter.ActualName = "Crossover";
194
195      afterCrossover.Name = "AfterCrossover";
196      afterCrossover.OperatorParameter.ActualName = "AfterCrossover";
197
198      mutator.Name = "Mutator";
199      mutator.OperatorParameter.ActualName = "Mutator";
200
201      doGASubsumptionBranch1.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
202
203      checkGASubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
204      checkGASubsumptionOperator.ExperiencesParameter.ActualName = "Experience";
205      checkGASubsumptionOperator.ErrorsParameter.ActualName = "Error";
206      checkGASubsumptionOperator.TempIDParameter.ActualName = TEMPID;
207      checkGASubsumptionOperator.ErrorZeroParameter.ActualName = "ErrorZero";
208      checkGASubsumptionOperator.ThetaSubsumptionParameter.ActualName = "ThetaSubsumption";
209      checkGASubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
210      checkGASubsumptionOperator.SubsumedParameter.ActualName = SUBSUMED;
211
212      subScopesRemover.RemoveAllSubScopes = true;
213
214      doGASubsumptionBranch2.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
215
216      executeGAsubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
217      executeGAsubsumptionOperator.TempIDParameter.ActualName = TEMPID;
218      executeGAsubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
219
220      subsumptionSelector.ConditionParameter.ActualName = SUBSUMED;
221      subsumptionSelector.CopySelected = new BoolValue(false);
222
223      subScopesCounter.Name = "Increment EvaluatedSolutions";
224      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
225
226      intCounter.Increment = new IntValue(1);
227      intCounter.ValueParameter.ActualName = "Generations";
228
229      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
230      comparator.LeftSideParameter.ActualName = "Generations";
231      comparator.ResultParameter.ActualName = "Terminate";
232      comparator.RightSideParameter.ActualName = "MaximumGenerations";
233
234      conditionalBranch.ConditionParameter.ActualName = "Terminate";
235      #endregion
236
237      #region Create operator graph
238      OperatorGraph.InitialOperator = variableCreator;
239      variableCreator.Successor = resultsCollector1;
240      resultsCollector1.Successor = tempIdAssigner;
241      tempIdAssigner.Successor = setSubsumptionFalseSubScopesProcessor;
242      setSubsumptionFalseSubScopesProcessor.Operator = setSubsumpByAssigner;
243      setSubsumpByAssigner.Successor = setSubsumptionFalseAssigner;
244      setSubsumptionFalseAssigner.Successor = null;
245      setSubsumptionFalseSubScopesProcessor.Successor = selector;
246      selector.Successor = subScopesProcessor1;
247      subScopesProcessor1.Operators.Add(new EmptyOperator());
248      subScopesProcessor1.Operators.Add(childrenCreator);
249      subScopesProcessor1.Successor = mergingReducer;
250      childrenCreator.Successor = uniformSubScopesProcessor1;
251      uniformSubScopesProcessor1.Operator = crossoverStochasticBranch;
252      uniformSubScopesProcessor1.Successor = subScopesCounter;
253      crossoverStochasticBranch.FirstBranch = crossover;
254      crossoverStochasticBranch.SecondBranch = randomSelector;
255      randomSelector.Successor = preservingRightReducer;
256      preservingRightReducer.Successor = afterCopyingParents;
257      crossoverStochasticBranch.Successor = mutator;
258      crossover.Successor = afterCrossover;
259      mutator.Successor = doGASubsumptionBranch1;
260      doGASubsumptionBranch1.TrueBranch = checkGASubsumptionOperator;
261      doGASubsumptionBranch1.FalseBranch = new EmptyOperator();
262      doGASubsumptionBranch1.Successor = subScopesRemover;
263      subScopesRemover.Successor = null;
264      subScopesCounter.Successor = null;
265      mergingReducer.Successor = doGASubsumptionBranch2;
266      doGASubsumptionBranch2.TrueBranch = executeGAsubsumptionOperator;
267      doGASubsumptionBranch2.FalseBranch = new EmptyOperator();
268      executeGAsubsumptionOperator.Successor = subsumptionSelector;
269      subsumptionSelector.Successor = subsumptionLeftReducer;
270      subsumptionLeftReducer.Successor = null;
271      doGASubsumptionBranch2.Successor = intCounter;
272      intCounter.Successor = comparator;
273      comparator.Successor = conditionalBranch;
274      conditionalBranch.FalseBranch = selector;
275      conditionalBranch.TrueBranch = null;
276      conditionalBranch.Successor = null;
277      #endregion
278    }
279
280    public override IOperation Apply() {
281      if (CrossoverParameter.ActualValue == null)
282        return null;
283      return base.Apply();
284    }
285
286    public void SetChildName(string childName) {
287      checkGASubsumptionOperator.ChildClassifiersParameter.ActualName = childName;
288      checkGASubsumptionOperator.ParentsClassifiersParameter.ActualName = childName;
289    }
290  }
291}
Note: See TracBrowser for help on using the repository browser.