Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1247: added parameters

File size: 20.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
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.", new IntValue(10)));
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      SubScopesCounter subScopesCounter1 = new SubScopesCounter();
161      IntCounter intCounter1 = 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      Comparator comparator1 = new Comparator();
169      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
170      Comparator comparator2 = 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      OffspringRestorer offspringRestorer = new OffspringRestorer();
178      MergingReducer mergingReducer = new MergingReducer();
179      IntCounter intCounter2 = new IntCounter();
180      SubScopesCounter subScopesCounter3 = new SubScopesCounter();
181      Comparator comparator3 = new Comparator();
182      Placeholder analyzer2 = new Placeholder();
183      ConditionalBranch conditionalBranch3 = new ConditionalBranch();
184      Comparator comparator4 = new Comparator();
185      ConditionalBranch conditionalBranch4 = new ConditionalBranch();
186      Assigner assigner2 = new Assigner();
187      Assigner assigner3 = new Assigner();
188      Assigner assigner4 = new Assigner();
189
190      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class RAPGA expects this to be called Generations
191      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentPopulationSize", new IntValue(0)));
192      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfCreatedOffspring", new IntValue(0)));
193      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfSuccessfulOffspring", new IntValue(0)));
194      variableCreator.CollectedValues.Add(new ValueParameter<ScopeList>("OffspringList", new ScopeList()));
195
196      assigner1.Name = "Initialize CurrentPopulationSize";
197      assigner1.LeftSideParameter.ActualName = "CurrentPopulationSize";
198      assigner1.RightSideParameter.ActualName = PopulationSizeParameter.Name;
199
200      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
201      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
202      resultsCollector.ResultsParameter.ActualName = "Results";
203
204      analyzer1.Name = "Analyzer";
205      analyzer1.OperatorParameter.ActualName = "Analyzer";
206
207      selector.Name = "Selector";
208      selector.OperatorParameter.ActualName = "Selector";
209
210      childrenCreator.ParentsPerChild = new IntValue(2);
211
212      uniformSubScopesProcessor.Parallel.Value = true;
213
214      crossover.Name = "Crossover";
215      crossover.OperatorParameter.ActualName = "Crossover";
216
217      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
218      stochasticBranch.RandomParameter.ActualName = "Random";
219
220      mutator.Name = "Mutator";
221      mutator.OperatorParameter.ActualName = "Mutator";
222
223      evaluator.Name = "Evaluator";
224      evaluator.OperatorParameter.ActualName = "Evaluator";
225
226      weightedParentsQualityComparator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
227      weightedParentsQualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
228      weightedParentsQualityComparator.MaximizationParameter.ActualName = MaximizationParameter.Name;
229      weightedParentsQualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
230      weightedParentsQualityComparator.ResultParameter.ActualName = "SuccessfulOffspring";
231
232      subScopesRemover.RemoveAllSubScopes = true;
233
234      subScopesCounter1.Name = "Increment NumberOfCreatedOffspring";
235      subScopesCounter1.ValueParameter.ActualName = "NumberOfCreatedOffspring";
236
237      intCounter1.Name = "Increment EvaluatedSolutions";
238      intCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
239      intCounter1.Increment = null;
240      intCounter1.IncrementParameter.ActualName = "NumberOfCreatedOffspring";
241
242      conditionalSelector.ConditionParameter.ActualName = "SuccessfulOffspring";
243      conditionalSelector.ConditionParameter.Depth = 1;
244      conditionalSelector.CopySelected.Value = false;
245
246      duplicateSelector.CopySelected.Value = false;
247
248      progressiveOffspringSelector.OffspringListParameter.ActualName = "OffspringList";
249      progressiveOffspringSelector.ElitesParameter.ActualName = ElitesParameter.Name;
250      progressiveOffspringSelector.MaximumPopulationSizeParameter.ActualName = MaximumPopulationSizeParameter.Name;
251
252      subScopesCounter2.Name = "Count Successful Offspring";
253      subScopesCounter2.ValueParameter.ActualName = "NumberOfSuccessfulOffspring";
254
255      comparator1.Name = "NumberOfSuccessfulOffspring >= MaximumPopulationSize";
256      comparator1.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
257      comparator1.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
258      comparator1.RightSideParameter.ActualName = MaximumPopulationSizeParameter.Name;
259      comparator1.ResultParameter.ActualName = "Break";
260
261      conditionalBranch1.Name = "Break?";
262      conditionalBranch1.ConditionParameter.ActualName = "Break";
263
264      comparator2.Name = "NumberOfCreatedOffspring >= Effort";
265      comparator2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
266      comparator2.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";
267      comparator2.RightSideParameter.ActualName = EffortParameter.Name;
268      comparator2.ResultParameter.ActualName = "Break";
269
270      conditionalBranch2.Name = "Break?";
271      conditionalBranch2.ConditionParameter.ActualName = "Break";
272
273      bestSelector.CopySelected = new BoolValue(false);
274      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
275      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
276      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
277
278      intCounter2.Name = "Increment Generations";
279      intCounter2.Increment = new IntValue(1);
280      intCounter2.ValueParameter.ActualName = "Generations";
281      subScopesCounter3.Name = "Set CurrentPopulationSize";
282
283      subScopesCounter3.ValueParameter.ActualName = "CurrentPopulationSize";
284      subScopesCounter3.AccumulateParameter.Value = new BoolValue(false);
285
286      comparator3.Name = "Generations >= MaximumGenerations";
287      comparator3.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
288      comparator3.LeftSideParameter.ActualName = "Generations";
289      comparator3.ResultParameter.ActualName = "Terminate";
290      comparator3.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
291
292      analyzer2.Name = "Analyzer";
293      analyzer2.OperatorParameter.ActualName = "Analyzer";
294
295      conditionalBranch3.Name = "Terminate?";
296      conditionalBranch3.ConditionParameter.ActualName = "Terminate";
297
298      comparator4.Name = "CurrentPopulationSize < MinimumPopulationSize";
299      comparator4.Comparison = new Comparison(ComparisonType.Less);
300      comparator4.LeftSideParameter.ActualName = "CurrentPopulationSize";
301      comparator4.RightSideParameter.ActualName = MinimumPopulationSizeParameter.Name;
302      comparator4.ResultParameter.ActualName = "Terminate";
303
304      conditionalBranch4.Name = "Terminate?";
305      conditionalBranch4.ConditionParameter.ActualName = "Terminate";
306
307      assigner2.Name = "Reset NumberOfCreatedOffspring";
308      assigner2.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";
309      assigner2.RightSideParameter.Value = new IntValue(0);
310
311      assigner3.Name = "Reset NumberOfSuccessfulOffspring";
312      assigner3.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
313      assigner3.RightSideParameter.Value = new IntValue(0);
314
315      assigner4.Name = "Reset OffspringList";
316      assigner4.LeftSideParameter.ActualName = "OffspringList";
317      assigner4.RightSideParameter.Value = new ScopeList();
318      #endregion
319
320      #region Create operator graph
321      OperatorGraph.InitialOperator = variableCreator;
322      variableCreator.Successor = assigner1;
323      assigner1.Successor = resultsCollector;
324      resultsCollector.Successor = analyzer1;
325      analyzer1.Successor = selector;
326      selector.Successor = subScopesProcessor1;
327      subScopesProcessor1.Operators.Add(new EmptyOperator());
328      subScopesProcessor1.Operators.Add(childrenCreator);
329      subScopesProcessor1.Successor = comparator1;
330      childrenCreator.Successor = uniformSubScopesProcessor;
331      uniformSubScopesProcessor.Operator = crossover;
332      uniformSubScopesProcessor.Successor = subScopesCounter1;
333      crossover.Successor = stochasticBranch;
334      stochasticBranch.FirstBranch = mutator;
335      stochasticBranch.SecondBranch = null;
336      mutator.Successor = null;
337      stochasticBranch.Successor = evaluator;
338      evaluator.Successor = weightedParentsQualityComparator;
339      weightedParentsQualityComparator.Successor = subScopesRemover;
340      subScopesCounter1.Successor = intCounter1;
341      intCounter1.Successor = conditionalSelector;
342      conditionalSelector.Successor = rightReducer1;
343      rightReducer1.Successor = duplicateSelector;
344      duplicateSelector.Successor = leftReducer1;
345      leftReducer1.Successor = progressiveOffspringSelector;
346      progressiveOffspringSelector.Successor = subScopesCounter2;
347      comparator1.Successor = conditionalBranch1;
348      conditionalBranch1.FalseBranch = comparator2;
349      conditionalBranch1.TrueBranch = subScopesProcessor2;
350      comparator2.Successor = conditionalBranch2;
351      conditionalBranch2.FalseBranch = leftReducer2;
352      conditionalBranch2.TrueBranch = subScopesProcessor2;
353      leftReducer2.Successor = selector;
354      subScopesProcessor2.Operators.Add(bestSelector);
355      subScopesProcessor2.Operators.Add(scopeCleaner);
356      subScopesProcessor2.Successor = mergingReducer;
357      bestSelector.Successor = rightReducer2;
358      rightReducer2.Successor = null;
359      scopeCleaner.Successor = offspringRestorer;
360      mergingReducer.Successor = intCounter2;
361      intCounter2.Successor = subScopesCounter3;
362      subScopesCounter3.Successor = analyzer2;
363      analyzer2.Successor = comparator3;
364      comparator3.Successor = conditionalBranch3;
365      conditionalBranch3.FalseBranch = comparator4;
366      conditionalBranch3.TrueBranch = null;
367      conditionalBranch3.Successor = null;
368      comparator4.Successor = conditionalBranch4;
369      conditionalBranch4.FalseBranch = assigner2;
370      conditionalBranch4.TrueBranch = null;
371      conditionalBranch4.Successor = null;
372      assigner2.Successor = assigner3;
373      assigner3.Successor = assigner4;
374      assigner4.Successor = selector;
375
376      #endregion
377    }
378
379    public override IOperation Apply() {
380      if (CrossoverParameter.ActualName == null)
381        return null;
382      return base.Apply();
383    }
384  }
385}
Note: See TracBrowser for help on using the repository browser.