Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1247: added duplication control

File size: 18.2 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 resultsCollector1 = new ResultsCollector();
140      Placeholder analyzer1 = new Placeholder();
141      Placeholder selector = new Placeholder();
142      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
143      ChildrenCreator childrenCreator = new ChildrenCreator();
144      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
145      Placeholder crossover = new Placeholder();
146      StochasticBranch stochasticBranch = new StochasticBranch();
147      Placeholder mutator = new Placeholder();
148      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
149      Placeholder evaluator = new Placeholder();
150      WeightedParentsQualityComparator comparator1 = new WeightedParentsQualityComparator();
151      ConditionalSelector conditionalSelector = new ConditionalSelector();
152      RightReducer rightReducer1 = new RightReducer();
153      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
154      SubScopesRemover subScopesRemover = new SubScopesRemover();
155      DuplicateSelector duplicateSelector = new DuplicateSelector();
156      LeftReducer leftReducer = new LeftReducer();
157      SubScopesCounter subScopesCounter1 = new SubScopesCounter();
158      IntCounter intCounter1 = new IntCounter();
159      Comparator comparator2 = new Comparator();
160      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
161      BestSelector bestSelector1 = new BestSelector();
162      BestSelector bestSelector2 = new BestSelector();
163      RightReducer rightReducer2 = new RightReducer();
164      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
165      BestSelector bestSelector3 = new BestSelector();
166      RightReducer rightReducer3 = new RightReducer();
167      MergingReducer mergingReducer = new MergingReducer();
168      IntCounter intCounter2 = new IntCounter();
169      Comparator comparator3 = new Comparator();
170      Placeholder analyzer2 = new Placeholder();
171      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
172      Assigner assigner2 = new Assigner();
173      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
174      ResultsCollector resultsCollector2 = new ResultsCollector();
175      Comparator comparator4 = new Comparator();
176      ConditionalBranch conditionalBranch3 = new ConditionalBranch();
177
178      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
179      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentPopulationSize", new IntValue(0)));
180
181      assigner1.Name = "Initialize CurrentPopulationSize";
182      assigner1.LeftSideParameter.ActualName = "CurrentPopulationSize";
183      assigner1.RightSideParameter.ActualName = EvaluatedSolutionsParameter.ActualName;
184
185      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
186      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
187      resultsCollector1.ResultsParameter.ActualName = "Results";
188
189      analyzer1.Name = "Analyzer";
190      analyzer1.OperatorParameter.ActualName = "Analyzer";
191
192      selector.Name = "Selector";
193      selector.OperatorParameter.ActualName = "Selector";
194
195      childrenCreator.ParentsPerChild = new IntValue(2);
196
197      crossover.Name = "Crossover";
198      crossover.OperatorParameter.ActualName = "Crossover";
199
200      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
201      stochasticBranch.RandomParameter.ActualName = "Random";
202
203      mutator.Name = "Mutator";
204      mutator.OperatorParameter.ActualName = "Mutator";
205
206      uniformSubScopesProcessor2.Parallel.Value = true;
207
208      evaluator.Name = "Evaluator";
209      evaluator.OperatorParameter.ActualName = "Evaluator";
210
211      comparator1.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
212      comparator1.LeftSideParameter.ActualName = QualityParameter.Name;
213      comparator1.MaximizationParameter.ActualName = MaximizationParameter.Name;
214      comparator1.RightSideParameter.ActualName = QualityParameter.Name;
215      comparator1.ResultParameter.ActualName = "SuccessfulOffspring";
216
217      conditionalSelector.ConditionParameter.ActualName = "SuccessfulOffspring";
218      conditionalSelector.ConditionParameter.Depth = 1;
219      conditionalSelector.CopySelected.Value = false;
220
221      duplicateSelector.CopySelected.Value = false;
222
223      uniformSubScopesProcessor3.Parallel.Value = true;
224
225      subScopesRemover.RemoveAllSubScopes = true;
226
227      subScopesCounter1.Name = "Count Successful Offspring";
228      subScopesCounter1.ValueParameter.ActualName = "NumberOfSuccessfulOffspring";
229
230      intCounter1.IncrementParameter.ActualName = "NumberOfSuccessfulOffspring";
231      intCounter1.IncrementParameter.Value = null;
232      intCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
233
234      comparator2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
235      comparator2.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring";
236      comparator2.RightSideParameter.ActualName = MaximumPopulationSizeParameter.Name;
237      comparator2.ResultParameter.ActualName = "SelectMaximum";
238
239      conditionalBranch1.ConditionParameter.ActualName = "SelectMaximum";
240
241      bestSelector1.CopySelected = new BoolValue(true);
242      bestSelector1.MaximizationParameter.ActualName = MaximizationParameter.Name;
243      bestSelector1.NumberOfSelectedSubScopesParameter.ActualName = MaximumPopulationSizeParameter.Name;
244      bestSelector1.QualityParameter.ActualName = QualityParameter.Name;
245
246      bestSelector2.CopySelected = new BoolValue(true);
247      bestSelector2.MaximizationParameter.ActualName = MaximizationParameter.Name;
248      bestSelector2.NumberOfSelectedSubScopesParameter.ActualName = "NumberOfSuccessfulOffspring";
249      bestSelector2.QualityParameter.ActualName = QualityParameter.Name;
250
251      bestSelector3.CopySelected = new BoolValue(false);
252      bestSelector3.MaximizationParameter.ActualName = MaximizationParameter.Name;
253      bestSelector3.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
254      bestSelector3.QualityParameter.ActualName = QualityParameter.Name;
255
256      intCounter2.Increment = new IntValue(1);
257      intCounter2.ValueParameter.ActualName = "Generations";
258
259      comparator3.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
260      comparator3.LeftSideParameter.ActualName = "Generations";
261      comparator3.ResultParameter.ActualName = "Terminate";
262      comparator3.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
263
264      analyzer2.Name = "Analyzer";
265      analyzer2.OperatorParameter.ActualName = "Analyzer";
266
267      conditionalBranch2.ConditionParameter.ActualName = "Terminate";
268
269      assigner2.Name = "Reset CurrentPopulationSize";
270      assigner2.LeftSideParameter.ActualName = "CurrentPopulationSize";
271      assigner2.RightSideParameter.Value = new IntValue(0);
272
273      subScopesCounter2.Name = "Set CurrentPopulationSize";
274      subScopesCounter2.ValueParameter.ActualName = "CurrentPopulationSize";
275
276      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
277      resultsCollector2.ResultsParameter.ActualName = "Results";
278
279      comparator4.Comparison = new Comparison(ComparisonType.Less);
280      comparator4.LeftSideParameter.ActualName = "CurrentPopulationSize";
281      comparator4.RightSideParameter.ActualName = MinimumPopulationSizeParameter.Name;
282      comparator4.ResultParameter.ActualName = "Terminate";
283
284      conditionalBranch3.ConditionParameter.ActualName = "Terminate";
285      #endregion
286
287      #region Create operator graph
288      OperatorGraph.InitialOperator = variableCreator;
289      variableCreator.Successor = assigner1;
290      assigner1.Successor = resultsCollector1;
291      resultsCollector1.Successor = analyzer1;
292      analyzer1.Successor = selector;
293      selector.Successor = subScopesProcessor1;
294      subScopesProcessor1.Operators.Add(new EmptyOperator());
295      subScopesProcessor1.Operators.Add(childrenCreator);
296      subScopesProcessor1.Successor = subScopesProcessor2;
297      childrenCreator.Successor = uniformSubScopesProcessor1;
298      uniformSubScopesProcessor1.Operator = crossover;
299      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
300      crossover.Successor = stochasticBranch;
301      stochasticBranch.FirstBranch = mutator;
302      stochasticBranch.SecondBranch = null;
303      stochasticBranch.Successor = null;
304      mutator.Successor = null;
305      subScopesRemover.Successor = null;
306      uniformSubScopesProcessor2.Operator = evaluator;
307      uniformSubScopesProcessor2.Successor = conditionalSelector;
308      evaluator.Successor = comparator1;
309      conditionalSelector.Successor = rightReducer1;
310      rightReducer1.Successor = uniformSubScopesProcessor3;
311      uniformSubScopesProcessor3.Operator = subScopesRemover;
312      uniformSubScopesProcessor3.Successor = duplicateSelector;
313      duplicateSelector.Successor = leftReducer;
314      leftReducer.Successor = subScopesCounter1;
315      subScopesCounter1.Successor = intCounter1;
316      intCounter1.Successor = comparator2;
317      comparator2.Successor = conditionalBranch1;
318      conditionalBranch1.TrueBranch = bestSelector1;
319      conditionalBranch1.FalseBranch = bestSelector2;
320      bestSelector1.Successor = rightReducer2;
321      bestSelector2.Successor = rightReducer2;
322      subScopesProcessor2.Operators.Add(bestSelector3);
323      subScopesProcessor2.Operators.Add(new EmptyOperator());
324      subScopesProcessor2.Successor = mergingReducer;
325      bestSelector3.Successor = rightReducer3;
326      rightReducer3.Successor = null;
327      mergingReducer.Successor = intCounter2;
328      intCounter2.Successor = comparator3;
329      comparator3.Successor = analyzer2;
330      analyzer2.Successor = conditionalBranch2;
331      conditionalBranch2.FalseBranch = assigner2;
332      conditionalBranch2.TrueBranch = null;
333      conditionalBranch2.Successor = null;
334      assigner2.Successor = subScopesCounter2;
335      subScopesCounter2.Successor = resultsCollector2;
336      resultsCollector2.Successor = comparator4;
337      comparator4.Successor = conditionalBranch3;
338      conditionalBranch3.FalseBranch = selector;
339      conditionalBranch3.TrueBranch = null;
340      conditionalBranch3.Successor = null;
341      #endregion
342    }
343
344    public override IOperation Apply() {
345      if (CrossoverParameter.ActualName == null)
346        return null;
347      return base.Apply();
348    }
349  }
350}
Note: See TracBrowser for help on using the repository browser.