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