Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.GAssist/3.3/GAssistMainLoop.cs @ 9334

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

#1980:

  • added Algorithms.GAssist
  • adapted Problems.DecisionListClassification and Encodings.DecisionList
File size: 12.9 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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.GAssist {
32  /// <summary>
33  /// An operator which represents the main loop of a genetic algorithm.
34  /// </summary>
35  [Item("GAssistMainLoop", "An operator which represents the main loop of GAssist.")]
36  [StorableClass]
37  public sealed class GAssistMainLoop : 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<PercentValue> CrossoverProbabilityParameter {
52      get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
53    }
54    public ValueLookupParameter<IOperator> CrossoverParameter {
55      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
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<IOperator> SpecialStagesParameter {
64      get { return (ValueLookupParameter<IOperator>)Parameters["SpecialStages"]; }
65    }
66    public ValueLookupParameter<IOperator> EvaluatorParameter {
67      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
68    }
69    public ValueLookupParameter<IntValue> ElitesParameter {
70      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
71    }
72    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
73      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
74    }
75    public ValueLookupParameter<VariableCollection> ResultsParameter {
76      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
77    }
78    public ValueLookupParameter<IOperator> AnalyzerParameter {
79      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
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    private ScopeParameter CurrentScopeParameter {
88      get { return (ScopeParameter)Parameters["CurrentScope"]; }
89    }
90
91    public IScope CurrentScope {
92      get { return CurrentScopeParameter.ActualValue; }
93    }
94    #endregion
95
96    [StorableConstructor]
97    private GAssistMainLoop(bool deserializing) : base(deserializing) { }
98    private GAssistMainLoop(GAssistMainLoop original, Cloner cloner)
99      : base(original, cloner) {
100    }
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new GAssistMainLoop(this, cloner);
103    }
104    public GAssistMainLoop()
105      : base() {
106      Initialize();
107    }
108
109    private void Initialize() {
110      #region Create parameters
111      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
112      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
113      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
114      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
115      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
116      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
117      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
118      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
119      Parameters.Add(new ValueLookupParameter<IOperator>("SpecialStages", "The operator used for the special stages of GAssist."));
120      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
122      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
123      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
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 ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
128      #endregion
129
130      #region Create operators
131      VariableCreator variableCreator = new VariableCreator();
132      ResultsCollector resultsCollector1 = new ResultsCollector();
133      Placeholder analyzer1 = new Placeholder();
134      Placeholder selector = new Placeholder();
135      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
136      ChildrenCreator childrenCreator = new ChildrenCreator();
137      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
138      StochasticBranch stochasticBranchCrossover = new StochasticBranch();
139      Placeholder crossover = new Placeholder();
140      StochasticBranch stochasticBranchMutator = new StochasticBranch();
141      Placeholder mutator = new Placeholder();
142      Placeholder specialStages = new Placeholder();
143      SubScopesRemover subScopesRemover = new SubScopesRemover();
144      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
145      Placeholder evaluator = new Placeholder();
146      SubScopesCounter subScopesCounter = new SubScopesCounter();
147      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
148      BestSelector bestSelector = new BestSelector();
149      RightReducer rightReducer = new RightReducer();
150      MergingReducer mergingReducer = new MergingReducer();
151      IntCounter intCounter = new IntCounter();
152      Comparator comparator = new Comparator();
153      Placeholder analyzer2 = new Placeholder();
154      ConditionalBranch conditionalBranch = new ConditionalBranch();
155
156      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
157
158      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
159      resultsCollector1.ResultsParameter.ActualName = "Results";
160
161      analyzer1.Name = "Analyzer";
162      analyzer1.OperatorParameter.ActualName = "Analyzer";
163
164      selector.Name = "Selector";
165      selector.OperatorParameter.ActualName = "Selector";
166
167      childrenCreator.ParentsPerChild = new IntValue(2);
168
169      stochasticBranchCrossover.ProbabilityParameter.ActualName = "CrossoverProbability";
170      stochasticBranchCrossover.RandomParameter.ActualName = "Random";
171
172      crossover.Name = "Crossover";
173      crossover.OperatorParameter.ActualName = "Crossover";
174
175      stochasticBranchMutator.ProbabilityParameter.ActualName = "MutationProbability";
176      stochasticBranchMutator.RandomParameter.ActualName = "Random";
177
178      mutator.Name = "Mutator";
179      mutator.OperatorParameter.ActualName = "Mutator";
180
181      specialStages.Name = "SpecialStages";
182      specialStages.OperatorParameter.ActualName = "SpecialStages";
183
184      subScopesRemover.RemoveAllSubScopes = true;
185
186      uniformSubScopesProcessor2.Parallel.Value = true;
187
188      evaluator.Name = "Evaluator";
189      evaluator.OperatorParameter.ActualName = "Evaluator";
190
191      subScopesCounter.Name = "Increment EvaluatedSolutions";
192      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
193
194      bestSelector.CopySelected = new BoolValue(false);
195      bestSelector.MaximizationParameter.ActualName = "Maximization";
196      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
197      bestSelector.QualityParameter.ActualName = "Quality";
198
199      intCounter.Increment = new IntValue(1);
200      intCounter.ValueParameter.ActualName = "Generations";
201
202      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
203      comparator.LeftSideParameter.ActualName = "Generations";
204      comparator.ResultParameter.ActualName = "Terminate";
205      comparator.RightSideParameter.ActualName = "MaximumGenerations";
206
207      analyzer2.Name = "Analyzer";
208      analyzer2.OperatorParameter.ActualName = "Analyzer";
209
210      conditionalBranch.ConditionParameter.ActualName = "Terminate";
211      #endregion
212
213      #region Create operator graph
214      OperatorGraph.InitialOperator = variableCreator;
215      variableCreator.Successor = resultsCollector1;
216      resultsCollector1.Successor = analyzer1;
217      analyzer1.Successor = selector;
218      selector.Successor = subScopesProcessor1;
219      subScopesProcessor1.Operators.Add(new EmptyOperator());
220      subScopesProcessor1.Operators.Add(childrenCreator);
221      subScopesProcessor1.Successor = subScopesProcessor2;
222      childrenCreator.Successor = uniformSubScopesProcessor1;
223      uniformSubScopesProcessor1.Operator = stochasticBranchCrossover;
224      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
225      stochasticBranchCrossover.FirstBranch = crossover;
226      stochasticBranchCrossover.SecondBranch = null;
227      stochasticBranchCrossover.Successor = stochasticBranchMutator;
228      crossover.Successor = null;
229      stochasticBranchMutator.FirstBranch = mutator;
230      stochasticBranchMutator.SecondBranch = null;
231      stochasticBranchMutator.Successor = specialStages;
232      mutator.Successor = null;
233      specialStages.Successor = subScopesRemover;
234      subScopesRemover.Successor = null;
235      uniformSubScopesProcessor2.Operator = evaluator;
236      uniformSubScopesProcessor2.Successor = subScopesCounter;
237      evaluator.Successor = null;
238      subScopesCounter.Successor = null;
239      subScopesProcessor2.Operators.Add(bestSelector);
240      subScopesProcessor2.Operators.Add(new EmptyOperator());
241      subScopesProcessor2.Successor = mergingReducer;
242      bestSelector.Successor = rightReducer;
243      rightReducer.Successor = null;
244      mergingReducer.Successor = intCounter;
245      intCounter.Successor = comparator;
246      comparator.Successor = analyzer2;
247      analyzer2.Successor = conditionalBranch;
248      conditionalBranch.FalseBranch = selector;
249      conditionalBranch.TrueBranch = null;
250      conditionalBranch.Successor = null;
251      #endregion
252    }
253
254    public override IOperation Apply() {
255      if (CrossoverParameter.ActualValue == null)
256        return null;
257      return base.Apply();
258    }
259  }
260}
Note: See TracBrowser for help on using the repository browser.