#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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.Analysis; 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.OffspringSelectionGeneticAlgorithm { /// /// An operator which represents the main loop of an offspring selection genetic algorithm. /// [Item("OffspringSelectionGeneticAlgorithmMainLoop", "An operator which represents the main loop of an offspring selection genetic algorithm.")] [StorableClass] public sealed class OffspringSelectionGeneticAlgorithmMainLoop : AlgorithmOperator { #region Parameter properties public ValueLookupParameter RandomParameter { get { return (ValueLookupParameter)Parameters["Random"]; } } public ValueLookupParameter MaximizationParameter { get { return (ValueLookupParameter)Parameters["Maximization"]; } } public ScopeTreeLookupParameter QualityParameter { get { return (ScopeTreeLookupParameter)Parameters["Quality"]; } } public ValueLookupParameter SelectorParameter { get { return (ValueLookupParameter)Parameters["Selector"]; } } public ValueLookupParameter CrossoverParameter { get { return (ValueLookupParameter)Parameters["Crossover"]; } } public ValueLookupParameter MutationProbabilityParameter { get { return (ValueLookupParameter)Parameters["MutationProbability"]; } } public ValueLookupParameter MutatorParameter { get { return (ValueLookupParameter)Parameters["Mutator"]; } } public ValueLookupParameter EvaluatorParameter { get { return (ValueLookupParameter)Parameters["Evaluator"]; } } public ValueLookupParameter ElitesParameter { get { return (ValueLookupParameter)Parameters["Elites"]; } } public ValueLookupParameter MaximumGenerationsParameter { get { return (ValueLookupParameter)Parameters["MaximumGenerations"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public ValueLookupParameter AnalyzerParameter { get { return (ValueLookupParameter)Parameters["Analyzer"]; } } public ValueLookupParameter SuccessRatioParameter { get { return (ValueLookupParameter)Parameters["SuccessRatio"]; } } public ValueLookupParameter ComparisonFactorModifierParameter { get { return (ValueLookupParameter)Parameters["ComparisonFactorModifier"]; } } public ValueLookupParameter MaximumSelectionPressureParameter { get { return (ValueLookupParameter)Parameters["MaximumSelectionPressure"]; } } public ValueLookupParameter OffspringSelectionBeforeMutationParameter { get { return (ValueLookupParameter)Parameters["OffspringSelectionBeforeMutation"]; } } #endregion [StorableConstructor] private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base() { } public OffspringSelectionGeneticAlgorithmMainLoop() : base() { Initialize(); } private void Initialize() { #region Create parameters Parameters.Add(new ValueLookupParameter("Random", "A pseudo random number generator.")); 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 ValueLookupParameter("BestKnownQuality", "The best known quality value found so far.")); Parameters.Add(new ValueLookupParameter("Selector", "The operator used to select solutions for reproduction.")); Parameters.Add(new ValueLookupParameter("Crossover", "The operator used to cross solutions.")); Parameters.Add(new ValueLookupParameter("MutationProbability", "The probability that the mutation operator is applied on a solution.")); Parameters.Add(new ValueLookupParameter("Mutator", "The operator used to mutate solutions.")); Parameters.Add(new ValueLookupParameter("Evaluator", "The operator used to evaluate solutions.")); Parameters.Add(new ValueLookupParameter("Elites", "The numer of elite solutions which are kept in each generation.")); Parameters.Add(new ValueLookupParameter("MaximumGenerations", "The maximum number of generations which should be processed.")); Parameters.Add(new ValueLookupParameter("Results", "The variable collection where results should be stored.")); Parameters.Add(new ValueLookupParameter("Analyzer", "The operator used to analyze each generation.")); Parameters.Add(new ValueLookupParameter("SuccessRatio", "The ratio of successful to total children that should be achieved.")); Parameters.Add(new ValueLookupParameter("ComparisonFactorModifier", "The operator used to modify the comparison factor.")); Parameters.Add(new ValueLookupParameter("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.")); Parameters.Add(new ValueLookupParameter("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation.")); #endregion #region Create operators VariableCreator variableCreator = new VariableCreator(); Placeholder comparisonFactorModifier1 = new Placeholder(); Placeholder analyzer1 = new Placeholder(); ResultsCollector resultsCollector1 = new ResultsCollector(); ResultsCollector resultsCollector2 = new ResultsCollector(); OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator(); IntCounter generationsCounter = new IntCounter(); Comparator maxGenerationsComparator = new Comparator(); Comparator maxSelectionPressureComparator = new Comparator(); Placeholder comparisonFactorModifier2 = new Placeholder(); Placeholder analyzer2 = new Placeholder(); ResultsCollector resultsCollector3 = new ResultsCollector(); ConditionalBranch conditionalBranch1 = new ConditionalBranch(); ConditionalBranch conditionalBranch2 = new ConditionalBranch(); variableCreator.CollectedValues.Add(new ValueParameter("Generations", new IntValue(0))); variableCreator.CollectedValues.Add(new ValueParameter("EvaluatedSolutions", new IntValue(0))); variableCreator.CollectedValues.Add(new ValueParameter("SelectionPressure", new DoubleValue(0))); variableCreator.CollectedValues.Add(new ValueParameter("CurrentSuccessRatio", new DoubleValue(0))); variableCreator.CollectedValues.Add(new ValueParameter("ComparisonFactor", new DoubleValue(0))); comparisonFactorModifier1.Name = "Initialize ComparisonFactor (placeholder)"; comparisonFactorModifier1.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name; analyzer1.Name = "Analyzer (placeholder)"; analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name; resultsCollector1.CopyValue = new BoolValue(false); resultsCollector1.CollectedValues.Add(new LookupParameter("Generations")); resultsCollector1.CollectedValues.Add(new LookupParameter("Curent Comparison Factor", null, "ComparisonFactor")); resultsCollector1.CollectedValues.Add(new LookupParameter("Current Selection Pressure", null, "SelectionPressure")); resultsCollector1.CollectedValues.Add(new LookupParameter("Current Success Ratio", null, "CurrentSuccessRatio")); resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name; resultsCollector2.CopyValue = new BoolValue(true); resultsCollector2.CollectedValues.Add(new LookupParameter("Evaluated Solutions", null, "EvaluatedSolutions")); resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name; mainOperator.ComparisonFactorParameter.ActualName = "ComparisonFactor"; mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name; mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio"; mainOperator.ElitesParameter.ActualName = ElitesParameter.Name; mainOperator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions"; mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name; mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name; mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name; mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name; mainOperator.MutatorParameter.ActualName = MutatorParameter.Name; mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name; mainOperator.QualityParameter.ActualName = QualityParameter.Name; mainOperator.RandomParameter.ActualName = RandomParameter.Name; mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure"; mainOperator.SelectorParameter.ActualName = SelectorParameter.Name; mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name; generationsCounter.Increment = new IntValue(1); generationsCounter.ValueParameter.ActualName = "Generations"; maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual); maxGenerationsComparator.LeftSideParameter.ActualName = "Generations"; maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations"; maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name; maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual); maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure"; maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure"; maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name; comparisonFactorModifier2.Name = "Update ComparisonFactor (placeholder)"; comparisonFactorModifier2.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name; analyzer2.Name = "Analyzer (placeholder)"; analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name; resultsCollector3.CopyValue = new BoolValue(true); resultsCollector3.CollectedValues.Add(new LookupParameter("Evaluated Solutions", null, "EvaluatedSolutions")); resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name; conditionalBranch1.Name = "MaximumSelectionPressure reached?"; conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure"; conditionalBranch2.Name = "MaximumGenerations reached?"; conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations"; #endregion #region Create operator graph OperatorGraph.InitialOperator = variableCreator; variableCreator.Successor = comparisonFactorModifier1; comparisonFactorModifier1.Successor = analyzer1; analyzer1.Successor = resultsCollector1; resultsCollector1.Successor = resultsCollector2; resultsCollector2.Successor = mainOperator; mainOperator.Successor = generationsCounter; generationsCounter.Successor = maxGenerationsComparator; maxGenerationsComparator.Successor = maxSelectionPressureComparator; maxSelectionPressureComparator.Successor = comparisonFactorModifier2; comparisonFactorModifier2.Successor = analyzer2; analyzer2.Successor = resultsCollector3; resultsCollector3.Successor = conditionalBranch1; conditionalBranch1.FalseBranch = conditionalBranch2; conditionalBranch1.TrueBranch = null; conditionalBranch1.Successor = null; conditionalBranch2.FalseBranch = mainOperator; conditionalBranch2.TrueBranch = null; conditionalBranch2.Successor = null; #endregion } } }