[11567] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 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;
|
---|
[11620] | 23 | using System.Collections.Generic;
|
---|
[11578] | 24 | using System.Linq;
|
---|
[11568] | 25 | using HeuristicLab.Analysis;
|
---|
[11567] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[11568] | 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Operators;
|
---|
[11567] | 30 | using HeuristicLab.Optimization;
|
---|
[11578] | 31 | using HeuristicLab.Optimization.Operators;
|
---|
[11568] | 32 | using HeuristicLab.Parameters;
|
---|
[11567] | 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11578] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
| 35 | using HeuristicLab.Random;
|
---|
| 36 | using HeuristicLab.Selection;
|
---|
[11567] | 37 |
|
---|
| 38 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
| 39 | [Item("ALPS Genetic Algorithm", "A genetic algorithm with an age-layered population structure.")]
|
---|
| 40 | [Creatable("Algorithms")]
|
---|
| 41 | [StorableClass]
|
---|
[11583] | 42 | public sealed class AlpsGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
[11567] | 43 | public string Filename { get; set; }
|
---|
| 44 |
|
---|
| 45 | #region Problem Properties
|
---|
| 46 | public override Type ProblemType {
|
---|
| 47 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
| 48 | }
|
---|
| 49 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 50 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
| 51 | set { base.Problem = value; }
|
---|
| 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | #region Parameter Properties
|
---|
[11568] | 56 | private IValueParameter<IntValue> SeedParameter {
|
---|
| 57 | get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
| 58 | }
|
---|
| 59 | private IValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 60 | get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
| 61 | }
|
---|
| 62 | private IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 63 | get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 64 | }
|
---|
| 65 | private IValueParameter<MultiAnalyzer> LayerAnalyzerParameter {
|
---|
| 66 | get { return (IValueParameter<MultiAnalyzer>)Parameters["LayerAnalyzer"]; }
|
---|
| 67 | }
|
---|
| 68 | private IValueParameter<IntValue> NumberOfLayersParameter {
|
---|
| 69 | get { return (IValueParameter<IntValue>)Parameters["NumberOfLayers"]; }
|
---|
| 70 | }
|
---|
| 71 | private IValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 72 | get { return (IValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 73 | }
|
---|
| 74 | private IValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 75 | get { return (IValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 76 | }
|
---|
| 77 | private IValueParameter<AgingScheme> AgingSchemeParameter {
|
---|
| 78 | get { return (IValueParameter<AgingScheme>)Parameters["AgingScheme"]; }
|
---|
| 79 | }
|
---|
| 80 | private IValueParameter<IntValue> AgeGapParameter {
|
---|
| 81 | get { return (IValueParameter<IntValue>)Parameters["AgeGap"]; }
|
---|
| 82 | }
|
---|
| 83 | private IValueParameter<IntArray> AgeLimitsParameter {
|
---|
| 84 | get { return (IValueParameter<IntArray>)Parameters["AgeLimits"]; }
|
---|
| 85 | }
|
---|
| 86 | private IValueParameter<ReductionOperation> AgeInheritanceParameter {
|
---|
| 87 | get { return (IValueParameter<ReductionOperation>)Parameters["AgeInheritance"]; }
|
---|
| 88 | }
|
---|
| 89 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 90 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
| 91 | }
|
---|
| 92 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 93 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
| 94 | }
|
---|
| 95 | private IValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 96 | get { return (IValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 97 | }
|
---|
| 98 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 99 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
| 100 | }
|
---|
| 101 | private IValueParameter<IntValue> ElitesParameter {
|
---|
| 102 | get { return (IValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 103 | }
|
---|
| 104 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 105 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 106 | }
|
---|
[11567] | 107 | #endregion
|
---|
| 108 |
|
---|
| 109 | #region Properties
|
---|
[11568] | 110 | public IntValue Seed {
|
---|
| 111 | get { return SeedParameter.Value; }
|
---|
| 112 | set { SeedParameter.Value = value; }
|
---|
| 113 | }
|
---|
| 114 | public BoolValue SetSeedRandomly {
|
---|
| 115 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 116 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 117 | }
|
---|
| 118 | public MultiAnalyzer Analyzer {
|
---|
| 119 | get { return AnalyzerParameter.Value; }
|
---|
| 120 | set { AnalyzerParameter.Value = value; }
|
---|
| 121 | }
|
---|
| 122 | public MultiAnalyzer LayerAnalyzer {
|
---|
| 123 | get { return LayerAnalyzerParameter.Value; }
|
---|
| 124 | set { LayerAnalyzerParameter.Value = value; }
|
---|
| 125 | }
|
---|
| 126 | public IntValue NumberOfLayers {
|
---|
| 127 | get { return NumberOfLayersParameter.Value; }
|
---|
| 128 | set { NumberOfLayersParameter.Value = value; }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public IntValue PopulationSize {
|
---|
| 132 | get { return PopulationSizeParameter.Value; }
|
---|
| 133 | set { PopulationSizeParameter.Value = value; }
|
---|
| 134 | }
|
---|
| 135 | public IntValue MaximumGenerations {
|
---|
| 136 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 137 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 138 | }
|
---|
| 139 | public AgingScheme AgingScheme {
|
---|
| 140 | get { return AgingSchemeParameter.Value; }
|
---|
| 141 | set { AgingSchemeParameter.Value = value; }
|
---|
| 142 | }
|
---|
| 143 | public IntValue AgeGap {
|
---|
| 144 | get { return AgeGapParameter.Value; }
|
---|
| 145 | set { AgeGapParameter.Value = value; }
|
---|
| 146 | }
|
---|
| 147 | public IntArray AgeLimits {
|
---|
| 148 | get { return AgeLimitsParameter.Value; }
|
---|
| 149 | set { AgeLimitsParameter.Value = value; }
|
---|
| 150 | }
|
---|
| 151 | public ReductionOperation AgeInheritance {
|
---|
| 152 | get { return AgeInheritanceParameter.Value; }
|
---|
| 153 | set { AgeInheritanceParameter.Value = value; }
|
---|
| 154 | }
|
---|
| 155 | public ISelector Selector {
|
---|
| 156 | get { return SelectorParameter.Value; }
|
---|
| 157 | set { SelectorParameter.Value = value; }
|
---|
| 158 | }
|
---|
| 159 | public ICrossover Crossover {
|
---|
| 160 | get { return CrossoverParameter.Value; }
|
---|
| 161 | set { CrossoverParameter.Value = value; }
|
---|
| 162 | }
|
---|
| 163 | public PercentValue MutationProbability {
|
---|
| 164 | get { return MutationProbabilityParameter.Value; }
|
---|
| 165 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 166 | }
|
---|
| 167 | public IManipulator Mutator {
|
---|
| 168 | get { return MutatorParameter.Value; }
|
---|
| 169 | set { MutatorParameter.Value = value; }
|
---|
| 170 | }
|
---|
| 171 | public IntValue Elites {
|
---|
| 172 | get { return ElitesParameter.Value; }
|
---|
| 173 | set { ElitesParameter.Value = value; }
|
---|
| 174 | }
|
---|
| 175 | public bool ReevaluteElites {
|
---|
| 176 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
| 177 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
| 178 | }
|
---|
[11578] | 179 |
|
---|
| 180 | private RandomCreator GlobalRandomCreator {
|
---|
| 181 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 182 | }
|
---|
| 183 | private SolutionsCreator SolutionsCreator {
|
---|
| 184 | get { return OperatorGraph.Iterate().OfType<SolutionsCreator>().First(); }
|
---|
| 185 | }
|
---|
[11580] | 186 | private AlpsGeneticAlgorithmMainLoop MainLoop {
|
---|
| 187 | get { return OperatorGraph.Iterate().OfType<AlpsGeneticAlgorithmMainLoop>().First(); }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[11676] | 190 | #endregion
|
---|
| 191 |
|
---|
| 192 | #region Preconfigured Analyzers
|
---|
[11578] | 193 | [Storable]
|
---|
| 194 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
| 195 | [Storable]
|
---|
| 196 | private BestAverageWorstQualityAnalyzer layerQualityAnalyzer;
|
---|
[11567] | 197 | #endregion
|
---|
| 198 |
|
---|
| 199 | [StorableConstructor]
|
---|
| 200 | private AlpsGeneticAlgorithm(bool deserializing)
|
---|
| 201 | : base(deserializing) { }
|
---|
| 202 | private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
|
---|
| 203 | : base(original, cloner) {
|
---|
[11578] | 204 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 205 | layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer);
|
---|
| 206 | Initialize();
|
---|
[11567] | 207 | }
|
---|
| 208 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 209 | return new AlpsGeneticAlgorithm(this, cloner);
|
---|
| 210 | }
|
---|
| 211 | public AlpsGeneticAlgorithm()
|
---|
| 212 | : base() {
|
---|
[11568] | 213 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 214 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 215 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
| 216 | Parameters.Add(new ValueParameter<MultiAnalyzer>("LayerAnalyzer", "The operator used to analyze each layer.", new MultiAnalyzer()));
|
---|
| 217 | Parameters.Add(new ValueParameter<IntValue>("NumberOfLayers", "The number of layers.", new IntValue(5)));
|
---|
| 218 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions each layer.", new IntValue(20)));
|
---|
| 219 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
| 220 | Parameters.Add(new ValueParameter<AgingScheme>("AgingScheme", "The aging scheme for setting the age-limits for the layers.", new AgingScheme(AgingSchemes.Polynomial)));
|
---|
| 221 | Parameters.Add(new ValueParameter<IntValue>("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers", new IntValue(5)));
|
---|
[11586] | 222 | Parameters.Add(new ValueParameter<IntArray>("AgeLimits", new IntArray(new[] { 5, 20, 45, 80, 125 })) { Hidden = true });
|
---|
[11578] | 223 | Parameters.Add(new ValueParameter<ReductionOperation>("AgeInheritance", "The operator for determining the age of an offspring based the parents' age.", new ReductionOperation(ReductionOperations.Max)) { Hidden = true });
|
---|
[11568] | 224 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 225 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
| 226 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
| 227 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 228 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
| 229 | 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 });
|
---|
[11578] | 230 |
|
---|
| 231 | var globalRandomCreator = new RandomCreator();
|
---|
| 232 | var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
|
---|
| 233 | var layer0Processor = new SubScopesProcessor();
|
---|
| 234 | var localRandomCreator = new LocalRandomCreator();
|
---|
[11585] | 235 | var layerVariableCreator = new VariableCreator();
|
---|
[11578] | 236 | var layerSolutionsCreator = new SolutionsCreator();
|
---|
| 237 | var initializeAgeProcessor = new UniformSubScopesProcessor();
|
---|
| 238 | var initializeAge = new VariableCreator() { Name = "Initialize Age" };
|
---|
[11676] | 239 | var initializeLayerPopulationSize = new SubScopesCounter() { Name = "Init LayerPopulationCounter" };
|
---|
| 240 | var initializeLocalEvaluatedSolutions = new Assigner() { Name = "Initialize LayerEvaluatedSolutions" };
|
---|
[11578] | 241 | var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
|
---|
| 242 | var resultsCollector = new ResultsCollector();
|
---|
[11580] | 243 | var mainLoop = new AlpsGeneticAlgorithmMainLoop();
|
---|
| 244 |
|
---|
[11578] | 245 | OperatorGraph.InitialOperator = globalRandomCreator;
|
---|
| 246 |
|
---|
| 247 | globalRandomCreator.RandomParameter.ActualName = "GlobalRandom";
|
---|
| 248 | globalRandomCreator.SeedParameter.Value = null;
|
---|
| 249 | globalRandomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 250 | globalRandomCreator.Successor = layer0Creator;
|
---|
| 251 |
|
---|
| 252 | layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
|
---|
| 253 | layer0Creator.Successor = layer0Processor;
|
---|
| 254 |
|
---|
| 255 | layer0Processor.Operators.Add(localRandomCreator);
|
---|
| 256 | layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
|
---|
| 257 |
|
---|
[11585] | 258 | localRandomCreator.Successor = layerVariableCreator;
|
---|
[11578] | 259 |
|
---|
[11585] | 260 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
|
---|
[11609] | 261 | //layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results", new ResultCollection()));
|
---|
[11585] | 262 | layerVariableCreator.Successor = layerSolutionsCreator;
|
---|
| 263 |
|
---|
[11578] | 264 | layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 265 | layerSolutionsCreator.Successor = initializeAgeProcessor;
|
---|
| 266 |
|
---|
| 267 | initializeAgeProcessor.Operator = initializeAge;
|
---|
[11676] | 268 | initializeAgeProcessor.Successor = initializeLayerPopulationSize;
|
---|
[11578] | 269 |
|
---|
[11676] | 270 | initializeLayerPopulationSize.ValueParameter.ActualName = "LayerPopulationSize";
|
---|
| 271 | initializeLayerPopulationSize.Successor = initializeLocalEvaluatedSolutions;
|
---|
| 272 |
|
---|
[11578] | 273 | initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
|
---|
| 274 | initializeAge.Successor = null;
|
---|
| 275 |
|
---|
[11676] | 276 | initializeLocalEvaluatedSolutions.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
| 277 | initializeLocalEvaluatedSolutions.RightSideParameter.ActualName = "LayerPopulationSize";
|
---|
[11578] | 278 | initializeLocalEvaluatedSolutions.Successor = null;
|
---|
| 279 |
|
---|
| 280 | initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
|
---|
| 281 | initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
|
---|
[11590] | 282 | initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
[11578] | 283 | initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
| 284 | initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
|
---|
| 285 |
|
---|
[11609] | 286 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
[11578] | 287 | resultsCollector.Successor = mainLoop;
|
---|
| 288 |
|
---|
| 289 | foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
|
---|
| 290 | SelectorParameter.ValidValues.Add(selector);
|
---|
| 291 | var porportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(s => s is ProportionalSelector);
|
---|
| 292 | if (porportionalSelector != null) SelectorParameter.Value = porportionalSelector;
|
---|
| 293 |
|
---|
| 294 | ParameterizeSelectors();
|
---|
| 295 |
|
---|
| 296 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 297 | layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 298 | ParameterizeAnalyzers();
|
---|
| 299 | UpdateAnalyzers();
|
---|
| 300 |
|
---|
| 301 | Initialize();
|
---|
[11567] | 302 | }
|
---|
[11578] | 303 |
|
---|
| 304 | public override void Prepare() {
|
---|
| 305 | if (Problem != null)
|
---|
| 306 | base.Prepare();
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | #region Events
|
---|
| 310 | protected override void OnProblemChanged() {
|
---|
| 311 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 312 | ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
|
---|
| 313 | foreach (var @operator in Problem.Operators.OfType<IOperator>())
|
---|
| 314 | ParameterizeStochasticOperator(@operator);
|
---|
| 315 | ParameterizeSolutionsCreator();
|
---|
| 316 | ParameterizeMainLoop();
|
---|
| 317 | ParameterizeSelectors();
|
---|
| 318 | ParameterizeAnalyzers();
|
---|
| 319 | ParameterizeIterationBasedOperators();
|
---|
| 320 | UpdateCrossovers();
|
---|
| 321 | UpdateMutators();
|
---|
| 322 | UpdateAnalyzers();
|
---|
| 323 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
| 324 | base.OnProblemChanged();
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 328 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 329 | ParameterizeSolutionsCreator();
|
---|
| 330 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 331 | }
|
---|
| 332 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 333 | ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
|
---|
| 334 | ParameterizeSolutionsCreator();
|
---|
| 335 | ParameterizeMainLoop();
|
---|
| 336 | ParameterizeSelectors();
|
---|
| 337 | ParameterizeAnalyzers();
|
---|
| 338 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
| 339 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 340 | }
|
---|
| 341 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 342 | foreach (var @operator in Problem.Operators.OfType<IOperator>())
|
---|
| 343 | ParameterizeStochasticOperator(@operator);
|
---|
| 344 | ParameterizeIterationBasedOperators();
|
---|
| 345 | UpdateCrossovers();
|
---|
| 346 | UpdateMutators();
|
---|
| 347 | UpdateAnalyzers();
|
---|
| 348 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 349 | }
|
---|
| 350 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 351 | ParameterizeMainLoop();
|
---|
| 352 | ParameterizeSelectors();
|
---|
| 353 | ParameterizeAnalyzers();
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 357 | PopulationSizeParameter.ValueChanged += PopulationSize_ValueChanged;
|
---|
| 358 | ParameterizeSelectors();
|
---|
| 359 | }
|
---|
| 360 | void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 361 | ParameterizeSelectors();
|
---|
| 362 | }
|
---|
| 363 | void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 364 | Elites.ValueChanged += ElitesParameter_ValueChanged;
|
---|
| 365 | ParameterizeSelectors();
|
---|
| 366 | }
|
---|
| 367 | void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
| 368 | ParameterizeSelectors();
|
---|
| 369 | }
|
---|
[11620] | 370 | private void AgeGapParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 371 | AgeGap.ValueChanged += AgeGap_ValueChanged;
|
---|
| 372 | RecalculateAgeLimits();
|
---|
| 373 | }
|
---|
| 374 | private void AgeGap_ValueChanged(object sender, EventArgs e) {
|
---|
| 375 | RecalculateAgeLimits();
|
---|
| 376 | }
|
---|
| 377 | private void AgingSchemeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 378 | AgingScheme.ValueChanged += AgingScheme_ValueChanged;
|
---|
| 379 | RecalculateAgeLimits();
|
---|
| 380 | }
|
---|
| 381 | private void AgingScheme_ValueChanged(object sender, EventArgs e) {
|
---|
| 382 | RecalculateAgeLimits();
|
---|
| 383 | }
|
---|
| 384 | private void NumberOfLayersParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 385 | NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
|
---|
| 386 | RecalculateAgeLimits();
|
---|
| 387 | }
|
---|
| 388 | private void NumberOfLayers_ValueChanged(object sender, EventArgs e) {
|
---|
| 389 | RecalculateAgeLimits();
|
---|
| 390 | }
|
---|
[11578] | 391 | #endregion
|
---|
| 392 |
|
---|
| 393 | #region Parameterization
|
---|
| 394 | private void Initialize() {
|
---|
| 395 | PopulationSizeParameter.ValueChanged += PopulationSizeParameter_ValueChanged;
|
---|
| 396 | PopulationSize.ValueChanged += PopulationSize_ValueChanged;
|
---|
| 397 | ElitesParameter.ValueChanged += ElitesParameter_ValueChanged;
|
---|
| 398 | Elites.ValueChanged += Elites_ValueChanged;
|
---|
| 399 | if (Problem != null)
|
---|
| 400 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[11620] | 401 | AgeGapParameter.ValueChanged += AgeGapParameter_ValueChanged;
|
---|
| 402 | AgeGap.ValueChanged += AgeGap_ValueChanged;
|
---|
| 403 | AgingSchemeParameter.ValueChanged += AgingSchemeParameter_ValueChanged;
|
---|
| 404 | AgingScheme.ValueChanged += AgingScheme_ValueChanged;
|
---|
| 405 | NumberOfLayersParameter.ValueChanged += NumberOfLayersParameter_ValueChanged;
|
---|
| 406 | NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
|
---|
[11578] | 407 | }
|
---|
| 408 | private void ParameterizeSolutionsCreator() {
|
---|
| 409 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 410 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
[11590] | 411 | MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 412 | MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
[11578] | 413 | }
|
---|
| 414 | private void ParameterizeMainLoop() {
|
---|
[11583] | 415 | MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 416 | MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 417 | MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[11586] | 418 | MainLoop.EldersEmigrator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 419 | MainLoop.EldersEmigrator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[11609] | 420 | MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
[11578] | 421 | }
|
---|
| 422 | private void ParameterizeSelectors() {
|
---|
| 423 | foreach (var selector in SelectorParameter.ValidValues) {
|
---|
| 424 | selector.CopySelected = new BoolValue(true);
|
---|
| 425 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
| 426 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
| 427 | ParameterizeStochasticOperatorForLayer(selector);
|
---|
| 428 | }
|
---|
| 429 | if (Problem != null) {
|
---|
| 430 | foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
| 431 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 432 | selector.MaximizationParameter.Hidden = true;
|
---|
| 433 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 434 | selector.QualityParameter.Hidden = true;
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 | private void ParameterizeAnalyzers() {
|
---|
| 439 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 440 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
| 441 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
| 442 | layerQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 443 | layerQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
| 444 | layerQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 445 | if (Problem != null) {
|
---|
| 446 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 447 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
| 448 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 449 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
| 450 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 451 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
| 452 | layerQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 453 | layerQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
| 454 | layerQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 455 | layerQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
| 456 | layerQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 457 | layerQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
| 460 | private void ParameterizeIterationBasedOperators() {
|
---|
| 461 | if (Problem != null) {
|
---|
| 462 | foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 463 | @operator.IterationsParameter.ActualName = "Generations";
|
---|
| 464 | @operator.IterationsParameter.Hidden = true;
|
---|
| 465 | @operator.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 466 | @operator.MaximumIterationsParameter.Hidden = true;
|
---|
| 467 | }
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 | private void ParameterizeStochasticOperator(IOperator @operator) {
|
---|
| 471 | var stochasticOperator = @operator as IStochasticOperator;
|
---|
| 472 | if (stochasticOperator != null) {
|
---|
| 473 | stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.RandomParameter.ActualName;
|
---|
| 474 | stochasticOperator.RandomParameter.Hidden = true;
|
---|
| 475 | }
|
---|
| 476 | }
|
---|
| 477 | private void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
|
---|
| 478 | var stochasticOperator = @operator as IStochasticOperator;
|
---|
| 479 | if (stochasticOperator != null) {
|
---|
| 480 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
| 481 | stochasticOperator.RandomParameter.Hidden = true;
|
---|
| 482 | }
|
---|
| 483 | }
|
---|
| 484 | #endregion
|
---|
| 485 |
|
---|
| 486 | #region Updates
|
---|
| 487 | private void UpdateCrossovers() {
|
---|
| 488 | var oldCrossover = CrossoverParameter.Value;
|
---|
| 489 | var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 490 | CrossoverParameter.ValidValues.Clear();
|
---|
| 491 | foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
|
---|
| 492 | ParameterizeStochasticOperatorForLayer(crossover);
|
---|
| 493 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 494 | }
|
---|
| 495 | if (oldCrossover != null) {
|
---|
| 496 | var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
|
---|
[11678] | 497 | if (crossover != null)
|
---|
| 498 | CrossoverParameter.Value = crossover;
|
---|
| 499 | else
|
---|
| 500 | oldCrossover = null;
|
---|
[11578] | 501 | }
|
---|
| 502 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 503 | CrossoverParameter.Value = defaultCrossover;
|
---|
| 504 | }
|
---|
| 505 | private void UpdateMutators() {
|
---|
| 506 | var oldMutator = MutatorParameter.Value;
|
---|
| 507 | MutatorParameter.ValidValues.Clear();
|
---|
| 508 | foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
|
---|
| 509 | ParameterizeStochasticOperatorForLayer(mutator);
|
---|
| 510 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 511 | }
|
---|
| 512 | if (oldMutator != null) {
|
---|
| 513 | var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
|
---|
[11678] | 514 | if (mutator != null)
|
---|
| 515 | MutatorParameter.Value = mutator;
|
---|
[11578] | 516 | }
|
---|
| 517 | }
|
---|
| 518 | private void UpdateAnalyzers() {
|
---|
| 519 | Analyzer.Operators.Clear();
|
---|
| 520 | LayerAnalyzer.Operators.Clear();
|
---|
| 521 |
|
---|
| 522 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
| 523 | LayerAnalyzer.Operators.Add(layerQualityAnalyzer, layerQualityAnalyzer.EnabledByDefault);
|
---|
| 524 |
|
---|
| 525 | if (Problem != null) {
|
---|
| 526 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 527 | foreach (var parameter in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 528 | parameter.Depth = 2;
|
---|
| 529 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
| 530 | }
|
---|
| 531 | }
|
---|
| 532 | }
|
---|
| 533 | #endregion
|
---|
[11620] | 534 |
|
---|
[11676] | 535 | #region AgeLimits calculation
|
---|
[11620] | 536 | private void RecalculateAgeLimits() {
|
---|
| 537 | IEnumerable<int> scheme;
|
---|
| 538 | switch (AgingScheme.Value) {
|
---|
| 539 | case AgingSchemes.Linear: scheme = LinearAgingScheme(); break;
|
---|
| 540 | case AgingSchemes.Fibonacci: scheme = FibonacciAgingScheme(); break;
|
---|
| 541 | case AgingSchemes.Polynomial: scheme = PolynomialAgingScheme(2); break;
|
---|
| 542 | case AgingSchemes.Exponential: scheme = ExponentialAgingScheme(2); break;
|
---|
| 543 | default: throw new NotSupportedException("Aging Scheme " + AgingScheme.Value + " is not supported.");
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | int ageGap = AgeGap.Value;
|
---|
| 547 | AgeLimits = new IntArray(scheme.Select(a => a * ageGap).Take(NumberOfLayers.Value).ToArray());
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | // 1 2 3 4 5 6 7
|
---|
| 551 | private static IEnumerable<int> LinearAgingScheme() {
|
---|
| 552 | for (int i = 0; ; i++)
|
---|
| 553 | yield return i + 1;
|
---|
| 554 | }
|
---|
| 555 | // 1 2 3 5 8 13 21
|
---|
| 556 | private static IEnumerable<int> FibonacciAgingScheme() {
|
---|
| 557 | for (int i = 1, next = 2, temp; ; temp = next, next = i + next, i = temp)
|
---|
| 558 | yield return i;
|
---|
| 559 | }
|
---|
| 560 | // (n^2): 1 2 4 9 16 25 36
|
---|
| 561 | private static IEnumerable<int> PolynomialAgingScheme(double exp) {
|
---|
| 562 | yield return 1;
|
---|
| 563 | yield return 2;
|
---|
| 564 | for (int i = 2; ; i++)
|
---|
| 565 | yield return (int)Math.Pow(i, exp);
|
---|
| 566 |
|
---|
| 567 | }
|
---|
| 568 | // 1 2 4 8 16 32 64
|
---|
| 569 | private static IEnumerable<int> ExponentialAgingScheme(double @base) {
|
---|
| 570 | for (int i = 0; ; i++)
|
---|
| 571 | yield return (int)Math.Pow(@base, i);
|
---|
| 572 | }
|
---|
[11676] | 573 | #endregion
|
---|
[11567] | 574 | }
|
---|
| 575 | } |
---|