Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • change standard parameter settings in ConditionActionClassificationProblem
  • fixed bug: if a parent is copied instead of a crossover, the new individual is inserted in the population correctly
  • added missing attributes to ConditionActionClassificationProblemData
File size: 15.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> 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    [StorableConstructor]
99    private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
100    private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
101      : base(original, cloner) {
102    }
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new LCSAdaptedGeneticAlgorithm(this, cloner);
105    }
106    public LCSAdaptedGeneticAlgorithm()
107      : base() {
108      Initialize();
109    }
110
111    private void Initialize() {
112      #region Create parameters
113      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
114      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
115      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
116      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
117      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCopyingParents", "The operator executed after copying a parent instead of using crossover."));
118      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
119      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCrossover", "The operator executed after crossing the solutions."));
120      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
121      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that druing the mutation operator a mutation takes place."));
122      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
123      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
124      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
125      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
126      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
127      Parameters.Add(new ValueLookupParameter<BoolValue>("DoGASubsumption", "Sets if GA subsumption is executed."));
128      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
129      #endregion
130
131      #region Create operators
132      VariableCreator variableCreator = new VariableCreator();
133      ResultsCollector resultsCollector1 = new ResultsCollector();
134      Placeholder selector = new Placeholder();
135      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
136      ChildrenCreator childrenCreator = new ChildrenCreator();
137      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
138      StochasticBranch crossoverStochasticBranch = new StochasticBranch();
139      RandomSelector randomSelector = new RandomSelector();
140      PreservingRightReducer preservingRightReducer = new PreservingRightReducer();
141      Placeholder afterCopyingParents = new Placeholder();
142      Placeholder crossover = new Placeholder();
143      Placeholder afterCrossover = new Placeholder();
144      Placeholder mutator = new Placeholder();
145      SubScopesRemover subScopesRemover = new SubScopesRemover();
146      SubScopesCounter subScopesCounter = new SubScopesCounter();
147      MergingReducer mergingReducer = new MergingReducer();
148      IntCounter intCounter = new IntCounter();
149      Comparator comparator = new Comparator();
150      ConditionalBranch conditionalBranch = new ConditionalBranch();
151
152      TempSubScopeIDAssigner tempIdAssigner = new TempSubScopeIDAssigner();
153      ConditionalBranch doGASubsumptionBranch1 = new ConditionalBranch();
154      UniformSubScopesProcessor setSubsumptionFalseSubScopesProcessor = new UniformSubScopesProcessor();
155      Assigner setSubsumpByAssigner = new Assigner();
156      Assigner setSubsumptionFalseAssigner = new Assigner();
157      CheckGASubsumptionOperator checkGASubsumptionOperator = new CheckGASubsumptionOperator();
158      ConditionalBranch doGASubsumptionBranch2 = new ConditionalBranch();
159      ExecuteGASubsumptionOperator executeGAsubsumptionOperator = new ExecuteGASubsumptionOperator();
160      ConditionalSelector subsumptionSelector = new ConditionalSelector();
161      LeftReducer subsumptionLeftReducer = new LeftReducer();
162
163      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
164
165      //resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
166      resultsCollector1.ResultsParameter.ActualName = "Results";
167
168      tempIdAssigner.LeftSideParameter.ActualName = TEMPID;
169
170      setSubsumpByAssigner.LeftSideParameter.ActualName = SUBSUMEDBY;
171      setSubsumpByAssigner.RightSideParameter.Value = new IntValue(-1);
172
173      setSubsumptionFalseAssigner.LeftSideParameter.ActualName = SUBSUMED;
174      setSubsumptionFalseAssigner.RightSideParameter.Value = new BoolValue(false);
175
176      selector.Name = "Selector";
177      selector.OperatorParameter.ActualName = "Selector";
178
179      childrenCreator.ParentsPerChild = new IntValue(2);
180
181      crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.ActualName;
182      crossoverStochasticBranch.RandomParameter.ActualName = "Random";
183
184      randomSelector.CopySelected.Value = true;
185      randomSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
186
187      afterCopyingParents.Name = "AfterCopyingParents";
188      afterCopyingParents.OperatorParameter.ActualName = "AfterCopyingParents";
189
190      crossover.Name = "Crossover";
191      crossover.OperatorParameter.ActualName = "Crossover";
192
193      afterCrossover.Name = "AfterCrossover";
194      afterCrossover.OperatorParameter.ActualName = "AfterCrossover";
195
196      mutator.Name = "Mutator";
197      mutator.OperatorParameter.ActualName = "Mutator";
198
199      doGASubsumptionBranch1.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
200
201      checkGASubsumptionOperator.ChildClassifiersParameter.ActualName = "CombinedIntegerVector";
202      checkGASubsumptionOperator.ParentsClassifiersParameter.ActualName = "CombinedIntegerVector";
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}
Note: See TracBrowser for help on using the repository browser.