using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Algorithms.ALPS; 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.IslandALPS { [Item("IslandAlpsOffspringSelectionAlgorithm", "An island version of ALPS with offspring selection")] [StorableClass] [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 100)] public class IslandAlpsOffspringSelectionAlgorithm : 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 #region ALPSParameter public IValueParameter AgeGapParameter { get { return (IValueParameter) Parameters["AgeGap"]; } } public IValueParameter> AgingSchemeParameter { get { return (IValueParameter>) Parameters["AgingScheme"]; } } public IValueParameter AgeInheritanceParameter { get { return (IValueParameter) Parameters["AgeInheritance"]; } } public IValueParameter AgeLimitsParameter { get { return (IValueParameter) Parameters["AgeLimits"]; } } public IValueParameter ReduceToPopulationSizeParameter { get { return (IValueParameter) Parameters["ReduceToPopulationSize"]; } } public IValueParameter NumberOfLayersParameter { get { return (IValueParameter) Parameters["NumberOfLayers"]; } } public IValueParameter LayerAnalyzerParameter { get { return (IValueParameter) Parameters["LayerAnalyzer"]; } } public IValueParameter GroupAnalyzerParameter { get { return (IValueParameter) Parameters["GroupAnalyzer"]; } } #endregion #region OSParameter public IFixedValueParameter SuccessRatioParameter { get { return (IFixedValueParameter) Parameters["SuccessRatio"]; } } public IFixedValueParameter ComparisonFactorParameter { get { return (IFixedValueParameter) Parameters["ComparisonFactor"]; } } public IFixedValueParameter MaximumSelectionPressureParameter { get { return (IFixedValueParameter) Parameters["MaximumSelectionPressure"]; } } public IFixedValueParameter OffspringSelectionBeforeMutationParameter { get { return (IFixedValueParameter) Parameters["OffspringSelectionBeforeMutation"]; } } public IFixedValueParameter SelectedParentsParameter { get { return (IFixedValueParameter) Parameters["SelectedParents"]; } } #endregion #region IslandParameter public IValueParameter IslandAnalyzerParameter { get { return (IValueParameter) Parameters["IslandAnalyzer"]; } } public IValueParameter NumberOfIslandsParameter { get { return (IValueParameter) Parameters["NumberOfIslands"]; } } public IValueParameter MigrationRateParameter { get { return (IValueParameter) Parameters["MigrationRate"]; } } public IValueParameter MigrationIntervalParameter { get { return (IValueParameter) Parameters["MigrationInterval"]; } } public IConstrainedValueParameter MigratorParameter { get { return (IConstrainedValueParameter) Parameters["Migrator"]; } } public IConstrainedValueParameter EmigrantsSelectorParameter { get { return (IConstrainedValueParameter) Parameters["EmigrantsSelector"]; } } public IConstrainedValueParameter ImmigrationReplacerParameter { get { return (IConstrainedValueParameter) Parameters["ImmigrationReplacer"]; } } #endregion #region GeneralParameter public IValueParameter SeedParameter { get { return (IValueParameter) Parameters["Seed"]; } } public IValueParameter SetSeedRandomlyParameter { get { return (IValueParameter) Parameters["SetSeedRandomly"]; } } public IValueParameter PopulationSizeParameter { get { return (IValueParameter) Parameters["PopulationSize"]; } } public IValueParameter AnalyzerParameter { get { return (IValueParameter) Parameters["Analyzer"]; } } public IConstrainedValueParameter SelectorParameter { get { return (IConstrainedValueParameter) Parameters["Selector"]; } } public IConstrainedValueParameter CrossoverParameter { get { return (IConstrainedValueParameter) Parameters["Crossover"]; } } public IValueParameter MutationProbabilityParameter { get { return (IValueParameter) Parameters["MutationProbability"]; } } public IConstrainedValueParameter MutatorParameter { get { return (IConstrainedValueParameter) Parameters["Mutator"]; } } public IValueParameter ElitesParameter { get { return (IValueParameter) Parameters["Elites"]; } } public IFixedValueParameter ReevaluateElitesParameter { get { return (IFixedValueParameter) Parameters["ReevaluateElites"]; } } public IValueParameter TerminatorParameter { get { return (IValueParameter) Parameters["Terminator"]; } } #endregion #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 IntValue PopulationSize { get { return PopulationSizeParameter.Value; } set { PopulationSizeParameter.Value = value; } } public IntValue NumberOfIslands { get { return NumberOfIslandsParameter.Value; } set { NumberOfIslandsParameter.Value = value; } } public MultiAnalyzer Analyzer { get { return AnalyzerParameter.Value; } set { AnalyzerParameter.Value = value; } } public MultiAnalyzer IslandAnalyzer { get { return IslandAnalyzerParameter.Value; } set { IslandAnalyzerParameter.Value = value; } } public MultiAnalyzer LayerAnalyzer { get { return LayerAnalyzerParameter.Value; } set { LayerAnalyzerParameter.Value = value; } } public MultiAnalyzer GroupAnalyzer { get { return GroupAnalyzerParameter.Value; } set { GroupAnalyzerParameter.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; } } public MultiTerminator Terminators { get { return TerminatorParameter.Value; } } public EnumValue AgingScheme { get { return AgingSchemeParameter.Value; } set { AgingSchemeParameter.Value = value; } } public IntValue AgeGap { get { return AgeGapParameter.Value; } set { AgeGapParameter.Value = value; } } public DoubleValue AgeInheritance { get { return AgeInheritanceParameter.Value; } set { AgeInheritanceParameter.Value = value; } } public IntArray AgeLimits { get { return AgeLimitsParameter.Value; } set { AgeLimitsParameter.Value = value; } } public IntValue NumberOfLayers { get { return NumberOfLayersParameter.Value; } set { NumberOfLayersParameter.Value = value; } } #endregion #region Helper Properties private IslandAlpsOffspringSelectionAlgorithmMainLoop MainLoop { get { return OperatorGraph.Iterate().OfType().First(); } } private SolutionsCreator SolutionsCreator { get { return OperatorGraph.Iterate().OfType().First(); } } #endregion #region Preconfigured Terminators [Storable] private readonly ComparisonTerminator generationsTerminator; [Storable] private readonly ComparisonTerminator evaluationsTerminator; [Storable] private readonly SingleObjectiveQualityTerminator qualityTerminator; [Storable] private readonly ExecutionTimeTerminator executionTimeTerminator; #endregion #region Preconfigured Analyzers [Storable] private readonly BestAverageWorstQualityAnalyzer qualityAnalyzer; [Storable] private readonly BestAverageWorstQualityAnalyzer islandQualityAnalyzer; [Storable] private readonly BestAverageWorstQualityAnalyzer layerQualityAnalyzer; [Storable] private readonly BestAverageWorstQualityAnalyzer groupQualityAnalyzer; [Storable] private readonly ValueAnalyzer layerSelectionPressureAnalyzer; [Storable] private readonly ValueAnalyzer currentSucessRatioAnalyzer; #endregion #region Constructors [StorableConstructor] public IslandAlpsOffspringSelectionAlgorithm(bool deserializing) : base(deserializing) {} [StorableHook(HookType.AfterDeserialization)] public void AfterDeserialization() { InitializeEventHandler(); } public IslandAlpsOffspringSelectionAlgorithm(IslandAlpsOffspringSelectionAlgorithm original, Cloner cloner) : base(original, cloner) { //clone Properties //analyzer qualityAnalyzer = cloner.Clone(original.qualityAnalyzer); islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer); layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer); groupQualityAnalyzer = cloner.Clone(original.groupQualityAnalyzer); layerSelectionPressureAnalyzer = cloner.Clone(original.layerSelectionPressureAnalyzer); currentSucessRatioAnalyzer = cloner.Clone(currentSucessRatioAnalyzer); //terminator generationsTerminator = cloner.Clone(original.generationsTerminator); evaluationsTerminator = cloner.Clone(original.evaluationsTerminator); qualityTerminator = cloner.Clone(original.qualityTerminator); executionTimeTerminator = cloner.Clone(original.executionTimeTerminator); InitializeEventHandler(); } public IslandAlpsOffspringSelectionAlgorithm() { AddParameters(); #region create Operators var globalRandomCreator = new RandomCreator(); var islandLayerCreator = new SubScopesCreator {Name = "Create Islands"}; var islandUssp = new UniformSubScopesProcessor(); var islandRandomCreator = new LocalRandomCreator(); var layerZeroCreator = new SubScopesCreator {Name = "Create Layer 0"}; var layerZeroUssp = new UniformSubScopesProcessor(); var layerRandomCreator = new LocalRandomCreator(); var islandSolutionsCounter = new DataReducer {Name = "Count IslandSolutions"}; var layerSolutionCreator = new SolutionsCreator(); var ageInitializerUssp = new UniformSubScopesProcessor(); var ageInitializerVc = new VariableCreator {Name = "Initialize Age"}; var evaluatedSolutionsVc = new VariableCreator {Name = "Create LayerEvaluatedSolutions"}; var evaluatedSolutionsCounter = new SubScopesCounter {Name = "Count LayerEvaluatedSolutions"}; var globalEvaluatedSolutionsCounter = new DataReducer {Name = "Count GlobalSolutions"}; var resultsCollector = new ResultsCollector(); var mainLoop = new IslandAlpsOffspringSelectionAlgorithmMainLoop(); #endregion #region Set selectors var selectors = ApplicationManager.Manager.GetInstances() .Where(s => !(s is IMultiObjectiveSelector)) .OrderBy(s => Name); foreach (var selector in selectors) SelectorParameter.ValidValues.Add(selector); var defaultSelector = SelectorParameter.ValidValues.OfType().FirstOrDefault(); if (defaultSelector != null) { defaultSelector.PressureParameter.Value = new DoubleValue(4.0); SelectorParameter.Value = defaultSelector; } #endregion #region Create Analyzers qualityAnalyzer = new BestAverageWorstQualityAnalyzer(); islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer(); layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer(); groupQualityAnalyzer = new BestAverageWorstQualityAnalyzer(); layerSelectionPressureAnalyzer = new ValueAnalyzer(); currentSucessRatioAnalyzer = new ValueAnalyzer(); #endregion #region Create terminators generationsTerminator = new ComparisonTerminator("Generations", ComparisonType.Less, new IntValue(1000)) { Name = "Generations" }; evaluationsTerminator = new ComparisonTerminator("EvaluatedSolutions", ComparisonType.Less, new IntValue(int.MaxValue)) {Name = "Evaluations"}; qualityTerminator = new SingleObjectiveQualityTerminator {Name = "Quality"}; executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5))); #endregion #region Operator Graph globalRandomCreator.RandomParameter.ActualName = "GlobalRandom"; globalRandomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name; globalRandomCreator.SetSeedRandomlyParameter.Value = null; globalRandomCreator.SeedParameter.ActualName = SeedParameter.Name; globalRandomCreator.SeedParameter.Value = null; //create islands islandLayerCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name; islandRandomCreator.GlobalRandomParameter.ActualName = globalRandomCreator.RandomParameter.ActualName; islandRandomCreator.LocalRandomParameter.ActualName = "IslandRandom"; layerZeroCreator.NumberOfSubScopesParameter.Value = new IntValue(1); islandSolutionsCounter.ParameterToReduce.Depth = 1; islandSolutionsCounter.ParameterToReduce.ActualName = "LayerEvaluatedSolutions"; islandSolutionsCounter.TargetParameter.ActualName = "IslandEvaluatedSolutions"; islandSolutionsCounter.ReductionOperation.Value.Value = ReductionOperations.Sum; islandSolutionsCounter.TargetOperation.Value.Value = ReductionOperations.Assign; layerRandomCreator.GlobalRandomParameter.ActualName = globalRandomCreator.RandomParameter.ActualName; layerRandomCreator.LocalRandomParameter.ActualName = "LayerRandom"; layerSolutionCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name; layerSolutionCreator.ParallelParameter.Value = new BoolValue(true); ageInitializerVc.CollectedValues.Add(new ValueParameter("Age", new DoubleValue(0))); evaluatedSolutionsVc.CollectedValues.Add(new ValueParameter("LayerEvaluatedSolutions", new IntValue(0))); evaluatedSolutionsCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions"; globalEvaluatedSolutionsCounter.ParameterToReduce.Depth = 1; globalEvaluatedSolutionsCounter.ParameterToReduce.ActualName = "IslandEvaluatedSolutions"; globalEvaluatedSolutionsCounter.TargetParameter.ActualName = "EvaluatedSolutions"; globalEvaluatedSolutionsCounter.ReductionOperation.Value.Value = ReductionOperations.Sum; globalEvaluatedSolutionsCounter.TargetOperation.Value.Value = ReductionOperations.Assign; resultsCollector.CollectedValues.Add(new LookupParameter("EvaluatedSolutions")); //flow OperatorGraph.InitialOperator = globalRandomCreator; globalRandomCreator.Successor = islandLayerCreator; islandLayerCreator.Successor = islandUssp; islandUssp.Operator = islandRandomCreator; islandRandomCreator.Successor = layerZeroCreator; layerZeroCreator.Successor = layerZeroUssp; layerZeroUssp.Operator = layerRandomCreator; layerRandomCreator.Successor = layerSolutionCreator; layerSolutionCreator.Successor = ageInitializerUssp; ageInitializerUssp.Operator = ageInitializerVc; ageInitializerVc.Successor = null; ageInitializerUssp.Successor = evaluatedSolutionsVc; evaluatedSolutionsVc.Successor = evaluatedSolutionsCounter; evaluatedSolutionsCounter.Successor = null; layerZeroUssp.Successor = islandSolutionsCounter; islandSolutionsCounter.Successor = null; islandUssp.Successor = globalEvaluatedSolutionsCounter; globalEvaluatedSolutionsCounter.Successor = resultsCollector; resultsCollector.Successor = mainLoop; #endregion #region parametrize UpdateAnalyzers(); ParameterizeAnalyzers(); ParameterizeSelectors(); UpdateTerminators(); ParameterizeAgeLimits(); ParametrizeMigration(); #endregion InitializeEventHandler(); } private void AddParameters() { #region ALPSParameter 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("AgeInheritance", "A weight that determines the age of a child after crossover based on the older (1.0) and younger (0.0) parent.", new DoubleValue(1))); Parameters.Add(new ValueParameter("AgeLimits", "The maximum age an individual is allowed to reach in a certain layer.", new IntArray(new int[0])) {Hidden = true}); Parameters.Add(new ValueParameter>("AgingScheme", "The aging scheme for setting the age-limits for the layers.", new EnumValue(ALPS.AgingScheme.Polynomial))); Parameters.Add(new ValueParameter("ReduceToPopulationSize", ".", new BoolValue(true))); Parameters.Add(new ValueParameter("NumberOfLayers", "", new IntValue(10000))); Parameters.Add(new ValueParameter("LayerAnalyzer", "The operator used to analyze each layer.", new MultiAnalyzer())); Parameters.Add(new ValueParameter("GroupAnalyzer", "The operator used to analyze each layergroup.", new MultiAnalyzer())); #endregion #region OS Parameters.Add(new FixedValueParameter("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1))); Parameters.Add(new FixedValueParameter("ComparisonFactor", "The comparison factor is used to determine whether the offspring should be compared to the better parent, the worse parent or a quality value linearly interpolated between them. It is in the range [0;1].", new DoubleValue(1))); Parameters.Add(new FixedValueParameter("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100))); Parameters.Add(new FixedValueParameter("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation.", new BoolValue(false))); Parameters.Add(new FixedValueParameter("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))); #endregion #region IslandParameter Parameters.Add(new ValueParameter("NumberOfIslands", "The number of islands.", new IntValue(5))); Parameters.Add(new ValueParameter("MigrationRate", "How many individuals should migrate.", new PercentValue(0.15))); Parameters.Add(new ValueParameter("MigrationInterval", "The number Generations before a migration occurs.", new IntValue(20))); Parameters.Add(new ValueParameter("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer())); Parameters.Add(new ConstrainedValueParameter("Migrator", "The migration strategy.")); Parameters.Add(new ConstrainedValueParameter("EmigrantsSelector", "Selects the individuals that will be migrated.")); Parameters.Add(new ConstrainedValueParameter("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants.")); #endregion #region GeneralParameter 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 ValueParameter("PopulationSize", "The size of the population of solutions.", new IntValue(100))); Parameters.Add(new ValueParameter("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer())); 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}); Parameters.Add(new ValueParameter("Terminator", "The termination criteria that defines if the algorithm should continue or stop.", new MultiTerminator())); #endregion } public override IDeepCloneable Clone(Cloner cloner) { return new IslandAlpsOffspringSelectionAlgorithm(this, cloner); } #endregion #region events protected override void RegisterProblemEvents() { base.RegisterProblemEvents(); var maximizationParameter = (IValueParameter)Problem.MaximizationParameter; if (maximizationParameter != null) maximizationParameter.ValueChanged += MaximizationParameter_ValueChanged; } protected override void DeregisterProblemEvents() { var maximizationParameter = (IValueParameter)Problem.MaximizationParameter; if (maximizationParameter != null) maximizationParameter.ValueChanged -= MaximizationParameter_ValueChanged; base.DeregisterProblemEvents(); } protected override void OnProblemChanged() { base.OnProblemChanged(); ParameterizeStochasticOperator(Problem.SolutionCreator); ParameterizeStochasticOperatorForLayer(Problem.Evaluator); foreach (var op in Problem.Operators.OfType()) { ParameterizeStochasticOperator(op); } ParameterizeSolutionsCreator(); ParameterizeMainLoop(); ParameterizeSelectors(); ParameterizeAnalyzers(); ParameterizeTerminators(); ParameterizeIterationBasedOperators(); Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; UpdateAnalyzers(); UpdateCrossovers(); UpdateMutators(); UpdateTerminators(); } protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { base.Problem_SolutionCreatorChanged(sender, e); ParameterizeStochasticOperator(Problem.SolutionCreator); ParameterizeStochasticOperatorForLayer(Problem.Evaluator); Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; ParameterizeSolutionsCreator(); ParameterizeAnalyzers(); } protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { base.Problem_EvaluatorChanged(sender, e); ParameterizeStochasticOperatorForLayer(Problem.Evaluator); ParameterizeSolutionsCreator(); ParameterizeMainLoop(); ParameterizeSelectors(); ParameterizeAnalyzers(); ParameterizeTerminators(); Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; UpdateAnalyzers(); UpdateCrossovers(); UpdateMutators(); UpdateTerminators(); } protected override void Problem_OperatorsChanged(object sender, EventArgs e) { base.Problem_OperatorsChanged(sender, e); ParameterizeIterationBasedOperators(); UpdateCrossovers(); UpdateMutators(); UpdateAnalyzers(); UpdateTerminators(); } private void InitializeEventHandler() { PopulationSizeParameter.ValueChanged += PopulationSizeParameter_ValueChanged; PopulationSize.ValueChanged += PopulationSize_ValueChanged; ElitesParameter.ValueChanged += ElitesParameter_ValueChanged; Elites.ValueChanged += Elites_ValueChanged; MigrationRateParameter.Value.ValueChanged += MigrationRate_ValueChanged; MigrationRateParameter.ValueChanged += MigrationRate_ValueChanged; Analyzer.Operators.ItemsAdded += AnalyzerOperators_ItemsAdded; IslandAnalyzer.Operators.ItemsAdded += IslandAnalyzerOperators_ItemsAdded; LayerAnalyzer.Operators.ItemsAdded += LayerAnalyzerOperators_ItemsAdded; GroupAnalyzer.Operators.ItemsAdded += GroupAnalyzerOperators_ItemsAdded; 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; qualityAnalyzer.CurrentBestQualityParameter.NameChanged += QualityAnalyzer_CurrentBestQualityParameter_NameChanged; } private void MigrationRate_ValueChanged(object sender, EventArgs e) { ParametrizeMigration(); } private void GroupAnalyzerOperators_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 = "GroupResults"; } foreach (var parameter in analyzer.Value.Parameters.OfType()) { parameter.Depth = 1; } } } private void IslandAnalyzerOperators_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 = "IslandResults"; } 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; } } } private void AnalyzerOperators_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) { foreach (var analyzer in e.Items) { foreach (var parameter in analyzer.Value.Parameters.OfType()) { parameter.Depth = 3; } } } private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { ParameterizeMainLoop(); ParameterizeSelectors(); ParameterizeAnalyzers(); } private void MaximizationParameter_ValueChanged(object sender, EventArgs e) { ParameterizeTerminators(); } private void QualityAnalyzer_CurrentBestQualityParameter_NameChanged(object sender, EventArgs e) { ParameterizeTerminators(); } private void ElitesParameter_ValueChanged(object sender, EventArgs e) { ParameterizeSelectors(); } private void Elites_ValueChanged(object sender, EventArgs e) { ParameterizeSelectors(); } private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) { ParameterizeSelectors(); } private void PopulationSize_ValueChanged(object sender, EventArgs e) { ParameterizeSelectors(); ParametrizeMigration(); } private void NumberOfLayersParameter_ValueChanged(object sender, EventArgs e) { NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged; ParameterizeAgeLimits(); } private void NumberOfLayers_ValueChanged(object sender, EventArgs e) { ParameterizeAgeLimits(); } private void AgeGapParameter_ValueChanged(object sender, EventArgs e) { AgeGap.ValueChanged += AgeGap_ValueChanged; ParameterizeAgeLimits(); } private void AgeGap_ValueChanged(object sender, EventArgs e) { ParameterizeAgeLimits(); } private void AgingSchemeParameter_ValueChanged(object sender, EventArgs e) { AgingScheme.ValueChanged += AgingScheme_ValueChanged; ParameterizeAgeLimits(); } private void AgingScheme_ValueChanged(object sender, EventArgs e) { ParameterizeAgeLimits(); } #endregion #region Update Functions private void UpdateCrossovers() { var oldCrossover = CrossoverParameter.Value; var defaultCrossover = Problem.Operators.OfType().FirstOrDefault(); CrossoverParameter.ValidValues.Clear(); foreach (var crossover in Problem.Operators.OfType().OrderBy(x => x.Name)) { ParameterizeStochasticOperatorForLayer(crossover); CrossoverParameter.ValidValues.Add(crossover); } if (oldCrossover != null) { var crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.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(x => x.Name)) { ParameterizeStochasticOperatorForLayer(mutator); MutatorParameter.ValidValues.Add(mutator); } if (oldMutator != null) { var mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType()); if (mutator != null) MutatorParameter.Value = mutator; } } private void UpdateAnalyzers() { LayerAnalyzer.Operators.Clear(); IslandAnalyzer.Operators.Clear(); GroupAnalyzer.Operators.Clear(); Analyzer.Operators.Clear(); //add analyzer GroupAnalyzer.Operators.Add(groupQualityAnalyzer, false); LayerAnalyzer.Operators.Add(layerQualityAnalyzer, false); LayerAnalyzer.Operators.Add(layerSelectionPressureAnalyzer, false); LayerAnalyzer.Operators.Add(currentSucessRatioAnalyzer, false); IslandAnalyzer.Operators.Add(islandQualityAnalyzer, false); Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault); //depth for each analyzer gets set when added if (Problem != null) { foreach (var analyzer in Problem.Operators.OfType()) { Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault); IslandAnalyzer.Operators.Add((IAnalyzer) analyzer.Clone(), false); LayerAnalyzer.Operators.Add((IAnalyzer) analyzer.Clone(), false); GroupAnalyzer.Operators.Add((IAnalyzer) analyzer.Clone(), false); } } //setting the right depth because adding analyzer sets the depth layerSelectionPressureAnalyzer.ValueParameter.Depth = 0; currentSucessRatioAnalyzer.ValueParameter.Depth = 0; } private void UpdateTerminators() { var newTerminators = new Dictionary { { generationsTerminator, !Terminators.Operators.Contains(generationsTerminator) || Terminators.Operators.ItemChecked(generationsTerminator) }, { evaluationsTerminator, Terminators.Operators.Contains(evaluationsTerminator) && Terminators.Operators.ItemChecked(evaluationsTerminator) }, { qualityTerminator, Terminators.Operators.Contains(qualityTerminator) && Terminators.Operators.ItemChecked(qualityTerminator) }, { executionTimeTerminator, Terminators.Operators.Contains(executionTimeTerminator) && Terminators.Operators.ItemChecked(executionTimeTerminator) } }; if (Problem != null) { foreach (var terminator in Problem.Operators.OfType()) newTerminators.Add(terminator, !Terminators.Operators.Contains(terminator) || Terminators.Operators.ItemChecked(terminator)); } Terminators.Operators.Clear(); foreach (var newTerminator in newTerminators) Terminators.Operators.Add(newTerminator.Key, newTerminator.Value); } private void ParameterizeAgeLimits() { var scheme = AgingScheme.Value; var ageGap = AgeGap.Value; var numberOfLayers = NumberOfLayers.Value; AgeLimits = scheme.CalculateAgeLimits(ageGap, numberOfLayers); } private void ParameterizeMainLoop() { MainLoop.AgeGapParameter.ActualName = AgeGapParameter.Name; MainLoop.AgeInheritanceParameter.ActualName = AgeInheritanceParameter.Name; MainLoop.AgeLimitsParameter.ActualName = AgeLimitsParameter.Name; MainLoop.ReduceToPopulationSizeParameter.ActualName = ReduceToPopulationSizeParameter.Name; MainLoop.AgeParameter.ActualName = "Age"; MainLoop.GlobalRandomParameter.ActualName = "GlobalRandom"; MainLoop.IslandRandomParameter.ActualName = "IslandRandom"; MainLoop.LayerRandomParameter.ActualName = "LayerRandom"; MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; MainLoop.LayerEvaluatedSolutionsParameter.ActualName = "LayerEvaluatedSolutions"; MainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name; MainLoop.NumberOfLayersParameter.ActualName = NumberOfLayersParameter.Name; MainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name; MainLoop.MigratorParameter.ActualName = MigratorParameter.Name; MainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name; MainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name; MainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name; MainLoop.CurrentPopulationSizeParameter.ActualName = "CurrentPopulationSize"; MainLoop.SelectorParameter.ActualName = SelectorParameter.Name; MainLoop.MutatorParameter.ActualName = MutatorParameter.Name; MainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name; MainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name; MainLoop.ElitesParameter.ActualName = ElitesParameter.Name; MainLoop.TerminatorParameter.ActualName = TerminatorParameter.Name; MainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name; MainLoop.LayerAnalyzerParameter.ActualName = LayerAnalyzerParameter.Name; MainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name; MainLoop.GroupAnalyzerParameter.ActualName = GroupAnalyzerParameter.Name; MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; MainLoop.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name; MainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name; MainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name; MainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name; //MainLoop.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name; } private void ParametrizeMigration() { var migrationSize = (int) Math.Ceiling(MigrationRateParameter.Value.Value*PopulationSize.Value); EmigrantsSelectorParameter.ValidValues.Clear(); foreach (var selector in ApplicationManager.Manager.GetInstances().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name)) { selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(migrationSize); selector.NumberOfSelectedSubScopesParameter.Hidden = true; ParameterizeStochasticOperatorForLayer(selector); EmigrantsSelectorParameter.ValidValues.Add(selector); } ImmigrationReplacerParameter.ValidValues.Clear(); foreach (var replacer in ApplicationManager.Manager.GetInstances().OrderBy(x => x.Name)) { ParameterizeStochasticOperatorForLayer(replacer); ImmigrationReplacerParameter.ValidValues.Add(replacer); } MigratorParameter.ValidValues.Clear(); MigratorParameter.ValidValues.Add(new LayerMigrator()); } private void ParameterizeSolutionsCreator() { SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; } private void ParameterizeSelectors() { foreach (var selector in SelectorParameter.ValidValues) { selector.CopySelected = new BoolValue(true); selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name; selector.NumberOfSelectedSubScopesParameter.Hidden = true; ParameterizeStochasticOperatorForLayer(selector); } if (Problem != null) { foreach (var selector in SelectorParameter.ValidValues.OfType()) { ParametrizeSingleObjectiveSelector(selector); } foreach (var replacer in EmigrantsSelectorParameter.ValidValues.OfType()) { ParametrizeSingleObjectiveSelector(replacer); } foreach (var selector in ImmigrationReplacerParameter.ValidValues.OfType()) { ParametrizeSingleObjectiveSelector(selector); } } } private void ParametrizeSingleObjectiveSelector(ISingleObjectiveSelector selector) { selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; selector.MaximizationParameter.Hidden = true; selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; selector.QualityParameter.Hidden = true; } private void ParameterizeAnalyzers() { layerQualityAnalyzer.ResultsParameter.ActualName = "LayerResults"; layerQualityAnalyzer.ResultsParameter.Hidden = true; islandQualityAnalyzer.ResultsParameter.ActualName = "IslandResults"; islandQualityAnalyzer.ResultsParameter.Hidden = true; qualityAnalyzer.ResultsParameter.ActualName = "Results"; qualityAnalyzer.ResultsParameter.Hidden = true; groupQualityAnalyzer.ResultsParameter.ActualName = "GroupResults"; groupQualityAnalyzer.ResultsParameter.Hidden = true; layerSelectionPressureAnalyzer.Name = "SelectionPressureAnalyzer Layer"; layerSelectionPressureAnalyzer.ResultsParameter.ActualName = "LayerResults"; layerSelectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure"; layerSelectionPressureAnalyzer.ValuesParameter.ActualName = "SelectionPressure History"; currentSucessRatioAnalyzer.Name = "CurrentSuccessRatio Analyzer"; currentSucessRatioAnalyzer.ResultsParameter.ActualName = "Results"; currentSucessRatioAnalyzer.ValueParameter.ActualName = "CurrentSuccessRatio"; currentSucessRatioAnalyzer.ValuesParameter.ActualName = "Success Ratio History"; if (Problem != null) { SetAnalyzerParameter(layerQualityAnalyzer); SetAnalyzerParameter(islandQualityAnalyzer); SetAnalyzerParameter(qualityAnalyzer); SetAnalyzerParameter(groupQualityAnalyzer); } } private void SetAnalyzerParameter(BestAverageWorstQualityAnalyzer analyzer) { analyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; analyzer.MaximizationParameter.Hidden = true; analyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; analyzer.QualityParameter.Hidden = true; analyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name; analyzer.BestKnownQualityParameter.Hidden = true; } private void ParameterizeTerminators() { qualityTerminator.Parameterize(qualityAnalyzer.CurrentBestQualityParameter, Problem); } private void ParameterizeIterationBasedOperators() { if (Problem != null) { foreach (var op in Problem.Operators.OfType()) { op.IterationsParameter.ActualName = "Generations"; op.IterationsParameter.Hidden = true; op.MaximumIterationsParameter.ActualName = generationsTerminator.ThresholdParameter.Name; op.MaximumIterationsParameter.Hidden = true; } } } private void ParameterizeStochasticOperator(IOperator op) { var stochasticOp = op as IStochasticOperator; if (stochasticOp != null) { stochasticOp.RandomParameter.ActualName = "GlobalRandom"; stochasticOp.RandomParameter.Hidden = true; } } private void ParameterizeStochasticOperatorForLayer(IOperator op) { var stochasticOp = op as IStochasticOperator; if (stochasticOp != null) { stochasticOp.RandomParameter.ActualName = "LayerRandom"; stochasticOp.RandomParameter.Hidden = true; } } #endregion } }