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