#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Analysis;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Optimization.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
using HeuristicLab.Random;
using HeuristicLab.Selection;
namespace HeuristicLab.Algorithms.ALPS {
[Item("ALPS Genetic Algorithm", "A genetic algorithm with an age-layered population structure.")]
[Creatable("Algorithms")]
[StorableClass]
public sealed class AlpsGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
public string Filename { get; set; }
#region Problem Properties
public override Type ProblemType {
get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
}
public new ISingleObjectiveHeuristicOptimizationProblem Problem {
get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
set { base.Problem = value; }
}
#endregion
#region Parameter Properties
private IValueParameter SeedParameter {
get { return (IValueParameter)Parameters["Seed"]; }
}
private IValueParameter SetSeedRandomlyParameter {
get { return (IValueParameter)Parameters["SetSeedRandomly"]; }
}
private IFixedValueParameter AnalyzerParameter {
get { return (IFixedValueParameter)Parameters["Analyzer"]; }
}
private IFixedValueParameter LayerAnalyzerParameter {
get { return (IFixedValueParameter)Parameters["LayerAnalyzer"]; }
}
private IValueParameter NumberOfLayersParameter {
get { return (IValueParameter)Parameters["NumberOfLayers"]; }
}
private IValueParameter PopulationSizeParameter {
get { return (IValueParameter)Parameters["PopulationSize"]; }
}
private IValueParameter MaximumGenerationsParameter {
get { return (IValueParameter)Parameters["MaximumGenerations"]; }
}
private IValueParameter AgingSchemeParameter {
get { return (IValueParameter)Parameters["AgingScheme"]; }
}
private IValueParameter AgeGapParameter {
get { return (IValueParameter)Parameters["AgeGap"]; }
}
private IValueParameter AgeLimitsParameter {
get { return (IValueParameter)Parameters["AgeLimits"]; }
}
private IValueParameter AgeInheritanceParameter {
get { return (IValueParameter)Parameters["AgeInheritance"]; }
}
private IValueParameter MatingPoolRangeParameter {
get { return (IValueParameter)Parameters["MatingPoolRange"]; }
}
private IValueParameter MatingPoolSelectionPercentageParameter {
get { return (IValueLookupParameter)Parameters["MatingPoolSelectionPercentage"]; }
}
public IConstrainedValueParameter SelectorParameter {
get { return (IConstrainedValueParameter)Parameters["Selector"]; }
}
public IConstrainedValueParameter CrossoverParameter {
get { return (IConstrainedValueParameter)Parameters["Crossover"]; }
}
private IValueParameter MutationProbabilityParameter {
get { return (IValueParameter)Parameters["MutationProbability"]; }
}
public IConstrainedValueParameter MutatorParameter {
get { return (IConstrainedValueParameter)Parameters["Mutator"]; }
}
private IValueParameter ElitesParameter {
get { return (IValueParameter)Parameters["Elites"]; }
}
private IFixedValueParameter ReevaluateElitesParameter {
get { return (IFixedValueParameter)Parameters["ReevaluateElites"]; }
}
#endregion
#region Properties
public IntValue Seed {
get { return SeedParameter.Value; }
set { SeedParameter.Value = value; }
}
public BoolValue SetSeedRandomly {
get { return SetSeedRandomlyParameter.Value; }
set { SetSeedRandomlyParameter.Value = value; }
}
public MultiAnalyzer Analyzer {
get { return AnalyzerParameter.Value; }
}
public MultiAnalyzer LayerAnalyzer {
get { return LayerAnalyzerParameter.Value; }
}
public IntValue NumberOfLayers {
get { return NumberOfLayersParameter.Value; }
set { NumberOfLayersParameter.Value = value; }
}
public IntArray PopulationSize {
get { return PopulationSizeParameter.Value; }
set { PopulationSizeParameter.Value = value; }
}
public IntValue MaximumGenerations {
get { return MaximumGenerationsParameter.Value; }
set { MaximumGenerationsParameter.Value = value; }
}
public AgingScheme AgingScheme {
get { return AgingSchemeParameter.Value; }
set { AgingSchemeParameter.Value = value; }
}
public IntValue AgeGap {
get { return AgeGapParameter.Value; }
set { AgeGapParameter.Value = value; }
}
public IntArray AgeLimits {
get { return AgeLimitsParameter.Value; }
set { AgeLimitsParameter.Value = value; }
}
public ReductionOperation AgeInheritance {
get { return AgeInheritanceParameter.Value; }
set { AgeInheritanceParameter.Value = value; }
}
public IntValue MatingPoolRange {
get { return MatingPoolRangeParameter.Value; }
set { MatingPoolRangeParameter.Value = value; }
}
public PercentValue MatingPoolSelectionPercentage {
get { return MatingPoolSelectionPercentageParameter.Value; }
set { MatingPoolSelectionPercentageParameter.Value = value; }
}
public ISelector Selector {
get { return SelectorParameter.Value; }
set { SelectorParameter.Value = value; }
}
public ICrossover Crossover {
get { return CrossoverParameter.Value; }
set { CrossoverParameter.Value = value; }
}
public PercentValue MutationProbability {
get { return MutationProbabilityParameter.Value; }
set { MutationProbabilityParameter.Value = value; }
}
public IManipulator Mutator {
get { return MutatorParameter.Value; }
set { MutatorParameter.Value = value; }
}
public IntValue Elites {
get { return ElitesParameter.Value; }
set { ElitesParameter.Value = value; }
}
public bool ReevaluteElites {
get { return ReevaluateElitesParameter.Value.Value; }
set { ReevaluateElitesParameter.Value.Value = value; }
}
private RandomCreator GlobalRandomCreator {
get { return (RandomCreator)OperatorGraph.InitialOperator; }
}
private SolutionsCreator SolutionsCreator {
get { return OperatorGraph.Iterate().OfType().First(); }
}
private AlpsGeneticAlgorithmMainLoop MainLoop {
get { return OperatorGraph.Iterate().OfType().First(); }
}
#endregion
#region Preconfigured Analyzers
[Storable]
private BestAverageWorstQualityAnalyzer qualityAnalyzer;
[Storable]
private BestAverageWorstQualityAnalyzer layerQualityAnalyzer;
#endregion
[StorableConstructor]
private AlpsGeneticAlgorithm(bool deserializing)
: base(deserializing) { }
private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
: base(original, cloner) {
qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer);
Initialize();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new AlpsGeneticAlgorithm(this, cloner);
}
public AlpsGeneticAlgorithm()
: base() {
Parameters.Add(new ValueParameter("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
Parameters.Add(new ValueParameter("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
Parameters.Add(new FixedValueParameter("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
Parameters.Add(new FixedValueParameter("LayerAnalyzer", "The operator used to analyze each layer.", new MultiAnalyzer()));
Parameters.Add(new ValueParameter("NumberOfLayers", "The number of layers.", new IntValue(10)));
Parameters.Add(new ValueParameter("PopulationSize", "The size of the population of solutions each layer.", new IntArray(new[] { 100 })));
Parameters.Add(new ValueParameter("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
Parameters.Add(new ValueParameter("AgingScheme", "The aging scheme for setting the age-limits for the layers.", new AgingScheme(AgingSchemes.Polynomial)));
Parameters.Add(new ValueParameter("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers", new IntValue(20)));
Parameters.Add(new ValueParameter("AgeLimits", new IntArray(new int[0])) { Hidden = true });
Parameters.Add(new ValueParameter("AgeInheritance", "The operator for determining the age of an offspring based the parents' age.", new ReductionOperation(ReductionOperations.Max)) { Hidden = true });
Parameters.Add(new ValueParameter("MatingPoolRange", "The range of layers used for creating a mating pool. (1 = current + previous layer)", new IntValue(1)) { Hidden = true });
Parameters.Add(new ValueParameter("MatingPoolSelectionPercentage", "Percentage of the previous layers used for creating a mating pool.", new PercentValue(1.0, restrictToUnitInterval: true)) { Hidden = true });
Parameters.Add(new ConstrainedValueParameter("Selector", "The operator used to select solutions for reproduction."));
Parameters.Add(new ConstrainedValueParameter("Crossover", "The operator used to cross solutions."));
Parameters.Add(new ValueParameter("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
Parameters.Add(new OptionalConstrainedValueParameter("Mutator", "The operator used to mutate solutions."));
Parameters.Add(new ValueParameter("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
Parameters.Add(new FixedValueParameter("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
var globalRandomCreator = new RandomCreator();
var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
var layer0Processor = new LayerUniformSubScopesProcessor();
var localRandomCreator = new LocalRandomCreator();
var layerVariableCreator = new VariableCreator();
var layerSolutionsCreator = new SolutionsCreator();
var initializeAgeProcessor = new UniformSubScopesProcessor();
var initializeAge = new VariableCreator() { Name = "Initialize Age" };
var initializeLayerPopulationSize = new SubScopesCounter() { Name = "Init LayerPopulationCounter" };
var initializeLocalEvaluatedSolutions = new Assigner() { Name = "Initialize LayerEvaluatedSolutions" };
var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
var resultsCollector = new ResultsCollector();
var mainLoop = new AlpsGeneticAlgorithmMainLoop();
OperatorGraph.InitialOperator = globalRandomCreator;
globalRandomCreator.RandomParameter.ActualName = "GlobalRandom";
globalRandomCreator.SeedParameter.Value = null;
globalRandomCreator.SetSeedRandomlyParameter.Value = null;
globalRandomCreator.Successor = layer0Creator;
layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
layer0Creator.Successor = layer0Processor;
layer0Processor.Operator = localRandomCreator;
layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
localRandomCreator.Successor = layerVariableCreator;
layerVariableCreator.CollectedValues.Add(new ValueParameter("Layer", new IntValue(0)));
layerVariableCreator.Successor = layerSolutionsCreator;
layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
layerSolutionsCreator.Successor = initializeAgeProcessor;
initializeAgeProcessor.Operator = initializeAge;
initializeAgeProcessor.Successor = initializeLayerPopulationSize;
initializeLayerPopulationSize.ValueParameter.ActualName = "LayerPopulationSize";
initializeLayerPopulationSize.Successor = initializeLocalEvaluatedSolutions;
initializeAge.CollectedValues.Add(new ValueParameter("Age", new IntValue(0)));
initializeAge.Successor = null;
initializeLocalEvaluatedSolutions.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
initializeLocalEvaluatedSolutions.RightSideParameter.ActualName = "LayerPopulationSize";
initializeLocalEvaluatedSolutions.Successor = null;
initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
resultsCollector.CollectedValues.Add(new LookupParameter("Evaluated Solutions", null, "EvaluatedSolutions"));
resultsCollector.Successor = mainLoop;
foreach (var selector in ApplicationManager.Manager.GetInstances().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
SelectorParameter.ValidValues.Add(selector);
var porportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(s => s is ProportionalSelector);
if (porportionalSelector != null) SelectorParameter.Value = porportionalSelector;
ParameterizeSelectors();
qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
RecalculateAgeLimits();
ParameterizeAnalyzers();
UpdateAnalyzers();
Initialize();
}
public override void Prepare() {
if (Problem != null)
base.Prepare();
}
#region Events
protected override void OnProblemChanged() {
ParameterizeStochasticOperator(Problem.SolutionCreator);
ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
foreach (var @operator in Problem.Operators.OfType())
ParameterizeStochasticOperator(@operator);
ParameterizeSolutionsCreator();
ParameterizeMainLoop();
ParameterizeSelectors();
ParameterizeAnalyzers();
ParameterizeIterationBasedOperators();
UpdateCrossovers();
UpdateMutators();
UpdateAnalyzers();
Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
base.OnProblemChanged();
}
protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
ParameterizeStochasticOperator(Problem.SolutionCreator);
ParameterizeSolutionsCreator();
base.Problem_SolutionCreatorChanged(sender, e);
}
protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
ParameterizeSolutionsCreator();
ParameterizeMainLoop();
ParameterizeSelectors();
ParameterizeAnalyzers();
Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
base.Problem_EvaluatorChanged(sender, e);
}
protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
foreach (var @operator in Problem.Operators.OfType())
ParameterizeStochasticOperator(@operator);
ParameterizeIterationBasedOperators();
UpdateCrossovers();
UpdateMutators();
UpdateAnalyzers();
base.Problem_OperatorsChanged(sender, e);
}
private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
ParameterizeMainLoop();
ParameterizeSelectors();
ParameterizeAnalyzers();
}
void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
PopulationSizeParameter.ValueChanged += PopulationSize_ValueChanged;
ParameterizeSelectors();
}
void PopulationSize_ValueChanged(object sender, EventArgs e) {
ParameterizeSelectors();
}
void ElitesParameter_ValueChanged(object sender, EventArgs e) {
Elites.ValueChanged += ElitesParameter_ValueChanged;
ParameterizeSelectors();
}
void Elites_ValueChanged(object sender, EventArgs e) {
ParameterizeSelectors();
}
private void AgeGapParameter_ValueChanged(object sender, EventArgs e) {
AgeGap.ValueChanged += AgeGap_ValueChanged;
RecalculateAgeLimits();
}
private void AgeGap_ValueChanged(object sender, EventArgs e) {
RecalculateAgeLimits();
}
private void AgingSchemeParameter_ValueChanged(object sender, EventArgs e) {
AgingScheme.ValueChanged += AgingScheme_ValueChanged;
RecalculateAgeLimits();
}
private void AgingScheme_ValueChanged(object sender, EventArgs e) {
RecalculateAgeLimits();
}
private void NumberOfLayersParameter_ValueChanged(object sender, EventArgs e) {
NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
RecalculateAgeLimits();
}
private void NumberOfLayers_ValueChanged(object sender, EventArgs e) {
RecalculateAgeLimits();
}
private void AnalyzerOperators_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
foreach (var analyzer in e.Items) {
foreach (var parameter in analyzer.Value.Parameters.OfType()) {
parameter.Depth = 2;
}
}
}
private void LayerAnalyzerOperators_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
foreach (var analyzer in e.Items) {
IParameter resultParameter;
if (analyzer.Value.Parameters.TryGetValue("Results", out resultParameter)) {
var lookupParameter = resultParameter as ILookupParameter;
if (lookupParameter != null)
lookupParameter.ActualName = "LayerResults";
}
foreach (var parameter in analyzer.Value.Parameters.OfType()) {
parameter.Depth = 1;
}
}
}
#endregion
#region Parameterization
private void Initialize() {
PopulationSizeParameter.ValueChanged += PopulationSizeParameter_ValueChanged;
//PopulationSize.ValueChanged += PopulationSize_ValueChanged; TODO
ElitesParameter.ValueChanged += ElitesParameter_ValueChanged;
Elites.ValueChanged += Elites_ValueChanged;
if (Problem != null)
Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
AgeGapParameter.ValueChanged += AgeGapParameter_ValueChanged;
AgeGap.ValueChanged += AgeGap_ValueChanged;
AgingSchemeParameter.ValueChanged += AgingSchemeParameter_ValueChanged;
AgingScheme.ValueChanged += AgingScheme_ValueChanged;
NumberOfLayersParameter.ValueChanged += NumberOfLayersParameter_ValueChanged;
NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
Analyzer.Operators.ItemsAdded += AnalyzerOperators_ItemsAdded;
LayerAnalyzer.Operators.ItemsAdded += LayerAnalyzerOperators_ItemsAdded;
}
private void ParameterizeSolutionsCreator() {
SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
}
private void ParameterizeMainLoop() {
MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
}
private void ParameterizeSelectors() {
foreach (var selector in SelectorParameter.ValidValues) {
selector.CopySelected = new BoolValue(true);
// Explicit setting of NumberOfSelectedSubScopesParameter is not required anymore because the NumberOfSelectedSubScopesCalculator calculates it itself
//selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize - Elites.Value));
selector.NumberOfSelectedSubScopesParameter.Hidden = true;
ParameterizeStochasticOperatorForLayer(selector);
}
if (Problem != null) {
foreach (var selector in SelectorParameter.ValidValues.OfType()) {
selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
selector.MaximizationParameter.Hidden = true;
selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
selector.QualityParameter.Hidden = true;
}
}
}
private void ParameterizeAnalyzers() {
qualityAnalyzer.ResultsParameter.ActualName = "Results";
qualityAnalyzer.ResultsParameter.Hidden = true;
qualityAnalyzer.QualityParameter.Depth = 2;
layerQualityAnalyzer.ResultsParameter.ActualName = "Results";
layerQualityAnalyzer.ResultsParameter.Hidden = true;
layerQualityAnalyzer.QualityParameter.Depth = 1;
if (Problem != null) {
qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
qualityAnalyzer.MaximizationParameter.Hidden = true;
qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
qualityAnalyzer.QualityParameter.Hidden = true;
qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
layerQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
layerQualityAnalyzer.MaximizationParameter.Hidden = true;
layerQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
layerQualityAnalyzer.QualityParameter.Hidden = true;
layerQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
layerQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
}
}
private void ParameterizeIterationBasedOperators() {
if (Problem != null) {
foreach (var @operator in Problem.Operators.OfType()) {
@operator.IterationsParameter.ActualName = "Generations";
@operator.IterationsParameter.Hidden = true;
@operator.MaximumIterationsParameter.ActualName = "MaximumGenerations";
@operator.MaximumIterationsParameter.Hidden = true;
}
}
}
private void ParameterizeStochasticOperator(IOperator @operator) {
var stochasticOperator = @operator as IStochasticOperator;
if (stochasticOperator != null) {
stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.RandomParameter.ActualName;
stochasticOperator.RandomParameter.Hidden = true;
}
}
private void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
var stochasticOperator = @operator as IStochasticOperator;
if (stochasticOperator != null) {
stochasticOperator.RandomParameter.ActualName = "LocalRandom";
stochasticOperator.RandomParameter.Hidden = true;
}
}
#endregion
#region Updates
private void UpdateCrossovers() {
var oldCrossover = CrossoverParameter.Value;
var defaultCrossover = Problem.Operators.OfType().FirstOrDefault();
CrossoverParameter.ValidValues.Clear();
foreach (var crossover in Problem.Operators.OfType().OrderBy(c => c.Name)) {
ParameterizeStochasticOperatorForLayer(crossover);
CrossoverParameter.ValidValues.Add(crossover);
}
if (oldCrossover != null) {
var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
if (crossover != null)
CrossoverParameter.Value = crossover;
else
oldCrossover = null;
}
if (oldCrossover == null && defaultCrossover != null)
CrossoverParameter.Value = defaultCrossover;
}
private void UpdateMutators() {
var oldMutator = MutatorParameter.Value;
MutatorParameter.ValidValues.Clear();
foreach (var mutator in Problem.Operators.OfType().OrderBy(m => m.Name)) {
ParameterizeStochasticOperatorForLayer(mutator);
MutatorParameter.ValidValues.Add(mutator);
}
if (oldMutator != null) {
var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
if (mutator != null)
MutatorParameter.Value = mutator;
}
}
private void UpdateAnalyzers() {
Analyzer.Operators.Clear();
LayerAnalyzer.Operators.Clear();
Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
LayerAnalyzer.Operators.Add(layerQualityAnalyzer, layerQualityAnalyzer.EnabledByDefault);
if (Problem != null) {
foreach (var analyzer in Problem.Operators.OfType()) {
Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
}
}
}
#endregion
#region AgeLimits calculation
private void RecalculateAgeLimits() {
IEnumerable scheme;
switch (AgingScheme.Value) {
case AgingSchemes.Linear: scheme = LinearAgingScheme(); break;
case AgingSchemes.Fibonacci: scheme = FibonacciAgingScheme(); break;
case AgingSchemes.Polynomial: scheme = PolynomialAgingScheme(2); break;
case AgingSchemes.Exponential: scheme = ExponentialAgingScheme(2); break;
default: throw new NotSupportedException("Aging Scheme " + AgingScheme.Value + " is not supported.");
}
int ageGap = AgeGap.Value;
AgeLimits = new IntArray(scheme.Select(a => a * ageGap).Take(NumberOfLayers.Value).ToArray());
}
// 1 2 3 4 5 6 7
private static IEnumerable LinearAgingScheme() {
for (int i = 0; ; i++)
yield return i + 1;
}
// 1 2 3 5 8 13 21
private static IEnumerable FibonacciAgingScheme() {
for (int i = 1, next = 2, temp; ; temp = next, next = i + next, i = temp)
yield return i;
}
// (n^2): 1 2 4 9 16 25 36
private static IEnumerable PolynomialAgingScheme(double exp) {
yield return 1;
yield return 2;
for (int i = 2; ; i++)
yield return (int)Math.Pow(i, exp);
}
// 1 2 4 8 16 32 64
private static IEnumerable ExponentialAgingScheme(double @base) {
for (int i = 0; ; i++)
yield return (int)Math.Pow(@base, i);
}
#endregion
}
}