Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4636 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 14.5 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.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Optimization.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
31  /// <summary>
32  /// An operator which represents the main loop of an offspring selection genetic algorithm.
33  /// </summary>
34  [Item("OffspringSelectionGeneticAlgorithmMainOperator", "An operator that represents the core of an offspring selection genetic algorithm.")]
35  [StorableClass]
36  public sealed class OffspringSelectionGeneticAlgorithmMainOperator : AlgorithmOperator {
37    #region Parameter properties
38    public ValueLookupParameter<IRandom> RandomParameter {
39      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
40    }
41    public ValueLookupParameter<BoolValue> MaximizationParameter {
42      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
43    }
44    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
45      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47    public ValueLookupParameter<IOperator> SelectorParameter {
48      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
49    }
50    public ValueLookupParameter<IOperator> CrossoverParameter {
51      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
52    }
53    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
54      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
55    }
56    public ValueLookupParameter<IOperator> MutatorParameter {
57      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
58    }
59    public ValueLookupParameter<IOperator> EvaluatorParameter {
60      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
61    }
62    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
63      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
64    }
65    public ValueLookupParameter<IntValue> ElitesParameter {
66      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
67    }
68    public LookupParameter<DoubleValue> ComparisonFactorParameter {
69      get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
70    }
71    public LookupParameter<DoubleValue> CurrentSuccessRatioParameter {
72      get { return (LookupParameter<DoubleValue>)Parameters["CurrentSuccessRatio"]; }
73    }
74    public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
75      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
76    }
77    public LookupParameter<DoubleValue> SelectionPressureParameter {
78      get { return (LookupParameter<DoubleValue>)Parameters["SelectionPressure"]; }
79    }
80    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
81      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
82    }
83    public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
84      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
85    }
86    #endregion
87
88    [StorableConstructor]
89    private OffspringSelectionGeneticAlgorithmMainOperator(bool deserializing) : base() { }
90    public OffspringSelectionGeneticAlgorithmMainOperator()
91      : base() {
92      Initialize();
93    }
94
95    private void Initialize() {
96      #region Create parameters
97      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
98      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
99      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
100      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
101      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
102      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
103      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
104      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
105      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
106      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
107      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]."));
108      Parameters.Add(new LookupParameter<DoubleValue>("CurrentSuccessRatio", "The current success ratio."));
109      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
110      Parameters.Add(new LookupParameter<DoubleValue>("SelectionPressure", "The actual selection pressure."));
111      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
112      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."));
113      #endregion
114
115      #region Create operators
116      Placeholder selector = new Placeholder();
117      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
118      ChildrenCreator childrenCreator = new ChildrenCreator();
119      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
120      Placeholder crossover = new Placeholder();
121      ConditionalBranch osBeforeMutationBranch = new ConditionalBranch();
122      Placeholder evaluator1 = new Placeholder();
123      IntCounter evaluationCounter1 = new IntCounter();
124      WeightedParentsQualityComparator qualityComparer1 = new WeightedParentsQualityComparator();
125      StochasticBranch mutationBranch1 = new StochasticBranch();
126      Placeholder mutator1 = new Placeholder();
127      Placeholder evaluator2 = new Placeholder();
128      IntCounter evaluationCounter2 = new IntCounter();
129      StochasticBranch mutationBranch2 = new StochasticBranch();
130      Placeholder mutator2 = new Placeholder();
131      Placeholder evaluator3 = new Placeholder();
132      IntCounter evaluationCounter3 = new IntCounter();
133      WeightedParentsQualityComparator qualityComparer2 = new WeightedParentsQualityComparator();
134      SubScopesRemover subScopesRemover = new SubScopesRemover();
135      OffspringSelector offspringSelector = new OffspringSelector();
136      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
137      BestSelector bestSelector = new BestSelector();
138      WorstSelector worstSelector = new WorstSelector();
139      RightReducer rightReducer = new RightReducer();
140      LeftReducer leftReducer = new LeftReducer();
141      MergingReducer mergingReducer = new MergingReducer();
142
143      selector.Name = "Selector (placeholder)";
144      selector.OperatorParameter.ActualName = SelectorParameter.Name;
145
146      childrenCreator.ParentsPerChild = new IntValue(2);
147
148      crossover.Name = "Crossover (placeholder)";
149      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
150
151      osBeforeMutationBranch.Name = "Apply OS before mutation?";
152      osBeforeMutationBranch.ConditionParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
153
154      evaluator1.Name = "Evaluator (placeholder)";
155      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
156
157      evaluationCounter1.Name = "EvaluatedSolutions++";
158      evaluationCounter1.Increment = new IntValue(1);
159      evaluationCounter1.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
160
161      qualityComparer1.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
162      qualityComparer1.LeftSideParameter.ActualName = QualityParameter.Name;
163      qualityComparer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
164      qualityComparer1.RightSideParameter.ActualName = QualityParameter.Name;
165      qualityComparer1.ResultParameter.ActualName = "SuccessfulOffspring";
166
167      mutationBranch1.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
168      mutationBranch1.RandomParameter.ActualName = RandomParameter.Name;
169
170      mutator1.Name = "Mutator (placeholder)";
171      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
172
173      evaluator2.Name = "Evaluator (placeholder)";
174      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
175
176      evaluationCounter2.Name = "EvaluatedSolutions++";
177      evaluationCounter2.Increment = new IntValue(1);
178      evaluationCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
179
180      mutationBranch2.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
181      mutationBranch2.RandomParameter.ActualName = RandomParameter.Name;
182
183      mutator2.Name = "Mutator (placeholder)";
184      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
185
186      evaluator3.Name = "Evaluator (placeholder)";
187      evaluator3.OperatorParameter.ActualName = EvaluatorParameter.Name;
188
189      evaluationCounter3.Name = "EvaluatedSolutions++";
190      evaluationCounter3.Increment = new IntValue(1);
191      evaluationCounter3.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
192
193      qualityComparer2.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
194      qualityComparer2.LeftSideParameter.ActualName = QualityParameter.Name;
195      qualityComparer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
196      qualityComparer2.RightSideParameter.ActualName = QualityParameter.Name;
197      qualityComparer2.ResultParameter.ActualName = "SuccessfulOffspring";
198
199      subScopesRemover.RemoveAllSubScopes = true;
200
201      offspringSelector.CurrentSuccessRatioParameter.ActualName = CurrentSuccessRatioParameter.Name;
202      offspringSelector.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
203      offspringSelector.SelectionPressureParameter.ActualName = SelectionPressureParameter.Name;
204      offspringSelector.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
205      offspringSelector.OffspringPopulationParameter.ActualName = "OffspringPopulation";
206      offspringSelector.OffspringPopulationWinnersParameter.ActualName = "OffspringPopulationWinners";
207      offspringSelector.SuccessfulOffspringParameter.ActualName = "SuccessfulOffspring";
208
209      bestSelector.CopySelected = new BoolValue(false);
210      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
211      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
212      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
213
214      worstSelector.CopySelected = new BoolValue(false);
215      worstSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
216      worstSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
217      worstSelector.QualityParameter.ActualName = QualityParameter.Name;
218      #endregion
219
220      #region Create operator graph
221      OperatorGraph.InitialOperator = selector;
222      selector.Successor = subScopesProcessor1;
223      subScopesProcessor1.Operators.Add(new EmptyOperator());
224      subScopesProcessor1.Operators.Add(childrenCreator);
225      subScopesProcessor1.Successor = offspringSelector;
226      childrenCreator.Successor = uniformSubScopesProcessor;
227      uniformSubScopesProcessor.Operator = crossover;
228      uniformSubScopesProcessor.Successor = null;
229      crossover.Successor = osBeforeMutationBranch;
230      osBeforeMutationBranch.TrueBranch = evaluator1;
231      osBeforeMutationBranch.FalseBranch = mutationBranch2;
232      osBeforeMutationBranch.Successor = subScopesRemover;
233      evaluator1.Successor = evaluationCounter1;
234      evaluationCounter1.Successor = qualityComparer1;
235      qualityComparer1.Successor = mutationBranch1;
236      mutationBranch1.FirstBranch = mutator1;
237      mutationBranch1.SecondBranch = null;
238      mutationBranch1.Successor = null;
239      mutator1.Successor = evaluator2;
240      evaluator2.Successor = evaluationCounter2;
241      evaluationCounter2.Successor = null;
242      mutationBranch2.FirstBranch = mutator2;
243      mutationBranch2.SecondBranch = null;
244      mutationBranch2.Successor = evaluator3;
245      mutator2.Successor = null;
246      evaluator3.Successor = evaluationCounter3;
247      evaluationCounter3.Successor = qualityComparer2;
248      subScopesRemover.Successor = null;
249      offspringSelector.OffspringCreator = selector;
250      offspringSelector.Successor = subScopesProcessor2;
251      subScopesProcessor2.Operators.Add(bestSelector);
252      subScopesProcessor2.Operators.Add(worstSelector);
253      subScopesProcessor2.Successor = mergingReducer;
254      bestSelector.Successor = rightReducer;
255      rightReducer.Successor = null;
256      worstSelector.Successor = leftReducer;
257      leftReducer.Successor = null;
258      mergingReducer.Successor = null;
259      #endregion
260    }
261
262    public override IOperation Apply() {
263      if (CrossoverParameter.ActualValue == null)
264        return null;
265      return base.Apply();
266    }
267  }
268}
Note: See TracBrowser for help on using the repository browser.