Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5304 was 5208, checked in by swagner, 14 years ago

Adapted EAs to enable parallel solution evaluation (#1333)

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