Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5341 was 5208, checked in by swagner, 14 years ago

Adapted EAs to enable parallel solution evaluation (#1333)

File size: 29.8 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. This operator is executed in parallel, if an engine is used which supports parallelization."));
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      SubScopesRemover subScopesRemover = new SubScopesRemover();
145      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
146      Placeholder strategyMutator2 = new Placeholder();
147      Placeholder mutator2 = new Placeholder();
148      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
149      Placeholder evaluator = 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      subScopesRemover.RemoveAllSubScopes = true;
198
199      strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
200      strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
201
202      mutator2.Name = "Mutator (placeholder)";
203      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
204
205      uniformSubScopesProcessor3.Parallel.Value = true;
206
207      evaluator.Name = "Evaluator (placeholder)";
208      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
209
210      plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
211
212      bestSelector.CopySelected = new BoolValue(false);
213      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
214      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
215      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
216
217      intCounter.Increment = new IntValue(1);
218      intCounter.ValueParameter.ActualName = "Generations";
219
220      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
221      comparator.LeftSideParameter.ActualName = "Generations";
222      comparator.ResultParameter.ActualName = "Terminate";
223      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
224
225      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
226      resultsCollector2.ResultsParameter.ActualName = "Results";
227
228      analyzer2.Name = "Analyzer (placeholder)";
229      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
230
231      conditionalBranch.ConditionParameter.ActualName = "Terminate";
232      #endregion
233
234      #region Create operator graph
235      OperatorGraph.InitialOperator = variableCreator;
236      variableCreator.Successor = resultsCollector1;
237      resultsCollector1.Successor = analyzer1;
238      analyzer1.Successor = selector;
239      selector.Successor = subScopesProcessor1;
240      subScopesProcessor1.Operators.Add(new EmptyOperator());
241      subScopesProcessor1.Operators.Add(useRecombinationComparator);
242      subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
243      useRecombinationComparator.Successor = useRecombinationBranch;
244      useRecombinationBranch.TrueBranch = childrenCreator;
245      useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
246      useRecombinationBranch.Successor = uniformSubScopesProcessor3;
247      childrenCreator.Successor = uniformSubScopesProcessor1;
248      uniformSubScopesProcessor1.Operator = recombinator;
249      uniformSubScopesProcessor1.Successor = null;
250      recombinator.Successor = strategyRecombinator;
251      strategyRecombinator.Successor = strategyMutator1;
252      strategyMutator1.Successor = mutator1;
253      mutator1.Successor = subScopesRemover;
254      subScopesRemover.Successor = null;
255      uniformSubScopesProcessor2.Operator = strategyMutator2;
256      uniformSubScopesProcessor2.Successor = null;
257      strategyMutator2.Successor = mutator2;
258      mutator2.Successor = null;
259      uniformSubScopesProcessor3.Operator = evaluator;
260      uniformSubScopesProcessor3.Successor = null;
261      evaluator.Successor = null;
262      plusOrCommaReplacementBranch.TrueBranch = plusReplacement;
263      plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
264      plusOrCommaReplacementBranch.Successor = bestSelector;
265      bestSelector.Successor = rightReducer;
266      rightReducer.Successor = intCounter;
267      intCounter.Successor = comparator;
268      comparator.Successor = resultsCollector2;
269      resultsCollector2.Successor = analyzer2;
270      analyzer2.Successor = conditionalBranch;
271      conditionalBranch.FalseBranch = selector;
272      conditionalBranch.TrueBranch = null;
273      conditionalBranch.Successor = null;
274      #endregion
275    }
276
277    public override IOperation Apply() {
278      if (MutatorParameter.ActualValue == null)
279        return null;
280      return base.Apply();
281    }
282  }
283}
Note: See TracBrowser for help on using the repository browser.