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.Selection;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
38 | /// <summary>
|
---|
39 | /// An island genetic algorithm.
|
---|
40 | /// </summary>
|
---|
41 | [Item("Island Genetic Algorithm", "An island genetic algorithm.")]
|
---|
42 | [Creatable("Algorithms")]
|
---|
43 | [StorableClass]
|
---|
44 | public sealed class IslandGeneticAlgorithm : EngineAlgorithm {
|
---|
45 |
|
---|
46 | #region Problem Properties
|
---|
47 | public override Type ProblemType {
|
---|
48 | get { return typeof(ISingleObjectiveProblem); }
|
---|
49 | }
|
---|
50 | public new ISingleObjectiveProblem Problem {
|
---|
51 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
52 | set { base.Problem = value; }
|
---|
53 | }
|
---|
54 | #endregion
|
---|
55 |
|
---|
56 | #region Parameter Properties
|
---|
57 | private ValueParameter<IntValue> SeedParameter {
|
---|
58 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
59 | }
|
---|
60 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
61 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
62 | }
|
---|
63 | private ValueParameter<IntValue> NumberOfIslandsParameter {
|
---|
64 | get { return (ValueParameter<IntValue>)Parameters["NumberOfIslands"]; }
|
---|
65 | }
|
---|
66 | private ValueParameter<IntValue> MigrationIntervalParameter {
|
---|
67 | get { return (ValueParameter<IntValue>)Parameters["MigrationInterval"]; }
|
---|
68 | }
|
---|
69 | private ValueParameter<PercentValue> MigrationRateParameter {
|
---|
70 | get { return (ValueParameter<PercentValue>)Parameters["MigrationRate"]; }
|
---|
71 | }
|
---|
72 | private ConstrainedValueParameter<IMigrator> MigratorParameter {
|
---|
73 | get { return (ConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
|
---|
74 | }
|
---|
75 | private ConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
|
---|
76 | get { return (ConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
|
---|
77 | }
|
---|
78 | private ConstrainedValueParameter<ISelector> ImmigrationSelectorParameter {
|
---|
79 | get { return (ConstrainedValueParameter<ISelector>)Parameters["ImmigrationSelector"]; }
|
---|
80 | }
|
---|
81 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
82 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
83 | }
|
---|
84 | private ValueParameter<IntValue> MaximumMigrationsParameter {
|
---|
85 | get { return (ValueParameter<IntValue>)Parameters["MaximumMigrations"]; }
|
---|
86 | }
|
---|
87 | private ConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
88 | get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
89 | }
|
---|
90 | private ConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
91 | get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
92 | }
|
---|
93 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
94 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
95 | }
|
---|
96 | private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
97 | get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
98 | }
|
---|
99 | private ValueParameter<IntValue> ElitesParameter {
|
---|
100 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
101 | }
|
---|
102 | private ValueParameter<BoolValue> ParallelParameter {
|
---|
103 | get { return (ValueParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
104 | }
|
---|
105 | #endregion
|
---|
106 |
|
---|
107 | #region Properties
|
---|
108 | public IntValue Seed {
|
---|
109 | get { return SeedParameter.Value; }
|
---|
110 | set { SeedParameter.Value = value; }
|
---|
111 | }
|
---|
112 | public BoolValue SetSeedRandomly {
|
---|
113 | get { return SetSeedRandomlyParameter.Value; }
|
---|
114 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
115 | }
|
---|
116 | public IntValue NumberOfIslands {
|
---|
117 | get { return NumberOfIslandsParameter.Value; }
|
---|
118 | set { NumberOfIslandsParameter.Value = value; }
|
---|
119 | }
|
---|
120 | public IntValue MigrationInterval {
|
---|
121 | get { return MigrationIntervalParameter.Value; }
|
---|
122 | set { MigrationIntervalParameter.Value = value; }
|
---|
123 | }
|
---|
124 | public PercentValue MigrationRate {
|
---|
125 | get { return MigrationRateParameter.Value; }
|
---|
126 | set { MigrationRateParameter.Value = value; }
|
---|
127 | }
|
---|
128 | public IMigrator Migrator {
|
---|
129 | get { return MigratorParameter.Value; }
|
---|
130 | set { MigratorParameter.Value = value; }
|
---|
131 | }
|
---|
132 | public ISelector EmigrantsSelector {
|
---|
133 | get { return EmigrantsSelectorParameter.Value; }
|
---|
134 | set { EmigrantsSelectorParameter.Value = value; }
|
---|
135 | }
|
---|
136 | public ISelector ImmigrationSelector {
|
---|
137 | get { return ImmigrationSelectorParameter.Value; }
|
---|
138 | set { ImmigrationSelectorParameter.Value = value; }
|
---|
139 | }
|
---|
140 | public IntValue PopulationSize {
|
---|
141 | get { return PopulationSizeParameter.Value; }
|
---|
142 | set { PopulationSizeParameter.Value = value; }
|
---|
143 | }
|
---|
144 | public IntValue MaximumMigrations {
|
---|
145 | get { return MaximumMigrationsParameter.Value; }
|
---|
146 | set { MaximumMigrationsParameter.Value = value; }
|
---|
147 | }
|
---|
148 | public ISelector Selector {
|
---|
149 | get { return SelectorParameter.Value; }
|
---|
150 | set { SelectorParameter.Value = value; }
|
---|
151 | }
|
---|
152 | public ICrossover Crossover {
|
---|
153 | get { return CrossoverParameter.Value; }
|
---|
154 | set { CrossoverParameter.Value = value; }
|
---|
155 | }
|
---|
156 | public PercentValue MutationProbability {
|
---|
157 | get { return MutationProbabilityParameter.Value; }
|
---|
158 | set { MutationProbabilityParameter.Value = value; }
|
---|
159 | }
|
---|
160 | public IManipulator Mutator {
|
---|
161 | get { return MutatorParameter.Value; }
|
---|
162 | set { MutatorParameter.Value = value; }
|
---|
163 | }
|
---|
164 | public IntValue Elites {
|
---|
165 | get { return ElitesParameter.Value; }
|
---|
166 | set { ElitesParameter.Value = value; }
|
---|
167 | }
|
---|
168 | public BoolValue Parallel {
|
---|
169 | get { return ParallelParameter.Value; }
|
---|
170 | set { ParallelParameter.Value = value; }
|
---|
171 | }
|
---|
172 | private List<ISelector> selectors;
|
---|
173 | private IEnumerable<ISelector> Selectors {
|
---|
174 | get { return selectors; }
|
---|
175 | }
|
---|
176 | private List<ISelector> emigrantsSelectors;
|
---|
177 | private List<ISelector> immigrationSelectors;
|
---|
178 | private List<IMigrator> migrators;
|
---|
179 | private RandomCreator RandomCreator {
|
---|
180 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
181 | }
|
---|
182 | private SolutionsCreator SolutionsCreator {
|
---|
183 | get { return (SolutionsCreator)((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor).Operator; }
|
---|
184 | }
|
---|
185 | private IslandGeneticAlgorithmMainLoop MainLoop {
|
---|
186 | get { return (IslandGeneticAlgorithmMainLoop)((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor).Successor; }
|
---|
187 | }
|
---|
188 | #endregion
|
---|
189 |
|
---|
190 | [StorableConstructor]
|
---|
191 | private IslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
192 | public IslandGeneticAlgorithm()
|
---|
193 | : base() {
|
---|
194 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
195 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
196 | Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
|
---|
197 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
|
---|
198 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
|
---|
199 | Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
|
---|
200 | Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
|
---|
201 | Parameters.Add(new ConstrainedValueParameter<ISelector>("ImmigrationSelector", "Selects the population from the unification of the original population and the immigrants."));
|
---|
202 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
203 | Parameters.Add(new ValueParameter<IntValue>("MaximumMigrations", "The maximum number of migrations that should occur.", new IntValue(100)));
|
---|
204 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
205 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
206 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
207 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
208 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
209 | Parameters.Add(new ValueParameter<BoolValue>("Parallel", "True if the islands should be run in parallel (also requires a parallel engine)", new BoolValue(false)));
|
---|
210 |
|
---|
211 | RandomCreator randomCreator = new RandomCreator();
|
---|
212 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
213 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
214 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
215 | IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
|
---|
216 | OperatorGraph.InitialOperator = randomCreator;
|
---|
217 |
|
---|
218 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
219 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
220 | randomCreator.SeedParameter.Value = null;
|
---|
221 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
222 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
223 | randomCreator.Successor = populationCreator;
|
---|
224 |
|
---|
225 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
226 | populationCreator.Successor = ussp1;
|
---|
227 |
|
---|
228 | ussp1.Operator = solutionsCreator;
|
---|
229 | ussp1.Successor = mainLoop;
|
---|
230 |
|
---|
231 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
232 | solutionsCreator.Successor = null;
|
---|
233 |
|
---|
234 | mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
|
---|
235 | mainLoop.ImmigrationSelectorParameter.ActualName = ImmigrationSelectorParameter.Name;
|
---|
236 | mainLoop.MaximumMigrationsParameter.ActualName = MaximumMigrationsParameter.Name;
|
---|
237 | mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
238 | mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
|
---|
239 | mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
|
---|
240 | mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
241 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
242 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
243 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
244 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
245 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
246 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
247 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
248 |
|
---|
249 | mainLoop.Successor = null;
|
---|
250 |
|
---|
251 | Initialize();
|
---|
252 | }
|
---|
253 |
|
---|
254 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
255 | IslandGeneticAlgorithm clone = (IslandGeneticAlgorithm)base.Clone(cloner);
|
---|
256 | clone.Initialize();
|
---|
257 | return clone;
|
---|
258 | }
|
---|
259 |
|
---|
260 | public override void Prepare() {
|
---|
261 | if (Problem != null) base.Prepare();
|
---|
262 | }
|
---|
263 |
|
---|
264 | #region Events
|
---|
265 | protected override void OnProblemChanged() {
|
---|
266 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
267 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
268 | ParameterizeStochasticOperator(Problem.Visualizer);
|
---|
269 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
270 | ParameterizeSolutionsCreator();
|
---|
271 | ParameterizeMainLoop();
|
---|
272 | ParameterizeSelectors();
|
---|
273 | UpdateCrossovers();
|
---|
274 | UpdateMutators();
|
---|
275 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
276 | if (Problem.Visualizer != null) Problem.Visualizer.VisualizationParameter.ActualNameChanged += new EventHandler(Visualizer_VisualizationParameter_ActualNameChanged);
|
---|
277 | base.OnProblemChanged();
|
---|
278 | }
|
---|
279 |
|
---|
280 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
281 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
282 | ParameterizeSolutionsCreator();
|
---|
283 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
284 | }
|
---|
285 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
286 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
287 | ParameterizeSolutionsCreator();
|
---|
288 | ParameterizeMainLoop();
|
---|
289 | ParameterizeSelectors();
|
---|
290 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
291 | base.Problem_EvaluatorChanged(sender, e);
|
---|
292 | }
|
---|
293 | protected override void Problem_VisualizerChanged(object sender, EventArgs e) {
|
---|
294 | ParameterizeStochasticOperator(Problem.Visualizer);
|
---|
295 | ParameterizeMainLoop();
|
---|
296 | if (Problem.Visualizer != null) Problem.Visualizer.VisualizationParameter.ActualNameChanged += new EventHandler(Visualizer_VisualizationParameter_ActualNameChanged);
|
---|
297 | base.Problem_VisualizerChanged(sender, e);
|
---|
298 | }
|
---|
299 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
300 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
301 | UpdateCrossovers();
|
---|
302 | UpdateMutators();
|
---|
303 | base.Problem_OperatorsChanged(sender, e);
|
---|
304 | }
|
---|
305 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
306 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
307 | ParameterizeSelectors();
|
---|
308 | }
|
---|
309 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
310 | ParameterizeSelectors();
|
---|
311 | }
|
---|
312 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
313 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
314 | ParameterizeSelectors();
|
---|
315 | }
|
---|
316 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
317 | ParameterizeSelectors();
|
---|
318 | }
|
---|
319 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
320 | ParameterizeMainLoop();
|
---|
321 | ParameterizeSelectors();
|
---|
322 | }
|
---|
323 | private void Visualizer_VisualizationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
324 | ParameterizeMainLoop();
|
---|
325 | }
|
---|
326 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
327 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
328 | ParameterizeSelectors();
|
---|
329 | }
|
---|
330 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
331 | ParameterizeSelectors();
|
---|
332 | }
|
---|
333 | #endregion
|
---|
334 |
|
---|
335 | #region Helpers
|
---|
336 | [StorableHook(HookType.AfterDeserialization)]
|
---|
337 | private void Initialize() {
|
---|
338 | InitializeSelectors();
|
---|
339 | UpdateSelectors();
|
---|
340 | InitializeMigrators();
|
---|
341 | UpdateMigrators();
|
---|
342 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
343 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
344 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
345 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
346 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
347 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
348 | if (Problem != null) {
|
---|
349 | UpdateCrossovers();
|
---|
350 | UpdateMutators();
|
---|
351 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
352 | if (Problem.Visualizer != null) Problem.Visualizer.VisualizationParameter.ActualNameChanged += new EventHandler(Visualizer_VisualizationParameter_ActualNameChanged);
|
---|
353 | }
|
---|
354 | }
|
---|
355 | private void ParameterizeSolutionsCreator() {
|
---|
356 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
357 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
358 | }
|
---|
359 | private void ParameterizeMainLoop() {
|
---|
360 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
361 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
362 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
363 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
364 | MainLoop.VisualizerParameter.ActualName = Problem.VisualizerParameter.Name;
|
---|
365 | if (Problem.Visualizer != null)
|
---|
366 | MainLoop.VisualizationParameter.ActualName = Problem.Visualizer.VisualizationParameter.ActualName;
|
---|
367 | }
|
---|
368 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
369 | if (op is IStochasticOperator)
|
---|
370 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
371 | }
|
---|
372 | private void InitializeSelectors() {
|
---|
373 | selectors = new List<ISelector>();
|
---|
374 | selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
375 | emigrantsSelectors = new List<ISelector>();
|
---|
376 | emigrantsSelectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
377 | immigrationSelectors = new List<ISelector>();
|
---|
378 | immigrationSelectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
379 | ParameterizeSelectors();
|
---|
380 | }
|
---|
381 | private void InitializeMigrators() {
|
---|
382 | migrators = new List<IMigrator>();
|
---|
383 | migrators.AddRange(ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name));
|
---|
384 | UpdateMigrators();
|
---|
385 | }
|
---|
386 | private void ParameterizeSelectors() {
|
---|
387 | foreach (ISelector selector in Selectors) {
|
---|
388 | selector.CopySelected = new BoolValue(true);
|
---|
389 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
390 | ParameterizeStochasticOperator(selector);
|
---|
391 | }
|
---|
392 | foreach (ISelector selector in emigrantsSelectors) {
|
---|
393 | selector.CopySelected = new BoolValue(true);
|
---|
394 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
395 | ParameterizeStochasticOperator(selector);
|
---|
396 | }
|
---|
397 | foreach (ISelector selector in immigrationSelectors) {
|
---|
398 | selector.CopySelected = new BoolValue(false);
|
---|
399 | selector.NumberOfSelectedSubScopesParameter.Value = PopulationSize;
|
---|
400 | ParameterizeStochasticOperator(selector);
|
---|
401 | }
|
---|
402 | if (Problem != null) {
|
---|
403 | foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
|
---|
404 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
405 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
406 | }
|
---|
407 | foreach (ISingleObjectiveSelector selector in emigrantsSelectors.OfType<ISingleObjectiveSelector>()) {
|
---|
408 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
409 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
410 | }
|
---|
411 | foreach (ISingleObjectiveSelector selector in immigrationSelectors.OfType<ISingleObjectiveSelector>()) {
|
---|
412 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
413 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 | private void UpdateSelectors() {
|
---|
418 | ISelector oldSelector = SelectorParameter.Value;
|
---|
419 | SelectorParameter.ValidValues.Clear();
|
---|
420 | foreach (ISelector selector in Selectors.OrderBy(x => x.Name))
|
---|
421 | SelectorParameter.ValidValues.Add(selector);
|
---|
422 |
|
---|
423 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
424 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
425 |
|
---|
426 | if (oldSelector != null) {
|
---|
427 | ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
428 | if (selector != null) SelectorParameter.Value = selector;
|
---|
429 | }
|
---|
430 |
|
---|
431 | oldSelector = EmigrantsSelector;
|
---|
432 | EmigrantsSelectorParameter.ValidValues.Clear();
|
---|
433 | foreach (ISelector selector in emigrantsSelectors)
|
---|
434 | EmigrantsSelectorParameter.ValidValues.Add(selector);
|
---|
435 | if (oldSelector != null) {
|
---|
436 | ISelector selector = EmigrantsSelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
437 | if (selector != null) EmigrantsSelectorParameter.Value = selector;
|
---|
438 | }
|
---|
439 |
|
---|
440 | oldSelector = ImmigrationSelectorParameter.Value;
|
---|
441 | ImmigrationSelectorParameter.ValidValues.Clear();
|
---|
442 | foreach (ISelector selector in immigrationSelectors)
|
---|
443 | ImmigrationSelectorParameter.ValidValues.Add(selector);
|
---|
444 | if (oldSelector != null) {
|
---|
445 | ISelector selector = ImmigrationSelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
446 | if (selector != null) ImmigrationSelectorParameter.Value = selector;
|
---|
447 | }
|
---|
448 | }
|
---|
449 | private void UpdateMigrators() {
|
---|
450 | IMigrator oldMigrator = Migrator;
|
---|
451 | MigratorParameter.ValidValues.Clear();
|
---|
452 | foreach (IMigrator migrator in migrators)
|
---|
453 | MigratorParameter.ValidValues.Add(migrator);
|
---|
454 | if (oldMigrator != null) {
|
---|
455 | IMigrator migrator = MigratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMigrator.GetType());
|
---|
456 | if (migrator != null) MigratorParameter.Value = migrator;
|
---|
457 | } else if (MigratorParameter.ValidValues.Count > 0) MigratorParameter.Value = MigratorParameter.ValidValues.First();
|
---|
458 | }
|
---|
459 | private void UpdateCrossovers() {
|
---|
460 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
461 | CrossoverParameter.ValidValues.Clear();
|
---|
462 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
463 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
464 | if (oldCrossover != null) {
|
---|
465 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
466 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
467 | }
|
---|
468 | }
|
---|
469 | private void UpdateMutators() {
|
---|
470 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
471 | MutatorParameter.ValidValues.Clear();
|
---|
472 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
473 | MutatorParameter.ValidValues.Add(mutator);
|
---|
474 | if (oldMutator != null) {
|
---|
475 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
476 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
477 | }
|
---|
478 | }
|
---|
479 | #endregion
|
---|
480 | }
|
---|
481 | }
|
---|