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

Last change on this file since 9089 was 9089, checked in by sforsten, 6 years ago

#1980:

  • added ConditionActionClassificationProblem
  • added ConditionActionEncoding
  • added Manipulators, Crossovers and an LCSAdaptedGeneticAlgorithm
  • changed LearningClassifierSystemMainLoop
File size: 11.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.Operators;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31namespace HeuristicLab.Algorithms.LearningClassifierSystems {
32  /// <summary>
33  /// An operator which represents the main loop of a genetic algorithm.
34  /// </summary>
35  [Item("LCSAdaptedGeneticAlgorithm", "An operator which represents the main loop of a genetic algorithm, which has been adapdet for learning classifier systems.")]
36  [StorableClass]
37  public sealed class LCSAdaptedGeneticAlgorithm : AlgorithmOperator {
38    #region Parameter properties
39    public ValueLookupParameter<IRandom> RandomParameter {
40      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    public ValueLookupParameter<BoolValue> MaximizationParameter {
43      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
44    }
45    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
46      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48    public ValueLookupParameter<IOperator> SelectorParameter {
49      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
50    }
51    public ValueLookupParameter<IOperator> CrossoverParameter {
52      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
53    }
54    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
55      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
56    }
57    public ValueLookupParameter<IOperator> MutatorParameter {
58      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
59    }
60    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
61      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
62    }
63    public ValueLookupParameter<VariableCollection> ResultsParameter {
64      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
65    }
66    public ValueLookupParameter<IOperator> AnalyzerParameter {
67      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
68    }
69    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
70      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
71    }
72    public ValueLookupParameter<IntValue> PopulationSizeParameter {
73      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
74    }
75    private ScopeParameter CurrentScopeParameter {
76      get { return (ScopeParameter)Parameters["CurrentScope"]; }
77    }
78
79    public IScope CurrentScope {
80      get { return CurrentScopeParameter.ActualValue; }
81    }
82    #endregion
83
84    [StorableConstructor]
85    private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
86    private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
87      : base(original, cloner) {
88    }
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new LCSAdaptedGeneticAlgorithm(this, cloner);
91    }
92    public LCSAdaptedGeneticAlgorithm()
93      : base() {
94      Initialize();
95    }
96
97    private void Initialize() {
98      #region Create parameters
99      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
100      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
101      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
102      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
103      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
104      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
105      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
106      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
107      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
108      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
109      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
110      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
111      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
112      #endregion
113
114      #region Create operators
115      VariableCreator variableCreator = new VariableCreator();
116      ResultsCollector resultsCollector1 = new ResultsCollector();
117      Placeholder analyzer1 = new Placeholder();
118      Placeholder selector = new Placeholder();
119      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
120      ChildrenCreator childrenCreator = new ChildrenCreator();
121      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
122      Placeholder crossover = new Placeholder();
123      DataReducer dataReducer = new DataReducer();
124      StochasticBranch stochasticBranch = new StochasticBranch();
125      Placeholder mutator = new Placeholder();
126      SubScopesRemover subScopesRemover = new SubScopesRemover();
127      SubScopesCounter subScopesCounter = new SubScopesCounter();
128      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
129      BestSelector bestSelector = new BestSelector();
130      RightReducer rightReducer = new RightReducer();
131      MergingReducer mergingReducer = new MergingReducer();
132      IntCounter intCounter = new IntCounter();
133      Comparator comparator = new Comparator();
134      Placeholder analyzer2 = new Placeholder();
135      ConditionalBranch conditionalBranch = new ConditionalBranch();
136
137      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
138
139      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
140      resultsCollector1.ResultsParameter.ActualName = "Results";
141
142      analyzer1.Name = "Analyzer";
143      analyzer1.OperatorParameter.ActualName = "Analyzer";
144
145      selector.Name = "Selector";
146      selector.OperatorParameter.ActualName = "Selector";
147
148      childrenCreator.ParentsPerChild = new IntValue(2);
149
150      crossover.Name = "Crossover";
151      crossover.OperatorParameter.ActualName = "Crossover";
152
153      dataReducer.Name = "Average Fitness of Parents";
154      dataReducer.ParameterToReduce.ActualName = "Fitness";
155      dataReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Avg);
156      dataReducer.TargetParameter.ActualName = "Fitness";
157      dataReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
158
159      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
160      stochasticBranch.RandomParameter.ActualName = "Random";
161
162      mutator.Name = "Mutator";
163      mutator.OperatorParameter.ActualName = "Mutator";
164
165      subScopesRemover.RemoveAllSubScopes = true;
166
167      subScopesCounter.Name = "Increment EvaluatedSolutions";
168      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
169
170      bestSelector.CopySelected = new BoolValue(false);
171      bestSelector.MaximizationParameter.ActualName = "Maximization";
172      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "N";
173      bestSelector.QualityParameter.ActualName = "Fitness";
174
175      intCounter.Increment = new IntValue(1);
176      intCounter.ValueParameter.ActualName = "Generations";
177
178      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
179      comparator.LeftSideParameter.ActualName = "Generations";
180      comparator.ResultParameter.ActualName = "Terminate";
181      comparator.RightSideParameter.ActualName = "MaximumGenerations";
182
183      analyzer2.Name = "Analyzer";
184      analyzer2.OperatorParameter.ActualName = "Analyzer";
185
186      conditionalBranch.ConditionParameter.ActualName = "Terminate";
187      #endregion
188
189      #region Create operator graph
190      OperatorGraph.InitialOperator = variableCreator;
191      variableCreator.Successor = resultsCollector1;
192      resultsCollector1.Successor = analyzer1;
193      analyzer1.Successor = selector;
194      selector.Successor = subScopesProcessor1;
195      subScopesProcessor1.Operators.Add(new EmptyOperator());
196      subScopesProcessor1.Operators.Add(childrenCreator);
197      subScopesProcessor1.Successor = subScopesProcessor2;
198      childrenCreator.Successor = uniformSubScopesProcessor1;
199      uniformSubScopesProcessor1.Operator = crossover;
200      uniformSubScopesProcessor1.Successor = subScopesCounter;
201      crossover.Successor = dataReducer;
202      dataReducer.Successor = stochasticBranch;
203      stochasticBranch.FirstBranch = mutator;
204      stochasticBranch.SecondBranch = null;
205      stochasticBranch.Successor = subScopesRemover;
206      mutator.Successor = null;
207      subScopesRemover.Successor = null;
208      subScopesCounter.Successor = null;
209      subScopesProcessor2.Operators.Add(bestSelector);
210      subScopesProcessor2.Operators.Add(new EmptyOperator());
211      subScopesProcessor2.Successor = mergingReducer;
212      bestSelector.Successor = rightReducer;
213      rightReducer.Successor = null;
214      mergingReducer.Successor = intCounter;
215      intCounter.Successor = comparator;
216      comparator.Successor = analyzer2;
217      analyzer2.Successor = conditionalBranch;
218      conditionalBranch.FalseBranch = selector;
219      conditionalBranch.TrueBranch = null;
220      conditionalBranch.Successor = null;
221      #endregion
222    }
223
224    public override IOperation Apply() {
225      if (CrossoverParameter.ActualValue == null)
226        return null;
227      return base.Apply();
228    }
229  }
230}
Note: See TracBrowser for help on using the repository browser.