source: branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/VOffspringSelectionGeneticAlgorithmMainOperator.cs @ 11510

Last change on this file since 11510 was 11510, checked in by ascheibe, 8 years ago

#2267 made offspring selector configurable

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