Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.EvolutionStrategy/3.3/EvolutionStrategyMainLoop.cs @ 10218

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

#2038: Merged r9553:9555, r9569, r9591:r9592, r9618 into the stable branch.

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