Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithmMainOperator.cs @ 16892

Last change on this file since 16892 was 16892, checked in by gkronber, 5 years ago

#2925 merged r16661:16890 from trunk to branch

File size: 20.6 KB
RevLine 
[3611]1#region License Information
2/* HeuristicLab
[16662]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3611]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
[16892]22using HEAL.Attic;
[4722]23using HeuristicLab.Common;
[3611]24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
32  /// <summary>
33  /// An operator which represents the main loop of an offspring selection genetic algorithm.
34  /// </summary>
35  [Item("OffspringSelectionGeneticAlgorithmMainOperator", "An operator that represents the core of an offspring selection genetic algorithm.")]
[16662]36  [StorableType("43910E64-FC79-4AFF-8049-F427442E32BF")]
[3611]37  public sealed class OffspringSelectionGeneticAlgorithmMainOperator : 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    }
[3659]45    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
46      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3611]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 LookupParameter<IntValue> EvaluatedSolutionsParameter {
64      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
65    }
66    public ValueLookupParameter<IntValue> ElitesParameter {
67      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
68    }
[9569]69    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
70      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
71    }
[3611]72    public LookupParameter<DoubleValue> ComparisonFactorParameter {
73      get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
74    }
75    public LookupParameter<DoubleValue> CurrentSuccessRatioParameter {
76      get { return (LookupParameter<DoubleValue>)Parameters["CurrentSuccessRatio"]; }
77    }
78    public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
79      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
80    }
81    public LookupParameter<DoubleValue> SelectionPressureParameter {
82      get { return (LookupParameter<DoubleValue>)Parameters["SelectionPressure"]; }
83    }
84    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
85      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
86    }
87    public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
88      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
89    }
[10643]90    public IValueLookupParameter<BoolValue> FillPopulationWithParentsParameter {
91      get { return (IValueLookupParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
92    }
[3611]93    #endregion
94
95    [StorableConstructor]
[16662]96    private OffspringSelectionGeneticAlgorithmMainOperator(StorableConstructorFlag _) : base(_) { }
[4722]97    private OffspringSelectionGeneticAlgorithmMainOperator(OffspringSelectionGeneticAlgorithmMainOperator original, Cloner cloner)
98      : base(original, cloner) {
99    }
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new OffspringSelectionGeneticAlgorithmMainOperator(this, cloner);
102    }
[3611]103    public OffspringSelectionGeneticAlgorithmMainOperator()
104      : base() {
105      Initialize();
106    }
107
[9569]108    [StorableHook(HookType.AfterDeserialization)]
109    private void AfterDeserialization() {
[9592]110      // BackwardsCompatibility3.3
[9591]111      #region Backwards compatible code, remove with 3.4
[9569]112      if (!Parameters.ContainsKey("ReevaluateElites")) {
113        Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
114      }
[10643]115      if (!Parameters.ContainsKey("FillPopulationWithParents"))
116        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."));
[9591]117      #endregion
[9569]118    }
119
[3611]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."));
[3659]124      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[3611]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."));
[5208]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."));
[3611]130      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
131      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
[9569]132      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
[3611]133      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]."));
134      Parameters.Add(new LookupParameter<DoubleValue>("CurrentSuccessRatio", "The current success ratio."));
135      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
136      Parameters.Add(new LookupParameter<DoubleValue>("SelectionPressure", "The actual selection pressure."));
137      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
138      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."));
[10643]139      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."));
[3611]140      #endregion
141
142      #region Create operators
143      Placeholder selector = new Placeholder();
144      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
145      ChildrenCreator childrenCreator = new ChildrenCreator();
146      ConditionalBranch osBeforeMutationBranch = new ConditionalBranch();
[5208]147      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
148      Placeholder crossover1 = new Placeholder();
149      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
[3611]150      Placeholder evaluator1 = new Placeholder();
[5356]151      SubScopesCounter subScopesCounter1 = new SubScopesCounter();
[3611]152      WeightedParentsQualityComparator qualityComparer1 = new WeightedParentsQualityComparator();
[5208]153      SubScopesRemover subScopesRemover1 = new SubScopesRemover();
154      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
[3611]155      StochasticBranch mutationBranch1 = new StochasticBranch();
156      Placeholder mutator1 = new Placeholder();
[5208]157      VariableCreator variableCreator1 = new VariableCreator();
158      VariableCreator variableCreator2 = new VariableCreator();
159      ConditionalSelector conditionalSelector = new ConditionalSelector();
160      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
161      UniformSubScopesProcessor uniformSubScopesProcessor4 = new UniformSubScopesProcessor();
[3611]162      Placeholder evaluator2 = new Placeholder();
[5356]163      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
[5208]164      MergingReducer mergingReducer1 = new MergingReducer();
165      UniformSubScopesProcessor uniformSubScopesProcessor5 = new UniformSubScopesProcessor();
166      Placeholder crossover2 = new Placeholder();
[3611]167      StochasticBranch mutationBranch2 = new StochasticBranch();
168      Placeholder mutator2 = new Placeholder();
[5208]169      UniformSubScopesProcessor uniformSubScopesProcessor6 = new UniformSubScopesProcessor();
[3611]170      Placeholder evaluator3 = new Placeholder();
[5356]171      SubScopesCounter subScopesCounter3 = new SubScopesCounter();
[3611]172      WeightedParentsQualityComparator qualityComparer2 = new WeightedParentsQualityComparator();
[5208]173      SubScopesRemover subScopesRemover2 = new SubScopesRemover();
[3611]174      OffspringSelector offspringSelector = new OffspringSelector();
[5208]175      SubScopesProcessor subScopesProcessor3 = new SubScopesProcessor();
[3713]176      BestSelector bestSelector = new BestSelector();
177      WorstSelector worstSelector = new WorstSelector();
[3611]178      RightReducer rightReducer = new RightReducer();
[3699]179      LeftReducer leftReducer = new LeftReducer();
[5208]180      MergingReducer mergingReducer2 = new MergingReducer();
[9569]181      ConditionalBranch reevaluateElitesBranch = new ConditionalBranch();
182      UniformSubScopesProcessor uniformSubScopesProcessor7 = new UniformSubScopesProcessor();
183      Placeholder evaluator4 = new Placeholder();
184      SubScopesCounter subScopesCounter4 = new SubScopesCounter();
[4068]185
[3611]186      selector.Name = "Selector (placeholder)";
187      selector.OperatorParameter.ActualName = SelectorParameter.Name;
188
189      childrenCreator.ParentsPerChild = new IntValue(2);
190
191      osBeforeMutationBranch.Name = "Apply OS before mutation?";
192      osBeforeMutationBranch.ConditionParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
193
[5208]194      crossover1.Name = "Crossover (placeholder)";
195      crossover1.OperatorParameter.ActualName = CrossoverParameter.Name;
196
197      uniformSubScopesProcessor2.Parallel.Value = true;
198
[3611]199      evaluator1.Name = "Evaluator (placeholder)";
200      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
201
[5356]202      subScopesCounter1.Name = "Increment EvaluatedSolutions";
203      subScopesCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
[3611]204
205      qualityComparer1.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
206      qualityComparer1.LeftSideParameter.ActualName = QualityParameter.Name;
207      qualityComparer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
208      qualityComparer1.RightSideParameter.ActualName = QualityParameter.Name;
209      qualityComparer1.ResultParameter.ActualName = "SuccessfulOffspring";
210
[5208]211      subScopesRemover1.RemoveAllSubScopes = true;
212
[3611]213      mutationBranch1.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
214      mutationBranch1.RandomParameter.ActualName = RandomParameter.Name;
215
216      mutator1.Name = "Mutator (placeholder)";
217      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
218
[5208]219      variableCreator1.Name = "MutatedOffspring = true";
[16892]220      variableCreator1.CollectedValues.Add(new ValueParameter<BoolValue>("MutatedOffspring", null, new BoolValue(true)) { GetsCollected = false });
[5208]221
222      variableCreator2.Name = "MutatedOffspring = false";
[16892]223      variableCreator2.CollectedValues.Add(new ValueParameter<BoolValue>("MutatedOffspring", null, new BoolValue(false)) { GetsCollected = false });
[5208]224
225      conditionalSelector.ConditionParameter.ActualName = "MutatedOffspring";
226      conditionalSelector.ConditionParameter.Depth = 1;
227      conditionalSelector.CopySelected.Value = false;
228
229      uniformSubScopesProcessor4.Parallel.Value = true;
230
[3611]231      evaluator2.Name = "Evaluator (placeholder)";
232      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
233
[5356]234      subScopesCounter2.Name = "Increment EvaluatedSolutions";
235      subScopesCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
[3611]236
[5208]237      crossover2.Name = "Crossover (placeholder)";
238      crossover2.OperatorParameter.ActualName = CrossoverParameter.Name;
239
[3611]240      mutationBranch2.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
241      mutationBranch2.RandomParameter.ActualName = RandomParameter.Name;
242
243      mutator2.Name = "Mutator (placeholder)";
244      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
245
[5208]246      uniformSubScopesProcessor6.Parallel.Value = true;
247
[3611]248      evaluator3.Name = "Evaluator (placeholder)";
249      evaluator3.OperatorParameter.ActualName = EvaluatorParameter.Name;
250
[5356]251      subScopesCounter3.Name = "Increment EvaluatedSolutions";
252      subScopesCounter3.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
[3611]253
254      qualityComparer2.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
255      qualityComparer2.LeftSideParameter.ActualName = QualityParameter.Name;
256      qualityComparer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
257      qualityComparer2.RightSideParameter.ActualName = QualityParameter.Name;
258      qualityComparer2.ResultParameter.ActualName = "SuccessfulOffspring";
259
[5208]260      subScopesRemover2.RemoveAllSubScopes = true;
[3611]261
262      offspringSelector.CurrentSuccessRatioParameter.ActualName = CurrentSuccessRatioParameter.Name;
263      offspringSelector.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
264      offspringSelector.SelectionPressureParameter.ActualName = SelectionPressureParameter.Name;
265      offspringSelector.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
[3740]266      offspringSelector.OffspringPopulationParameter.ActualName = "OffspringPopulation";
267      offspringSelector.OffspringPopulationWinnersParameter.ActualName = "OffspringPopulationWinners";
268      offspringSelector.SuccessfulOffspringParameter.ActualName = "SuccessfulOffspring";
[10643]269      offspringSelector.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
[3611]270
[3713]271      bestSelector.CopySelected = new BoolValue(false);
272      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
273      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
274      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
[3699]275
[3713]276      worstSelector.CopySelected = new BoolValue(false);
277      worstSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
278      worstSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
279      worstSelector.QualityParameter.ActualName = QualityParameter.Name;
[9569]280
281      reevaluateElitesBranch.ConditionParameter.ActualName = "ReevaluateElites";
282      reevaluateElitesBranch.Name = "Reevaluate elites ?";
283
284      uniformSubScopesProcessor7.Parallel.Value = true;
285
286      evaluator4.Name = "Evaluator (placeholder)";
287      evaluator4.OperatorParameter.ActualName = EvaluatorParameter.Name;
288
289      subScopesCounter4.Name = "Increment EvaluatedSolutions";
290      subScopesCounter4.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
[3611]291      #endregion
292
293      #region Create operator graph
294      OperatorGraph.InitialOperator = selector;
295      selector.Successor = subScopesProcessor1;
296      subScopesProcessor1.Operators.Add(new EmptyOperator());
297      subScopesProcessor1.Operators.Add(childrenCreator);
298      subScopesProcessor1.Successor = offspringSelector;
[5208]299      childrenCreator.Successor = osBeforeMutationBranch;
300      osBeforeMutationBranch.TrueBranch = uniformSubScopesProcessor1;
301      osBeforeMutationBranch.FalseBranch = uniformSubScopesProcessor5;
302      osBeforeMutationBranch.Successor = null;
303      uniformSubScopesProcessor1.Operator = crossover1;
304      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
305      crossover1.Successor = null;
306      uniformSubScopesProcessor2.Operator = evaluator1;
[5356]307      uniformSubScopesProcessor2.Successor = subScopesCounter1;
308      evaluator1.Successor = qualityComparer1;
[5208]309      qualityComparer1.Successor = subScopesRemover1;
310      subScopesRemover1.Successor = null;
[5356]311      subScopesCounter1.Successor = uniformSubScopesProcessor3;
[5208]312      uniformSubScopesProcessor3.Operator = mutationBranch1;
313      uniformSubScopesProcessor3.Successor = conditionalSelector;
[3611]314      mutationBranch1.FirstBranch = mutator1;
[5208]315      mutationBranch1.SecondBranch = variableCreator2;
[3611]316      mutationBranch1.Successor = null;
[5208]317      mutator1.Successor = variableCreator1;
318      variableCreator1.Successor = null;
319      variableCreator2.Successor = null;
320      conditionalSelector.Successor = subScopesProcessor2;
321      subScopesProcessor2.Operators.Add(new EmptyOperator());
322      subScopesProcessor2.Operators.Add(uniformSubScopesProcessor4);
323      subScopesProcessor2.Successor = mergingReducer1;
324      uniformSubScopesProcessor4.Operator = evaluator2;
[5356]325      uniformSubScopesProcessor4.Successor = subScopesCounter2;
326      evaluator2.Successor = null;
327      subScopesCounter2.Successor = null;
[5208]328      mergingReducer1.Successor = null;
329      uniformSubScopesProcessor5.Operator = crossover2;
330      uniformSubScopesProcessor5.Successor = uniformSubScopesProcessor6;
331      crossover2.Successor = mutationBranch2;
[3611]332      mutationBranch2.FirstBranch = mutator2;
333      mutationBranch2.SecondBranch = null;
[5208]334      mutationBranch2.Successor = null;
[3611]335      mutator2.Successor = null;
[5208]336      uniformSubScopesProcessor6.Operator = evaluator3;
[5356]337      uniformSubScopesProcessor6.Successor = subScopesCounter3;
338      evaluator3.Successor = qualityComparer2;
[5208]339      qualityComparer2.Successor = subScopesRemover2;
340      subScopesRemover2.Successor = null;
[5356]341      subScopesCounter3.Successor = null;
[3611]342      offspringSelector.OffspringCreator = selector;
[5208]343      offspringSelector.Successor = subScopesProcessor3;
344      subScopesProcessor3.Operators.Add(bestSelector);
345      subScopesProcessor3.Operators.Add(worstSelector);
346      subScopesProcessor3.Successor = mergingReducer2;
[3713]347      bestSelector.Successor = rightReducer;
[9569]348      rightReducer.Successor = reevaluateElitesBranch;
349      reevaluateElitesBranch.TrueBranch = uniformSubScopesProcessor7;
350      uniformSubScopesProcessor7.Operator = evaluator4;
351      uniformSubScopesProcessor7.Successor = subScopesCounter4;
352      subScopesCounter4.Successor = null;
353      reevaluateElitesBranch.FalseBranch = null;
354      reevaluateElitesBranch.Successor = null;
[3713]355      worstSelector.Successor = leftReducer;
[3699]356      leftReducer.Successor = null;
[5208]357      mergingReducer2.Successor = null;
[3611]358      #endregion
359    }
[3715]360
361    public override IOperation Apply() {
362      if (CrossoverParameter.ActualValue == null)
363        return null;
364      return base.Apply();
365    }
[3611]366  }
367}
Note: See TracBrowser for help on using the repository browser.