#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.Linq; 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.SteadyState { [Item("ALPS Steady-State Genetic Algorithm", "A genetic algorithmn with a steady-state age-layered population structure")] [Creatable("Algorithms")] [StorableClass] public class AlpsSsGeneticAlgorithm : Alps { #region Parameter Properties private IValueParameter LayerSizeParameter { get { return (IValueParameter)Parameters["LayerSize"]; } } private IValueParameter MaximumIterationsParameter { get { return (IValueParameter)Parameters["MaximumIterations"]; } } 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"]; } } private IValueParameter BatchSizeParameter { get { return (IValueParameter)Parameters["BatchSize"]; } } #endregion #region Properties public IntValue LayerSize { get { return LayerSizeParameter.Value; } set { LayerSizeParameter.Value = value; } } public IntValue MaximumIterations { get { return MaximumIterationsParameter.Value; } set { MaximumIterationsParameter.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; } } #endregion private AlpsSsGeneticAlgorithmMainLoop MainLoop { get { return OperatorGraph.Iterate().OfType().First(); } } [StorableConstructor] private AlpsSsGeneticAlgorithm(bool deserializing) : base(deserializing) { } private AlpsSsGeneticAlgorithm(AlpsSsGeneticAlgorithm original, Cloner cloner) : base(original, cloner) { Initialize(); } public override IDeepCloneable Clone(Cloner cloner) { return new AlpsSsGeneticAlgorithm(this, cloner); } public AlpsSsGeneticAlgorithm() : base() { Parameters.Add(new ValueParameter("LayerSize", "The size of the population of solutions each layer.", new IntValue(100))); Parameters.Add(new ValueParameter("MaximumIterations", "The maximum number of iterations that should be processed.", new IntValue(1000))); 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("BatchSize", "Number of inner iterations before updates and analyzers are fired.", new IntValue(100)) { Hidden = true }); AgeInheritance = new ReductionOperation(ReductionOperations.Min); var randomCreator = new RandomCreator(); var workingScopeCreator = new NamedSubScopesCreator() { Name = "Create Working- and Layers-Scope" }; var layersProcessor = new NamedSubScopeProcessor() { Name = "Process Layers-Scope" }; var layerCreator = new SubScopesCreator() { Name = "Create Layers" }; var layerProcessor = new UniformSubScopesProcessor(); var layerVariableCreator = new VariableCreator() { Name = "Initialize Layer" }; var layerNumberCreator = new ScopeIndexAssigner() { Name = "Create Layer Number" }; var layerSolutionsCreator = new SolutionsCreator(); var initializeAgeProcessor = new UniformSubScopesProcessor(); var initializeAge = new VariableCreator() { Name = "Initialize Age" }; var initializeLocalEvaluatedSolutions = new ExpressionCalculator() { Name = "Initialize local EvaluatedSolutions" }; var initializeEvaluatedSolutions = new Assigner() { Name = "Initialize EvaluatedSolutions" }; var initializePopulationSize = new Assigner() { Name = "Initialize PopulationSize" }; var resultsCollector = new ResultsCollector(); var mainLoop = new AlpsSsGeneticAlgorithmMainLoop(); OperatorGraph.InitialOperator = randomCreator; randomCreator.SeedParameter.Value = null; randomCreator.SetSeedRandomlyParameter.Value = null; randomCreator.Successor = workingScopeCreator; workingScopeCreator.NamesParameter.Value = new StringArray(new[] { "WorkingScope", "LayersScope", }); workingScopeCreator.Successor = layersProcessor; layersProcessor.TargetScopeParameter.ActualName = "LayersScope"; layersProcessor.Operator = layerCreator; layersProcessor.Successor = initializeLocalEvaluatedSolutions; layerCreator.NumberOfSubScopesParameter.ActualName = "NumberOfLayers"; layerCreator.NumberOfSubScopesParameter.Value = null; layerCreator.Successor = layerProcessor; layerProcessor.Operator = layerVariableCreator; layerVariableCreator.CollectedValues.Add(new ValueParameter("LayerResults")); layerVariableCreator.CollectedValues.Add(new ValueParameter("Layer")); layerVariableCreator.Successor = layerNumberCreator; layerNumberCreator.ValueParameter.ActualName = "Layer"; layerNumberCreator.Successor = layerSolutionsCreator; layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = LayerSizeParameter.Name; layerSolutionsCreator.Successor = initializeAgeProcessor; initializeAgeProcessor.Operator = initializeAge; initializeAge.CollectedValues.Add(new ValueParameter("EvalsCreated", new IntValue(1))); initializeAge.CollectedValues.Add(new ValueParameter("LastMove", new IntValue(1))); initializeLocalEvaluatedSolutions.ExpressionResultParameter.ActualName = "LocalEvaluatedSolutions"; initializeLocalEvaluatedSolutions.ExpressionParameter.Value = new StringValue("LayerSize NumberOfLayers * toint"); initializeLocalEvaluatedSolutions.CollectedValues.Add(new LookupParameter("LayerSize")); initializeLocalEvaluatedSolutions.CollectedValues.Add(new LookupParameter("NumberOfLayers")); initializeLocalEvaluatedSolutions.Successor = initializeEvaluatedSolutions; initializeEvaluatedSolutions.LeftSideParameter.ActualName = "EvaluatedSolutions"; initializeEvaluatedSolutions.RightSideParameter.ActualName = "LocalEvaluatedSolutions"; initializeEvaluatedSolutions.Successor = initializePopulationSize; initializePopulationSize.LeftSideParameter.ActualName = "PopulationSize"; initializePopulationSize.RightSideParameter.ActualName = "EvaluatedSolutions"; initializePopulationSize.Successor = resultsCollector; resultsCollector.CollectedValues.Add(new LookupParameter("EvaluatedSolutions")); resultsCollector.CollectedValues.Add(new ScopeTreeLookupParameter("LayerResults", "Result set for each layer", "LayerResults", 2)); resultsCollector.Successor = mainLoop; foreach (var selector in ApplicationManager.Manager.GetInstances().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name)) SelectorParameter.ValidValues.Add(selector); var tournamentSelector = SelectorParameter.ValidValues.OfType().FirstOrDefault(); if (tournamentSelector != null) { tournamentSelector.GroupSizeParameter.Value = new IntValue(4); SelectorParameter.Value = tournamentSelector; } ParameterizeSelectors(); Initialize(); } #region Events protected override void OnProblemChanged() { base.OnProblemChanged(); ParameterizeSolutionsCreator(); ParameterizeMainLoop(); ParameterizeSelectors(); ParameterizeIterationBasedOperators(); UpdateCrossovers(); UpdateMutators(); } protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { base.Problem_SolutionCreatorChanged(sender, e); ParameterizeSolutionsCreator(); } protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { base.Problem_EvaluatorChanged(sender, e); ParameterizeSolutionsCreator(); ParameterizeMainLoop(); ParameterizeSelectors(); } protected override void Problem_OperatorsChanged(object sender, EventArgs e) { base.Problem_OperatorsChanged(sender, e); ParameterizeIterationBasedOperators(); UpdateCrossovers(); UpdateMutators(); } protected override void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { base.Evaluator_QualityParameter_ActualNameChanged(sender, e); ParameterizeMainLoop(); ParameterizeSelectors(); } #endregion #region Parameterization private void Initialize() { } private void ParameterizeSolutionsCreator() { } private void ParameterizeMainLoop() { MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; } private void ParameterizeSelectors() { foreach (var selector in SelectorParameter.ValidValues) { selector.CopySelected = new BoolValue(true); selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2); 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 ParameterizeIterationBasedOperators() { if (Problem != null) { foreach (var @operator in Problem.Operators.OfType()) { @operator.IterationsParameter.ActualName = "Iteration"; @operator.IterationsParameter.Hidden = true; @operator.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name; @operator.MaximumIterationsParameter.Hidden = true; } } } protected override void ParameterizeStochasticOperator(IOperator @operator) { var stochasticOperator = @operator as IStochasticOperator; if (stochasticOperator != null) { stochasticOperator.RandomParameter.ActualName = "Random"; stochasticOperator.RandomParameter.Hidden = true; } } protected override void ParameterizeStochasticOperatorForLayer(IOperator @operator) { var stochasticOperator = @operator as IStochasticOperator; if (stochasticOperator != null) { stochasticOperator.RandomParameter.ActualName = "Random"; 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; } } #endregion } }