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

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

#1980:

  • included an adapted version of GA correctly
  • added action set subsumption
  • added deletion after GA and before covering
File size: 10.5 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<IOperator> AfterCrossoverParameter {
55      get { return (ValueLookupParameter<IOperator>)Parameters["AfterCrossover"]; }
56    }
57    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
58      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
59    }
60    public ValueLookupParameter<IOperator> MutatorParameter {
61      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
62    }
63    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
64      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
65    }
66    public ValueLookupParameter<VariableCollection> ResultsParameter {
67      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
68    }
69    public ValueLookupParameter<IOperator> AnalyzerParameter {
70      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
71    }
72    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
73      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
74    }
75    public ValueLookupParameter<IntValue> PopulationSizeParameter {
76      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
77    }
78    private ScopeParameter CurrentScopeParameter {
79      get { return (ScopeParameter)Parameters["CurrentScope"]; }
80    }
81
82    public IScope CurrentScope {
83      get { return CurrentScopeParameter.ActualValue; }
84    }
85    #endregion
86
87    [StorableConstructor]
88    private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
89    private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
90      : base(original, cloner) {
91    }
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new LCSAdaptedGeneticAlgorithm(this, cloner);
94    }
95    public LCSAdaptedGeneticAlgorithm()
96      : base() {
97      Initialize();
98    }
99
100    private void Initialize() {
101      #region Create parameters
102      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
103      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
104      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
105      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
106      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
107      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCrossover", "The operator executed after crossing the solutions."));
108      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
109      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
110      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
111      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
112      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
113      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
114      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
115      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
116      #endregion
117
118      #region Create operators
119      VariableCreator variableCreator = new VariableCreator();
120      ResultsCollector resultsCollector1 = new ResultsCollector();
121      Placeholder analyzer1 = new Placeholder();
122      Placeholder selector = new Placeholder();
123      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
124      ChildrenCreator childrenCreator = new ChildrenCreator();
125      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
126      Placeholder crossover = new Placeholder();
127      Placeholder afterCrossover = new Placeholder();
128      StochasticBranch stochasticBranch = new StochasticBranch();
129      Placeholder mutator = new Placeholder();
130      SubScopesRemover subScopesRemover = new SubScopesRemover();
131      SubScopesCounter subScopesCounter = new SubScopesCounter();
132      MergingReducer mergingReducer = new MergingReducer();
133      IntCounter intCounter = new IntCounter();
134      Comparator comparator = new Comparator();
135      Placeholder analyzer2 = new Placeholder();
136      ConditionalBranch conditionalBranch = new ConditionalBranch();
137
138      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
139
140      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
141      resultsCollector1.ResultsParameter.ActualName = "Results";
142
143      analyzer1.Name = "Analyzer";
144      analyzer1.OperatorParameter.ActualName = "Analyzer";
145
146      selector.Name = "Selector";
147      selector.OperatorParameter.ActualName = "Selector";
148
149      childrenCreator.ParentsPerChild = new IntValue(2);
150
151      crossover.Name = "Crossover";
152      crossover.OperatorParameter.ActualName = "Crossover";
153
154      afterCrossover.Name = "AfterCrossover";
155      afterCrossover.OperatorParameter.ActualName = "AfterCrossover";
156
157      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
158      stochasticBranch.RandomParameter.ActualName = "Random";
159
160      mutator.Name = "Mutator";
161      mutator.OperatorParameter.ActualName = "Mutator";
162
163      subScopesRemover.RemoveAllSubScopes = true;
164
165      subScopesCounter.Name = "Increment EvaluatedSolutions";
166      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
167
168      intCounter.Increment = new IntValue(1);
169      intCounter.ValueParameter.ActualName = "Generations";
170
171      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
172      comparator.LeftSideParameter.ActualName = "Generations";
173      comparator.ResultParameter.ActualName = "Terminate";
174      comparator.RightSideParameter.ActualName = "MaximumGenerations";
175
176      analyzer2.Name = "Analyzer";
177      analyzer2.OperatorParameter.ActualName = "Analyzer";
178
179      conditionalBranch.ConditionParameter.ActualName = "Terminate";
180      #endregion
181
182      #region Create operator graph
183      OperatorGraph.InitialOperator = variableCreator;
184      variableCreator.Successor = resultsCollector1;
185      resultsCollector1.Successor = analyzer1;
186      analyzer1.Successor = selector;
187      selector.Successor = subScopesProcessor1;
188      subScopesProcessor1.Operators.Add(new EmptyOperator());
189      subScopesProcessor1.Operators.Add(childrenCreator);
190      subScopesProcessor1.Successor = mergingReducer;
191      childrenCreator.Successor = uniformSubScopesProcessor1;
192      uniformSubScopesProcessor1.Operator = crossover;
193      uniformSubScopesProcessor1.Successor = subScopesCounter;
194      crossover.Successor = afterCrossover;
195      afterCrossover.Successor = stochasticBranch;
196      stochasticBranch.FirstBranch = mutator;
197      stochasticBranch.SecondBranch = null;
198      stochasticBranch.Successor = subScopesRemover;
199      mutator.Successor = null;
200      subScopesRemover.Successor = null;
201      subScopesCounter.Successor = null;
202      mergingReducer.Successor = intCounter;
203      intCounter.Successor = comparator;
204      comparator.Successor = analyzer2;
205      analyzer2.Successor = conditionalBranch;
206      conditionalBranch.FalseBranch = selector;
207      conditionalBranch.TrueBranch = null;
208      conditionalBranch.Successor = null;
209      #endregion
210    }
211
212    public override IOperation Apply() {
213      if (CrossoverParameter.ActualValue == null)
214        return null;
215      return base.Apply();
216    }
217  }
218}
Note: See TracBrowser for help on using the repository browser.