Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGAMainLoop.cs @ 8377

Last change on this file since 8377 was 8377, checked in by jkarder, 12 years ago

#1247:

  • added support for batch offspring creation
  • added population size analyzer
  • improved operator graph
File size: 19.7 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.RAPGA {
32  /// <summary>
33  /// An operator which represents the main loop of a relevant alleles preserving genetic algorithm.
34  /// </summary>
35  [Item("RAPGAMainLoop", "An operator which represents the main loop of a relevant alleles preserving genetic algorithm.")]
36  [StorableClass]
37  public sealed class RAPGAMainLoop : 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<IOperator> EvaluatorParameter {
61      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
62    }
63    public ValueLookupParameter<IntValue> ElitesParameter {
64      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
65    }
66    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
67      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
68    }
69    public ValueLookupParameter<VariableCollection> ResultsParameter {
70      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
71    }
72    public ValueLookupParameter<IOperator> AnalyzerParameter {
73      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
74    }
75    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
76      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
77    }
78    public ValueLookupParameter<IntValue> PopulationSizeParameter {
79      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
80    }
81    public IValueLookupParameter<IntValue> MinimumPopulationSizeParameter {
82      get { return (IValueLookupParameter<IntValue>)Parameters["MinimumPopulationSize"]; }
83    }
84    public IValueLookupParameter<IntValue> MaximumPopulationSizeParameter {
85      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
86    }
87    public IValueLookupParameter<DoubleValue> ComparisonFactorParameter {
88      get { return (IValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
89    }
90    public IValueLookupParameter<IntValue> EffortParameter {
91      get { return (IValueLookupParameter<IntValue>)Parameters["Effort"]; }
92    }
93    private ScopeParameter CurrentScopeParameter {
94      get { return (ScopeParameter)Parameters["CurrentScope"]; }
95    }
96
97    public IScope CurrentScope {
98      get { return CurrentScopeParameter.ActualValue; }
99    }
100    #endregion
101
102    [StorableConstructor]
103    private RAPGAMainLoop(bool deserializing) : base(deserializing) { }
104    private RAPGAMainLoop(RAPGAMainLoop original, Cloner cloner) : base(original, cloner) { }
105    public RAPGAMainLoop()
106      : base() {
107      Initialize();
108    }
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new RAPGAMainLoop(this, cloner);
111    }
112
113    private void Initialize() {
114      #region Create parameters
115      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
116      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
117      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
118      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
119      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
120      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
121      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
122      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."));
123      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
124      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
125      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
126      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
127      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
128      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
129      Parameters.Add(new ValueLookupParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions."));
130      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions."));
131      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor."));
132      Parameters.Add(new ValueLookupParameter<IntValue>("Effort", "The maximum number of offspring created in each generation."));
133      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
134      #endregion
135
136      #region Create operators
137      VariableCreator variableCreator = new VariableCreator();
138      Assigner assigner1 = new Assigner();
139      ResultsCollector resultsCollector = new ResultsCollector();
140      Placeholder analyzer1 = new Placeholder();
141      Placeholder selector = new Placeholder();
142      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
143      ChildrenCreator childrenCreator = new ChildrenCreator();
144      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
145      Placeholder crossover = new Placeholder();
146      StochasticBranch stochasticBranch = new StochasticBranch();
147      Placeholder mutator = new Placeholder();
148      Placeholder evaluator = new Placeholder();
149      WeightedParentsQualityComparator weightedParentsQualityComparator = new WeightedParentsQualityComparator();
150      SubScopesRemover subScopesRemover = new SubScopesRemover();
151      SubScopesCounter subScopesCounter1 = new SubScopesCounter();
152      IntCounter intCounter1 = new IntCounter();
153      ConditionalSelector conditionalSelector = new ConditionalSelector();
154      RightReducer rightReducer1 = new RightReducer();
155      DuplicatesSelector duplicateSelector = new DuplicatesSelector();
156      LeftReducer leftReducer1 = new LeftReducer();
157      ProgressiveOffspringPreserver progressiveOffspringSelector = new ProgressiveOffspringPreserver();
158      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
159      Comparator comparator1 = new Comparator();
160      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
161      Comparator comparator2 = new Comparator();
162      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
163      LeftReducer leftReducer2 = new LeftReducer();
164      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
165      BestSelector bestSelector = new BestSelector();
166      RightReducer rightReducer2 = new RightReducer();
167      ScopeCleaner scopeCleaner = new ScopeCleaner();
168      OffspringRestorer offspringRestorer = new OffspringRestorer();
169      MergingReducer mergingReducer = new MergingReducer();
170      IntCounter intCounter2 = new IntCounter();
171      SubScopesCounter subScopesCounter3 = new SubScopesCounter();
172      Comparator comparator3 = new Comparator();
173      Placeholder analyzer2 = new Placeholder();
174      ConditionalBranch conditionalBranch3 = new ConditionalBranch();
175      Comparator comparator4 = new Comparator();
176      ConditionalBranch conditionalBranch4 = new ConditionalBranch();
177      Assigner assigner2 = new Assigner();
178      Assigner assigner3 = new Assigner();
179      Assigner assigner4 = new Assigner();
180
181      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class RAPGA expects this to be called Generations
182      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentPopulationSize", new IntValue(0)));
183      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfCreatedOffspring", new IntValue(0)));
184      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfSuccessfulOffspring", new IntValue(0)));
185      variableCreator.CollectedValues.Add(new ValueParameter<ScopeList>("OffspringList", new ScopeList()));
186
187      assigner1.Name = "Initialize CurrentPopulationSize";
188      assigner1.LeftSideParameter.ActualName = "CurrentPopulationSize";
189      assigner1.RightSideParameter.ActualName = PopulationSizeParameter.Name;
190
191      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
192      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
193      resultsCollector.ResultsParameter.ActualName = "Results";
194
195      analyzer1.Name = "Analyzer";
196      analyzer1.OperatorParameter.ActualName = "Analyzer";
197
198      selector.Name = "Selector";
199      selector.OperatorParameter.ActualName = "Selector";
200
201      childrenCreator.ParentsPerChild = new IntValue(2);
202
203      uniformSubScopesProcessor.Parallel.Value = true;
204
205      crossover.Name = "Crossover";
206      crossover.OperatorParameter.ActualName = "Crossover";
207
208      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
209      stochasticBranch.RandomParameter.ActualName = "Random";
210
211      mutator.Name = "Mutator";
212      mutator.OperatorParameter.ActualName = "Mutator";
213
214      evaluator.Name = "Evaluator";
215      evaluator.OperatorParameter.ActualName = "Evaluator";
216
217      weightedParentsQualityComparator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
218      weightedParentsQualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
219      weightedParentsQualityComparator.MaximizationParameter.ActualName = MaximizationParameter.Name;
220      weightedParentsQualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
221      weightedParentsQualityComparator.ResultParameter.ActualName = "SuccessfulOffspring";
222
223      subScopesRemover.RemoveAllSubScopes = true;
224
225      subScopesCounter1.Name = "Increment NumberOfCreatedOffspring";
226      subScopesCounter1.ValueParameter.ActualName = "NumberOfCreatedOffspring";
227
228      intCounter1.Name = "Increment EvaluatedSolutions";
229      intCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
230      intCounter1.Increment = null;
231      intCounter1.IncrementParameter.ActualName = "NumberOfCreatedOffspring";
232
233      conditionalSelector.ConditionParameter.ActualName = "SuccessfulOffspring";
234      conditionalSelector.ConditionParameter.Depth = 1;
235      conditionalSelector.CopySelected.Value = false;
236
237      duplicateSelector.CopySelected.Value = false;
238
239      progressiveOffspringSelector.OffspringListParameter.ActualName = "OffspringList";
240      progressiveOffspringSelector.ElitesParameter.ActualName = ElitesParameter.Name;
241      progressiveOffspringSelector.MaximumPopulationSizeParameter.ActualName = MaximumPopulationSizeParameter.Name;
242
243      subScopesCounter2.Name = "Count Successful Offspring";
244      subScopesCounter2.ValueParameter.ActualName = "NumberOfSuccessfulOffspring";
245
246      comparator1.Name = "NumberOfSuccessfulOffspring >= MaximumPopulationSize";
247      comparator1.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
248      comparator1.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
249      comparator1.RightSideParameter.ActualName = MaximumPopulationSizeParameter.Name;
250      comparator1.ResultParameter.ActualName = "Break";
251
252      conditionalBranch1.Name = "Break?";
253      conditionalBranch1.ConditionParameter.ActualName = "Break";
254
255      comparator2.Name = "NumberOfCreatedOffspring >= Effort";
256      comparator2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
257      comparator2.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";
258      comparator2.RightSideParameter.ActualName = EffortParameter.Name;
259      comparator2.ResultParameter.ActualName = "Break";
260
261      conditionalBranch2.Name = "Break?";
262      conditionalBranch2.ConditionParameter.ActualName = "Break";
263
264      bestSelector.CopySelected = new BoolValue(false);
265      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
266      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
267      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
268
269      intCounter2.Name = "Increment Generations";
270      intCounter2.Increment = new IntValue(1);
271      intCounter2.ValueParameter.ActualName = "Generations";
272      subScopesCounter3.Name = "Set CurrentPopulationSize";
273
274      subScopesCounter3.ValueParameter.ActualName = "CurrentPopulationSize";
275      subScopesCounter3.AccumulateParameter.Value = new BoolValue(false);
276
277      comparator3.Name = "Generations >= MaximumGenerations";
278      comparator3.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
279      comparator3.LeftSideParameter.ActualName = "Generations";
280      comparator3.ResultParameter.ActualName = "Terminate";
281      comparator3.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
282
283      analyzer2.Name = "Analyzer";
284      analyzer2.OperatorParameter.ActualName = "Analyzer";
285
286      conditionalBranch3.Name = "Terminate?";
287      conditionalBranch3.ConditionParameter.ActualName = "Terminate";
288
289      comparator4.Name = "CurrentPopulationSize < MinimumPopulationSize";
290      comparator4.Comparison = new Comparison(ComparisonType.Less);
291      comparator4.LeftSideParameter.ActualName = "CurrentPopulationSize";
292      comparator4.RightSideParameter.ActualName = MinimumPopulationSizeParameter.Name;
293      comparator4.ResultParameter.ActualName = "Terminate";
294
295      conditionalBranch4.Name = "Terminate?";
296      conditionalBranch4.ConditionParameter.ActualName = "Terminate";
297
298      assigner2.Name = "Reset NumberOfCreatedOffspring";
299      assigner2.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";
300      assigner2.RightSideParameter.Value = new IntValue(0);
301
302      assigner3.Name = "Reset NumberOfSuccessfulOffspring";
303      assigner3.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
304      assigner3.RightSideParameter.Value = new IntValue(0);
305
306      assigner4.Name = "Reset OffspringList";
307      assigner4.LeftSideParameter.ActualName = "OffspringList";
308      assigner4.RightSideParameter.Value = new ScopeList();
309      #endregion
310
311      #region Create operator graph
312      OperatorGraph.InitialOperator = variableCreator;
313      variableCreator.Successor = assigner1;
314      assigner1.Successor = resultsCollector;
315      resultsCollector.Successor = analyzer1;
316      analyzer1.Successor = selector;
317      selector.Successor = subScopesProcessor1;
318      subScopesProcessor1.Operators.Add(new EmptyOperator());
319      subScopesProcessor1.Operators.Add(childrenCreator);
320      subScopesProcessor1.Successor = comparator1;
321      childrenCreator.Successor = uniformSubScopesProcessor;
322      uniformSubScopesProcessor.Operator = crossover;
323      uniformSubScopesProcessor.Successor = subScopesCounter1;
324      crossover.Successor = stochasticBranch;
325      stochasticBranch.FirstBranch = mutator;
326      stochasticBranch.SecondBranch = null;
327      mutator.Successor = null;
328      stochasticBranch.Successor = evaluator;
329      evaluator.Successor = weightedParentsQualityComparator;
330      weightedParentsQualityComparator.Successor = subScopesRemover;
331      subScopesCounter1.Successor = intCounter1;
332      intCounter1.Successor = conditionalSelector;
333      conditionalSelector.Successor = rightReducer1;
334      rightReducer1.Successor = duplicateSelector;
335      duplicateSelector.Successor = leftReducer1;
336      leftReducer1.Successor = progressiveOffspringSelector;
337      progressiveOffspringSelector.Successor = subScopesCounter2;
338      comparator1.Successor = conditionalBranch1;
339      conditionalBranch1.FalseBranch = comparator2;
340      conditionalBranch1.TrueBranch = subScopesProcessor2;
341      comparator2.Successor = conditionalBranch2;
342      conditionalBranch2.FalseBranch = leftReducer2;
343      conditionalBranch2.TrueBranch = subScopesProcessor2;
344      leftReducer2.Successor = selector;
345      subScopesProcessor2.Operators.Add(bestSelector);
346      subScopesProcessor2.Operators.Add(scopeCleaner);
347      subScopesProcessor2.Successor = mergingReducer;
348      bestSelector.Successor = rightReducer2;
349      rightReducer2.Successor = null;
350      scopeCleaner.Successor = offspringRestorer;
351      mergingReducer.Successor = intCounter2;
352      intCounter2.Successor = subScopesCounter3;
353      subScopesCounter3.Successor = analyzer2;
354      analyzer2.Successor = comparator3;
355      comparator3.Successor = conditionalBranch3;
356      conditionalBranch3.FalseBranch = comparator4;
357      conditionalBranch3.TrueBranch = null;
358      conditionalBranch3.Successor = null;
359      comparator4.Successor = conditionalBranch4;
360      conditionalBranch4.FalseBranch = assigner2;
361      conditionalBranch4.TrueBranch = null;
362      conditionalBranch4.Successor = null;
363      assigner2.Successor = assigner3;
364      assigner3.Successor = assigner4;
365      assigner4.Successor = selector;
366
367      #endregion
368    }
369
370    public override IOperation Apply() {
371      if (CrossoverParameter.ActualName == null)
372        return null;
373      return base.Apply();
374    }
375  }
376}
Note: See TracBrowser for help on using the repository browser.