Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Algorithms.EvolutionStrategy/3.3/EvolutionStrategyMainLoop.cs @ 10879

Last change on this file since 10879 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 29.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.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.EvolutionStrategy {
32  /// <summary>
33  /// An operator which represents the main loop of an evolution strategy (EvolutionStrategy).
34  /// </summary>
35  [Item("EvolutionStrategyMainLoop", "An operator which represents the main loop of an evolution strategy (EvolutionStrategy).")]
36  [StorableClass]
37  public sealed class EvolutionStrategyMainLoop : 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<DoubleValue> BestKnownQualityParameter {
49      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
50    }
51    public ValueLookupParameter<IntValue> PopulationSizeParameter {
52      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
53    }
54    public ValueLookupParameter<IntValue> ParentsPerChildParameter {
55      get { return (ValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
56    }
57    public ValueLookupParameter<IntValue> ChildrenParameter {
58      get { return (ValueLookupParameter<IntValue>)Parameters["Children"]; }
59    }
60    public ValueLookupParameter<BoolValue> PlusSelectionParameter {
61      get { return (ValueLookupParameter<BoolValue>)Parameters["PlusSelection"]; }
62    }
63    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
64      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
65    }
66    public ValueLookupParameter<IOperator> MutatorParameter {
67      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
68    }
69    public ValueLookupParameter<IOperator> RecombinatorParameter {
70      get { return (ValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
71    }
72    public ValueLookupParameter<IOperator> EvaluatorParameter {
73      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
74    }
75    public ValueLookupParameter<VariableCollection> ResultsParameter {
76      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
77    }
78    public ValueLookupParameter<IOperator> AnalyzerParameter {
79      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
80    }
81    private ScopeParameter CurrentScopeParameter {
82      get { return (ScopeParameter)Parameters["CurrentScope"]; }
83    }
84    private ValueLookupParameter<IOperator> StrategyParameterManipulatorParameter {
85      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterManipulator"]; }
86    }
87    private ValueLookupParameter<IOperator> StrategyParameterCrossoverParameter {
88      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterCrossover"]; }
89    }
90
91    public IScope CurrentScope {
92      get { return CurrentScopeParameter.ActualValue; }
93    }
94    #endregion
95
96    [StorableConstructor]
97    private EvolutionStrategyMainLoop(bool deserializing) : base(deserializing) { }
98    private EvolutionStrategyMainLoop(EvolutionStrategyMainLoop original, Cloner cloner)
99      : base(original, cloner) {
100    }
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new EvolutionStrategyMainLoop(this, cloner);
103    }
104    public EvolutionStrategyMainLoop()
105      : base() {
106      Initialize();
107    }
108
109    private void Initialize() {
110      #region Create parameters
111      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
112      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
113      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
114      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
115      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population."));
116      Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined."));
117      Parameters.Add(new ValueLookupParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population."));
118      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
119      Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population)."));
120      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
121      Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
122      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
123      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
125      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
126      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterManipulator", "The operator to mutate the endogeneous strategy parameters."));
127      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterCrossover", "The operator to cross the endogeneous strategy parameters."));
128      #endregion
129
130      #region Create operators
131      VariableCreator variableCreator = new VariableCreator();
132      ResultsCollector resultsCollector1 = new ResultsCollector();
133      Placeholder analyzer1 = new Placeholder();
134      WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
135      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
136      Comparator useRecombinationComparator = new Comparator();
137      ConditionalBranch useRecombinationBranch = new ConditionalBranch();
138      ChildrenCreator childrenCreator = new ChildrenCreator();
139      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
140      Placeholder recombinator = new Placeholder();
141      Placeholder strategyRecombinator = new Placeholder();
142      Placeholder strategyMutator1 = new Placeholder();
143      Placeholder mutator1 = new Placeholder();
144      Placeholder evaluator1 = new Placeholder();
145      SubScopesRemover subScopesRemover = new SubScopesRemover();
146      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
147      Placeholder strategyMutator2 = new Placeholder();
148      Placeholder mutator2 = new Placeholder();
149      Placeholder evaluator2 = new Placeholder();
150      ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
151      MergingReducer plusReplacement = new MergingReducer();
152      RightReducer commaReplacement = new RightReducer();
153      BestSelector bestSelector = new BestSelector();
154      RightReducer rightReducer = new RightReducer();
155      IntCounter intCounter = new IntCounter();
156      Comparator comparator = new Comparator();
157      ResultsCollector resultsCollector2 = new ResultsCollector();
158      Placeholder analyzer2 = new Placeholder();
159      ConditionalBranch conditionalBranch = new ConditionalBranch();
160
161      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class EvolutionStrategy expects this to be called Generations
162
163      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
164      resultsCollector1.ResultsParameter.ActualName = "Results";
165
166      analyzer1.Name = "Analyzer (placeholder)";
167      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
168
169      selector.Name = "ES Random Selector";
170      selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
171      selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
172
173      useRecombinationComparator.Name = "ParentsPerChild > 1";
174      useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
175      useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
176      useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
177      useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
178
179      useRecombinationBranch.Name = "Use Recombination?";
180      useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
181
182      childrenCreator.ParentsPerChild = null;
183      childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
184
185      recombinator.Name = "Recombinator (placeholder)";
186      recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
187
188      strategyRecombinator.Name = "Strategy Parameter Recombinator (placeholder)";
189      strategyRecombinator.OperatorParameter.ActualName = StrategyParameterCrossoverParameter.Name;
190
191      strategyMutator1.Name = "Strategy Parameter Manipulator (placeholder)";
192      strategyMutator1.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
193
194      mutator1.Name = "Mutator (placeholder)";
195      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
196
197      evaluator1.Name = "Evaluator (placeholder)";
198      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
199
200      subScopesRemover.RemoveAllSubScopes = true;
201
202      strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
203      strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
204
205      mutator2.Name = "Mutator (placeholder)";
206      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
207
208      evaluator2.Name = "Evaluator (placeholder)";
209      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
210
211      plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
212
213      bestSelector.CopySelected = new BoolValue(false);
214      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
215      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
216      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
217
218      intCounter.Increment = new IntValue(1);
219      intCounter.ValueParameter.ActualName = "Generations";
220
221      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
222      comparator.LeftSideParameter.ActualName = "Generations";
223      comparator.ResultParameter.ActualName = "Terminate";
224      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
225
226      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
227      resultsCollector2.ResultsParameter.ActualName = "Results";
228
229      analyzer2.Name = "Analyzer (placeholder)";
230      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
231
232      conditionalBranch.ConditionParameter.ActualName = "Terminate";
233      #endregion
234
235      #region Create operator graph
236      OperatorGraph.InitialOperator = variableCreator;
237      variableCreator.Successor = resultsCollector1;
238      resultsCollector1.Successor = analyzer1;
239      analyzer1.Successor = selector;
240      selector.Successor = subScopesProcessor1;
241      subScopesProcessor1.Operators.Add(new EmptyOperator());
242      subScopesProcessor1.Operators.Add(useRecombinationComparator);
243      subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
244      useRecombinationComparator.Successor = useRecombinationBranch;
245      useRecombinationBranch.TrueBranch = childrenCreator;
246      useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
247      useRecombinationBranch.Successor = null;
248      childrenCreator.Successor = uniformSubScopesProcessor1;
249      uniformSubScopesProcessor1.Operator = recombinator;
250      uniformSubScopesProcessor1.Successor = null;
251      recombinator.Successor = strategyRecombinator;
252      strategyRecombinator.Successor = strategyMutator1;
253      strategyMutator1.Successor = mutator1;
254      mutator1.Successor = evaluator1;
255      evaluator1.Successor = subScopesRemover;
256      subScopesRemover.Successor = null;
257      uniformSubScopesProcessor2.Operator = strategyMutator2;
258      uniformSubScopesProcessor2.Successor = null;
259      strategyMutator2.Successor = mutator2;
260      mutator2.Successor = evaluator2;
261      plusOrCommaReplacementBranch.TrueBranch = plusReplacement;
262      plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
263      plusOrCommaReplacementBranch.Successor = bestSelector;
264      bestSelector.Successor = rightReducer;
265      rightReducer.Successor = intCounter;
266      intCounter.Successor = comparator;
267      comparator.Successor = resultsCollector2;
268      resultsCollector2.Successor = analyzer2;
269      analyzer2.Successor = conditionalBranch;
270      conditionalBranch.FalseBranch = selector;
271      conditionalBranch.TrueBranch = null;
272      conditionalBranch.Successor = null;
273      #endregion
274    }
275
276    public override IOperation Apply() {
277      if (MutatorParameter.ActualValue == null)
278        return null;
279      return base.Apply();
280    }
281  }
282}
Note: See TracBrowser for help on using the repository browser.