Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Algorithms.RAPGA/3.3/RAPGAMainLoop.cs @ 16189

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

#1247: integrated branch into trunk

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