Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CMAES/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAEvolutionStrategyMainLoop.cs @ 9121

Last change on this file since 9121 was 9121, checked in by abeham, 11 years ago

#1961: Added wiring code, removed obsolete parameters, simplified mainloop slightly, changed sigmabounds to a matrix

  • Property svn:mime-type set to application/octet-stream
File size: 25.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.CMAEvolutionStrategy {
32  [Item("CMAEvolutionStrategyMainLoop", "An operator which represents the main loop of a CMA evolution strategy.")]
33  [StorableClass]
34  public sealed class CMAEvolutionStrategyMainLoop : AlgorithmOperator {
35    #region Parameter properties
36    public IValueLookupParameter<IRandom> RandomParameter {
37      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    public IValueLookupParameter<BoolValue> MaximizationParameter {
40      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
43      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
44    }
45    public IValueLookupParameter<DoubleValue> BestKnownQualityParameter {
46      get { return (IValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
47    }
48    public IValueLookupParameter<IntValue> PopulationSizeParameter {
49      get { return (IValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
50    }
51    public IValueLookupParameter<IntValue> MaximumGenerationsParameter {
52      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
53    }
54    public IValueLookupParameter<IntValue> MaximumEvaluatedSolutionsParameter {
55      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
56    }
57    public IValueLookupParameter<IOperator> MutatorParameter {
58      get { return (IValueLookupParameter<IOperator>)Parameters["Mutator"]; }
59    }
60    public IValueLookupParameter<IOperator> RecombinatorParameter {
61      get { return (IValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
62    }
63    public IValueLookupParameter<IOperator> EvaluatorParameter {
64      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
65    }
66    public IValueLookupParameter<VariableCollection> ResultsParameter {
67      get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; }
68    }
69    public IValueLookupParameter<IOperator> AnalyzerParameter {
70      get { return (IValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
71    }
72    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
73      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
74    }
75    public ILookupParameter<IntValue> GenerationsParameter {
76      get { return (ILookupParameter<IntValue>)Parameters["Generations"]; }
77    }
78    public ILookupParameter<IntValue> MuParameter {
79      get { return (ILookupParameter<IntValue>)Parameters["Mu"]; }
80    }
81    public IValueLookupParameter<IOperator> CMAESUpdaterParameter {
82      get { return (IValueLookupParameter<IOperator>)Parameters["CMAESUpdater"]; }
83    }
84    #endregion
85
86    [StorableConstructor]
87    private CMAEvolutionStrategyMainLoop(bool deserializing) : base(deserializing) { }
88    private CMAEvolutionStrategyMainLoop(CMAEvolutionStrategyMainLoop original, Cloner cloner)
89      : base(original, cloner) {
90    }
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new CMAEvolutionStrategyMainLoop(this, cloner);
93    }
94    public CMAEvolutionStrategyMainLoop()
95      : base() {
96      Initialize();
97    }
98
99    private void Initialize() {
100      #region Create parameters
101      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
102      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
103      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
104      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
105      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "λ (lambda) - the size of the population."));
106      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
107      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions that should be computed."));
108      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
109      Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
110      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."));
111      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
112      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
113      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
114      Parameters.Add(new LookupParameter<IntValue>("Generations", "The current generation that is being processed."));
115      Parameters.Add(new LookupParameter<IntValue>("Mu", "The number of offspring considered for updating of the strategy parameters."));
116      Parameters.Add(new ValueLookupParameter<IOperator>("CMAESUpdater", "The operator to update the strategy parameters."));
117      #endregion
118
119      #region Create operators
120      var analyzer = new Placeholder();
121      var selector = new LeftSelector();
122      var ssp1 = new SubScopesProcessor();
123      var eo1 = new EmptyOperator();
124      var childrenCreator = new ChildrenCreator();
125      var ssp2 = new SubScopesProcessor();
126      var ussp1 = new UniformSubScopesProcessor();
127      var mutator = new Placeholder();
128      var ussp2 = new UniformSubScopesProcessor();
129      var evaluator1 = new Placeholder();
130      var evaluationsCounter = new IntCounter();
131      var selector2 = new BestSelector();
132      var reducer2 = new RightReducer();
133      var recombinator = new Placeholder();
134      var evaluator2 = new Placeholder();
135      var updater = new Placeholder();
136      var reducer1 = new RightReducer();
137      var ssp3 = new SubScopesProcessor();
138      var subScopesRemover = new SubScopesRemover();
139      var generationsCounter = new IntCounter();
140      var comparatorEvaluations = new Comparator();
141      var branchEvaluations = new ConditionalBranch();
142      var comparatorGenerations = new Comparator();
143      var branchGenerations = new ConditionalBranch();
144      var analyzer2 = new Placeholder();
145
146      analyzer.Name = "Analyzer (placeholder)";
147      analyzer.OperatorParameter.ActualName = AnalyzerParameter.Name;
148
149      selector.CopySelected = new BoolValue(true);
150      selector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
151
152      childrenCreator.ParentsPerChild = null;
153      childrenCreator.ParentsPerChildParameter.ActualName = PopulationSizeParameter.Name;
154
155      ussp1.Name = "Mutate Offspring";
156      ussp1.Parallel = new BoolValue(false);
157
158      mutator.Name = "Mutator (placeholder)";
159      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
160
161      ussp2.Name = "Evaluate Offspring";
162      ussp2.Parallel = new BoolValue(true);
163
164      evaluator1.Name = "Evaluator (placeholder)";
165      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
166
167      evaluationsCounter.Name = "Count EvaluatedSolutions";
168      evaluationsCounter.Increment = null;
169      evaluationsCounter.IncrementParameter.ActualName = PopulationSizeParameter.Name;
170
171      selector2.MaximizationParameter.ActualName = MaximizationParameter.Name;
172      selector2.QualityParameter.ActualName = QualityParameter.Name;
173      selector2.NumberOfSelectedSubScopesParameter.ActualName = MuParameter.Name;
174      selector2.CopySelected = new BoolValue(false);
175
176      reducer2.Name = "Drop Worst";
177
178      recombinator.Name = "Update Mean (placeholder)";
179      recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
180
181      evaluator2.Name = "Evaluator (placeholder)";
182      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
183
184      updater.Name = "Update Strategy Parameters";
185      updater.OperatorParameter.ActualName = CMAESUpdaterParameter.Name;
186
187      reducer1.Name = "Move to new mean";
188
189      ssp3.Name = "Cleanup";
190
191      subScopesRemover.Name = "Drop Best Offspring";
192      subScopesRemover.RemoveAllSubScopes = true;
193
194      generationsCounter.Name = "Generations++";
195      generationsCounter.Increment = new IntValue(1);
196      generationsCounter.ValueParameter.ActualName = "Generations";
197
198      comparatorEvaluations.Name = "Compare EvaluatedSolutions";
199      comparatorEvaluations.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
200      comparatorEvaluations.LeftSideParameter.ActualName = "EvaluatedSolutions";
201      comparatorEvaluations.ResultParameter.ActualName = "Terminate";
202      comparatorEvaluations.RightSideParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
203
204      branchEvaluations.Name = "Terminate on MaximumEvaluations";
205      branchEvaluations.ConditionParameter.ActualName = "Terminate";
206
207      comparatorGenerations.Name = "Compare Generations";
208      comparatorGenerations.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
209      comparatorGenerations.LeftSideParameter.ActualName = "Generations";
210      comparatorGenerations.ResultParameter.ActualName = "Terminate";
211      comparatorGenerations.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
212
213      branchGenerations.Name = "Terminate on MaximumGenerations";
214      branchGenerations.ConditionParameter.ActualName = "Terminate";
215
216      analyzer2.Name = "Analyzer (Placeholder)";
217      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
218      #endregion
219
220      #region Create operator graph
221      OperatorGraph.InitialOperator = analyzer;
222      analyzer.Successor = selector;
223      selector.Successor = ssp1;
224      ssp1.Operators.Add(eo1);
225      ssp1.Operators.Add(childrenCreator);
226      ssp1.Successor = generationsCounter;
227      eo1.Successor = null;
228      childrenCreator.Successor = ssp2;
229      ssp2.Operators.Add(ussp1);
230      ssp2.Successor = null;
231      ussp1.Operator = mutator;
232      ussp1.Successor = ussp2;
233      mutator.Successor = null;
234      ussp2.Operator = evaluator1;
235      ussp2.Successor = evaluationsCounter;
236      evaluator1.Successor = null;
237      evaluationsCounter.Successor = selector2;
238      selector2.Successor = reducer2;
239      reducer2.Successor = recombinator;
240      recombinator.Successor = evaluator2;
241      evaluator2.Successor = null;
242      generationsCounter.Successor = updater;
243      updater.Successor = reducer1;
244      reducer1.Successor = ssp3;
245      ssp3.Operators.Add(subScopesRemover);
246      ssp3.Successor = comparatorEvaluations;
247      subScopesRemover.Successor = null;
248      comparatorEvaluations.Successor = branchEvaluations;
249      branchEvaluations.FalseBranch = comparatorGenerations;
250      branchEvaluations.TrueBranch = analyzer2;
251      branchEvaluations.Successor = null;
252      comparatorGenerations.Successor = branchGenerations;
253      branchGenerations.FalseBranch = analyzer;
254      branchGenerations.TrueBranch = analyzer2;
255      branchGenerations.Successor = null;
256      analyzer2.Successor = null;
257      #endregion
258    }
259
260    public override IOperation Apply() {
261      if (MutatorParameter.ActualValue == null || RecombinatorParameter.ActualValue == null)
262        return null;
263      return base.Apply();
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.