1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Optimization.Operators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
37 | /// <summary>
|
---|
38 | /// An offspring selection genetic algorithm.
|
---|
39 | /// </summary>
|
---|
40 | [Item("Offspring Selection Genetic Algorithm", "An offspring selection genetic algorithm (Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press).")]
|
---|
41 | [Creatable("Algorithms")]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class OffspringSelectionGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
44 | public string Filename { get; set; }
|
---|
45 |
|
---|
46 | #region Problem Properties
|
---|
47 | public override Type ProblemType {
|
---|
48 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
49 | }
|
---|
50 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
51 | get { return (ISingleObjectiveHeuristicOptimizationProblem)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> PopulationSizeParameter {
|
---|
64 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
65 | }
|
---|
66 | private ConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
67 | get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
68 | }
|
---|
69 | private ConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
70 | get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
71 | }
|
---|
72 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
73 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
74 | }
|
---|
75 | private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
76 | get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
77 | }
|
---|
78 | private ValueParameter<IntValue> ElitesParameter {
|
---|
79 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
80 | }
|
---|
81 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
82 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
83 | }
|
---|
84 | private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
85 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
86 | }
|
---|
87 | private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
|
---|
88 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
|
---|
89 | }
|
---|
90 | private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
|
---|
91 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
|
---|
92 | }
|
---|
93 | private OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
|
---|
94 | get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
|
---|
95 | }
|
---|
96 | private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
97 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
98 | }
|
---|
99 | private ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
100 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
101 | }
|
---|
102 | private ValueLookupParameter<IntValue> SelectedParentsParameter {
|
---|
103 | get { return (ValueLookupParameter<IntValue>)Parameters["SelectedParents"]; }
|
---|
104 | }
|
---|
105 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
106 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
107 | }
|
---|
108 | private ValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
|
---|
109 | get { return (ValueParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | #region Properties
|
---|
114 | public IntValue Seed {
|
---|
115 | get { return SeedParameter.Value; }
|
---|
116 | set { SeedParameter.Value = value; }
|
---|
117 | }
|
---|
118 | public BoolValue SetSeedRandomly {
|
---|
119 | get { return SetSeedRandomlyParameter.Value; }
|
---|
120 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
121 | }
|
---|
122 | public IntValue PopulationSize {
|
---|
123 | get { return PopulationSizeParameter.Value; }
|
---|
124 | set { PopulationSizeParameter.Value = value; }
|
---|
125 | }
|
---|
126 | public ISelector Selector {
|
---|
127 | get { return SelectorParameter.Value; }
|
---|
128 | set { SelectorParameter.Value = value; }
|
---|
129 | }
|
---|
130 | public ICrossover Crossover {
|
---|
131 | get { return CrossoverParameter.Value; }
|
---|
132 | set { CrossoverParameter.Value = value; }
|
---|
133 | }
|
---|
134 | public PercentValue MutationProbability {
|
---|
135 | get { return MutationProbabilityParameter.Value; }
|
---|
136 | set { MutationProbabilityParameter.Value = value; }
|
---|
137 | }
|
---|
138 | public IManipulator Mutator {
|
---|
139 | get { return MutatorParameter.Value; }
|
---|
140 | set { MutatorParameter.Value = value; }
|
---|
141 | }
|
---|
142 | public IntValue Elites {
|
---|
143 | get { return ElitesParameter.Value; }
|
---|
144 | set { ElitesParameter.Value = value; }
|
---|
145 | }
|
---|
146 | public IntValue MaximumGenerations {
|
---|
147 | get { return MaximumGenerationsParameter.Value; }
|
---|
148 | set { MaximumGenerationsParameter.Value = value; }
|
---|
149 | }
|
---|
150 | public DoubleValue SuccessRatio {
|
---|
151 | get { return SuccessRatioParameter.Value; }
|
---|
152 | set { SuccessRatioParameter.Value = value; }
|
---|
153 | }
|
---|
154 | public DoubleValue ComparisonFactorLowerBound {
|
---|
155 | get { return ComparisonFactorLowerBoundParameter.Value; }
|
---|
156 | set { ComparisonFactorLowerBoundParameter.Value = value; }
|
---|
157 | }
|
---|
158 | public DoubleValue ComparisonFactorUpperBound {
|
---|
159 | get { return ComparisonFactorUpperBoundParameter.Value; }
|
---|
160 | set { ComparisonFactorUpperBoundParameter.Value = value; }
|
---|
161 | }
|
---|
162 | public IDiscreteDoubleValueModifier ComparisonFactorModifier {
|
---|
163 | get { return ComparisonFactorModifierParameter.Value; }
|
---|
164 | set { ComparisonFactorModifierParameter.Value = value; }
|
---|
165 | }
|
---|
166 | public DoubleValue MaximumSelectionPressure {
|
---|
167 | get { return MaximumSelectionPressureParameter.Value; }
|
---|
168 | set { MaximumSelectionPressureParameter.Value = value; }
|
---|
169 | }
|
---|
170 | public BoolValue OffspringSelectionBeforeMutation {
|
---|
171 | get { return OffspringSelectionBeforeMutationParameter.Value; }
|
---|
172 | set { OffspringSelectionBeforeMutationParameter.Value = value; }
|
---|
173 | }
|
---|
174 | public IntValue SelectedParents {
|
---|
175 | get { return SelectedParentsParameter.Value; }
|
---|
176 | set { SelectedParentsParameter.Value = value; }
|
---|
177 | }
|
---|
178 | public MultiAnalyzer Analyzer {
|
---|
179 | get { return AnalyzerParameter.Value; }
|
---|
180 | set { AnalyzerParameter.Value = value; }
|
---|
181 | }
|
---|
182 | public IntValue MaximumEvaluatedSolutions {
|
---|
183 | get { return MaximumEvaluatedSolutionsParameter.Value; }
|
---|
184 | set { MaximumEvaluatedSolutionsParameter.Value = value; }
|
---|
185 | }
|
---|
186 | private RandomCreator RandomCreator {
|
---|
187 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
188 | }
|
---|
189 | private SolutionsCreator SolutionsCreator {
|
---|
190 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
191 | }
|
---|
192 | private OffspringSelectionGeneticAlgorithmMainLoop MainLoop {
|
---|
193 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
194 | }
|
---|
195 | [Storable]
|
---|
196 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
197 | [Storable]
|
---|
198 | private ValueAnalyzer selectionPressureAnalyzer;
|
---|
199 | [Storable]
|
---|
200 | private SuccessfulOffspringAnalyzer successfulOffspringAnalyzer;
|
---|
201 | #endregion
|
---|
202 |
|
---|
203 | [StorableConstructor]
|
---|
204 | private OffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
205 | [StorableHook(HookType.AfterDeserialization)]
|
---|
206 | private void AfterDeserialization() {
|
---|
207 | #region Backwards Compatibility
|
---|
208 | if (successfulOffspringAnalyzer == null)
|
---|
209 | successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
|
---|
210 | #endregion
|
---|
211 |
|
---|
212 | Initialize();
|
---|
213 | }
|
---|
214 | private OffspringSelectionGeneticAlgorithm(OffspringSelectionGeneticAlgorithm original, Cloner cloner)
|
---|
215 | : base(original, cloner) {
|
---|
216 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
217 | selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
|
---|
218 | successfulOffspringAnalyzer = cloner.Clone(original.successfulOffspringAnalyzer);
|
---|
219 | Initialize();
|
---|
220 | }
|
---|
221 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
222 | return new OffspringSelectionGeneticAlgorithm(this, cloner);
|
---|
223 | }
|
---|
224 | public OffspringSelectionGeneticAlgorithm()
|
---|
225 | : base() {
|
---|
226 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
227 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
228 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
229 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
230 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
231 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
232 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
233 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
234 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
235 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
|
---|
236 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
|
---|
237 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
|
---|
238 | Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
|
---|
239 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
|
---|
240 | 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)));
|
---|
241 | Parameters.Add(new ValueLookupParameter<IntValue>("SelectedParents", "How much parents should be selected each time the offspring selection step is performed until the population is filled. This parameter should be about the same or twice the size of PopulationSize for smaller problems, and less for large problems.", new IntValue(200)));
|
---|
242 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
243 | Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
|
---|
244 |
|
---|
245 | RandomCreator randomCreator = new RandomCreator();
|
---|
246 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
247 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
248 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
249 | OffspringSelectionGeneticAlgorithmMainLoop mainLoop = new OffspringSelectionGeneticAlgorithmMainLoop();
|
---|
250 | OperatorGraph.InitialOperator = randomCreator;
|
---|
251 |
|
---|
252 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
253 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
254 | randomCreator.SeedParameter.Value = null;
|
---|
255 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
256 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
257 | randomCreator.Successor = solutionsCreator;
|
---|
258 |
|
---|
259 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
260 | solutionsCreator.Successor = subScopesCounter;
|
---|
261 |
|
---|
262 | subScopesCounter.Name = "Initialize EvaluatedSolutions";
|
---|
263 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
264 | subScopesCounter.Successor = resultsCollector;
|
---|
265 |
|
---|
266 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", "", "EvaluatedSolutions"));
|
---|
267 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
268 | resultsCollector.Successor = mainLoop;
|
---|
269 |
|
---|
270 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
271 | mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
272 | mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
|
---|
273 | mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
274 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
275 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
276 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
277 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
278 | mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
279 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
280 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
281 | mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
282 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
283 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
284 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
285 | mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
286 |
|
---|
287 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
288 | SelectorParameter.ValidValues.Add(selector);
|
---|
289 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
290 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
291 | ParameterizeSelectors();
|
---|
292 |
|
---|
293 | foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
|
---|
294 | ComparisonFactorModifierParameter.ValidValues.Add(modifier);
|
---|
295 | IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
|
---|
296 | if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
|
---|
297 | ParameterizeComparisonFactorModifiers();
|
---|
298 |
|
---|
299 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
300 | selectionPressureAnalyzer = new ValueAnalyzer();
|
---|
301 | successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
|
---|
302 | ParameterizeAnalyzers();
|
---|
303 | UpdateAnalyzers();
|
---|
304 |
|
---|
305 | Initialize();
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 |
|
---|
310 | public override void Prepare() {
|
---|
311 | if (Problem != null) base.Prepare();
|
---|
312 | }
|
---|
313 |
|
---|
314 | #region Events
|
---|
315 | protected override void OnProblemChanged() {
|
---|
316 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
317 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
318 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
319 | ParameterizeSolutionsCreator();
|
---|
320 | ParameterizMainLoop();
|
---|
321 | ParameterizeSelectors();
|
---|
322 | ParameterizeAnalyzers();
|
---|
323 | ParameterizeIterationBasedOperators();
|
---|
324 | UpdateCrossovers();
|
---|
325 | UpdateMutators();
|
---|
326 | UpdateAnalyzers();
|
---|
327 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
328 | base.OnProblemChanged();
|
---|
329 | }
|
---|
330 |
|
---|
331 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
332 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
333 | ParameterizeSolutionsCreator();
|
---|
334 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
335 | }
|
---|
336 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
337 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
338 | ParameterizeSolutionsCreator();
|
---|
339 | ParameterizMainLoop();
|
---|
340 | ParameterizeSelectors();
|
---|
341 | ParameterizeAnalyzers();
|
---|
342 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
343 | base.Problem_EvaluatorChanged(sender, e);
|
---|
344 | }
|
---|
345 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
346 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
347 | ParameterizeIterationBasedOperators();
|
---|
348 | UpdateCrossovers();
|
---|
349 | UpdateMutators();
|
---|
350 | UpdateAnalyzers();
|
---|
351 | base.Problem_OperatorsChanged(sender, e);
|
---|
352 | }
|
---|
353 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
354 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
355 | ParameterizeSelectors();
|
---|
356 | }
|
---|
357 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
358 | ParameterizeSelectors();
|
---|
359 | }
|
---|
360 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
361 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
362 | ParameterizeSelectors();
|
---|
363 | }
|
---|
364 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
365 | ParameterizeSelectors();
|
---|
366 | }
|
---|
367 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
368 | ParameterizMainLoop();
|
---|
369 | ParameterizeSelectors();
|
---|
370 | ParameterizeAnalyzers();
|
---|
371 | }
|
---|
372 | #endregion
|
---|
373 |
|
---|
374 | #region Helpers
|
---|
375 | private void Initialize() {
|
---|
376 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
377 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
378 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
379 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
380 | if (Problem != null) {
|
---|
381 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
382 | }
|
---|
383 | }
|
---|
384 | private void ParameterizeSolutionsCreator() {
|
---|
385 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
386 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
387 | }
|
---|
388 | private void ParameterizMainLoop() {
|
---|
389 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
390 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
391 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
392 | }
|
---|
393 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
394 | if (op is IStochasticOperator)
|
---|
395 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
396 | }
|
---|
397 | private void ParameterizeSelectors() {
|
---|
398 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
399 | selector.CopySelected = new BoolValue(true);
|
---|
400 | selector.NumberOfSelectedSubScopesParameter.Value = null;
|
---|
401 | selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
|
---|
402 | ParameterizeStochasticOperator(selector);
|
---|
403 | }
|
---|
404 | if (Problem != null) {
|
---|
405 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
406 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
407 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | }
|
---|
411 | private void ParameterizeAnalyzers() {
|
---|
412 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
413 | selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
|
---|
414 | selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
415 | selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
|
---|
416 | selectionPressureAnalyzer.ValueParameter.Depth = 0;
|
---|
417 | selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
|
---|
418 | successfulOffspringAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
419 | successfulOffspringAnalyzer.GenerationsParameter.ActualName = "Generations";
|
---|
420 | successfulOffspringAnalyzer.SuccessfulOffspringFlagParameter.Value.Value = "SuccessfulOffspring";
|
---|
421 | successfulOffspringAnalyzer.DepthParameter.Value = new IntValue(1);
|
---|
422 | if (Problem != null) {
|
---|
423 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
424 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
425 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
426 | }
|
---|
427 | }
|
---|
428 | private void ParameterizeComparisonFactorModifiers() {
|
---|
429 | foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
|
---|
430 | modifier.IndexParameter.ActualName = "Generations";
|
---|
431 | modifier.EndIndexParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
432 | modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
|
---|
433 | modifier.StartIndexParameter.Value = new IntValue(0);
|
---|
434 | modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
435 | modifier.ValueParameter.ActualName = "ComparisonFactor";
|
---|
436 | }
|
---|
437 | }
|
---|
438 | private void ParameterizeIterationBasedOperators() {
|
---|
439 | if (Problem != null) {
|
---|
440 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
441 | op.IterationsParameter.ActualName = "Generations";
|
---|
442 | op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
443 | }
|
---|
444 | }
|
---|
445 | }
|
---|
446 | private void UpdateCrossovers() {
|
---|
447 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
448 | CrossoverParameter.ValidValues.Clear();
|
---|
449 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
450 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
451 | if (oldCrossover != null) {
|
---|
452 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
453 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
454 | }
|
---|
455 | }
|
---|
456 | private void UpdateMutators() {
|
---|
457 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
458 | MutatorParameter.ValidValues.Clear();
|
---|
459 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
460 | MutatorParameter.ValidValues.Add(mutator);
|
---|
461 | if (oldMutator != null) {
|
---|
462 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
463 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
464 | }
|
---|
465 | }
|
---|
466 | private void UpdateAnalyzers() {
|
---|
467 | Analyzer.Operators.Clear();
|
---|
468 | if (Problem != null) {
|
---|
469 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
470 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
471 | param.Depth = 1;
|
---|
472 | Analyzer.Operators.Add(analyzer);
|
---|
473 | }
|
---|
474 | }
|
---|
475 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
476 | Analyzer.Operators.Add(selectionPressureAnalyzer);
|
---|
477 | Analyzer.Operators.Add(successfulOffspringAnalyzer);
|
---|
478 | Analyzer.Operators.SetItemCheckedState(successfulOffspringAnalyzer, false);
|
---|
479 | }
|
---|
480 | private OffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
481 | IOperator mainLoop = start;
|
---|
482 | while (mainLoop != null && !(mainLoop is OffspringSelectionGeneticAlgorithmMainLoop))
|
---|
483 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
484 | if (mainLoop == null) return null;
|
---|
485 | else return (OffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
|
---|
486 | }
|
---|
487 | #endregion
|
---|
488 | }
|
---|
489 | }
|
---|