Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3816 was 3740, checked in by abeham, 14 years ago

#893

  • Changed the way offspring selection works
  • Added depth parameter to ScopeTreeLookupParameter
File size: 14.6 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.Analysis;
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() { }
91    public OffspringSelectionGeneticAlgorithmMainOperator()
92      : base() {
93      Initialize();
94    }
95
96    private void Initialize() {
97      #region Create parameters
98      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
99      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
100      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
101      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
102      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
103      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
104      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
105      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
106      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
107      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
108      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]."));
109      Parameters.Add(new LookupParameter<DoubleValue>("CurrentSuccessRatio", "The current success ratio."));
110      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
111      Parameters.Add(new LookupParameter<DoubleValue>("SelectionPressure", "The actual selection pressure."));
112      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
113      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."));
114      #endregion
115
116      #region Create operators
117      Placeholder selector = new Placeholder();
118      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
119      ChildrenCreator childrenCreator = new ChildrenCreator();
120      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
121      Placeholder crossover = new Placeholder();
122      ConditionalBranch osBeforeMutationBranch = new ConditionalBranch();
123      Placeholder evaluator1 = new Placeholder();
124      IntCounter evaluationCounter1 = new IntCounter();
125      WeightedParentsQualityComparator qualityComparer1 = new WeightedParentsQualityComparator();
126      StochasticBranch mutationBranch1 = new StochasticBranch();
127      Placeholder mutator1 = new Placeholder();
128      Placeholder evaluator2 = new Placeholder();
129      IntCounter evaluationCounter2 = new IntCounter();
130      StochasticBranch mutationBranch2 = new StochasticBranch();
131      Placeholder mutator2 = new Placeholder();
132      Placeholder evaluator3 = new Placeholder();
133      IntCounter evaluationCounter3 = new IntCounter();
134      WeightedParentsQualityComparator qualityComparer2 = new WeightedParentsQualityComparator();
135      SubScopesRemover subScopesRemover = new SubScopesRemover();
136      OffspringSelector offspringSelector = new OffspringSelector();
137      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
138      BestSelector bestSelector = new BestSelector();
139      WorstSelector worstSelector = new WorstSelector();
140      RightReducer rightReducer = new RightReducer();
141      LeftReducer leftReducer = new LeftReducer();
142      MergingReducer mergingReducer = new MergingReducer();
143     
144      selector.Name = "Selector (placeholder)";
145      selector.OperatorParameter.ActualName = SelectorParameter.Name;
146
147      childrenCreator.ParentsPerChild = new IntValue(2);
148
149      crossover.Name = "Crossover (placeholder)";
150      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
151
152      osBeforeMutationBranch.Name = "Apply OS before mutation?";
153      osBeforeMutationBranch.ConditionParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
154
155      evaluator1.Name = "Evaluator (placeholder)";
156      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
157
158      evaluationCounter1.Name = "EvaluatedSolutions++";
159      evaluationCounter1.Increment = new IntValue(1);
160      evaluationCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
161
162      qualityComparer1.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
163      qualityComparer1.LeftSideParameter.ActualName = QualityParameter.Name;
164      qualityComparer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
165      qualityComparer1.RightSideParameter.ActualName = QualityParameter.Name;
166      qualityComparer1.ResultParameter.ActualName = "SuccessfulOffspring";
167
168      mutationBranch1.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
169      mutationBranch1.RandomParameter.ActualName = RandomParameter.Name;
170
171      mutator1.Name = "Mutator (placeholder)";
172      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
173
174      evaluator2.Name = "Evaluator (placeholder)";
175      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
176
177      evaluationCounter2.Name = "EvaluatedSolutions++";
178      evaluationCounter2.Increment = new IntValue(1);
179      evaluationCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
180
181      mutationBranch2.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
182      mutationBranch2.RandomParameter.ActualName = RandomParameter.Name;
183
184      mutator2.Name = "Mutator (placeholder)";
185      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
186
187      evaluator3.Name = "Evaluator (placeholder)";
188      evaluator3.OperatorParameter.ActualName = EvaluatorParameter.Name;
189
190      evaluationCounter3.Name = "EvaluatedSolutions++";
191      evaluationCounter3.Increment = new IntValue(1);
192      evaluationCounter3.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
193
194      qualityComparer2.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
195      qualityComparer2.LeftSideParameter.ActualName = QualityParameter.Name;
196      qualityComparer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
197      qualityComparer2.RightSideParameter.ActualName = QualityParameter.Name;
198      qualityComparer2.ResultParameter.ActualName = "SuccessfulOffspring";
199
200      subScopesRemover.RemoveAllSubScopes = true;
201
202      offspringSelector.CurrentSuccessRatioParameter.ActualName = CurrentSuccessRatioParameter.Name;
203      offspringSelector.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
204      offspringSelector.SelectionPressureParameter.ActualName = SelectionPressureParameter.Name;
205      offspringSelector.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
206      offspringSelector.OffspringPopulationParameter.ActualName = "OffspringPopulation";
207      offspringSelector.OffspringPopulationWinnersParameter.ActualName = "OffspringPopulationWinners";
208      offspringSelector.SuccessfulOffspringParameter.ActualName = "SuccessfulOffspring";
209
210      bestSelector.CopySelected = new BoolValue(false);
211      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
212      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
213      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
214
215      worstSelector.CopySelected = new BoolValue(false);
216      worstSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
217      worstSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
218      worstSelector.QualityParameter.ActualName = QualityParameter.Name;
219      #endregion
220
221      #region Create operator graph
222      OperatorGraph.InitialOperator = selector;
223      selector.Successor = subScopesProcessor1;
224      subScopesProcessor1.Operators.Add(new EmptyOperator());
225      subScopesProcessor1.Operators.Add(childrenCreator);
226      subScopesProcessor1.Successor = offspringSelector;
227      childrenCreator.Successor = uniformSubScopesProcessor;
228      uniformSubScopesProcessor.Operator = crossover;
229      uniformSubScopesProcessor.Successor = null;
230      crossover.Successor = osBeforeMutationBranch;
231      osBeforeMutationBranch.TrueBranch = evaluator1;
232      osBeforeMutationBranch.FalseBranch = mutationBranch2;
233      osBeforeMutationBranch.Successor = subScopesRemover;
234      evaluator1.Successor = evaluationCounter1;
235      evaluationCounter1.Successor = qualityComparer1;
236      qualityComparer1.Successor = mutationBranch1;
237      mutationBranch1.FirstBranch = mutator1;
238      mutationBranch1.SecondBranch = null;
239      mutationBranch1.Successor = null;
240      mutator1.Successor = evaluator2;
241      evaluator2.Successor = evaluationCounter2;
242      evaluationCounter2.Successor = null;
243      mutationBranch2.FirstBranch = mutator2;
244      mutationBranch2.SecondBranch = null;
245      mutationBranch2.Successor = evaluator3;
246      mutator2.Successor = null;
247      evaluator3.Successor = evaluationCounter3;
248      evaluationCounter3.Successor = qualityComparer2;
249      subScopesRemover.Successor = null;
250      offspringSelector.OffspringCreator = selector;
251      offspringSelector.Successor = subScopesProcessor2;
252      subScopesProcessor2.Operators.Add(bestSelector);
253      subScopesProcessor2.Operators.Add(worstSelector);
254      subScopesProcessor2.Successor = mergingReducer;
255      bestSelector.Successor = rightReducer;
256      rightReducer.Successor = null;
257      worstSelector.Successor = leftReducer;
258      leftReducer.Successor = null;
259      mergingReducer.Successor = null;
260      #endregion
261    }
262
263    public override IOperation Apply() {
264      if (CrossoverParameter.ActualValue == null)
265        return null;
266      return base.Apply();
267    }
268  }
269}
Note: See TracBrowser for help on using the repository browser.