Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Enhanced OSALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsOs2MainOperator.cs @ 15432

Last change on this file since 15432 was 15432, checked in by pfleck, 5 years ago

#2849: AlpsOs2 with a ContinuousMatingPoolCreator

File size: 21.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.ALPS {
32  /// <summary>
33  /// An operator which represents the main loop of an offspring selection genetic algorithm.
34  /// </summary>
35  [Item("AlpsOs2MainOperator", "An operator that represents the core of an alps offspring selection genetic algorithm.")]
36  [StorableClass]
37  public sealed class AlpsOs2MainOperator : AlgorithmOperator {
38    #region Parameter properties
39    public IValueLookupParameter<IRandom> RandomParameter {
40      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    public IValueLookupParameter<IOperator> EvaluatorParameter {
43      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
44    }
45    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
46      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
47    }
48    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
49      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
50    }
51    public IValueLookupParameter<BoolValue> MaximizationParameter {
52      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
53    }
54
55    public ILookupParameter<IntValue> PopulationSizeParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
57    }
58
59    public IValueLookupParameter<IOperator> SelectorParameter {
60      get { return (IValueLookupParameter<IOperator>)Parameters["Selector"]; }
61    }
62    public IValueLookupParameter<IOperator> CrossoverParameter {
63      get { return (IValueLookupParameter<IOperator>)Parameters["Crossover"]; }
64    }
65    public IValueLookupParameter<IOperator> MutatorParameter {
66      get { return (IValueLookupParameter<IOperator>)Parameters["Mutator"]; }
67    }
68    public IValueLookupParameter<PercentValue> MutationProbabilityParameter {
69      get { return (IValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
70    }
71    public IValueLookupParameter<IntValue> ElitesParameter {
72      get { return (IValueLookupParameter<IntValue>)Parameters["Elites"]; }
73    }
74    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
75      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
76    }
77
78    public ILookupParameter<DoubleValue> ComparisonFactorParameter {
79      get { return (ILookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
80    }
81    public ILookupParameter<DoubleValue> CurrentSuccessRatioParameter {
82      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentSuccessRatio"]; }
83    }
84    public IValueLookupParameter<DoubleValue> SuccessRatioParameter {
85      get { return (IValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
86    }
87    public ILookupParameter<DoubleValue> SelectionPressureParameter {
88      get { return (ILookupParameter<DoubleValue>)Parameters["SelectionPressure"]; }
89    }
90    public IValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
91      get { return (IValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
92    }
93    public IValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
94      get { return (IValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
95    }
96    public IValueLookupParameter<BoolValue> FillPopulationWithParentsParameter {
97      get { return (IValueLookupParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
98    }
99
100    public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
101      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
102    }
103    public IValueLookupParameter<DoubleValue> AgeInheritanceParameter {
104      get { return (IValueLookupParameter<DoubleValue>)Parameters["AgeInheritance"]; }
105    }
106    public IValueLookupParameter<DoubleValue> AgeIncrementParameter {
107      get { return (IValueLookupParameter<DoubleValue>)Parameters["AgeIncrement"]; }
108    }
109    #endregion
110
111    #region Constructor, Cloning & Persistence
112    public AlpsOs2MainOperator()
113     : base() {
114      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
115
116      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."));
117      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
118      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
119      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
120
121      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions in each layer."));
122      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
123      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
125      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
126      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
127      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
128
129      Parameters.Add(new LookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor is used to determine whether the offspring should be compared to the better parent, the worse parent or a quality value linearly interpolated between them. It is in the range [0;1]."));
130      Parameters.Add(new LookupParameter<DoubleValue>("CurrentSuccessRatio", "The current success ratio."));
131      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
132      Parameters.Add(new LookupParameter<DoubleValue>("SelectionPressure", "The actual selection pressure."));
133      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
134      Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation."));
135      Parameters.Add(new ValueLookupParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded."));
136
137      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The age of individuals."));
138      Parameters.Add(new ValueLookupParameter<DoubleValue>("AgeInheritance", "A weight that determines the age of a child after crossover based on the older (1.0) and younger (0.0) parent."));
139      Parameters.Add(new ValueLookupParameter<DoubleValue>("AgeIncrement", "The value the age the individuals is incremented if they survives a generation."));
140
141
142      var selector = new Placeholder();
143      var subScopesProcessor1 = new SubScopesProcessor();
144      var childrenCreator = new ChildrenCreator();
145      var osBeforeMutationBranch = new ConditionalBranch();
146      var uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
147      var crossover1 = new Placeholder();
148      var uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
149      var evaluator1 = new Placeholder();
150      var subScopesCounter1 = new SubScopesCounter();
151      var qualityComparer1 = new WeightedParentsQualityComparator();
152      var ageCalculator1 = new WeightingReducer() { Name = "Calculate Age" };
153      var subScopesRemover1 = new SubScopesRemover();
154      var uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
155      var mutationBranch1 = new StochasticBranch();
156      var mutator1 = new Placeholder();
157      var variableCreator1 = new VariableCreator();
158      var variableCreator2 = new VariableCreator();
159      var conditionalSelector = new ConditionalSelector();
160      var subScopesProcessor2 = new SubScopesProcessor();
161      var uniformSubScopesProcessor4 = new UniformSubScopesProcessor();
162      var evaluator2 = new Placeholder();
163      var subScopesCounter2 = new SubScopesCounter();
164      var mergingReducer1 = new MergingReducer();
165      var uniformSubScopesProcessor5 = new UniformSubScopesProcessor();
166      var crossover2 = new Placeholder();
167      var mutationBranch2 = new StochasticBranch();
168      var mutator2 = new Placeholder();
169      var uniformSubScopesProcessor6 = new UniformSubScopesProcessor();
170      var evaluator3 = new Placeholder();
171      var subScopesCounter3 = new SubScopesCounter();
172      var qualityComparer2 = new WeightedParentsQualityComparator();
173      var ageCalculator2 = new WeightingReducer() { Name = "Calculate Age" };
174      var subScopesRemover2 = new SubScopesRemover();
175      var offspringSelector = new AlpsOffspringSelector();
176      var subScopesProcessor3 = new SubScopesProcessor();
177      var bestSelector = new BestSelector();
178      var worstSelector = new WorstSelector();
179      var rightReducer = new RightReducer();
180      var leftReducer = new LeftReducer();
181      var mergingReducer2 = new MergingReducer();
182      var reevaluateElitesBranch = new ConditionalBranch();
183      var uniformSubScopesProcessor7 = new UniformSubScopesProcessor();
184      var evaluator4 = new Placeholder();
185      var subScopesCounter4 = new SubScopesCounter();
186      var incrementAgeProcessor = new UniformSubScopesProcessor();
187      var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
188
189
190      OperatorGraph.InitialOperator = selector;
191
192      selector.Name = "Selector (placeholder)";
193      selector.OperatorParameter.ActualName = SelectorParameter.Name;
194      selector.Successor = subScopesProcessor1;
195
196      subScopesProcessor1.Operators.Add(new EmptyOperator());
197      subScopesProcessor1.Operators.Add(childrenCreator);
198      subScopesProcessor1.Successor = offspringSelector;
199
200      childrenCreator.ParentsPerChild = new IntValue(2);
201      childrenCreator.Successor = osBeforeMutationBranch;
202
203      osBeforeMutationBranch.Name = "Apply OS before mutation?";
204      osBeforeMutationBranch.ConditionParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
205      osBeforeMutationBranch.TrueBranch = uniformSubScopesProcessor1;
206      osBeforeMutationBranch.FalseBranch = uniformSubScopesProcessor5;
207      osBeforeMutationBranch.Successor = null;
208
209      uniformSubScopesProcessor1.Operator = crossover1;
210      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
211
212      crossover1.Name = "Crossover (placeholder)";
213      crossover1.OperatorParameter.ActualName = CrossoverParameter.Name;
214      crossover1.Successor = null;
215
216      uniformSubScopesProcessor2.Parallel.Value = true;
217      uniformSubScopesProcessor2.Operator = evaluator1;
218      uniformSubScopesProcessor2.Successor = subScopesCounter1;
219
220      evaluator1.Name = "Evaluator (placeholder)";
221      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
222      evaluator1.Successor = qualityComparer1;
223
224      subScopesCounter1.Name = "Increment EvaluatedSolutions";
225      subScopesCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
226      subScopesCounter1.Successor = uniformSubScopesProcessor3;
227
228      uniformSubScopesProcessor3.Operator = mutationBranch1;
229      uniformSubScopesProcessor3.Successor = conditionalSelector;
230
231      qualityComparer1.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
232      qualityComparer1.LeftSideParameter.ActualName = QualityParameter.Name;
233      qualityComparer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
234      qualityComparer1.RightSideParameter.ActualName = QualityParameter.Name;
235      qualityComparer1.ResultParameter.ActualName = "SuccessfulOffspring";
236      qualityComparer1.Successor = ageCalculator1;
237
238      ageCalculator1.ParameterToReduce.ActualName = AgeParameter.Name;
239      ageCalculator1.TargetParameter.ActualName = AgeParameter.Name;
240      ageCalculator1.WeightParameter.ActualName = AgeInheritanceParameter.Name;
241      ageCalculator1.Successor = subScopesRemover1;
242
243      subScopesRemover1.RemoveAllSubScopes = true;
244      subScopesRemover1.Successor = null;
245
246      mutationBranch1.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
247      mutationBranch1.RandomParameter.ActualName = RandomParameter.Name;
248      mutationBranch1.FirstBranch = mutator1;
249      mutationBranch1.SecondBranch = variableCreator2;
250      mutationBranch1.Successor = null;
251
252      mutator1.Name = "Mutator (placeholder)";
253      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
254      mutator1.Successor = variableCreator1;
255
256      variableCreator1.Name = "MutatedOffspring = true";
257      variableCreator1.CollectedValues.Add(new ValueParameter<BoolValue>("MutatedOffspring", null, new BoolValue(true), false));
258      variableCreator1.Successor = null;
259
260      variableCreator2.Name = "MutatedOffspring = false";
261      variableCreator2.CollectedValues.Add(new ValueParameter<BoolValue>("MutatedOffspring", null, new BoolValue(false), false));
262      variableCreator2.Successor = null;
263
264      conditionalSelector.ConditionParameter.ActualName = "MutatedOffspring";
265      conditionalSelector.ConditionParameter.Depth = 1;
266      conditionalSelector.CopySelected.Value = false;
267      conditionalSelector.Successor = subScopesProcessor2;
268
269      subScopesProcessor2.Operators.Add(new EmptyOperator());
270      subScopesProcessor2.Operators.Add(uniformSubScopesProcessor4);
271      subScopesProcessor2.Successor = mergingReducer1;
272
273      mergingReducer1.Successor = null;
274
275      uniformSubScopesProcessor4.Parallel.Value = true;
276      uniformSubScopesProcessor4.Operator = evaluator2;
277      uniformSubScopesProcessor4.Successor = subScopesCounter2;
278
279      evaluator2.Name = "Evaluator (placeholder)";
280      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
281      evaluator2.Successor = null;
282
283      subScopesCounter2.Name = "Increment EvaluatedSolutions";
284      subScopesCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
285      subScopesCounter2.Successor = null;
286
287      uniformSubScopesProcessor5.Operator = crossover2;
288      uniformSubScopesProcessor5.Successor = uniformSubScopesProcessor6;
289
290      crossover2.Name = "Crossover (placeholder)";
291      crossover2.OperatorParameter.ActualName = CrossoverParameter.Name;
292      crossover2.Successor = mutationBranch2;
293
294      mutationBranch2.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
295      mutationBranch2.RandomParameter.ActualName = RandomParameter.Name;
296      mutationBranch2.FirstBranch = mutator2;
297      mutationBranch2.SecondBranch = null;
298      mutationBranch2.Successor = null;
299
300      mutator2.Name = "Mutator (placeholder)";
301      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
302      mutator2.Successor = null;
303
304      uniformSubScopesProcessor6.Parallel.Value = true;
305      uniformSubScopesProcessor6.Operator = evaluator3;
306      uniformSubScopesProcessor6.Successor = subScopesCounter3;
307
308      evaluator3.Name = "Evaluator (placeholder)";
309      evaluator3.OperatorParameter.ActualName = EvaluatorParameter.Name;
310      evaluator3.Successor = qualityComparer2;
311
312      subScopesCounter3.Name = "Increment EvaluatedSolutions";
313      subScopesCounter3.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
314
315      qualityComparer2.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
316      qualityComparer2.LeftSideParameter.ActualName = QualityParameter.Name;
317      qualityComparer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
318      qualityComparer2.RightSideParameter.ActualName = QualityParameter.Name;
319      qualityComparer2.ResultParameter.ActualName = "SuccessfulOffspring";
320      qualityComparer2.Successor = ageCalculator2;
321
322      ageCalculator2.ParameterToReduce.ActualName = AgeParameter.Name;
323      ageCalculator2.TargetParameter.ActualName = AgeParameter.Name;
324      ageCalculator2.WeightParameter.ActualName = AgeInheritanceParameter.Name;
325      ageCalculator2.Successor = subScopesRemover2;
326
327      subScopesRemover2.RemoveAllSubScopes = true;
328      subScopesRemover2.Successor = null;
329
330      subScopesCounter3.Successor = null;
331
332      offspringSelector.CurrentSuccessRatioParameter.ActualName = CurrentSuccessRatioParameter.Name;
333      offspringSelector.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
334      offspringSelector.SelectionPressureParameter.ActualName = SelectionPressureParameter.Name;
335      offspringSelector.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
336      offspringSelector.OffspringPopulationParameter.ActualName = "OffspringPopulation";
337      offspringSelector.OffspringPopulationWinnersParameter.ActualName = "OffspringPopulationWinners";
338      offspringSelector.SuccessfulOffspringParameter.ActualName = "SuccessfulOffspring";
339      offspringSelector.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
340      offspringSelector.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
341      offspringSelector.OffspringCreator = selector;
342      offspringSelector.Successor = subScopesProcessor3;
343
344      subScopesProcessor3.Operators.Add(bestSelector);
345      subScopesProcessor3.Operators.Add(worstSelector);
346      subScopesProcessor3.Successor = mergingReducer2;
347
348      bestSelector.CopySelected = new BoolValue(false);
349      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
350      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
351      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
352      bestSelector.Successor = rightReducer;
353
354      rightReducer.Successor = reevaluateElitesBranch;
355
356      reevaluateElitesBranch.ConditionParameter.ActualName = "ReevaluateElites";
357      reevaluateElitesBranch.Name = "Reevaluate elites ?";
358      reevaluateElitesBranch.TrueBranch = uniformSubScopesProcessor7;
359      reevaluateElitesBranch.FalseBranch = null;
360      reevaluateElitesBranch.Successor = null;
361
362      uniformSubScopesProcessor7.Parallel.Value = true;
363      uniformSubScopesProcessor7.Operator = evaluator4;
364      uniformSubScopesProcessor7.Successor = subScopesCounter4;
365
366      evaluator4.Name = "Evaluator (placeholder)";
367      evaluator4.OperatorParameter.ActualName = EvaluatorParameter.Name;
368
369      subScopesCounter4.Name = "Increment EvaluatedSolutions";
370      subScopesCounter4.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
371      subScopesCounter4.Successor = null;
372
373      worstSelector.CopySelected = new BoolValue(false);
374      worstSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
375      worstSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
376      worstSelector.QualityParameter.ActualName = QualityParameter.Name;
377      worstSelector.Successor = leftReducer;
378
379      leftReducer.Successor = null;
380
381      mergingReducer2.Successor = incrementAgeProcessor;
382
383      incrementAgeProcessor.Operator = ageIncrementor;
384      incrementAgeProcessor.Successor = null;
385
386      ageIncrementor.ValueParameter.ActualName = AgeParameter.Name;
387      ageIncrementor.IncrementParameter.Value = null;
388      ageIncrementor.IncrementParameter.ActualName = AgeIncrementParameter.Name;
389      ageIncrementor.Successor = null;
390    }
391
392    private AlpsOs2MainOperator(AlpsOs2MainOperator original, Cloner cloner)
393      : base(original, cloner) {
394    }
395    public override IDeepCloneable Clone(Cloner cloner) {
396      return new AlpsOs2MainOperator(this, cloner);
397    }
398
399    [StorableConstructor]
400    private AlpsOs2MainOperator(bool deserializing)
401      : base(deserializing) { }
402    #endregion
403
404    public override IOperation Apply() {
405      if (CrossoverParameter.ActualValue == null)
406        return null;
407      return base.Apply();
408    }
409  }
410}
Note: See TracBrowser for help on using the repository browser.