#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.Random; namespace HeuristicLab.Algorithms.ALPS { [Item("ALPS", "Base class for all ALPS-based algorithms.")] [StorableClass] public abstract class Alps : 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> 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 AgeInheritanceReductionParameter { get { return (IValueParameter)Parameters["AgeInheritanceReduction"]; } } private IValueParameter MatingPoolRangeParameter { get { return (IValueParameter)Parameters["MatingPoolRange"]; } } private IValueParameter MatingPoolSelectionPercentageParameter { get { return (IValueLookupParameter)Parameters["MatingPoolSelectionPercentage"]; } } private IValueParameter TerminatorParameter { get { return (IValueParameter)Parameters["Terminator"]; } } #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 EnumValue 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 EnumValue AgeInheritance { get { return AgeInheritanceParameter.Value; } set { AgeInheritanceParameter.Value = value; } } private ReductionOperation AgeInheritanceReduction { get { return AgeInheritanceReductionParameter.Value; } set { AgeInheritanceReductionParameter.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 MultiTerminator Terminators { get { return TerminatorParameter.Value; } } #endregion #region Helper Properties protected RandomCreator GlobalRandomCreator { get { return (RandomCreator)OperatorGraph.InitialOperator; } } protected SolutionsCreator SolutionsCreator { get { return OperatorGraph.Iterate().OfType().First(); } } #endregion #region Preconfigured Analyzers [Storable] private BestAverageWorstQualityAnalyzer qualityAnalyzer; [Storable] private BestAverageWorstQualityAnalyzer layerQualityAnalyzer; #endregion #region Preconfigured Terminators [Storable] private ComparisonTerminator evaluationsTerminator; [Storable] private SingleObjectiveQualityTerminator qualityTerminator; [Storable] private ExecutionTimeTerminator executionTimeTerminator; #endregion [StorableConstructor] protected Alps(bool deserializing) : base(deserializing) { } protected Alps(Alps original, Cloner cloner) : base(original, cloner) { qualityAnalyzer = cloner.Clone(original.qualityAnalyzer); layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer); evaluationsTerminator = cloner.Clone(original.evaluationsTerminator); qualityTerminator = cloner.Clone(original.qualityTerminator); executionTimeTerminator = cloner.Clone(original.executionTimeTerminator); Initialize(); } protected Alps() : 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>("AgingScheme", "The aging scheme for setting the age-limits for the layers.", new EnumValue(ALPS.AgingScheme.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 EnumValue(ALPS.AgeInheritance.Older))); Parameters.Add(new ValueParameter("AgeInheritanceReduction") { 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 ValueParameter("Terminator", "The termination criteria which sould be checked.", new MultiTerminator())); qualityAnalyzer = new BestAverageWorstQualityAnalyzer(); layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer(); ParameterizeAgeLimits(); ParameterizeAgeInheritanceReduction(); ParameterizeAnalyzers(); UpdateAnalyzers(); CreateTerminators(); ParameterizeTerminators(); UpdateTerminators(); Initialize(); } #region Events public override void Prepare() { if (Problem != null) base.Prepare(); } protected override void OnProblemChanged() { base.OnProblemChanged(); ParameterizeStochasticOperator(Problem.SolutionCreator); ParameterizeStochasticOperatorForLayer(Problem.Evaluator); foreach (var @operator in Problem.Operators.OfType()) ParameterizeStochasticOperator(@operator); ParameterizeSolutionsCreator(); ParameterizeAnalyzers(); ParameterizeTerminators(); UpdateAnalyzers(); UpdateTerminators(); Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; } protected override void RegisterProblemEvents() { base.RegisterProblemEvents(); var maximizationParameter = (IValueParameter)Problem.MaximizationParameter; if (maximizationParameter != null) maximizationParameter.ValueChanged += new EventHandler(MaximizationParameter_ValueChanged); } protected override void DeregisterProblemEvents() { var maximizationParameter = (IValueParameter)Problem.MaximizationParameter; if (maximizationParameter != null) maximizationParameter.ValueChanged -= new EventHandler(MaximizationParameter_ValueChanged); base.DeregisterProblemEvents(); } protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { base.Problem_SolutionCreatorChanged(sender, e); ParameterizeStochasticOperator(Problem.SolutionCreator); ParameterizeSolutionsCreator(); } protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { base.Problem_EvaluatorChanged(sender, e); ParameterizeStochasticOperatorForLayer(Problem.Evaluator); ParameterizeSolutionsCreator(); ParameterizeAnalyzers(); Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; } protected override void Problem_OperatorsChanged(object sender, EventArgs e) { foreach (var @operator in Problem.Operators.OfType()) ParameterizeStochasticOperator(@operator); UpdateAnalyzers(); UpdateTerminators(); base.Problem_OperatorsChanged(sender, e); } protected virtual void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { ParameterizeAnalyzers(); } private void MaximizationParameter_ValueChanged(object sender, EventArgs e) { ParameterizeTerminators(); } private void QualityAnalyzer_CurrentBestQualityParameter_NameChanged(object sender, EventArgs e) { ParameterizeTerminators(); } 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(); } private void NumberOfLayersParameter_ValueChanged(object sender, EventArgs e) { NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged; ParameterizeAgeLimits(); } private void NumberOfLayers_ValueChanged(object sender, EventArgs e) { ParameterizeAgeLimits(); } private void AgeInheritanceParameter_ValueChanged(object sender, EventArgs e) { AgeInheritance.ValueChanged += AgeInheritance_ValueChanged; ParameterizeAgeInheritanceReduction(); } private void AgeInheritance_ValueChanged(object sender, EventArgs e) { ParameterizeAgeInheritanceReduction(); } 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() { 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; AgeInheritanceParameter.ValueChanged += AgeInheritanceParameter_ValueChanged; AgeInheritance.ValueChanged += AgeInheritance_ValueChanged; Analyzer.Operators.ItemsAdded += AnalyzerOperators_ItemsAdded; LayerAnalyzer.Operators.ItemsAdded += LayerAnalyzerOperators_ItemsAdded; qualityAnalyzer.CurrentBestQualityParameter.NameChanged += new EventHandler(QualityAnalyzer_CurrentBestQualityParameter_NameChanged); } private void ParameterizeSolutionsCreator() { SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; } 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 ParameterizeTerminators() { qualityTerminator.Parameterize(qualityAnalyzer.CurrentBestQualityParameter, Problem); } private void ParameterizeAgeInheritanceReduction() { AgeInheritanceReduction.Value = GetAgeInheritanceReduction(AgeInheritance.Value); } protected abstract ReductionOperations GetAgeInheritanceReduction(AgeInheritance ageInheritance); private void ParameterizeAgeLimits() { var scheme = AgingScheme.Value; int ageGap = AgeGap.Value; int numberOfLayers = NumberOfLayers.Value; AgeLimits = scheme.CalculateAgeLimits(ageGap, numberOfLayers); } protected virtual void ParameterizeStochasticOperator(IOperator @operator) { var stochasticOperator = @operator as IStochasticOperator; if (stochasticOperator != null) { stochasticOperator.RandomParameter.ActualName = "GlobalRandom"; stochasticOperator.RandomParameter.Hidden = true; } } protected virtual 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 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); } } } protected virtual void UpdateTerminators() { var newTerminators = new Dictionary { {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); } protected virtual void CreateTerminators() { 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 } }