Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.EvolutionStrategy/3.3/EvolutionStrategyMainLoop.cs @ 9591

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

#2038: Added backwards compatibility regions into the modified algorithms for elites reevaluation.

File size: 34.7 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.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 IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
64      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
65    }
66    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
67      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
68    }
69    public ValueLookupParameter<IOperator> MutatorParameter {
70      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
71    }
72    public ValueLookupParameter<IOperator> RecombinatorParameter {
73      get { return (ValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
74    }
75    public ValueLookupParameter<IOperator> EvaluatorParameter {
76      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
77    }
78    public ValueLookupParameter<VariableCollection> ResultsParameter {
79      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
80    }
81    public ValueLookupParameter<IOperator> AnalyzerParameter {
82      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
83    }
84    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
85      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
86    }
87    private ScopeParameter CurrentScopeParameter {
88      get { return (ScopeParameter)Parameters["CurrentScope"]; }
89    }
90    private ValueLookupParameter<IOperator> StrategyParameterManipulatorParameter {
91      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterManipulator"]; }
92    }
93    private ValueLookupParameter<IOperator> StrategyParameterCrossoverParameter {
94      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterCrossover"]; }
95    }
96
97    public IScope CurrentScope {
98      get { return CurrentScopeParameter.ActualValue; }
99    }
100    #endregion
101
102    [StorableConstructor]
103    private EvolutionStrategyMainLoop(bool deserializing) : base(deserializing) { }
104    private EvolutionStrategyMainLoop(EvolutionStrategyMainLoop original, Cloner cloner)
105      : base(original, cloner) {
106    }
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new EvolutionStrategyMainLoop(this, cloner);
109    }
110    public EvolutionStrategyMainLoop()
111      : base() {
112      Initialize();
113    }
114
115    [StorableHook(HookType.AfterDeserialization)]
116    private void AfterDeserialization() {
117      #region Backwards compatible code, remove with 3.4
118      if (!Parameters.ContainsKey("ReevaluateElites")) {
119        Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
120      }
121      #endregion
122    }
123
124    private void Initialize() {
125      #region Create parameters
126      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
127      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
128      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
129      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
130      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population."));
131      Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined."));
132      Parameters.Add(new ValueLookupParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population."));
133      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
134      Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population)."));
135      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
136      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
137      Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
138      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."));
139      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
140      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
141      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
142      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
143      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterManipulator", "The operator to mutate the endogeneous strategy parameters."));
144      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterCrossover", "The operator to cross the endogeneous strategy parameters."));
145      #endregion
146
147      #region Create operators
148      VariableCreator variableCreator = new VariableCreator();
149      ResultsCollector resultsCollector1 = new ResultsCollector();
150      Placeholder analyzer1 = new Placeholder();
151      WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
152      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
153      Comparator useRecombinationComparator = new Comparator();
154      ConditionalBranch useRecombinationBranch = new ConditionalBranch();
155      ChildrenCreator childrenCreator = new ChildrenCreator();
156      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
157      Placeholder recombinator = new Placeholder();
158      Placeholder strategyRecombinator = new Placeholder();
159      Placeholder strategyMutator1 = new Placeholder();
160      Placeholder mutator1 = new Placeholder();
161      SubScopesRemover subScopesRemover = new SubScopesRemover();
162      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
163      Placeholder strategyMutator2 = new Placeholder();
164      Placeholder mutator2 = new Placeholder();
165      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
166      Placeholder evaluator = new Placeholder();
167      SubScopesCounter subScopesCounter = new SubScopesCounter();
168      ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
169      MergingReducer plusReplacement = new MergingReducer();
170      RightReducer commaReplacement = new RightReducer();
171      BestSelector bestSelector = new BestSelector();
172      RightReducer rightReducer = new RightReducer();
173      IntCounter intCounter = new IntCounter();
174      Comparator comparator = new Comparator();
175      Placeholder analyzer2 = new Placeholder();
176      ConditionalBranch conditionalBranch = new ConditionalBranch();
177      ConditionalBranch reevaluateElitesBranch = new ConditionalBranch();
178      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
179      UniformSubScopesProcessor uniformSubScopesProcessor4 = new UniformSubScopesProcessor();
180      Placeholder evaluator2 = new Placeholder();
181      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
182
183
184      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class EvolutionStrategy expects this to be called Generations
185
186      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
187      resultsCollector1.ResultsParameter.ActualName = "Results";
188
189      analyzer1.Name = "Analyzer (placeholder)";
190      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
191
192      selector.Name = "ES Random Selector";
193      selector.RandomParameter.ActualName = RandomParameter.Name;
194      selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
195      selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
196
197      useRecombinationComparator.Name = "ParentsPerChild > 1";
198      useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
199      useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
200      useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
201      useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
202
203      useRecombinationBranch.Name = "Use Recombination?";
204      useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
205
206      childrenCreator.ParentsPerChild = null;
207      childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
208
209      recombinator.Name = "Recombinator (placeholder)";
210      recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
211
212      strategyRecombinator.Name = "Strategy Parameter Recombinator (placeholder)";
213      strategyRecombinator.OperatorParameter.ActualName = StrategyParameterCrossoverParameter.Name;
214
215      strategyMutator1.Name = "Strategy Parameter Manipulator (placeholder)";
216      strategyMutator1.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
217
218      mutator1.Name = "Mutator (placeholder)";
219      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
220
221      subScopesRemover.RemoveAllSubScopes = true;
222
223      strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
224      strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
225
226      mutator2.Name = "Mutator (placeholder)";
227      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
228
229      uniformSubScopesProcessor3.Parallel.Value = true;
230
231      evaluator.Name = "Evaluator (placeholder)";
232      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
233
234      subScopesCounter.Name = "Increment EvaluatedSolutions";
235      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
236
237      plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
238
239      bestSelector.CopySelected = new BoolValue(false);
240      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
241      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
242      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
243
244      intCounter.Increment = new IntValue(1);
245      intCounter.ValueParameter.ActualName = "Generations";
246
247      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
248      comparator.LeftSideParameter.ActualName = "Generations";
249      comparator.ResultParameter.ActualName = "Terminate";
250      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
251
252      analyzer2.Name = "Analyzer (placeholder)";
253      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
254
255      conditionalBranch.ConditionParameter.ActualName = "Terminate";
256
257      reevaluateElitesBranch.ConditionParameter.ActualName = "ReevaluateElites";
258      reevaluateElitesBranch.Name = "Reevaluate elites ?";
259
260      uniformSubScopesProcessor4.Parallel.Value = true;
261
262      evaluator2.Name = "Evaluator (placeholder)";
263      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
264
265      subScopesCounter2.Name = "Increment EvaluatedSolutions";
266      subScopesCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
267      #endregion
268
269      #region Create operator graph
270      OperatorGraph.InitialOperator = variableCreator;
271      variableCreator.Successor = resultsCollector1;
272      resultsCollector1.Successor = analyzer1;
273      analyzer1.Successor = selector;
274      selector.Successor = subScopesProcessor1;
275      subScopesProcessor1.Operators.Add(new EmptyOperator());
276      subScopesProcessor1.Operators.Add(useRecombinationComparator);
277      subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
278      useRecombinationComparator.Successor = useRecombinationBranch;
279      useRecombinationBranch.TrueBranch = childrenCreator;
280      useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
281      useRecombinationBranch.Successor = uniformSubScopesProcessor3;
282      childrenCreator.Successor = uniformSubScopesProcessor1;
283      uniformSubScopesProcessor1.Operator = recombinator;
284      uniformSubScopesProcessor1.Successor = null;
285      recombinator.Successor = strategyRecombinator;
286      strategyRecombinator.Successor = strategyMutator1;
287      strategyMutator1.Successor = mutator1;
288      mutator1.Successor = subScopesRemover;
289      subScopesRemover.Successor = null;
290      uniformSubScopesProcessor2.Operator = strategyMutator2;
291      uniformSubScopesProcessor2.Successor = null;
292      strategyMutator2.Successor = mutator2;
293      mutator2.Successor = null;
294      uniformSubScopesProcessor3.Operator = evaluator;
295      uniformSubScopesProcessor3.Successor = subScopesCounter;
296      evaluator.Successor = null;
297      subScopesCounter.Successor = null;
298
299      plusOrCommaReplacementBranch.TrueBranch = reevaluateElitesBranch;
300      reevaluateElitesBranch.TrueBranch = subScopesProcessor2;
301      reevaluateElitesBranch.FalseBranch = null;
302      subScopesProcessor2.Operators.Add(uniformSubScopesProcessor4);
303      subScopesProcessor2.Operators.Add(new EmptyOperator());
304      uniformSubScopesProcessor4.Operator = evaluator2;
305      uniformSubScopesProcessor4.Successor = subScopesCounter2;
306      subScopesCounter2.Successor = null;
307      reevaluateElitesBranch.Successor = plusReplacement;
308
309      plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
310      plusOrCommaReplacementBranch.Successor = bestSelector;
311      bestSelector.Successor = rightReducer;
312      rightReducer.Successor = intCounter;
313      intCounter.Successor = comparator;
314      comparator.Successor = analyzer2;
315      analyzer2.Successor = conditionalBranch;
316      conditionalBranch.FalseBranch = selector;
317      conditionalBranch.TrueBranch = null;
318      conditionalBranch.Successor = null;
319      #endregion
320    }
321
322    public override IOperation Apply() {
323      if (MutatorParameter.ActualValue == null)
324        return null;
325      return base.Apply();
326    }
327  }
328}
Note: See TracBrowser for help on using the repository browser.