Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithmMainOperator.cs @ 9591

Last change on this file since 9591 was 9591, checked in by mkommend, 11 years ago

#2038: Added backwards compatibility regions into the modified algorithms for elites reevaluation.

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