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 | using HeuristicLab.Selection;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
39 | /// <summary>
|
---|
40 | /// The self-adaptive segregative genetic algorithm with simulated annealing aspects (Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press).
|
---|
41 | /// </summary>
|
---|
42 | [Item("SASEGASA", "The self-adaptive segregative genetic algorithm with simulated annealing aspects (Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press).")]
|
---|
43 | [Creatable("Algorithms")]
|
---|
44 | [StorableClass]
|
---|
45 | public sealed class SASEGASA : EngineAlgorithm {
|
---|
46 |
|
---|
47 | #region Problem Properties
|
---|
48 | public override Type ProblemType {
|
---|
49 | get { return typeof(ISingleObjectiveProblem); }
|
---|
50 | }
|
---|
51 | public new ISingleObjectiveProblem Problem {
|
---|
52 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
53 | set { base.Problem = value; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Parameter Properties
|
---|
58 | private ValueParameter<IntValue> SeedParameter {
|
---|
59 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
60 | }
|
---|
61 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
62 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
63 | }
|
---|
64 | private ValueParameter<IntValue> NumberOfVillagesParameter {
|
---|
65 | get { return (ValueParameter<IntValue>)Parameters["NumberOfVillages"]; }
|
---|
66 | }
|
---|
67 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
68 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
69 | }
|
---|
70 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
71 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
72 | }
|
---|
73 | private ConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
74 | get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
75 | }
|
---|
76 | private ConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
77 | get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
78 | }
|
---|
79 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
80 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
81 | }
|
---|
82 | private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
83 | get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
84 | }
|
---|
85 | private ValueParameter<IntValue> ElitesParameter {
|
---|
86 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
87 | }
|
---|
88 | private ValueParameter<BoolValue> ParallelParameter {
|
---|
89 | get { return (ValueParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
90 | }
|
---|
91 | private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
92 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
93 | }
|
---|
94 | private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
|
---|
95 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
|
---|
96 | }
|
---|
97 | private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
|
---|
98 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
|
---|
99 | }
|
---|
100 | private OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
|
---|
101 | get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
|
---|
102 | }
|
---|
103 | private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
104 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
105 | }
|
---|
106 | private ValueLookupParameter<DoubleValue> FinalMaximumSelectionPressureParameter {
|
---|
107 | get { return (ValueLookupParameter<DoubleValue>)Parameters["FinalMaximumSelectionPressure"]; }
|
---|
108 | }
|
---|
109 | private ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
110 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
111 | }
|
---|
112 | private ValueLookupParameter<IntValue> SelectedParentsParameter {
|
---|
113 | get { return (ValueLookupParameter<IntValue>)Parameters["SelectedParents"]; }
|
---|
114 | }
|
---|
115 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
116 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
117 | }
|
---|
118 | private ValueParameter<MultiAnalyzer> VillageAnalyzerParameter {
|
---|
119 | get { return (ValueParameter<MultiAnalyzer>)Parameters["VillageAnalyzer"]; }
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 |
|
---|
123 | #region Properties
|
---|
124 | public IntValue Seed {
|
---|
125 | get { return SeedParameter.Value; }
|
---|
126 | set { SeedParameter.Value = value; }
|
---|
127 | }
|
---|
128 | public BoolValue SetSeedRandomly {
|
---|
129 | get { return SetSeedRandomlyParameter.Value; }
|
---|
130 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
131 | }
|
---|
132 | public IntValue NumberOfVillages {
|
---|
133 | get { return NumberOfVillagesParameter.Value; }
|
---|
134 | set { NumberOfVillagesParameter.Value = value; }
|
---|
135 | }
|
---|
136 | public IntValue PopulationSize {
|
---|
137 | get { return PopulationSizeParameter.Value; }
|
---|
138 | set { PopulationSizeParameter.Value = value; }
|
---|
139 | }
|
---|
140 | public IntValue MaximumGenerations {
|
---|
141 | get { return MaximumGenerationsParameter.Value; }
|
---|
142 | set { MaximumGenerationsParameter.Value = value; }
|
---|
143 | }
|
---|
144 | public ISelector Selector {
|
---|
145 | get { return SelectorParameter.Value; }
|
---|
146 | set { SelectorParameter.Value = value; }
|
---|
147 | }
|
---|
148 | public ICrossover Crossover {
|
---|
149 | get { return CrossoverParameter.Value; }
|
---|
150 | set { CrossoverParameter.Value = value; }
|
---|
151 | }
|
---|
152 | public PercentValue MutationProbability {
|
---|
153 | get { return MutationProbabilityParameter.Value; }
|
---|
154 | set { MutationProbabilityParameter.Value = value; }
|
---|
155 | }
|
---|
156 | public IManipulator Mutator {
|
---|
157 | get { return MutatorParameter.Value; }
|
---|
158 | set { MutatorParameter.Value = value; }
|
---|
159 | }
|
---|
160 | public IntValue Elites {
|
---|
161 | get { return ElitesParameter.Value; }
|
---|
162 | set { ElitesParameter.Value = value; }
|
---|
163 | }
|
---|
164 | public BoolValue Parallel {
|
---|
165 | get { return ParallelParameter.Value; }
|
---|
166 | set { ParallelParameter.Value = value; }
|
---|
167 | }
|
---|
168 | public DoubleValue SuccessRatio {
|
---|
169 | get { return SuccessRatioParameter.Value; }
|
---|
170 | set { SuccessRatioParameter.Value = value; }
|
---|
171 | }
|
---|
172 | public DoubleValue ComparisonFactorLowerBound {
|
---|
173 | get { return ComparisonFactorLowerBoundParameter.Value; }
|
---|
174 | set { ComparisonFactorLowerBoundParameter.Value = value; }
|
---|
175 | }
|
---|
176 | public DoubleValue ComparisonFactorUpperBound {
|
---|
177 | get { return ComparisonFactorUpperBoundParameter.Value; }
|
---|
178 | set { ComparisonFactorUpperBoundParameter.Value = value; }
|
---|
179 | }
|
---|
180 | public IDiscreteDoubleValueModifier ComparisonFactorModifier {
|
---|
181 | get { return ComparisonFactorModifierParameter.Value; }
|
---|
182 | set { ComparisonFactorModifierParameter.Value = value; }
|
---|
183 | }
|
---|
184 | public DoubleValue MaximumSelectionPressure {
|
---|
185 | get { return MaximumSelectionPressureParameter.Value; }
|
---|
186 | set { MaximumSelectionPressureParameter.Value = value; }
|
---|
187 | }
|
---|
188 | public DoubleValue FinalMaximumSelectionPressure {
|
---|
189 | get { return FinalMaximumSelectionPressureParameter.Value; }
|
---|
190 | set { FinalMaximumSelectionPressureParameter.Value = value; }
|
---|
191 | }
|
---|
192 | public BoolValue OffspringSelectionBeforeMutation {
|
---|
193 | get { return OffspringSelectionBeforeMutationParameter.Value; }
|
---|
194 | set { OffspringSelectionBeforeMutationParameter.Value = value; }
|
---|
195 | }
|
---|
196 | public IntValue SelectedParents {
|
---|
197 | get { return SelectedParentsParameter.Value; }
|
---|
198 | set { SelectedParentsParameter.Value = value; }
|
---|
199 | }
|
---|
200 | public MultiAnalyzer Analyzer {
|
---|
201 | get { return AnalyzerParameter.Value; }
|
---|
202 | set { AnalyzerParameter.Value = value; }
|
---|
203 | }
|
---|
204 | public MultiAnalyzer VillageAnalyzer {
|
---|
205 | get { return VillageAnalyzerParameter.Value; }
|
---|
206 | set { VillageAnalyzerParameter.Value = value; }
|
---|
207 | }
|
---|
208 | private RandomCreator RandomCreator {
|
---|
209 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
210 | }
|
---|
211 | private UniformSubScopesProcessor VillageProcessor {
|
---|
212 | get { return ((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor); }
|
---|
213 | }
|
---|
214 | private SolutionsCreator SolutionsCreator {
|
---|
215 | get { return (SolutionsCreator)VillageProcessor.Operator; }
|
---|
216 | }
|
---|
217 | private SASEGASAMainLoop MainLoop {
|
---|
218 | get { return (SASEGASAMainLoop)VillageProcessor.Successor; }
|
---|
219 | }
|
---|
220 | [Storable]
|
---|
221 | private BestAverageWorstQualityAnalyzer villageQualityAnalyzer;
|
---|
222 | [Storable]
|
---|
223 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
224 | [Storable]
|
---|
225 | private ValueAnalyzer villageSelectionPressureAnalyzer;
|
---|
226 | [Storable]
|
---|
227 | private ValueAnalyzer selectionPressureAnalyzer;
|
---|
228 | #endregion
|
---|
229 |
|
---|
230 | [StorableConstructor]
|
---|
231 | private SASEGASA(bool deserializing) : base(deserializing) { }
|
---|
232 | public SASEGASA()
|
---|
233 | : base() {
|
---|
234 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
235 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
236 | Parameters.Add(new ValueParameter<IntValue>("NumberOfVillages", "The initial number of villages.", new IntValue(10)));
|
---|
237 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
238 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
239 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
240 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
241 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
242 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
243 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
244 | Parameters.Add(new ValueParameter<BoolValue>("Parallel", "True if the villages should be run in parallel (also requires a parallel engine)", new BoolValue(false)));
|
---|
245 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
|
---|
246 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0.3)));
|
---|
247 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(0.7)));
|
---|
248 | Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
|
---|
249 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
|
---|
250 | Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaximumSelectionPressure", "The maximum selection pressure used when there is only one village left.", new DoubleValue(100)));
|
---|
251 | 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)));
|
---|
252 | Parameters.Add(new ValueLookupParameter<IntValue>("SelectedParents", "Should be about 2 * PopulationSize, for large problems use a smaller value to decrease memory footprint.", new IntValue(200)));
|
---|
253 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the villages.", new MultiAnalyzer()));
|
---|
254 | Parameters.Add(new ValueParameter<MultiAnalyzer>("VillageAnalyzer", "The operator used to analyze each village.", new MultiAnalyzer()));
|
---|
255 |
|
---|
256 | RandomCreator randomCreator = new RandomCreator();
|
---|
257 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
258 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
259 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
260 | SASEGASAMainLoop mainLoop = new SASEGASAMainLoop();
|
---|
261 | OperatorGraph.InitialOperator = randomCreator;
|
---|
262 |
|
---|
263 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
264 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
265 | randomCreator.SeedParameter.Value = null;
|
---|
266 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
267 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
268 | randomCreator.Successor = populationCreator;
|
---|
269 |
|
---|
270 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfVillagesParameter.Name;
|
---|
271 | populationCreator.Successor = ussp1;
|
---|
272 |
|
---|
273 | ussp1.Parallel = null;
|
---|
274 | ussp1.ParallelParameter.ActualName = ParallelParameter.Name;
|
---|
275 | ussp1.Operator = solutionsCreator;
|
---|
276 | ussp1.Successor = mainLoop;
|
---|
277 |
|
---|
278 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
279 | solutionsCreator.Successor = null;
|
---|
280 |
|
---|
281 | mainLoop.NumberOfVillagesParameter.ActualName = NumberOfVillagesParameter.Name;
|
---|
282 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
283 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
284 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
285 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
286 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
287 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
288 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
289 | mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
290 | mainLoop.ComparisonFactorLowerBoundParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
291 | mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
292 | mainLoop.ComparisonFactorUpperBoundParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
|
---|
293 | mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
294 | mainLoop.FinalMaximumSelectionPressureParameter.ActualName = FinalMaximumSelectionPressureParameter.Name;
|
---|
295 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
296 | mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
297 | mainLoop.Successor = null;
|
---|
298 |
|
---|
299 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
300 | SelectorParameter.ValidValues.Add(selector);
|
---|
301 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
302 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
303 |
|
---|
304 | ParameterizeSelectors();
|
---|
305 |
|
---|
306 | foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
|
---|
307 | ComparisonFactorModifierParameter.ValidValues.Add(modifier);
|
---|
308 | IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
|
---|
309 | if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
|
---|
310 | ParameterizeComparisonFactorModifiers();
|
---|
311 |
|
---|
312 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
313 | villageQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
314 | selectionPressureAnalyzer = new ValueAnalyzer();
|
---|
315 | villageSelectionPressureAnalyzer = new ValueAnalyzer();
|
---|
316 | ParameterizeAnalyzers();
|
---|
317 | UpdateAnalyzers();
|
---|
318 |
|
---|
319 | Initialize();
|
---|
320 | }
|
---|
321 |
|
---|
322 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
323 | SASEGASA clone = (SASEGASA)base.Clone(cloner);
|
---|
324 | clone.qualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(qualityAnalyzer);
|
---|
325 | clone.villageQualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(villageQualityAnalyzer);
|
---|
326 | clone.selectionPressureAnalyzer = (ValueAnalyzer)cloner.Clone(selectionPressureAnalyzer);
|
---|
327 | clone.villageSelectionPressureAnalyzer = (ValueAnalyzer)cloner.Clone(villageSelectionPressureAnalyzer);
|
---|
328 | clone.Initialize();
|
---|
329 | return clone;
|
---|
330 | }
|
---|
331 |
|
---|
332 | public override void Prepare() {
|
---|
333 | if (Problem != null) base.Prepare();
|
---|
334 | }
|
---|
335 |
|
---|
336 | #region Events
|
---|
337 | protected override void OnProblemChanged() {
|
---|
338 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
339 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
340 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
341 | ParameterizeSolutionsCreator();
|
---|
342 | ParameterizeMainLoop();
|
---|
343 | ParameterizeSelectors();
|
---|
344 | ParameterizeAnalyzers();
|
---|
345 | UpdateCrossovers();
|
---|
346 | UpdateMutators();
|
---|
347 | UpdateAnalyzers();
|
---|
348 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
349 | base.OnProblemChanged();
|
---|
350 | }
|
---|
351 |
|
---|
352 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
353 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
354 | ParameterizeSolutionsCreator();
|
---|
355 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
356 | }
|
---|
357 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
358 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
359 | ParameterizeSolutionsCreator();
|
---|
360 | ParameterizeMainLoop();
|
---|
361 | ParameterizeSelectors();
|
---|
362 | ParameterizeAnalyzers();
|
---|
363 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
364 | base.Problem_EvaluatorChanged(sender, e);
|
---|
365 | }
|
---|
366 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
367 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
368 | UpdateCrossovers();
|
---|
369 | UpdateMutators();
|
---|
370 | UpdateAnalyzers();
|
---|
371 | base.Problem_OperatorsChanged(sender, e);
|
---|
372 | }
|
---|
373 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
374 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
375 | ParameterizeSelectors();
|
---|
376 | }
|
---|
377 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
378 | ParameterizeSelectors();
|
---|
379 | }
|
---|
380 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
381 | NumberOfVillages.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
382 | ParameterizeSelectors();
|
---|
383 | }
|
---|
384 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
385 | ParameterizeSelectors();
|
---|
386 | }
|
---|
387 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
388 | ParameterizeMainLoop();
|
---|
389 | ParameterizeSelectors();
|
---|
390 | ParameterizeAnalyzers();
|
---|
391 | }
|
---|
392 | private void MaximumGenerationsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
393 | MaximumGenerations.ValueChanged += new EventHandler(MaximumGenerations_ValueChanged);
|
---|
394 | MaximumGenerations_ValueChanged(sender, e);
|
---|
395 | }
|
---|
396 | private void MaximumGenerations_ValueChanged(object sender, EventArgs e) {
|
---|
397 | if (MaximumGenerations.Value < NumberOfVillages.Value) NumberOfVillages.Value = MaximumGenerations.Value;
|
---|
398 | ParameterizeMainLoop();
|
---|
399 | }
|
---|
400 | private void NumberOfVillagesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
401 | NumberOfVillages.ValueChanged += new EventHandler(NumberOfVillages_ValueChanged);
|
---|
402 | NumberOfVillages_ValueChanged(sender, e);
|
---|
403 | }
|
---|
404 | private void NumberOfVillages_ValueChanged(object sender, EventArgs e) {
|
---|
405 | if (NumberOfVillages.Value > MaximumGenerations.Value) MaximumGenerations.Value = NumberOfVillages.Value;
|
---|
406 | ParameterizeComparisonFactorModifiers();
|
---|
407 | ParameterizeMainLoop();
|
---|
408 | }
|
---|
409 | #endregion
|
---|
410 |
|
---|
411 | #region Helpers
|
---|
412 | [StorableHook(HookType.AfterDeserialization)]
|
---|
413 | private void Initialize() {
|
---|
414 | NumberOfVillagesParameter.ValueChanged += new EventHandler(NumberOfVillagesParameter_ValueChanged);
|
---|
415 | NumberOfVillages.ValueChanged += new EventHandler(NumberOfVillages_ValueChanged);
|
---|
416 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
417 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
418 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
419 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
420 | MaximumGenerationsParameter.ValueChanged += new EventHandler(MaximumGenerationsParameter_ValueChanged);
|
---|
421 | MaximumGenerations.ValueChanged += new EventHandler(MaximumGenerations_ValueChanged);
|
---|
422 | if (Problem != null) {
|
---|
423 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
424 | }
|
---|
425 | }
|
---|
426 | private void ParameterizeSolutionsCreator() {
|
---|
427 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
428 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
429 | }
|
---|
430 | private void ParameterizeMainLoop() {
|
---|
431 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
432 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
433 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
434 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
435 | MainLoop.MigrationIntervalParameter.Value = new IntValue(MaximumGenerations.Value / NumberOfVillages.Value);
|
---|
436 | }
|
---|
437 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
438 | if (op is IStochasticOperator)
|
---|
439 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
440 | }
|
---|
441 | private void ParameterizeSelectors() {
|
---|
442 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
443 | selector.CopySelected = new BoolValue(true);
|
---|
444 | selector.NumberOfSelectedSubScopesParameter.Value = null;
|
---|
445 | selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
|
---|
446 | ParameterizeStochasticOperator(selector);
|
---|
447 | }
|
---|
448 | if (Problem != null) {
|
---|
449 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
450 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
451 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
452 | }
|
---|
453 | }
|
---|
454 | }
|
---|
455 | private void ParameterizeAnalyzers() {
|
---|
456 | villageQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
457 | villageQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
458 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
459 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
460 |
|
---|
461 | villageSelectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
462 | villageSelectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
|
---|
463 | villageSelectionPressureAnalyzer.ValueParameter.Depth = 0;
|
---|
464 | villageSelectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
|
---|
465 | villageSelectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
|
---|
466 |
|
---|
467 | selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
468 | selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
|
---|
469 | selectionPressureAnalyzer.ValueParameter.Depth = 1;
|
---|
470 | selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
|
---|
471 | selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
|
---|
472 |
|
---|
473 | if (Problem != null) {
|
---|
474 | villageQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
475 | villageQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
476 | villageQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
477 |
|
---|
478 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
479 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
480 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
481 | }
|
---|
482 | }
|
---|
483 | private void ParameterizeComparisonFactorModifiers() {
|
---|
484 | foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
|
---|
485 | modifier.IndexParameter.ActualName = "Reunifications";
|
---|
486 | modifier.StartIndexParameter.Value = new IntValue(0);
|
---|
487 | modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
488 | modifier.EndIndexParameter.Value = new IntValue(NumberOfVillages.Value - 1);
|
---|
489 | modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
|
---|
490 | modifier.ValueParameter.ActualName = "ComparisonFactor";
|
---|
491 | }
|
---|
492 | }
|
---|
493 | private void UpdateCrossovers() {
|
---|
494 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
495 | CrossoverParameter.ValidValues.Clear();
|
---|
496 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
497 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
498 | if (oldCrossover != null) {
|
---|
499 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
500 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
501 | }
|
---|
502 | }
|
---|
503 | private void UpdateMutators() {
|
---|
504 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
505 | MutatorParameter.ValidValues.Clear();
|
---|
506 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
507 | MutatorParameter.ValidValues.Add(mutator);
|
---|
508 | if (oldMutator != null) {
|
---|
509 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
510 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | private void UpdateAnalyzers() {
|
---|
514 | VillageAnalyzer.Operators.Clear();
|
---|
515 | Analyzer.Operators.Clear();
|
---|
516 | VillageAnalyzer.Operators.Add(villageQualityAnalyzer);
|
---|
517 | VillageAnalyzer.Operators.Add(villageSelectionPressureAnalyzer);
|
---|
518 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
519 | Analyzer.Operators.Add(selectionPressureAnalyzer);
|
---|
520 | if (Problem != null) {
|
---|
521 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {
|
---|
522 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
523 | param.Depth = 2;
|
---|
524 | Analyzer.Operators.Add(analyzer);
|
---|
525 | }
|
---|
526 | }
|
---|
527 | }
|
---|
528 | #endregion
|
---|
529 | }
|
---|
530 | }
|
---|