#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.Evolutionary; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Selection; namespace HeuristicLab.Algorithms.SGA { /// /// An operator which represents a Standard Genetic Algorithm. /// [Item("SGAOperator", "An operator which represents a Standard Genetic Algorithm.")] [Creatable("Test")] public class SGAOperator : AlgorithmOperator { #region Parameter properties public ValueLookupParameter RandomParameter { get { return (ValueLookupParameter)Parameters["Random"]; } } public ValueLookupParameter MaximizationParameter { get { return (ValueLookupParameter)Parameters["Maximization"]; } } public SubScopesLookupParameter QualityParameter { get { return (SubScopesLookupParameter)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"]; } } private ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } #endregion public SGAOperator() : base() { #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 SubScopesLookupParameter("Quality", "The value which represents the quality of a solution.")); 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 ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the SGA should be applied.")); #endregion #region Create operator graph SubScopesSorter subScopesSorter1 = new SubScopesSorter(); Placeholder selector = new Placeholder(); SequentialSubScopesProcessor sequentialSubScopesProcessor1 = new SequentialSubScopesProcessor(); ChildrenCreator childrenCreator = new ChildrenCreator(); UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor = new UniformSequentialSubScopesProcessor(); Placeholder crossover = new Placeholder(); StochasticBranch stochasticBranch = new StochasticBranch(); Placeholder mutator = new Placeholder(); Placeholder evaluator = new Placeholder(); SubScopesRemover subScopesRemover = new SubScopesRemover(); SubScopesSorter subScopesSorter2 = new SubScopesSorter(); SequentialSubScopesProcessor sequentialSubScopesProcessor2 = new SequentialSubScopesProcessor(); LeftSelector leftSelector = new LeftSelector(); RightReducer rightReducer = new RightReducer(); MergingReducer mergingReducer = new MergingReducer(); IntCounter intCounter = new IntCounter(); Comparator comparator = new Comparator(); BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator = new BestAverageWorstQualityCalculator(); ResultsCollector resultsCollector = new ResultsCollector(); ConditionalBranch conditionalBranch = new ConditionalBranch(); subScopesSorter1.DescendingParameter.ActualName = "Maximization"; subScopesSorter1.ValueParameter.ActualName = "Quality"; OperatorGraph.InitialOperator = subScopesSorter1; subScopesSorter1.Successor = selector; selector.Name = "Selector"; selector.OperatorParameter.ActualName = "Selector"; selector.Successor = sequentialSubScopesProcessor1; sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator()); sequentialSubScopesProcessor1.Operators.Add(childrenCreator); sequentialSubScopesProcessor1.Successor = sequentialSubScopesProcessor2; childrenCreator.ParentsPerChild = new IntData(2); childrenCreator.Successor = uniformSequentialSubScopesProcessor; uniformSequentialSubScopesProcessor.Operator = crossover; uniformSequentialSubScopesProcessor.Successor = subScopesSorter2; crossover.Name = "Crossover"; crossover.OperatorParameter.ActualName = "Crossover"; crossover.Successor = stochasticBranch; stochasticBranch.FirstBranch = mutator; stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability"; stochasticBranch.RandomParameter.ActualName = "Random"; stochasticBranch.SecondBranch = null; stochasticBranch.Successor = evaluator; mutator.Name = "Mutator"; mutator.OperatorParameter.ActualName = "Mutator"; mutator.Successor = null; evaluator.Name = "Evaluator"; evaluator.OperatorParameter.ActualName = "Evaluator"; evaluator.Successor = subScopesRemover; subScopesRemover.RemoveAllSubScopes = true; subScopesRemover.Successor = null; subScopesSorter2.DescendingParameter.ActualName = "Maximization"; subScopesSorter2.ValueParameter.ActualName = "Quality"; subScopesSorter2.Successor = null; sequentialSubScopesProcessor2.Operators.Add(leftSelector); sequentialSubScopesProcessor2.Operators.Add(new EmptyOperator()); sequentialSubScopesProcessor2.Successor = mergingReducer; leftSelector.CopySelected = new BoolData(false); leftSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites"; leftSelector.Successor = rightReducer; rightReducer.Successor = null; mergingReducer.Successor = intCounter; intCounter.Increment = new IntData(1); intCounter.ValueParameter.ActualName = "Generations"; intCounter.Successor = comparator; comparator.Comparison = new ComparisonData(Comparison.GreaterOrEqual); comparator.LeftSideParameter.ActualName = "Generations"; comparator.ResultParameter.ActualName = "Terminate"; comparator.RightSideParameter.ActualName = "MaximumGenerations"; comparator.Successor = bestAverageWorstQualityCalculator; bestAverageWorstQualityCalculator.AverageQualityParameter.ActualName = "AverageQuality"; bestAverageWorstQualityCalculator.BestQualityParameter.ActualName = "BestQuality"; bestAverageWorstQualityCalculator.MaximizationParameter.ActualName = "Maximization"; bestAverageWorstQualityCalculator.QualityParameter.ActualName = "Quality"; bestAverageWorstQualityCalculator.WorstQualityParameter.ActualName = "WorstQuality"; bestAverageWorstQualityCalculator.Successor = resultsCollector; LookupParameter bestQuality = new LookupParameter("BestQuality"); bestQuality.ActualName = "BestQuality"; resultsCollector.CollectedValues.Add(bestQuality); LookupParameter averageQuality = new LookupParameter("AverageQuality"); averageQuality.ActualName = "AverageQuality"; resultsCollector.CollectedValues.Add(averageQuality); LookupParameter worstQuality = new LookupParameter("WorstQuality"); worstQuality.ActualName = "WorstQuality"; resultsCollector.CollectedValues.Add(worstQuality); resultsCollector.ResultsParameter.ActualName = "Results"; resultsCollector.Successor = conditionalBranch; conditionalBranch.ConditionParameter.ActualName = "Terminate"; conditionalBranch.FalseBranch = subScopesSorter1; conditionalBranch.TrueBranch = null; conditionalBranch.Successor = null; #endregion } } }