Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

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