Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6988 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 30.5 KB
RevLine 
[3130]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
82      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
83    }
84    private ScopeParameter CurrentScopeParameter {
85      get { return (ScopeParameter)Parameters["CurrentScope"]; }
86    }
87    private ValueLookupParameter<IOperator> StrategyParameterManipulatorParameter {
88      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterManipulator"]; }
89    }
90    private ValueLookupParameter<IOperator> StrategyParameterCrossoverParameter {
91      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterCrossover"]; }
92    }
93
94    public IScope CurrentScope {
95      get { return CurrentScopeParameter.ActualValue; }
96    }
97    #endregion
98
99    [StorableConstructor]
100    private EvolutionStrategyMainLoop(bool deserializing) : base(deserializing) { }
101    private EvolutionStrategyMainLoop(EvolutionStrategyMainLoop original, Cloner cloner)
102      : base(original, cloner) {
103    }
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new EvolutionStrategyMainLoop(this, cloner);
106    }
107    public EvolutionStrategyMainLoop()
108      : base() {
109      Initialize();
110    }
111
112    private void Initialize() {
113      #region Create parameters
114      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
115      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
116      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
117      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
118      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population."));
119      Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined."));
120      Parameters.Add(new ValueLookupParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
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. This operator is executed in parallel, if an engine is used which supports parallelization."));
126      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
127      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
128      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
129      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
130      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterManipulator", "The operator to mutate the endogeneous strategy parameters."));
131      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterCrossover", "The operator to cross the endogeneous strategy parameters."));
132      #endregion
133
134      #region Create operators
135      VariableCreator variableCreator = new VariableCreator();
136      ResultsCollector resultsCollector1 = new ResultsCollector();
137      Placeholder analyzer1 = new Placeholder();
138      WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
139      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
140      Comparator useRecombinationComparator = new Comparator();
141      ConditionalBranch useRecombinationBranch = new ConditionalBranch();
142      ChildrenCreator childrenCreator = new ChildrenCreator();
143      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
144      Placeholder recombinator = new Placeholder();
145      Placeholder strategyRecombinator = new Placeholder();
146      Placeholder strategyMutator1 = new Placeholder();
147      Placeholder mutator1 = new Placeholder();
148      SubScopesRemover subScopesRemover = new SubScopesRemover();
149      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
150      Placeholder strategyMutator2 = new Placeholder();
151      Placeholder mutator2 = new Placeholder();
152      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
153      Placeholder evaluator = new Placeholder();
154      SubScopesCounter subScopesCounter = new SubScopesCounter();
155      ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
156      MergingReducer plusReplacement = new MergingReducer();
157      RightReducer commaReplacement = new RightReducer();
158      BestSelector bestSelector = new BestSelector();
159      RightReducer rightReducer = new RightReducer();
160      IntCounter intCounter = new IntCounter();
161      Comparator comparator = new Comparator();
162      Placeholder analyzer2 = new Placeholder();
163      ConditionalBranch conditionalBranch = new ConditionalBranch();
164
165      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class EvolutionStrategy expects this to be called Generations
166
167      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
168      resultsCollector1.ResultsParameter.ActualName = "Results";
169
170      analyzer1.Name = "Analyzer (placeholder)";
171      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
172
173      selector.Name = "ES Random Selector";
174      selector.RandomParameter.ActualName = RandomParameter.Name;
175      selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
176      selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
177
178      useRecombinationComparator.Name = "ParentsPerChild > 1";
179      useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
180      useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
181      useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
182      useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
183
184      useRecombinationBranch.Name = "Use Recombination?";
185      useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
186
187      childrenCreator.ParentsPerChild = null;
188      childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
189
190      recombinator.Name = "Recombinator (placeholder)";
191      recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
192
193      strategyRecombinator.Name = "Strategy Parameter Recombinator (placeholder)";
194      strategyRecombinator.OperatorParameter.ActualName = StrategyParameterCrossoverParameter.Name;
195
196      strategyMutator1.Name = "Strategy Parameter Manipulator (placeholder)";
197      strategyMutator1.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
198
199      mutator1.Name = "Mutator (placeholder)";
200      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
201
202      subScopesRemover.RemoveAllSubScopes = true;
203
204      strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
205      strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
206
207      mutator2.Name = "Mutator (placeholder)";
208      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
209
210      uniformSubScopesProcessor3.Parallel.Value = true;
211
212      evaluator.Name = "Evaluator (placeholder)";
213      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
214
215      subScopesCounter.Name = "Increment EvaluatedSolutions";
216      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
217
218      plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
219
220      bestSelector.CopySelected = new BoolValue(false);
221      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
222      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
223      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
224
225      intCounter.Increment = new IntValue(1);
226      intCounter.ValueParameter.ActualName = "Generations";
227
228      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
229      comparator.LeftSideParameter.ActualName = "Generations";
230      comparator.ResultParameter.ActualName = "Terminate";
231      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
232
233      analyzer2.Name = "Analyzer (placeholder)";
234      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
235
236      conditionalBranch.ConditionParameter.ActualName = "Terminate";
237      #endregion
238
239      #region Create operator graph
240      OperatorGraph.InitialOperator = variableCreator;
241      variableCreator.Successor = resultsCollector1;
242      resultsCollector1.Successor = analyzer1;
243      analyzer1.Successor = selector;
244      selector.Successor = subScopesProcessor1;
245      subScopesProcessor1.Operators.Add(new EmptyOperator());
246      subScopesProcessor1.Operators.Add(useRecombinationComparator);
247      subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
248      useRecombinationComparator.Successor = useRecombinationBranch;
249      useRecombinationBranch.TrueBranch = childrenCreator;
250      useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
251      useRecombinationBranch.Successor = uniformSubScopesProcessor3;
252      childrenCreator.Successor = uniformSubScopesProcessor1;
253      uniformSubScopesProcessor1.Operator = recombinator;
254      uniformSubScopesProcessor1.Successor = null;
255      recombinator.Successor = strategyRecombinator;
256      strategyRecombinator.Successor = strategyMutator1;
257      strategyMutator1.Successor = mutator1;
258      mutator1.Successor = subScopesRemover;
259      subScopesRemover.Successor = null;
260      uniformSubScopesProcessor2.Operator = strategyMutator2;
261      uniformSubScopesProcessor2.Successor = null;
262      strategyMutator2.Successor = mutator2;
263      mutator2.Successor = null;
264      uniformSubScopesProcessor3.Operator = evaluator;
265      uniformSubScopesProcessor3.Successor = subScopesCounter;
266      evaluator.Successor = null;
267      subScopesCounter.Successor = null;
268      plusOrCommaReplacementBranch.TrueBranch = plusReplacement;
269      plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
270      plusOrCommaReplacementBranch.Successor = bestSelector;
271      bestSelector.Successor = rightReducer;
272      rightReducer.Successor = intCounter;
273      intCounter.Successor = comparator;
274      comparator.Successor = analyzer2;
275      analyzer2.Successor = conditionalBranch;
276      conditionalBranch.FalseBranch = selector;
277      conditionalBranch.TrueBranch = null;
278      conditionalBranch.Successor = null;
279      #endregion
280    }
281
282    public override IOperation Apply() {
283      if (MutatorParameter.ActualValue == null)
284        return null;
285      return base.Apply();
286    }
287  }
288}
Note: See TracBrowser for help on using the repository browser.