Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 28.9 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.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Optimization.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.Algorithms.EvolutionStrategy {
31  /// <summary>
32  /// An operator which represents the main loop of an evolution strategy (EvolutionStrategy).
33  /// </summary>
34  [Item("EvolutionStrategyMainLoop", "An operator which represents the main loop of an evolution strategy (EvolutionStrategy).")]
35  [StorableClass]
36  public sealed class EvolutionStrategyMainLoop : AlgorithmOperator {
37    #region Parameter properties
38    public ValueLookupParameter<IRandom> RandomParameter {
39      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
40    }
41    public ValueLookupParameter<BoolValue> MaximizationParameter {
42      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
43    }
44    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
45      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
48      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
49    }
50    public ValueLookupParameter<IntValue> PopulationSizeParameter {
51      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
52    }
53    public ValueLookupParameter<IntValue> ParentsPerChildParameter {
54      get { return (ValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
55    }
56    public ValueLookupParameter<IntValue> ChildrenParameter {
57      get { return (ValueLookupParameter<IntValue>)Parameters["Children"]; }
58    }
59    public ValueLookupParameter<BoolValue> PlusSelectionParameter {
60      get { return (ValueLookupParameter<BoolValue>)Parameters["PlusSelection"]; }
61    }
62    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
63      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
64    }
65    public ValueLookupParameter<IOperator> MutatorParameter {
66      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
67    }
68    public ValueLookupParameter<IOperator> RecombinatorParameter {
69      get { return (ValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
70    }
71    public ValueLookupParameter<IOperator> EvaluatorParameter {
72      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
73    }
74    public ValueLookupParameter<VariableCollection> ResultsParameter {
75      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
76    }
77    public ValueLookupParameter<IOperator> AnalyzerParameter {
78      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
79    }
80    private ScopeParameter CurrentScopeParameter {
81      get { return (ScopeParameter)Parameters["CurrentScope"]; }
82    }
83    private ValueLookupParameter<IOperator> StrategyParameterManipulatorParameter {
84      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterManipulator"]; }
85    }
86    private ValueLookupParameter<IOperator> StrategyParameterCrossoverParameter {
87      get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterCrossover"]; }
88    }
89
90    public IScope CurrentScope {
91      get { return CurrentScopeParameter.ActualValue; }
92    }
93    #endregion
94
95    [StorableConstructor]
96    private EvolutionStrategyMainLoop(bool deserializing) : base() { }
97    public EvolutionStrategyMainLoop()
98      : base() {
99      Initialize();
100    }
101
102    private void Initialize() {
103      #region Create parameters
104      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
105      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
106      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
107      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
108      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population."));
109      Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined."));
110      Parameters.Add(new ValueLookupParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population."));
111      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
112      Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population)."));
113      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
114      Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
115      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
116      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
117      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
118      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
119      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterManipulator", "The operator to mutate the endogeneous strategy parameters."));
120      Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterCrossover", "The operator to cross the endogeneous strategy parameters."));
121      #endregion
122
123      #region Create operators
124      VariableCreator variableCreator = new VariableCreator();
125      ResultsCollector resultsCollector1 = new ResultsCollector();
126      Placeholder analyzer1 = new Placeholder();
127      WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
128      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
129      Comparator useRecombinationComparator = new Comparator();
130      ConditionalBranch useRecombinationBranch = new ConditionalBranch();
131      ChildrenCreator childrenCreator = new ChildrenCreator();
132      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
133      Placeholder recombinator = new Placeholder();
134      Placeholder strategyRecombinator = new Placeholder();
135      Placeholder strategyMutator1 = new Placeholder();
136      Placeholder mutator1 = new Placeholder();
137      Placeholder evaluator1 = new Placeholder();
138      SubScopesRemover subScopesRemover = new SubScopesRemover();
139      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
140      Placeholder strategyMutator2 = new Placeholder();
141      Placeholder mutator2 = new Placeholder();
142      Placeholder evaluator2 = new Placeholder();
143      ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
144      MergingReducer plusReplacement = new MergingReducer();
145      RightReducer commaReplacement = new RightReducer();
146      BestSelector bestSelector = new BestSelector();
147      RightReducer rightReducer = new RightReducer();
148      IntCounter intCounter = new IntCounter();
149      Comparator comparator = new Comparator();
150      ResultsCollector resultsCollector2 = new ResultsCollector();
151      Placeholder analyzer2 = new Placeholder();
152      ConditionalBranch conditionalBranch = new ConditionalBranch();
153
154      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class EvolutionStrategy expects this to be called Generations
155
156      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
157      resultsCollector1.ResultsParameter.ActualName = "Results";
158
159      analyzer1.Name = "Analyzer (placeholder)";
160      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
161
162      selector.Name = "ES Random Selector";
163      selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
164      selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
165
166      useRecombinationComparator.Name = "ParentsPerChild > 1";
167      useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
168      useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
169      useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
170      useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
171
172      useRecombinationBranch.Name = "Use Recombination?";
173      useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
174
175      childrenCreator.ParentsPerChild = null;
176      childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
177
178      recombinator.Name = "Recombinator (placeholder)";
179      recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
180
181      strategyRecombinator.Name = "Strategy Parameter Recombinator (placeholder)";
182      strategyRecombinator.OperatorParameter.ActualName = StrategyParameterCrossoverParameter.Name;
183
184      strategyMutator1.Name = "Strategy Parameter Manipulator (placeholder)";
185      strategyMutator1.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
186
187      mutator1.Name = "Mutator (placeholder)";
188      mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
189
190      evaluator1.Name = "Evaluator (placeholder)";
191      evaluator1.OperatorParameter.ActualName = EvaluatorParameter.Name;
192
193      subScopesRemover.RemoveAllSubScopes = true;
194
195      strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
196      strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
197
198      mutator2.Name = "Mutator (placeholder)";
199      mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
200
201      evaluator2.Name = "Evaluator (placeholder)";
202      evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
203
204      plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
205
206      bestSelector.CopySelected = new BoolValue(false);
207      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
208      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
209      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
210
211      intCounter.Increment = new IntValue(1);
212      intCounter.ValueParameter.ActualName = "Generations";
213
214      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
215      comparator.LeftSideParameter.ActualName = "Generations";
216      comparator.ResultParameter.ActualName = "Terminate";
217      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
218
219      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
220      resultsCollector2.ResultsParameter.ActualName = "Results";
221
222      analyzer2.Name = "Analyzer (placeholder)";
223      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
224
225      conditionalBranch.ConditionParameter.ActualName = "Terminate";
226      #endregion
227
228      #region Create operator graph
229      OperatorGraph.InitialOperator = variableCreator;
230      variableCreator.Successor = resultsCollector1;
231      resultsCollector1.Successor = analyzer1;
232      analyzer1.Successor = selector;
233      selector.Successor = subScopesProcessor1;
234      subScopesProcessor1.Operators.Add(new EmptyOperator());
235      subScopesProcessor1.Operators.Add(useRecombinationComparator);
236      subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
237      useRecombinationComparator.Successor = useRecombinationBranch;
238      useRecombinationBranch.TrueBranch = childrenCreator;
239      useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
240      useRecombinationBranch.Successor = null;
241      childrenCreator.Successor = uniformSubScopesProcessor1;
242      uniformSubScopesProcessor1.Operator = recombinator;
243      uniformSubScopesProcessor1.Successor = null;
244      recombinator.Successor = strategyRecombinator;
245      strategyRecombinator.Successor = strategyMutator1;
246      strategyMutator1.Successor = mutator1;
247      mutator1.Successor = evaluator1;
248      evaluator1.Successor = subScopesRemover;
249      subScopesRemover.Successor = null;
250      uniformSubScopesProcessor2.Operator = strategyMutator2;
251      uniformSubScopesProcessor2.Successor = null;
252      strategyMutator2.Successor = mutator2;
253      mutator2.Successor = evaluator2;
254      plusOrCommaReplacementBranch.TrueBranch = plusReplacement;
255      plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
256      plusOrCommaReplacementBranch.Successor = bestSelector;
257      bestSelector.Successor = rightReducer;
258      rightReducer.Successor = intCounter;
259      intCounter.Successor = comparator;
260      comparator.Successor = resultsCollector2;
261      resultsCollector2.Successor = analyzer2;
262      analyzer2.Successor = conditionalBranch;
263      conditionalBranch.FalseBranch = selector;
264      conditionalBranch.TrueBranch = null;
265      conditionalBranch.Successor = null;
266      #endregion
267    }
268
269    public override IOperation Apply() {
270      if (MutatorParameter.ActualValue == null)
271        return null;
272      return base.Apply();
273    }
274  }
275}
Note: See TracBrowser for help on using the repository browser.