Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2038: Added reevaluation of elites in ES, IslandGA, IslandOSGA, OSGA, SASEGASA, and RAPGA.

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