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.Analysis;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Operators;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 | using HeuristicLab.Random;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
38 | /// <summary>
|
---|
39 | /// An offspring selection island genetic algorithm.
|
---|
40 | /// </summary>
|
---|
41 | [Item("Island Offspring Selection Genetic Algorithm", "An island offspring selection genetic algorithm.")]
|
---|
42 | [Creatable("Algorithms")]
|
---|
43 | [StorableClass]
|
---|
44 | public sealed class IslandOffspringSelectionGeneticAlgorithm : 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<IReplacer> ImmigrationReplacerParameter {
|
---|
79 | get { return (ConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
|
---|
80 | }
|
---|
81 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
82 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
83 | }
|
---|
84 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
85 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
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 | private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
106 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
107 | }
|
---|
108 | private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
|
---|
109 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
|
---|
110 | }
|
---|
111 | private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
|
---|
112 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
|
---|
113 | }
|
---|
114 | private OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
|
---|
115 | get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
|
---|
116 | }
|
---|
117 | private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
118 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
119 | }
|
---|
120 | private ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
121 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
122 | }
|
---|
123 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
124 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
125 | }
|
---|
126 | private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
|
---|
127 | get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region Properties
|
---|
132 | public IntValue Seed {
|
---|
133 | get { return SeedParameter.Value; }
|
---|
134 | set { SeedParameter.Value = value; }
|
---|
135 | }
|
---|
136 | public BoolValue SetSeedRandomly {
|
---|
137 | get { return SetSeedRandomlyParameter.Value; }
|
---|
138 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
139 | }
|
---|
140 | public IntValue NumberOfIslands {
|
---|
141 | get { return NumberOfIslandsParameter.Value; }
|
---|
142 | set { NumberOfIslandsParameter.Value = value; }
|
---|
143 | }
|
---|
144 | public IntValue MigrationInterval {
|
---|
145 | get { return MigrationIntervalParameter.Value; }
|
---|
146 | set { MigrationIntervalParameter.Value = value; }
|
---|
147 | }
|
---|
148 | public PercentValue MigrationRate {
|
---|
149 | get { return MigrationRateParameter.Value; }
|
---|
150 | set { MigrationRateParameter.Value = value; }
|
---|
151 | }
|
---|
152 | public IMigrator Migrator {
|
---|
153 | get { return MigratorParameter.Value; }
|
---|
154 | set { MigratorParameter.Value = value; }
|
---|
155 | }
|
---|
156 | public ISelector EmigrantsSelector {
|
---|
157 | get { return EmigrantsSelectorParameter.Value; }
|
---|
158 | set { EmigrantsSelectorParameter.Value = value; }
|
---|
159 | }
|
---|
160 | public IReplacer ImmigrationReplacer {
|
---|
161 | get { return ImmigrationReplacerParameter.Value; }
|
---|
162 | set { ImmigrationReplacerParameter.Value = value; }
|
---|
163 | }
|
---|
164 | public IntValue PopulationSize {
|
---|
165 | get { return PopulationSizeParameter.Value; }
|
---|
166 | set { PopulationSizeParameter.Value = value; }
|
---|
167 | }
|
---|
168 | public IntValue MaximumGenerations {
|
---|
169 | get { return MaximumGenerationsParameter.Value; }
|
---|
170 | set { MaximumGenerationsParameter.Value = value; }
|
---|
171 | }
|
---|
172 | public ISelector Selector {
|
---|
173 | get { return SelectorParameter.Value; }
|
---|
174 | set { SelectorParameter.Value = value; }
|
---|
175 | }
|
---|
176 | public ICrossover Crossover {
|
---|
177 | get { return CrossoverParameter.Value; }
|
---|
178 | set { CrossoverParameter.Value = value; }
|
---|
179 | }
|
---|
180 | public PercentValue MutationProbability {
|
---|
181 | get { return MutationProbabilityParameter.Value; }
|
---|
182 | set { MutationProbabilityParameter.Value = value; }
|
---|
183 | }
|
---|
184 | public IManipulator Mutator {
|
---|
185 | get { return MutatorParameter.Value; }
|
---|
186 | set { MutatorParameter.Value = value; }
|
---|
187 | }
|
---|
188 | public IntValue Elites {
|
---|
189 | get { return ElitesParameter.Value; }
|
---|
190 | set { ElitesParameter.Value = value; }
|
---|
191 | }
|
---|
192 | public BoolValue Parallel {
|
---|
193 | get { return ParallelParameter.Value; }
|
---|
194 | set { ParallelParameter.Value = value; }
|
---|
195 | }
|
---|
196 | private DoubleValue SuccessRatio {
|
---|
197 | get { return SuccessRatioParameter.Value; }
|
---|
198 | set { SuccessRatioParameter.Value = value; }
|
---|
199 | }
|
---|
200 | private DoubleValue ComparisonFactorLowerBound {
|
---|
201 | get { return ComparisonFactorLowerBoundParameter.Value; }
|
---|
202 | set { ComparisonFactorLowerBoundParameter.Value = value; }
|
---|
203 | }
|
---|
204 | private DoubleValue ComparisonFactorUpperBound {
|
---|
205 | get { return ComparisonFactorUpperBoundParameter.Value; }
|
---|
206 | set { ComparisonFactorUpperBoundParameter.Value = value; }
|
---|
207 | }
|
---|
208 | private IDiscreteDoubleValueModifier ComparisonFactorModifier {
|
---|
209 | get { return ComparisonFactorModifierParameter.Value; }
|
---|
210 | set { ComparisonFactorModifierParameter.Value = value; }
|
---|
211 | }
|
---|
212 | private DoubleValue MaximumSelectionPressure {
|
---|
213 | get { return MaximumSelectionPressureParameter.Value; }
|
---|
214 | set { MaximumSelectionPressureParameter.Value = value; }
|
---|
215 | }
|
---|
216 | private BoolValue OffspringSelectionBeforeMutation {
|
---|
217 | get { return OffspringSelectionBeforeMutationParameter.Value; }
|
---|
218 | set { OffspringSelectionBeforeMutationParameter.Value = value; }
|
---|
219 | }
|
---|
220 | public MultiAnalyzer Analyzer {
|
---|
221 | get { return AnalyzerParameter.Value; }
|
---|
222 | set { AnalyzerParameter.Value = value; }
|
---|
223 | }
|
---|
224 | public MultiAnalyzer IslandAnalyzer {
|
---|
225 | get { return IslandAnalyzerParameter.Value; }
|
---|
226 | set { IslandAnalyzerParameter.Value = value; }
|
---|
227 | }
|
---|
228 | private List<ISelector> selectors;
|
---|
229 | private IEnumerable<ISelector> Selectors {
|
---|
230 | get { return selectors; }
|
---|
231 | }
|
---|
232 | private List<IDiscreteDoubleValueModifier> comparisonFactorModifiers;
|
---|
233 | private List<ISelector> emigrantsSelectors;
|
---|
234 | private List<IReplacer> immigrationReplacers;
|
---|
235 | private List<IMigrator> migrators;
|
---|
236 | private RandomCreator RandomCreator {
|
---|
237 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
238 | }
|
---|
239 | private UniformSubScopesProcessor IslandProcessor {
|
---|
240 | get { return ((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor); }
|
---|
241 | }
|
---|
242 | private SolutionsCreator SolutionsCreator {
|
---|
243 | get { return (SolutionsCreator)IslandProcessor.Operator; }
|
---|
244 | }
|
---|
245 | private IslandOffspringSelectionGeneticAlgorithmMainLoop MainLoop {
|
---|
246 | get { return (IslandOffspringSelectionGeneticAlgorithmMainLoop)IslandProcessor.Successor; }
|
---|
247 | }
|
---|
248 | private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
|
---|
249 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
250 | private ValueAnalyzer islandSelectionPressureAnalyzer;
|
---|
251 | private ValueAnalyzer selectionPressureAnalyzer;
|
---|
252 | #endregion
|
---|
253 |
|
---|
254 | [StorableConstructor]
|
---|
255 | private IslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
256 | public IslandOffspringSelectionGeneticAlgorithm()
|
---|
257 | : base() {
|
---|
258 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
259 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
260 | Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
|
---|
261 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
|
---|
262 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
|
---|
263 | Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
|
---|
264 | Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
|
---|
265 | Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
|
---|
266 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions of each island.", new IntValue(100)));
|
---|
267 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(100)));
|
---|
268 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
269 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
270 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
271 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
272 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
273 | Parameters.Add(new ValueParameter<BoolValue>("Parallel", "True if the islands should be run in parallel (also requires a parallel engine)", new BoolValue(false)));
|
---|
274 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
|
---|
275 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
|
---|
276 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
|
---|
277 | Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
|
---|
278 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
|
---|
279 | Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation.", new BoolValue(false)));
|
---|
280 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
281 | Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
|
---|
282 |
|
---|
283 | RandomCreator randomCreator = new RandomCreator();
|
---|
284 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
285 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
286 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
287 | IslandOffspringSelectionGeneticAlgorithmMainLoop mainLoop = new IslandOffspringSelectionGeneticAlgorithmMainLoop();
|
---|
288 | OperatorGraph.InitialOperator = randomCreator;
|
---|
289 |
|
---|
290 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
291 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
292 | randomCreator.SeedParameter.Value = null;
|
---|
293 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
294 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
295 | randomCreator.Successor = populationCreator;
|
---|
296 |
|
---|
297 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
298 | populationCreator.Successor = ussp1;
|
---|
299 |
|
---|
300 | ussp1.Parallel = null;
|
---|
301 | ussp1.ParallelParameter.ActualName = ParallelParameter.Name;
|
---|
302 | ussp1.Operator = solutionsCreator;
|
---|
303 | ussp1.Successor = mainLoop;
|
---|
304 |
|
---|
305 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
306 | solutionsCreator.Successor = null;
|
---|
307 |
|
---|
308 | mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
|
---|
309 | mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
|
---|
310 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
311 | mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
312 | mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
|
---|
313 | mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
|
---|
314 | mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
315 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
316 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
317 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
318 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
319 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
320 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
321 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
322 | mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
323 | mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
|
---|
324 | mainLoop.ComparisonFactorLowerBoundParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
325 | mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
326 | mainLoop.ComparisonFactorUpperBoundParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
|
---|
327 | mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
328 | mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
329 |
|
---|
330 | mainLoop.Successor = null;
|
---|
331 |
|
---|
332 | Initialize();
|
---|
333 | }
|
---|
334 |
|
---|
335 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
336 | IslandOffspringSelectionGeneticAlgorithm clone = (IslandOffspringSelectionGeneticAlgorithm)base.Clone(cloner);
|
---|
337 | clone.Initialize();
|
---|
338 | return clone;
|
---|
339 | }
|
---|
340 |
|
---|
341 | public override void Prepare() {
|
---|
342 | if (Problem != null) base.Prepare();
|
---|
343 | }
|
---|
344 |
|
---|
345 | #region Events
|
---|
346 | protected override void OnProblemChanged() {
|
---|
347 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
348 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
349 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
350 | ParameterizeSolutionsCreator();
|
---|
351 | ParameterizeMainLoop();
|
---|
352 | ParameterizeSelectors();
|
---|
353 | ParameterizeAnalyzers();
|
---|
354 | UpdateCrossovers();
|
---|
355 | UpdateMutators();
|
---|
356 | UpdateAnalyzers();
|
---|
357 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
358 | base.OnProblemChanged();
|
---|
359 | }
|
---|
360 |
|
---|
361 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
362 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
363 | ParameterizeSolutionsCreator();
|
---|
364 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
365 | }
|
---|
366 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
367 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
368 | ParameterizeSolutionsCreator();
|
---|
369 | ParameterizeMainLoop();
|
---|
370 | ParameterizeSelectors();
|
---|
371 | ParameterizeAnalyzers();
|
---|
372 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
373 | base.Problem_EvaluatorChanged(sender, e);
|
---|
374 | }
|
---|
375 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
376 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
377 | UpdateCrossovers();
|
---|
378 | UpdateMutators();
|
---|
379 | UpdateAnalyzers();
|
---|
380 | base.Problem_OperatorsChanged(sender, e);
|
---|
381 | }
|
---|
382 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
383 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
384 | ParameterizeSelectors();
|
---|
385 | }
|
---|
386 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
387 | ParameterizeSelectors();
|
---|
388 | }
|
---|
389 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
390 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
391 | ParameterizeSelectors();
|
---|
392 | }
|
---|
393 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
394 | ParameterizeSelectors();
|
---|
395 | }
|
---|
396 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
397 | ParameterizeMainLoop();
|
---|
398 | ParameterizeSelectors();
|
---|
399 | ParameterizeAnalyzers();
|
---|
400 | }
|
---|
401 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
402 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
403 | ParameterizeSelectors();
|
---|
404 | }
|
---|
405 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
406 | ParameterizeSelectors();
|
---|
407 | }
|
---|
408 | private void MaximumMigrationsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
409 | MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
|
---|
410 | ParameterizeComparisonFactorModifiers();
|
---|
411 | }
|
---|
412 | private void MaximumMigrations_ValueChanged(object sender, EventArgs e) {
|
---|
413 | ParameterizeComparisonFactorModifiers();
|
---|
414 | }
|
---|
415 | private void MigrationIntervalParameter_ValueChanged(object sender, EventArgs e) {
|
---|
416 | MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
|
---|
417 | ParameterizeComparisonFactorModifiers();
|
---|
418 | }
|
---|
419 | private void MigrationInterval_ValueChanged(object sender, EventArgs e) {
|
---|
420 | ParameterizeComparisonFactorModifiers();
|
---|
421 | }
|
---|
422 | #endregion
|
---|
423 |
|
---|
424 | #region Helpers
|
---|
425 | [StorableHook(HookType.AfterDeserialization)]
|
---|
426 | private void Initialize() {
|
---|
427 | InitializeSelectors();
|
---|
428 | InitializeAnalyzers();
|
---|
429 | UpdateSelectors();
|
---|
430 | UpdateAnalyzers();
|
---|
431 | InitializeComparisonFactorModifiers();
|
---|
432 | UpdateComparisonFactorModifiers();
|
---|
433 | InitializeMigrators();
|
---|
434 | UpdateMigrators();
|
---|
435 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
436 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
437 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
438 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
439 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
440 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
441 | MigrationIntervalParameter.ValueChanged += new EventHandler(MigrationIntervalParameter_ValueChanged);
|
---|
442 | MigrationInterval.ValueChanged += new EventHandler(MigrationInterval_ValueChanged);
|
---|
443 | MaximumGenerationsParameter.ValueChanged += new EventHandler(MaximumMigrationsParameter_ValueChanged);
|
---|
444 | MaximumGenerations.ValueChanged += new EventHandler(MaximumMigrations_ValueChanged);
|
---|
445 | if (Problem != null) {
|
---|
446 | UpdateCrossovers();
|
---|
447 | UpdateMutators();
|
---|
448 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
449 | }
|
---|
450 | }
|
---|
451 | private void ParameterizeSolutionsCreator() {
|
---|
452 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
453 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
454 | }
|
---|
455 | private void ParameterizeMainLoop() {
|
---|
456 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
457 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
458 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
459 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
460 | }
|
---|
461 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
462 | if (op is IStochasticOperator)
|
---|
463 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
464 | }
|
---|
465 | private void InitializeSelectors() {
|
---|
466 | selectors = new List<ISelector>();
|
---|
467 | selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
468 | emigrantsSelectors = new List<ISelector>();
|
---|
469 | emigrantsSelectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
470 | immigrationReplacers = new List<IReplacer>();
|
---|
471 | immigrationReplacers.AddRange(ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name));
|
---|
472 | ParameterizeSelectors();
|
---|
473 | }
|
---|
474 | private void InitializeAnalyzers() {
|
---|
475 | islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
476 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
477 | islandSelectionPressureAnalyzer = new ValueAnalyzer();
|
---|
478 | selectionPressureAnalyzer = new ValueAnalyzer();
|
---|
479 | ParameterizeAnalyzers();
|
---|
480 | }
|
---|
481 | private void InitializeComparisonFactorModifiers() {
|
---|
482 | comparisonFactorModifiers = new List<IDiscreteDoubleValueModifier>();
|
---|
483 | comparisonFactorModifiers.AddRange(ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name));
|
---|
484 | ParameterizeComparisonFactorModifiers();
|
---|
485 | }
|
---|
486 | private void InitializeMigrators() {
|
---|
487 | migrators = new List<IMigrator>();
|
---|
488 | migrators.AddRange(ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name));
|
---|
489 | UpdateMigrators();
|
---|
490 | }
|
---|
491 | private void ParameterizeSelectors() {
|
---|
492 | foreach (ISelector selector in Selectors) {
|
---|
493 | selector.CopySelected = new BoolValue(true);
|
---|
494 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
495 | ParameterizeStochasticOperator(selector);
|
---|
496 | }
|
---|
497 | foreach (ISelector selector in emigrantsSelectors) {
|
---|
498 | selector.CopySelected = new BoolValue(true);
|
---|
499 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
500 | ParameterizeStochasticOperator(selector);
|
---|
501 | }
|
---|
502 | foreach (IReplacer selector in immigrationReplacers) {
|
---|
503 | ParameterizeStochasticOperator(selector);
|
---|
504 | }
|
---|
505 | if (Problem != null) {
|
---|
506 | foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
|
---|
507 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
508 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
509 | }
|
---|
510 | foreach (ISingleObjectiveSelector selector in emigrantsSelectors.OfType<ISingleObjectiveSelector>()) {
|
---|
511 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
512 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
513 | }
|
---|
514 | foreach (ISingleObjectiveReplacer replacer in immigrationReplacers.OfType<ISingleObjectiveReplacer>()) {
|
---|
515 | replacer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
516 | replacer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
517 | }
|
---|
518 | }
|
---|
519 | }
|
---|
520 | private void ParameterizeAnalyzers() {
|
---|
521 | islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
522 | islandQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
523 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
524 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
525 |
|
---|
526 | islandSelectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
527 | islandSelectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
|
---|
528 | islandSelectionPressureAnalyzer.ValueParameter.Depth = 0;
|
---|
529 | islandSelectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
|
---|
530 | islandSelectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
|
---|
531 |
|
---|
532 | selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
533 | selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
|
---|
534 | selectionPressureAnalyzer.ValueParameter.Depth = 1;
|
---|
535 | selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
|
---|
536 | selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
|
---|
537 |
|
---|
538 | if (Problem != null) {
|
---|
539 | islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
540 | islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
541 | islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
542 |
|
---|
543 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
544 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
545 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
546 | }
|
---|
547 | }
|
---|
548 | private void ParameterizeComparisonFactorModifiers() {
|
---|
549 | foreach (IDiscreteDoubleValueModifier modifier in comparisonFactorModifiers) {
|
---|
550 | modifier.IndexParameter.ActualName = "Generations";
|
---|
551 | modifier.EndIndexParameter.Value = new IntValue(MigrationInterval.Value * MaximumGenerations.Value);
|
---|
552 | modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
|
---|
553 | modifier.StartIndexParameter.Value = new IntValue(0);
|
---|
554 | modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
555 | modifier.ValueParameter.ActualName = "ComparisonFactor";
|
---|
556 | }
|
---|
557 | }
|
---|
558 | private void UpdateSelectors() {
|
---|
559 | ISelector oldSelector = SelectorParameter.Value;
|
---|
560 | SelectorParameter.ValidValues.Clear();
|
---|
561 | foreach (ISelector selector in Selectors.OrderBy(x => x.Name))
|
---|
562 | SelectorParameter.ValidValues.Add(selector);
|
---|
563 |
|
---|
564 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
565 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
566 |
|
---|
567 | if (oldSelector != null) {
|
---|
568 | ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
569 | if (selector != null) SelectorParameter.Value = selector;
|
---|
570 | }
|
---|
571 |
|
---|
572 | oldSelector = EmigrantsSelector;
|
---|
573 | EmigrantsSelectorParameter.ValidValues.Clear();
|
---|
574 | foreach (ISelector selector in emigrantsSelectors)
|
---|
575 | EmigrantsSelectorParameter.ValidValues.Add(selector);
|
---|
576 | if (oldSelector != null) {
|
---|
577 | ISelector selector = EmigrantsSelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
578 | if (selector != null) EmigrantsSelectorParameter.Value = selector;
|
---|
579 | }
|
---|
580 |
|
---|
581 | IReplacer oldReplacer = ImmigrationReplacerParameter.Value;
|
---|
582 | ImmigrationReplacerParameter.ValidValues.Clear();
|
---|
583 | foreach (IReplacer replacer in immigrationReplacers)
|
---|
584 | ImmigrationReplacerParameter.ValidValues.Add(replacer);
|
---|
585 | if (oldSelector != null) {
|
---|
586 | IReplacer replacer = ImmigrationReplacerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
587 | if (replacer != null) ImmigrationReplacerParameter.Value = replacer;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | private void UpdateComparisonFactorModifiers() {
|
---|
591 | IDiscreteDoubleValueModifier oldModifier = ComparisonFactorModifier;
|
---|
592 |
|
---|
593 | ComparisonFactorModifierParameter.ValidValues.Clear();
|
---|
594 | foreach (IDiscreteDoubleValueModifier modifier in comparisonFactorModifiers)
|
---|
595 | ComparisonFactorModifierParameter.ValidValues.Add(modifier);
|
---|
596 |
|
---|
597 | if (oldModifier != null) {
|
---|
598 | IDiscreteDoubleValueModifier mod = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldModifier.GetType());
|
---|
599 | if (mod != null) ComparisonFactorModifierParameter.Value = mod;
|
---|
600 | }
|
---|
601 | }
|
---|
602 | private void UpdateMigrators() {
|
---|
603 | IMigrator oldMigrator = Migrator;
|
---|
604 | MigratorParameter.ValidValues.Clear();
|
---|
605 | foreach (IMigrator migrator in migrators)
|
---|
606 | MigratorParameter.ValidValues.Add(migrator);
|
---|
607 | if (oldMigrator != null) {
|
---|
608 | IMigrator migrator = MigratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMigrator.GetType());
|
---|
609 | if (migrator != null) MigratorParameter.Value = migrator;
|
---|
610 | } else if (MigratorParameter.ValidValues.Count > 0) MigratorParameter.Value = MigratorParameter.ValidValues.First();
|
---|
611 | }
|
---|
612 | private void UpdateCrossovers() {
|
---|
613 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
614 | CrossoverParameter.ValidValues.Clear();
|
---|
615 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
616 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
617 | if (oldCrossover != null) {
|
---|
618 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
619 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
620 | }
|
---|
621 | }
|
---|
622 | private void UpdateMutators() {
|
---|
623 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
624 | MutatorParameter.ValidValues.Clear();
|
---|
625 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
626 | MutatorParameter.ValidValues.Add(mutator);
|
---|
627 | if (oldMutator != null) {
|
---|
628 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
629 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
630 | }
|
---|
631 | }
|
---|
632 | private void UpdateAnalyzers() {
|
---|
633 | IslandAnalyzer.Operators.Clear();
|
---|
634 | Analyzer.Operators.Clear();
|
---|
635 | IslandAnalyzer.Operators.Add(islandQualityAnalyzer);
|
---|
636 | IslandAnalyzer.Operators.Add(islandSelectionPressureAnalyzer);
|
---|
637 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
638 | Analyzer.Operators.Add(selectionPressureAnalyzer);
|
---|
639 | if (Problem != null) {
|
---|
640 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {
|
---|
641 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
642 | param.Depth = 2;
|
---|
643 | Analyzer.Operators.Add(analyzer);
|
---|
644 | }
|
---|
645 | }
|
---|
646 | }
|
---|
647 | #endregion
|
---|
648 | }
|
---|
649 | }
|
---|