#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Selection; namespace HeuristicLab.Algorithms.ALPS.SteadyState { [Item("AlpsSsGeneticAlgorithmMainLoop", "An ALPS steady-state genetic algorithm main loop operator.")] [StorableClass] public class AlpsSsGeneticAlgorithmMainLoop : AlgorithmOperator { #region Parameter Properties public ValueLookupParameter MaximizationParameter { get { return (ValueLookupParameter)Parameters["Maximization"]; } } public ScopeTreeLookupParameter QualityParameter { get { return (ScopeTreeLookupParameter)Parameters["Quality"]; } } public ILookupParameter MaximumIterationsParameter { get { return (ILookupParameter)Parameters["MaximumIterations"]; } } public ILookupParameter AnalyzerParameter { get { return (ILookupParameter)Parameters["Analyzer"]; } } public ILookupParameter LayerAnalyzerParameter { get { return (ILookupParameter)Parameters["LayerAnalyzer"]; } } #endregion [StorableConstructor] private AlpsSsGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { } private AlpsSsGeneticAlgorithmMainLoop(AlpsSsGeneticAlgorithmMainLoop original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new AlpsSsGeneticAlgorithmMainLoop(this, cloner); } public AlpsSsGeneticAlgorithmMainLoop() : base() { Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false.")); Parameters.Add(new ScopeTreeLookupParameter("Quality", "The value which represents the quality of a solution.")); Parameters.Add(new LookupParameter("MaximumIterations", "The maximum number of iterations that the algorithm should process.")); Parameters.Add(new LookupParameter("Analyzer", "The operator used to the analyze all individuals.")); Parameters.Add(new LookupParameter("LayerAnalyzer", "The operator used to analyze each layer.")); var variableCreator = new VariableCreator() { Name = "Initialize" }; var randomScopeProcessor = new RandomLayerProcessor() { Name = "Select a layer" }; // TODO LayerSubScopeProcessor for Array conversion var isLayerZeroComperator = new Comparator() { Name = "IsLayerZero = Layer == 0" }; var isLayerZeroBranch = new ConditionalBranch() { Name = "IsLayerZero?" }; var isDoInitBranch = new ConditionalBranch() { Name = "DoInit?" }; var setTargetIndedxToNextInit = new Assigner() { Name = "TargetIndex = NextInit" }; var incrementNextInit = new IntCounter() { Name = "Incr. NextInit" }; var checkInitFinished = new Comparator() { Name = "DoInit = NextInit >= PopulationSize" }; var createWorkingScope = new BestSelector(); var workingScopeProcessor = new SubScopesProcessor() { Name = "Working Scope Processor" }; var createRandomIndividual = new SolutionsCreator() { Name = "Create random Individual" }; var initializeAgeProcessor = new UniformSubScopesProcessor(); var initializeAge = new Assigner() { Name = "Initialize Age" }; var selectRandomTargetIndex = new RandomIntAssigner(); var copyLayer = new BestSelector(); var copyLayerProcessor = new SubScopesProcessor(); var matingPoolCreator = new SteadyStateMatingPoolCreator() { Name = "Create MatingPool" }; var matingPoolSize = new SubScopesCounter() { Name = "MatingPoolSize" }; var matingPoolSizeMin2 = new Comparator() { Name = "ValidParents = MatingPoolSize >= 2" }; var validParentsBranch = new ConditionalBranch() { Name = "ValidParents?" }; var mainOperator = new EmptyOperator(); // TODO var reactivateInit = new Assigner() { Name = "DoInit = true" }; var resetNextIndex = new Assigner() { Name = "NextInit = 1" }; var resetTargetIndex = new Assigner() { Name = "TargetIndex = 0" }; var clearMatingPool = new SubScopesRemover() { Name = "Clear WorkingScope" }; var tryMoveUp = new EmptyOperator() { Name = "Try Move Up" }; // TODO var setNewIndividual = new EmptyOperator() { Name = "Set New Individual" }; var iterationsComparator = new Comparator() { Name = "Iterations >= MaximumIterations" }; var terminateBranch = new ConditionalBranch() { Name = "Terminate?" }; OperatorGraph.InitialOperator = variableCreator; variableCreator.CollectedValues.Add(new ValueParameter("DoInit", new BoolValue(false))); variableCreator.CollectedValues.Add(new ValueParameter("NextInit", new IntValue(0))); variableCreator.CollectedValues.Add(new ValueParameter("OpenLayers", new IntValue(1))); variableCreator.CollectedValues.Add(new ValueParameter("TargetIndex", new IntValue(0))); variableCreator.Successor = randomScopeProcessor; randomScopeProcessor.Operator = isLayerZeroComperator; isLayerZeroComperator.LeftSideParameter.ActualName = "Layer"; isLayerZeroComperator.RightSideParameter.Value = new IntValue(0); isLayerZeroComperator.ResultParameter.ActualName = "IsLayerZero"; isLayerZeroComperator.Comparison = new Comparison(ComparisonType.Equal); isLayerZeroComperator.Successor = isLayerZeroBranch; isLayerZeroBranch.ConditionParameter.ActualName = "IsLayerZero"; isLayerZeroBranch.TrueBranch = isDoInitBranch; isLayerZeroBranch.FalseBranch = selectRandomTargetIndex; isLayerZeroBranch.Successor = tryMoveUp; isDoInitBranch.ConditionParameter.ActualName = "DoInit"; isDoInitBranch.TrueBranch = setTargetIndedxToNextInit; isDoInitBranch.FalseBranch = selectRandomTargetIndex; setTargetIndedxToNextInit.LeftSideParameter.ActualName = "TargetIndex"; setTargetIndedxToNextInit.RightSideParameter.ActualName = "NextInit"; setTargetIndedxToNextInit.Successor = incrementNextInit; incrementNextInit.ValueParameter.ActualName = "NextInit"; incrementNextInit.Increment = new IntValue(1); incrementNextInit.Successor = checkInitFinished; checkInitFinished.LeftSideParameter.ActualName = "NextInit"; checkInitFinished.RightSideParameter.ActualName = "PopulationSize"; checkInitFinished.Comparison = new Comparison(ComparisonType.GreaterOrEqual); checkInitFinished.ResultParameter.ActualName = "DoInit"; checkInitFinished.Successor = createWorkingScope; createWorkingScope.NumberOfSelectedSubScopesParameter.Value = new IntValue(0); createWorkingScope.CopySelected = new BoolValue(false); createWorkingScope.Successor = workingScopeProcessor; workingScopeProcessor.Operators.Add(createRandomIndividual); workingScopeProcessor.Operators.Add(new EmptyOperator()); createRandomIndividual.NumberOfSolutions = new IntValue(1); createRandomIndividual.Successor = initializeAgeProcessor; initializeAgeProcessor.Operator = initializeAge; initializeAge.LeftSideParameter.ActualName = "EvalsCreated"; initializeAge.RightSideParameter.ActualName = "EvaluatedSolutions"; selectRandomTargetIndex.LeftSideParameter.ActualName = "TargetIndex"; selectRandomTargetIndex.MinimumParameter.Value = new IntValue(0); selectRandomTargetIndex.MaximumParameter.ActualName = "PopulationSize"; selectRandomTargetIndex.Successor = copyLayer; copyLayer.NumberOfSelectedSubScopesParameter.ActualName = "LayerPopulationSize"; copyLayer.CopySelected = new BoolValue(true); copyLayer.Successor = copyLayerProcessor; copyLayerProcessor.Operators.Add(new EmptyOperator()); copyLayerProcessor.Operators.Add(matingPoolCreator); matingPoolCreator.Successor = matingPoolSize; matingPoolSize.ValueParameter.ActualName = "MatingPoolSize"; matingPoolSize.Successor = matingPoolSizeMin2; matingPoolSizeMin2.LeftSideParameter.ActualName = "MatingPoolSize"; matingPoolSizeMin2.RightSideParameter.Value = new IntValue(2); matingPoolSizeMin2.Comparison = new Comparison(ComparisonType.GreaterOrEqual); matingPoolSizeMin2.ResultParameter.ActualName = "ValidParents"; matingPoolSizeMin2.Successor = validParentsBranch; validParentsBranch.TrueBranch = mainOperator; validParentsBranch.FalseBranch = reactivateInit; reactivateInit.Successor = resetNextIndex; reactivateInit.LeftSideParameter.ActualName = "DoInit"; reactivateInit.RightSideParameter.Value = new BoolValue(true); resetNextIndex.Successor = resetTargetIndex; resetNextIndex.LeftSideParameter.ActualName = "NextIndex"; resetNextIndex.RightSideParameter.Value = new IntValue(1); resetTargetIndex.Successor = clearMatingPool; resetTargetIndex.LeftSideParameter.ActualName = "TargetIndex"; resetTargetIndex.RightSideParameter.Value = new IntValue(0); clearMatingPool.Successor = createRandomIndividual; tryMoveUp.Successor = setNewIndividual; setNewIndividual.Successor = iterationsComparator; iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual); iterationsComparator.LeftSideParameter.ActualName = "Iterations"; iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name; iterationsComparator.ResultParameter.ActualName = "Terminate"; iterationsComparator.Successor = terminateBranch; terminateBranch.ConditionParameter.ActualName = "Terminate"; terminateBranch.FalseBranch = randomScopeProcessor; } } }