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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Optimization.Operators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 | using HeuristicLab.Analysis;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.EvolutionStrategy {
|
---|
38 | /// <summary>
|
---|
39 | /// A standard genetic algorithm.
|
---|
40 | /// </summary>
|
---|
41 | [Item("Evolution Strategy", "An evolution strategy.")]
|
---|
42 | [Creatable("Algorithms")]
|
---|
43 | [StorableClass]
|
---|
44 | public sealed class EvolutionStrategy : EngineAlgorithm {
|
---|
45 | #region Problem Properties
|
---|
46 | public override Type ProblemType {
|
---|
47 | get { return typeof(ISingleObjectiveProblem); }
|
---|
48 | }
|
---|
49 | public new ISingleObjectiveProblem Problem {
|
---|
50 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
51 | set { base.Problem = value; }
|
---|
52 | }
|
---|
53 | #endregion
|
---|
54 |
|
---|
55 | #region Parameter Properties
|
---|
56 | private ValueParameter<IntValue> SeedParameter {
|
---|
57 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
58 | }
|
---|
59 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
60 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
61 | }
|
---|
62 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
63 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
64 | }
|
---|
65 | private ValueParameter<IntValue> ParentsPerChildParameter {
|
---|
66 | get { return (ValueParameter<IntValue>)Parameters["ParentsPerChild"]; }
|
---|
67 | }
|
---|
68 | private ValueParameter<IntValue> ChildrenParameter {
|
---|
69 | get { return (ValueParameter<IntValue>)Parameters["Children"]; }
|
---|
70 | }
|
---|
71 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
72 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
73 | }
|
---|
74 | private ValueParameter<BoolValue> PlusSelectionParameter {
|
---|
75 | get { return (ValueParameter<BoolValue>)Parameters["PlusSelection"]; }
|
---|
76 | }
|
---|
77 | private ConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
78 | get { return (ConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
79 | }
|
---|
80 | private OptionalConstrainedValueParameter<ICrossover> RecombinatorParameter {
|
---|
81 | get { return (OptionalConstrainedValueParameter<ICrossover>)Parameters["Recombinator"]; }
|
---|
82 | }
|
---|
83 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
84 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
85 | }
|
---|
86 | private OptionalConstrainedValueParameter<IStrategyParameterCreator> StrategyParameterCreatorParameter {
|
---|
87 | get { return (OptionalConstrainedValueParameter<IStrategyParameterCreator>)Parameters["StrategyParameterCreator"]; }
|
---|
88 | }
|
---|
89 | private OptionalConstrainedValueParameter<IStrategyParameterCrossover> StrategyParameterCrossoverParameter {
|
---|
90 | get { return (OptionalConstrainedValueParameter<IStrategyParameterCrossover>)Parameters["StrategyParameterCrossover"]; }
|
---|
91 | }
|
---|
92 | private OptionalConstrainedValueParameter<IStrategyParameterManipulator> StrategyParameterManipulatorParameter {
|
---|
93 | get { return (OptionalConstrainedValueParameter<IStrategyParameterManipulator>)Parameters["StrategyParameterManipulator"]; }
|
---|
94 | }
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | #region Properties
|
---|
98 | public IntValue Seed {
|
---|
99 | get { return SeedParameter.Value; }
|
---|
100 | set { SeedParameter.Value = value; }
|
---|
101 | }
|
---|
102 | public BoolValue SetSeedRandomly {
|
---|
103 | get { return SetSeedRandomlyParameter.Value; }
|
---|
104 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
105 | }
|
---|
106 | public IntValue PopulationSize {
|
---|
107 | get { return PopulationSizeParameter.Value; }
|
---|
108 | set { PopulationSizeParameter.Value = value; }
|
---|
109 | }
|
---|
110 | public IntValue ParentsPerChild {
|
---|
111 | get { return ParentsPerChildParameter.Value; }
|
---|
112 | set { ParentsPerChildParameter.Value = value; }
|
---|
113 | }
|
---|
114 | public IntValue Children {
|
---|
115 | get { return ChildrenParameter.Value; }
|
---|
116 | set { ChildrenParameter.Value = value; }
|
---|
117 | }
|
---|
118 | public IntValue MaximumGenerations {
|
---|
119 | get { return MaximumGenerationsParameter.Value; }
|
---|
120 | set { MaximumGenerationsParameter.Value = value; }
|
---|
121 | }
|
---|
122 | private BoolValue PlusSelection {
|
---|
123 | get { return PlusSelectionParameter.Value; }
|
---|
124 | set { PlusSelectionParameter.Value = value; }
|
---|
125 | }
|
---|
126 | public IManipulator Mutator {
|
---|
127 | get { return MutatorParameter.Value; }
|
---|
128 | set { MutatorParameter.Value = value; }
|
---|
129 | }
|
---|
130 | public ICrossover Recombinator {
|
---|
131 | get { return RecombinatorParameter.Value; }
|
---|
132 | set { RecombinatorParameter.Value = value; }
|
---|
133 | }
|
---|
134 | public MultiAnalyzer Analyzer {
|
---|
135 | get { return AnalyzerParameter.Value; }
|
---|
136 | set { AnalyzerParameter.Value = value; }
|
---|
137 | }
|
---|
138 | public IStrategyParameterCreator StrategyParameterCreator {
|
---|
139 | get { return StrategyParameterCreatorParameter.Value; }
|
---|
140 | set { StrategyParameterCreatorParameter.Value = value; }
|
---|
141 | }
|
---|
142 | public IStrategyParameterCrossover StrategyParameterCrossover {
|
---|
143 | get { return StrategyParameterCrossoverParameter.Value; }
|
---|
144 | set { StrategyParameterCrossoverParameter.Value = value; }
|
---|
145 | }
|
---|
146 | public IStrategyParameterManipulator StrategyParameterManipulator {
|
---|
147 | get { return StrategyParameterManipulatorParameter.Value; }
|
---|
148 | set { StrategyParameterManipulatorParameter.Value = value; }
|
---|
149 | }
|
---|
150 |
|
---|
151 | private RandomCreator RandomCreator {
|
---|
152 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
153 | }
|
---|
154 | private SolutionsCreator SolutionsCreator {
|
---|
155 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
156 | }
|
---|
157 | private EvolutionStrategyMainLoop MainLoop {
|
---|
158 | get { return (EvolutionStrategyMainLoop)((UniformSubScopesProcessor)SolutionsCreator.Successor).Successor; }
|
---|
159 | }
|
---|
160 | [Storable]
|
---|
161 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
162 | #endregion
|
---|
163 |
|
---|
164 | public EvolutionStrategy()
|
---|
165 | : base() {
|
---|
166 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
167 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
168 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population.", new IntValue(5)));
|
---|
169 | Parameters.Add(new ValueParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined.", new IntValue(1)));
|
---|
170 | Parameters.Add(new ValueParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population.", new IntValue(10)));
|
---|
171 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
172 | Parameters.Add(new ValueParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population).", new BoolValue(true)));
|
---|
173 | Parameters.Add(new OptionalConstrainedValueParameter<ICrossover>("Recombinator", "The operator used to cross solutions."));
|
---|
174 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
175 | Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCreator>("StrategyParameterCreator", "The operator that creates the strategy parameters."));
|
---|
176 | Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCrossover>("StrategyParameterCrossover", "The operator that recombines the strategy parameters."));
|
---|
177 | Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterManipulator>("StrategyParameterManipulator", "The operator that manipulates the strategy parameters."));
|
---|
178 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
179 |
|
---|
180 | RandomCreator randomCreator = new RandomCreator();
|
---|
181 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
182 | UniformSubScopesProcessor strategyVectorProcessor = new UniformSubScopesProcessor();
|
---|
183 | Placeholder strategyVectorCreator = new Placeholder();
|
---|
184 | EvolutionStrategyMainLoop mainLoop = new EvolutionStrategyMainLoop();
|
---|
185 | OperatorGraph.InitialOperator = randomCreator;
|
---|
186 |
|
---|
187 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
188 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
189 | randomCreator.SeedParameter.Value = null;
|
---|
190 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
191 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
192 | randomCreator.Successor = solutionsCreator;
|
---|
193 |
|
---|
194 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
195 | solutionsCreator.Successor = strategyVectorProcessor;
|
---|
196 |
|
---|
197 | strategyVectorProcessor.Operator = strategyVectorCreator;
|
---|
198 | strategyVectorProcessor.Successor = mainLoop;
|
---|
199 |
|
---|
200 | strategyVectorCreator.OperatorParameter.ActualName = "StrategyParameterCreator";
|
---|
201 |
|
---|
202 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
203 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
204 | mainLoop.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
205 | mainLoop.ChildrenParameter.ActualName = ChildrenParameter.Name;
|
---|
206 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
207 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
208 | mainLoop.RecombinatorParameter.ActualName = RecombinatorParameter.Name;
|
---|
209 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
210 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
211 |
|
---|
212 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
213 | ParameterizeAnalyzers();
|
---|
214 | UpdateAnalyzers();
|
---|
215 |
|
---|
216 | Initialize();
|
---|
217 | }
|
---|
218 | [StorableConstructor]
|
---|
219 | private EvolutionStrategy(bool deserializing) : base(deserializing) { }
|
---|
220 |
|
---|
221 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
222 | EvolutionStrategy clone = (EvolutionStrategy)base.Clone(cloner);
|
---|
223 | clone.qualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(qualityAnalyzer);
|
---|
224 | clone.Initialize();
|
---|
225 | return clone;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public override void Prepare() {
|
---|
229 | if (Problem != null) base.Prepare();
|
---|
230 | }
|
---|
231 |
|
---|
232 | #region Events
|
---|
233 | protected override void OnProblemChanged() {
|
---|
234 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
235 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
236 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
237 | ParameterizeSolutionsCreator();
|
---|
238 | ParameterizeMainLoop();
|
---|
239 | ParameterizeAnalyzers();
|
---|
240 | UpdateRecombinators();
|
---|
241 | UpdateMutators();
|
---|
242 | UpdateAnalyzers();
|
---|
243 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
244 | base.OnProblemChanged();
|
---|
245 | }
|
---|
246 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
247 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
248 | ParameterizeSolutionsCreator();
|
---|
249 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
250 | }
|
---|
251 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
252 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
253 | ParameterizeSolutionsCreator();
|
---|
254 | ParameterizeMainLoop();
|
---|
255 | ParameterizeAnalyzers();
|
---|
256 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
257 | base.Problem_EvaluatorChanged(sender, e);
|
---|
258 | }
|
---|
259 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
260 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
261 | UpdateRecombinators();
|
---|
262 | UpdateMutators();
|
---|
263 | UpdateAnalyzers();
|
---|
264 | base.Problem_OperatorsChanged(sender, e);
|
---|
265 | }
|
---|
266 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
267 | ParameterizeMainLoop();
|
---|
268 | ParameterizeAnalyzers();
|
---|
269 | }
|
---|
270 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
271 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
272 | PopulationSize_ValueChanged(null, EventArgs.Empty);
|
---|
273 | }
|
---|
274 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
275 | if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
|
---|
276 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
277 | Children.Value = PopulationSize.Value;
|
---|
278 | if (PopulationSize.Value < ParentsPerChild.Value)
|
---|
279 | ParentsPerChild.Value = PopulationSize.Value;
|
---|
280 | }
|
---|
281 | private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
|
---|
282 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
283 | ParentsPerChild_ValueChanged(null, EventArgs.Empty);
|
---|
284 | }
|
---|
285 | private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
|
---|
286 | if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
|
---|
287 | ParentsPerChild.Value = 1;
|
---|
288 | if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
|
---|
289 | if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
|
---|
290 | PopulationSize.Value = ParentsPerChild.Value;
|
---|
291 | }
|
---|
292 | private void ChildrenParameter_ValueChanged(object sender, EventArgs e) {
|
---|
293 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
294 | Children_ValueChanged(null, EventArgs.Empty);
|
---|
295 | }
|
---|
296 | private void Children_ValueChanged(object sender, EventArgs e) {
|
---|
297 | if (Children.Value <= 0) Children.Value = 1;
|
---|
298 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
299 | PopulationSize.Value = Children.Value;
|
---|
300 | }
|
---|
301 | private void PlusSelectionParameter_ValueChanged(object sender, EventArgs e) {
|
---|
302 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
303 | PlusSelection_ValueChanged(null, EventArgs.Empty);
|
---|
304 | }
|
---|
305 | private void PlusSelection_ValueChanged(object sender, EventArgs e) {
|
---|
306 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
307 | Children.Value = PopulationSize.Value;
|
---|
308 | }
|
---|
309 | private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
310 | if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
|
---|
311 | else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
|
---|
312 | if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
|
---|
313 | if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
314 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
315 | }
|
---|
316 | }
|
---|
317 | private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
318 | if (Mutator is ISelfAdaptiveManipulator) {
|
---|
319 | UpdateStrategyParameterOperators();
|
---|
320 | if (StrategyParameterCreatorParameter.ValidValues.Count == 0) throw new InvalidOperationException("ERROR: There is no strategy parameter creator for this manipulation operator.");
|
---|
321 | } else {
|
---|
322 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
323 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
324 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
325 | UpdateRecombinators();
|
---|
326 | }
|
---|
327 | }
|
---|
328 | private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
329 | if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
|
---|
330 | StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
331 | }
|
---|
332 | private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
|
---|
333 | if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
334 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
335 | }
|
---|
336 | #endregion
|
---|
337 |
|
---|
338 | #region Helpers
|
---|
339 | [StorableHook(HookType.AfterDeserialization)]
|
---|
340 | private void Initialize() {
|
---|
341 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
342 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
343 | ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
|
---|
344 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
345 | ChildrenParameter.ValueChanged += new EventHandler(ChildrenParameter_ValueChanged);
|
---|
346 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
347 | PlusSelectionParameter.ValueChanged += new EventHandler(PlusSelectionParameter_ValueChanged);
|
---|
348 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
349 | RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
|
---|
350 | MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
|
---|
351 | StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
|
---|
352 | StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
|
---|
353 | if (Problem != null)
|
---|
354 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
355 | }
|
---|
356 | private void ParameterizeSolutionsCreator() {
|
---|
357 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
358 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
359 | }
|
---|
360 | private void ParameterizeMainLoop() {
|
---|
361 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
362 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
363 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
364 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
365 | }
|
---|
366 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
367 | if (op is IStochasticOperator)
|
---|
368 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
369 | }
|
---|
370 | private void ParameterizeAnalyzers() {
|
---|
371 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
372 | if (Problem != null) {
|
---|
373 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
374 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
375 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | private void UpdateStrategyParameterOperators() {
|
---|
379 | IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
|
---|
380 | IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
|
---|
381 | IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
|
---|
382 | ClearStrategyParameterOperators();
|
---|
383 | ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
|
---|
384 | if (manipulator != null) {
|
---|
385 | var operators = Problem.Operators.Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
|
---|
386 | foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
|
---|
387 | StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
|
---|
388 | foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
|
---|
389 | StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
|
---|
390 | foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
|
---|
391 | StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
|
---|
392 |
|
---|
393 | if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
|
---|
394 | RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
|
---|
395 |
|
---|
396 | if (oldStrategyCreator != null) {
|
---|
397 | IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
|
---|
398 | if (tmp1 != null) StrategyParameterCreator = tmp1;
|
---|
399 | } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
400 | if (oldStrategyCrossover != null) {
|
---|
401 | IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
|
---|
402 | if (tmp2 != null) StrategyParameterCrossover = tmp2;
|
---|
403 | } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
404 | if (oldStrategyManipulator != null) {
|
---|
405 | IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
|
---|
406 | if (tmp3 != null) StrategyParameterManipulator = tmp3;
|
---|
407 | } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
|
---|
408 | }
|
---|
409 | }
|
---|
410 | private void ClearStrategyParameterOperators() {
|
---|
411 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
412 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
413 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
414 | }
|
---|
415 | private void UpdateRecombinators() {
|
---|
416 | ICrossover oldRecombinator = Recombinator;
|
---|
417 | RecombinatorParameter.ValidValues.Clear();
|
---|
418 | foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
419 | RecombinatorParameter.ValidValues.Add(recombinator);
|
---|
420 | }
|
---|
421 | if (oldRecombinator != null) {
|
---|
422 | ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
|
---|
423 | if (recombinator != null) RecombinatorParameter.Value = recombinator;
|
---|
424 | }
|
---|
425 | }
|
---|
426 | private void UpdateMutators() {
|
---|
427 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
428 | MutatorParameter.ValidValues.Clear();
|
---|
429 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
430 | MutatorParameter.ValidValues.Add(mutator);
|
---|
431 | if (oldMutator != null) {
|
---|
432 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
433 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
434 | }
|
---|
435 | }
|
---|
436 | private void UpdateAnalyzers() {
|
---|
437 | Analyzer.Operators.Clear();
|
---|
438 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
439 | if (Problem != null) {
|
---|
440 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name))
|
---|
441 | Analyzer.Operators.Add(analyzer);
|
---|
442 | }
|
---|
443 | }
|
---|
444 | #endregion
|
---|
445 | }
|
---|
446 | }
|
---|