1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Selection;
|
---|
30 |
|
---|
31 | namespace 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 IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
|
---|
64 | get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
65 | }
|
---|
66 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
67 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
68 | }
|
---|
69 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
70 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
71 | }
|
---|
72 | public ValueLookupParameter<IOperator> RecombinatorParameter {
|
---|
73 | get { return (ValueLookupParameter<IOperator>)Parameters["Recombinator"]; }
|
---|
74 | }
|
---|
75 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
76 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
77 | }
|
---|
78 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
79 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
80 | }
|
---|
81 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
82 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
83 | }
|
---|
84 | public LookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
85 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
86 | }
|
---|
87 | private ScopeParameter CurrentScopeParameter {
|
---|
88 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
89 | }
|
---|
90 | private ValueLookupParameter<IOperator> StrategyParameterManipulatorParameter {
|
---|
91 | get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterManipulator"]; }
|
---|
92 | }
|
---|
93 | private ValueLookupParameter<IOperator> StrategyParameterCrossoverParameter {
|
---|
94 | get { return (ValueLookupParameter<IOperator>)Parameters["StrategyParameterCrossover"]; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public IScope CurrentScope {
|
---|
98 | get { return CurrentScopeParameter.ActualValue; }
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | [StorableConstructor]
|
---|
103 | private EvolutionStrategyMainLoop(bool deserializing) : base(deserializing) { }
|
---|
104 | private EvolutionStrategyMainLoop(EvolutionStrategyMainLoop original, Cloner cloner)
|
---|
105 | : base(original, cloner) {
|
---|
106 | }
|
---|
107 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
108 | return new EvolutionStrategyMainLoop(this, cloner);
|
---|
109 | }
|
---|
110 | public EvolutionStrategyMainLoop()
|
---|
111 | : base() {
|
---|
112 | Initialize();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void Initialize() {
|
---|
116 | #region Create parameters
|
---|
117 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
118 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
119 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
120 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
121 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population."));
|
---|
122 | Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined."));
|
---|
123 | Parameters.Add(new ValueLookupParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population."));
|
---|
124 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
125 | Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population)."));
|
---|
126 | Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
|
---|
127 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
128 | Parameters.Add(new ValueLookupParameter<IOperator>("Recombinator", "The operator used to cross solutions."));
|
---|
129 | 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."));
|
---|
130 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
131 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
132 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
133 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the EvolutionStrategy should be applied."));
|
---|
134 | Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterManipulator", "The operator to mutate the endogeneous strategy parameters."));
|
---|
135 | Parameters.Add(new ValueLookupParameter<IOperator>("StrategyParameterCrossover", "The operator to cross the endogeneous strategy parameters."));
|
---|
136 | #endregion
|
---|
137 |
|
---|
138 | #region Create operators
|
---|
139 | VariableCreator variableCreator = new VariableCreator();
|
---|
140 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
141 | Placeholder analyzer1 = new Placeholder();
|
---|
142 | WithoutRepeatingBatchedRandomSelector selector = new WithoutRepeatingBatchedRandomSelector();
|
---|
143 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
144 | Comparator useRecombinationComparator = new Comparator();
|
---|
145 | ConditionalBranch useRecombinationBranch = new ConditionalBranch();
|
---|
146 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
147 | UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
|
---|
148 | Placeholder recombinator = new Placeholder();
|
---|
149 | Placeholder strategyRecombinator = new Placeholder();
|
---|
150 | Placeholder strategyMutator1 = new Placeholder();
|
---|
151 | Placeholder mutator1 = new Placeholder();
|
---|
152 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
153 | UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
|
---|
154 | Placeholder strategyMutator2 = new Placeholder();
|
---|
155 | Placeholder mutator2 = new Placeholder();
|
---|
156 | UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
|
---|
157 | Placeholder evaluator = new Placeholder();
|
---|
158 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
159 | ConditionalBranch plusOrCommaReplacementBranch = new ConditionalBranch();
|
---|
160 | MergingReducer plusReplacement = new MergingReducer();
|
---|
161 | RightReducer commaReplacement = new RightReducer();
|
---|
162 | BestSelector bestSelector = new BestSelector();
|
---|
163 | RightReducer rightReducer = new RightReducer();
|
---|
164 | IntCounter intCounter = new IntCounter();
|
---|
165 | Comparator comparator = new Comparator();
|
---|
166 | Placeholder analyzer2 = new Placeholder();
|
---|
167 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
168 | ConditionalBranch reevaluateElitesBranch = new ConditionalBranch();
|
---|
169 | SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
|
---|
170 | UniformSubScopesProcessor uniformSubScopesProcessor4 = new UniformSubScopesProcessor();
|
---|
171 | Placeholder evaluator2 = new Placeholder();
|
---|
172 | SubScopesCounter subScopesCounter2 = new SubScopesCounter();
|
---|
173 |
|
---|
174 |
|
---|
175 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class EvolutionStrategy expects this to be called Generations
|
---|
176 |
|
---|
177 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
178 | resultsCollector1.ResultsParameter.ActualName = "Results";
|
---|
179 |
|
---|
180 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
181 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
182 |
|
---|
183 | selector.Name = "ES Random Selector";
|
---|
184 | selector.RandomParameter.ActualName = RandomParameter.Name;
|
---|
185 | selector.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
186 | selector.ChildrenParameter.ActualName = ChildrenParameter.Name;
|
---|
187 |
|
---|
188 | useRecombinationComparator.Name = "ParentsPerChild > 1";
|
---|
189 | useRecombinationComparator.LeftSideParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
190 | useRecombinationComparator.RightSideParameter.Value = new IntValue(1);
|
---|
191 | useRecombinationComparator.Comparison = new Comparison(ComparisonType.Greater);
|
---|
192 | useRecombinationComparator.ResultParameter.ActualName = "UseRecombination";
|
---|
193 |
|
---|
194 | useRecombinationBranch.Name = "Use Recombination?";
|
---|
195 | useRecombinationBranch.ConditionParameter.ActualName = "UseRecombination";
|
---|
196 |
|
---|
197 | childrenCreator.ParentsPerChild = null;
|
---|
198 | childrenCreator.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
199 |
|
---|
200 | recombinator.Name = "Recombinator (placeholder)";
|
---|
201 | recombinator.OperatorParameter.ActualName = RecombinatorParameter.Name;
|
---|
202 |
|
---|
203 | strategyRecombinator.Name = "Strategy Parameter Recombinator (placeholder)";
|
---|
204 | strategyRecombinator.OperatorParameter.ActualName = StrategyParameterCrossoverParameter.Name;
|
---|
205 |
|
---|
206 | strategyMutator1.Name = "Strategy Parameter Manipulator (placeholder)";
|
---|
207 | strategyMutator1.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
|
---|
208 |
|
---|
209 | mutator1.Name = "Mutator (placeholder)";
|
---|
210 | mutator1.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
211 |
|
---|
212 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
213 |
|
---|
214 | strategyMutator2.Name = "Strategy Parameter Manipulator (placeholder)";
|
---|
215 | strategyMutator2.OperatorParameter.ActualName = StrategyParameterManipulatorParameter.Name;
|
---|
216 |
|
---|
217 | mutator2.Name = "Mutator (placeholder)";
|
---|
218 | mutator2.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
219 |
|
---|
220 | uniformSubScopesProcessor3.Parallel.Value = true;
|
---|
221 |
|
---|
222 | evaluator.Name = "Evaluator (placeholder)";
|
---|
223 | evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
224 |
|
---|
225 | subScopesCounter.Name = "Increment EvaluatedSolutions";
|
---|
226 | subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
227 |
|
---|
228 | plusOrCommaReplacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
|
---|
229 |
|
---|
230 | bestSelector.CopySelected = new BoolValue(false);
|
---|
231 | bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
232 | bestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
|
---|
233 | bestSelector.QualityParameter.ActualName = QualityParameter.Name;
|
---|
234 |
|
---|
235 | intCounter.Increment = new IntValue(1);
|
---|
236 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
237 |
|
---|
238 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
239 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
240 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
241 | comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
242 |
|
---|
243 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
244 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
245 |
|
---|
246 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
247 |
|
---|
248 | reevaluateElitesBranch.ConditionParameter.ActualName = "ReevaluateElites";
|
---|
249 | reevaluateElitesBranch.Name = "Reevaluate elites ?";
|
---|
250 |
|
---|
251 | uniformSubScopesProcessor4.Parallel.Value = true;
|
---|
252 |
|
---|
253 | evaluator2.Name = "Evaluator (placeholder)";
|
---|
254 | evaluator2.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
255 |
|
---|
256 | subScopesCounter2.Name = "Increment EvaluatedSolutions";
|
---|
257 | subScopesCounter2.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
258 | #endregion
|
---|
259 |
|
---|
260 | #region Create operator graph
|
---|
261 | OperatorGraph.InitialOperator = variableCreator;
|
---|
262 | variableCreator.Successor = resultsCollector1;
|
---|
263 | resultsCollector1.Successor = analyzer1;
|
---|
264 | analyzer1.Successor = selector;
|
---|
265 | selector.Successor = subScopesProcessor1;
|
---|
266 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
267 | subScopesProcessor1.Operators.Add(useRecombinationComparator);
|
---|
268 | subScopesProcessor1.Successor = plusOrCommaReplacementBranch;
|
---|
269 | useRecombinationComparator.Successor = useRecombinationBranch;
|
---|
270 | useRecombinationBranch.TrueBranch = childrenCreator;
|
---|
271 | useRecombinationBranch.FalseBranch = uniformSubScopesProcessor2;
|
---|
272 | useRecombinationBranch.Successor = uniformSubScopesProcessor3;
|
---|
273 | childrenCreator.Successor = uniformSubScopesProcessor1;
|
---|
274 | uniformSubScopesProcessor1.Operator = recombinator;
|
---|
275 | uniformSubScopesProcessor1.Successor = null;
|
---|
276 | recombinator.Successor = strategyRecombinator;
|
---|
277 | strategyRecombinator.Successor = strategyMutator1;
|
---|
278 | strategyMutator1.Successor = mutator1;
|
---|
279 | mutator1.Successor = subScopesRemover;
|
---|
280 | subScopesRemover.Successor = null;
|
---|
281 | uniformSubScopesProcessor2.Operator = strategyMutator2;
|
---|
282 | uniformSubScopesProcessor2.Successor = null;
|
---|
283 | strategyMutator2.Successor = mutator2;
|
---|
284 | mutator2.Successor = null;
|
---|
285 | uniformSubScopesProcessor3.Operator = evaluator;
|
---|
286 | uniformSubScopesProcessor3.Successor = subScopesCounter;
|
---|
287 | evaluator.Successor = null;
|
---|
288 | subScopesCounter.Successor = null;
|
---|
289 |
|
---|
290 | plusOrCommaReplacementBranch.TrueBranch = reevaluateElitesBranch;
|
---|
291 | reevaluateElitesBranch.TrueBranch = subScopesProcessor2;
|
---|
292 | reevaluateElitesBranch.FalseBranch = null;
|
---|
293 | subScopesProcessor2.Operators.Add(uniformSubScopesProcessor4);
|
---|
294 | subScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
295 | uniformSubScopesProcessor4.Operator = evaluator2;
|
---|
296 | uniformSubScopesProcessor4.Successor = subScopesCounter2;
|
---|
297 | subScopesCounter2.Successor = null;
|
---|
298 | reevaluateElitesBranch.Successor = plusReplacement;
|
---|
299 |
|
---|
300 | plusOrCommaReplacementBranch.FalseBranch = commaReplacement;
|
---|
301 | plusOrCommaReplacementBranch.Successor = bestSelector;
|
---|
302 | bestSelector.Successor = rightReducer;
|
---|
303 | rightReducer.Successor = intCounter;
|
---|
304 | intCounter.Successor = comparator;
|
---|
305 | comparator.Successor = analyzer2;
|
---|
306 | analyzer2.Successor = conditionalBranch;
|
---|
307 | conditionalBranch.FalseBranch = selector;
|
---|
308 | conditionalBranch.TrueBranch = null;
|
---|
309 | conditionalBranch.Successor = null;
|
---|
310 | #endregion
|
---|
311 | }
|
---|
312 |
|
---|
313 | public override IOperation Apply() {
|
---|
314 | if (MutatorParameter.ActualValue == null)
|
---|
315 | return null;
|
---|
316 | return base.Apply();
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|