Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3174 was 3148, checked in by abeham, 15 years ago

Fixed a bug in the evolution strategy and included some parameter sanity checks #932

File size: 42.7 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.Analysis;
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 SubScopesLookupParameter<DoubleValue> QualityParameter {
46      get { return (SubScopesLookupParameter<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<DoubleMatrix> StrategyVectorBoundsParameter {
61      get { return (ValueLookupParameter<DoubleMatrix>)Parameters["StrategyVectorBounds"]; }
62    }
63    public ValueLookupParameter<DoubleValue> GeneralLearningRateParameter {
64      get { return (ValueLookupParameter<DoubleValue>)Parameters["GeneralLearningRate"]; }
65    }
66    public ValueLookupParameter<DoubleValue> LearningRateParameter {
67      get { return (ValueLookupParameter<DoubleValue>)Parameters["LearningRate"]; }
68    }
69    public ValueLookupParameter<BoolValue> PlusSelectionParameter {
70      get { return (ValueLookupParameter<BoolValue>)Parameters["PlusSelection"]; }
71    }
72    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
73      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
74    }
75    public ValueLookupParameter<IOperator> MutatorParameter {
76      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
77    }
78    public ValueLookupParameter<IOperator> RecombinatorParameter {
79      get { return (ValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
80    }
81    public ValueLookupParameter<IOperator> EvaluatorParameter {
82      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
83    }
84    public ValueLookupParameter<VariableCollection> ResultsParameter {
85      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
86    }
87    public ValueLookupParameter<IOperator> VisualizerParameter {
88      get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
89    }
90    public LookupParameter<IItem> VisualizationParameter {
91      get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
92    }
93    private ScopeParameter CurrentScopeParameter {
94      get { return (ScopeParameter)Parameters["CurrentScope"]; }
95    }
96
97    public IScope CurrentScope {
98      get { return CurrentScopeParameter.ActualValue; }
99    }
100    #endregion
101
102    [StorableConstructor]
103    private EvolutionStrategyMainLoop(bool deserializing) : base() { }
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 SubScopesLookupParameter<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 LookupParameter<DoubleMatrix>("StrategyVectorBounds", "2 column matrix with one row for each dimension specifying upper and lower bound for the strategy vector. If there are less rows than dimensions, the strategy vector will be read in a cycle."));
120      Parameters.Add(new ValueLookupParameter<DoubleValue>("GeneralLearningRate", "τ0 (tau0) - the factor with which adjustments in the strategy vector is dampened over all dimensions. Recommendation is to use 1/Sqrt(2*ProblemDimension)."));
121      Parameters.Add(new ValueLookupParameter<DoubleValue>("LearningRate", "τ (tau) - the factor with which adjustments in the strategy vector are dampened in a single dimension. Recommendation is to use 1/Sqrt(2*Sqrt(ProblemDimension))."));
122      Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population)."));
123      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
125      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
126      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
127      Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
128      Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
129      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
130      #endregion
131
132      #region Create operators
133      VariableCreator variableCreator = new VariableCreator();
134      BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
135      BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
136      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator1 = new BestAverageWorstQualityCalculator();
137      DataTableValuesCollector dataTableValuesCollector1 = new DataTableValuesCollector();
138      QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
139      Placeholder visualizer1 = new Placeholder();
140      ResultsCollector resultsCollector = new ResultsCollector();
141      WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
142      SequentialSubScopesProcessor sequentialSubScopesProcessor1 = new SequentialSubScopesProcessor();
143      Comparator useRecombinationComparator = new Comparator();
144      ConditionalBranch useRecombinationBranch = new ConditionalBranch();
145      ChildrenCreator childrenCreator = new ChildrenCreator();
146      UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor1 = new UniformSequentialSubScopesProcessor();
147      Placeholder recombinator = new Placeholder();
148      Placeholder mutator1 = new Placeholder();
149      Placeholder evaluator1 = new Placeholder();
150      SubScopesRemover subScopesRemover = new SubScopesRemover();
151      UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor2 = new UniformSequentialSubScopesProcessor();
152      Placeholder mutator2 = new Placeholder();
153      Placeholder evaluator2 = new Placeholder();
154      ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
155      MergingReducer plusReplacement = new MergingReducer();
156      RightReducer commaReplacement = new RightReducer();
157      BestSelector bestSelector = new BestSelector();
158      RightReducer rightReducer = new RightReducer();
159      IntCounter intCounter = new IntCounter();
160      Comparator comparator = new Comparator();
161      BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
162      BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
163      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator2 = new BestAverageWorstQualityCalculator();
164      DataTableValuesCollector dataTableValuesCollector2 = new DataTableValuesCollector();
165      QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
166      Placeholder visualizer2 = new Placeholder();
167      ConditionalBranch conditionalBranch = new ConditionalBranch();
168
169      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
170
171      bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
172      bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
173      bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
174
175      bestQualityMemorizer2.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
176      bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
177      bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
178
179      bestAverageWorstQualityCalculator1.AverageQualityParameter.ActualName = "CurrentAverageQuality";
180      bestAverageWorstQualityCalculator1.BestQualityParameter.ActualName = "CurrentBestQuality";
181      bestAverageWorstQualityCalculator1.MaximizationParameter.ActualName = MaximizationParameter.Name;
182      bestAverageWorstQualityCalculator1.QualityParameter.ActualName = QualityParameter.Name;
183      bestAverageWorstQualityCalculator1.WorstQualityParameter.ActualName = "CurrentWorstQuality";
184
185      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best Quality", null, "CurrentBestQuality"));
186      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average Quality", null, "CurrentAverageQuality"));
187      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst Quality", null, "CurrentWorstQuality"));
188      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
189      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
190      dataTableValuesCollector1.DataTableParameter.ActualName = "Qualities";
191
192      qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
193      qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
194      qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
195      qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
196
197      visualizer1.Name = "Visualizer (placeholder)";
198      visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
199
200      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
201      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best Quality", null, "CurrentBestQuality"));
202      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average Quality", null, "CurrentAverageQuality"));
203      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst Quality", null, "CurrentWorstQuality"));
204      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
205      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
206      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
207      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
208      resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
209      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
210      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
211
212      selector.Name = "ES Random Selector";
213      selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
214      selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
215
216      useRecombinationComparator.Name = "ParentsPerChild > 1";
217      useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
218      useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
219      useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
220      useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
221
222      useRecombinationBranch.Name = "Use Recombination?";
223      useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
224
225      childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
226
227      recombinator.Name = "Recombinator (placeholder)";
228      recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
229
230      mutator1.Name = "Mutator (placeholder)";
231      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
232
233      evaluator1.Name = "Evaluator (placeholder)";
234      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
235
236      subScopesRemover.RemoveAllSubScopes = true;
237
238      mutator2.Name = "Mutator (placeholder)";
239      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
240
241      evaluator2.Name = "Evaluator (placeholder)";
242      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
243
244      plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
245
246      bestSelector.CopySelected = new BoolValue(false);
247      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
248      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
249      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
250
251      intCounter.Increment = new IntValue(1);
252      intCounter.ValueParameter.ActualName = "Generations";
253
254      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
255      comparator.LeftSideParameter.ActualName = "Generations";
256      comparator.ResultParameter.ActualName = "Terminate";
257      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
258
259      bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
260      bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
261      bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
262
263      bestQualityMemorizer4.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
264      bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
265      bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
266
267      bestAverageWorstQualityCalculator2.AverageQualityParameter.ActualName = "CurrentAverageQuality";
268      bestAverageWorstQualityCalculator2.BestQualityParameter.ActualName = "CurrentBestQuality";
269      bestAverageWorstQualityCalculator2.MaximizationParameter.ActualName = MaximizationParameter.Name;
270      bestAverageWorstQualityCalculator2.QualityParameter.ActualName = QualityParameter.Name;
271      bestAverageWorstQualityCalculator2.WorstQualityParameter.ActualName = "CurrentWorstQuality";
272
273      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best Quality", null, "CurrentBestQuality"));
274      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average Quality", null, "CurrentAverageQuality"));
275      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst Quality", null, "CurrentWorstQuality"));
276      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
277      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
278      dataTableValuesCollector2.DataTableParameter.ActualName = "Qualities";
279
280      qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
281      qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
282      qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
283      qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
284
285      visualizer2.Name = "Visualizer (placeholder)";
286      visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
287
288      conditionalBranch.ConditionParameter.ActualName = "Terminate";
289      #endregion
290
291      #region Create operator graph
292      OperatorGraph.InitialOperator = variableCreator;
293      variableCreator.Successor = bestQualityMemorizer1;
294      bestQualityMemorizer1.Successor = bestQualityMemorizer2;
295      bestQualityMemorizer2.Successor = bestAverageWorstQualityCalculator1;
296      bestAverageWorstQualityCalculator1.Successor = dataTableValuesCollector1;
297      dataTableValuesCollector1.Successor = qualityDifferenceCalculator1;
298      qualityDifferenceCalculator1.Successor = visualizer1;
299      visualizer1.Successor = resultsCollector;
300      resultsCollector.Successor = selector;
301      selector.Successor = sequentialSubScopesProcessor1;
302      sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator());
303      sequentialSubScopesProcessor1.Operators.Add(useRecombinationComparator);
304      sequentialSubScopesProcessor1.Successor = plusOrCommaReplacementBranch;
305      useRecombinationComparator.Successor = useRecombinationBranch;
306      useRecombinationBranch.TrueBranch = childrenCreator;
307      useRecombinationBranch.FalseBranch = uniformSequentialSubScopesProcessor2;
308      useRecombinationBranch.Successor = null;
309      childrenCreator.Successor = uniformSequentialSubScopesProcessor1;
310      uniformSequentialSubScopesProcessor1.Operator = recombinator;
311      uniformSequentialSubScopesProcessor1.Successor = null;
312      recombinator.Successor = mutator1;
313      mutator1.Successor = evaluator1;
314      evaluator1.Successor = subScopesRemover;
315      subScopesRemover.Successor = null;
316      uniformSequentialSubScopesProcessor2.Operator = mutator2;
317      uniformSequentialSubScopesProcessor2.Successor = null;
318      mutator2.Successor = evaluator2;
319      plusOrCommaReplacementBranch.TrueBranch = plusReplacement;
320      plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
321      plusOrCommaReplacementBranch.Successor = bestSelector;
322      bestSelector.Successor = rightReducer;
323      rightReducer.Successor = intCounter;
324      intCounter.Successor = comparator;
325      comparator.Successor = bestQualityMemorizer3;
326      bestQualityMemorizer3.Successor = bestQualityMemorizer4;
327      bestQualityMemorizer4.Successor = bestAverageWorstQualityCalculator2;
328      bestAverageWorstQualityCalculator2.Successor = dataTableValuesCollector2;
329      dataTableValuesCollector2.Successor = qualityDifferenceCalculator2;
330      qualityDifferenceCalculator2.Successor = visualizer2;
331      visualizer2.Successor = conditionalBranch;
332      conditionalBranch.FalseBranch = selector;
333      conditionalBranch.TrueBranch = null;
334      conditionalBranch.Successor = null;
335      #endregion
336    }
337  }
338}
Note: See TracBrowser for help on using the repository browser.