#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Encodings.ConditionActionEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization.Operators; using HeuristicLab.Optimization.Operators.LCS; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Selection; namespace HeuristicLab.Algorithms.LearningClassifierSystems { /// /// An operator which represents the main loop of a genetic algorithm. /// [Item("LCSAdaptedGeneticAlgorithm", "An operator which represents the main loop of a genetic algorithm, which has been adapdet for learning classifier systems.")] [StorableClass] public sealed class LCSAdaptedGeneticAlgorithm : AlgorithmOperator { private const string TEMPID = "TempID"; private const string SUBSUMEDBY = "SubsumedBy"; private const string SUBSUMED = "Subsumed"; #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 AfterCopyingParentsParameter { get { return (ValueLookupParameter)Parameters["AfterCopyingParents"]; } } public ValueLookupParameter CrossoverParameter { get { return (ValueLookupParameter)Parameters["Crossover"]; } } public ValueLookupParameter AfterCrossoverParameter { get { return (ValueLookupParameter)Parameters["AfterCrossover"]; } } public ValueLookupParameter MutationProbabilityParameter { get { return (ValueLookupParameter)Parameters["MutationProbability"]; } } public ValueLookupParameter CrossoverProbabilityParameter { get { return (ValueLookupParameter)Parameters["CrossoverProbability"]; } } public ValueLookupParameter MutatorParameter { get { return (ValueLookupParameter)Parameters["Mutator"]; } } public ValueLookupParameter MaximumGenerationsParameter { get { return (ValueLookupParameter)Parameters["MaximumGenerations"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public ValueLookupParameter EvaluatedSolutionsParameter { get { return (ValueLookupParameter)Parameters["EvaluatedSolutions"]; } } public ValueLookupParameter PopulationSizeParameter { get { return (ValueLookupParameter)Parameters["PopulationSize"]; } } public ValueLookupParameter DoGASubsumptionParameter { get { return (ValueLookupParameter)Parameters["DoGASubsumption"]; } } private ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } #endregion private CheckGASubsumptionOperator checkGASubsumptionOperator; [StorableConstructor] private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { } private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new LCSAdaptedGeneticAlgorithm(this, cloner); } public LCSAdaptedGeneticAlgorithm() : 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("Selector", "The operator used to select solutions for reproduction.")); Parameters.Add(new ValueLookupParameter("AfterCopyingParents", "The operator executed after copying a parent instead of using crossover.")); Parameters.Add(new ValueLookupParameter("Crossover", "The operator used to cross solutions.")); Parameters.Add(new ValueLookupParameter("AfterCrossover", "The operator executed after crossing the solutions.")); Parameters.Add(new ValueLookupParameter("CrossoverProbability", "The probability that the crossover operator is applied on a solution.")); Parameters.Add(new ValueLookupParameter("MutationProbability", "The probability that druing the mutation operator a mutation takes place.")); Parameters.Add(new ValueLookupParameter("Mutator", "The operator used to mutate solutions.")); 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("EvaluatedSolutions", "The number of times solutions have been evaluated.")); Parameters.Add(new ValueLookupParameter("PopulationSize", "The size of the population.")); Parameters.Add(new ValueLookupParameter("DoGASubsumption", "Sets if GA subsumption is executed.")); Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied.")); #endregion #region Create operators VariableCreator variableCreator = new VariableCreator(); ResultsCollector resultsCollector1 = new ResultsCollector(); Placeholder selector = new Placeholder(); SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor(); ChildrenCreator childrenCreator = new ChildrenCreator(); UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor(); StochasticBranch crossoverStochasticBranch = new StochasticBranch(); RandomSelector randomSelector = new RandomSelector(); PreservingRightReducer preservingRightReducer = new PreservingRightReducer(); Placeholder afterCopyingParents = new Placeholder(); Placeholder crossover = new Placeholder(); Placeholder afterCrossover = new Placeholder(); Placeholder mutator = new Placeholder(); SubScopesRemover subScopesRemover = new SubScopesRemover(); SubScopesCounter subScopesCounter = new SubScopesCounter(); MergingReducer mergingReducer = new MergingReducer(); IntCounter intCounter = new IntCounter(); Comparator comparator = new Comparator(); ConditionalBranch conditionalBranch = new ConditionalBranch(); TempSubScopeIDAssigner tempIdAssigner = new TempSubScopeIDAssigner(); ConditionalBranch doGASubsumptionBranch1 = new ConditionalBranch(); UniformSubScopesProcessor setSubsumptionFalseSubScopesProcessor = new UniformSubScopesProcessor(); Assigner setSubsumpByAssigner = new Assigner(); Assigner setSubsumptionFalseAssigner = new Assigner(); checkGASubsumptionOperator = new CheckGASubsumptionOperator(); ConditionalBranch doGASubsumptionBranch2 = new ConditionalBranch(); ExecuteGASubsumptionOperator executeGAsubsumptionOperator = new ExecuteGASubsumptionOperator(); ConditionalSelector subsumptionSelector = new ConditionalSelector(); LeftReducer subsumptionLeftReducer = new LeftReducer(); variableCreator.CollectedValues.Add(new ValueParameter("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations //resultsCollector1.CollectedValues.Add(new LookupParameter("Iterations")); resultsCollector1.ResultsParameter.ActualName = "Results"; tempIdAssigner.LeftSideParameter.ActualName = TEMPID; setSubsumpByAssigner.LeftSideParameter.ActualName = SUBSUMEDBY; setSubsumpByAssigner.RightSideParameter.Value = new IntValue(-1); setSubsumptionFalseAssigner.LeftSideParameter.ActualName = SUBSUMED; setSubsumptionFalseAssigner.RightSideParameter.Value = new BoolValue(false); selector.Name = "Selector"; selector.OperatorParameter.ActualName = "Selector"; childrenCreator.ParentsPerChild = new IntValue(2); crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.ActualName; crossoverStochasticBranch.RandomParameter.ActualName = "Random"; randomSelector.CopySelected.Value = true; randomSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1); afterCopyingParents.Name = "AfterCopyingParents"; afterCopyingParents.OperatorParameter.ActualName = "AfterCopyingParents"; crossover.Name = "Crossover"; crossover.OperatorParameter.ActualName = "Crossover"; afterCrossover.Name = "AfterCrossover"; afterCrossover.OperatorParameter.ActualName = "AfterCrossover"; mutator.Name = "Mutator"; mutator.OperatorParameter.ActualName = "Mutator"; doGASubsumptionBranch1.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName; checkGASubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity"; checkGASubsumptionOperator.ExperiencesParameter.ActualName = "Experience"; checkGASubsumptionOperator.ErrorsParameter.ActualName = "Error"; checkGASubsumptionOperator.TempIDParameter.ActualName = TEMPID; checkGASubsumptionOperator.ErrorZeroParameter.ActualName = "ErrorZero"; checkGASubsumptionOperator.ThetaSubsumptionParameter.ActualName = "ThetaSubsumption"; checkGASubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY; checkGASubsumptionOperator.SubsumedParameter.ActualName = SUBSUMED; subScopesRemover.RemoveAllSubScopes = true; doGASubsumptionBranch2.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName; executeGAsubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity"; executeGAsubsumptionOperator.TempIDParameter.ActualName = TEMPID; executeGAsubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY; subsumptionSelector.ConditionParameter.ActualName = SUBSUMED; subsumptionSelector.CopySelected = new BoolValue(false); subScopesCounter.Name = "Increment EvaluatedSolutions"; subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name; intCounter.Increment = new IntValue(1); intCounter.ValueParameter.ActualName = "Generations"; comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual); comparator.LeftSideParameter.ActualName = "Generations"; comparator.ResultParameter.ActualName = "Terminate"; comparator.RightSideParameter.ActualName = "MaximumGenerations"; conditionalBranch.ConditionParameter.ActualName = "Terminate"; #endregion #region Create operator graph OperatorGraph.InitialOperator = variableCreator; variableCreator.Successor = resultsCollector1; resultsCollector1.Successor = tempIdAssigner; tempIdAssigner.Successor = setSubsumptionFalseSubScopesProcessor; setSubsumptionFalseSubScopesProcessor.Operator = setSubsumpByAssigner; setSubsumpByAssigner.Successor = setSubsumptionFalseAssigner; setSubsumptionFalseAssigner.Successor = null; setSubsumptionFalseSubScopesProcessor.Successor = selector; selector.Successor = subScopesProcessor1; subScopesProcessor1.Operators.Add(new EmptyOperator()); subScopesProcessor1.Operators.Add(childrenCreator); subScopesProcessor1.Successor = mergingReducer; childrenCreator.Successor = uniformSubScopesProcessor1; uniformSubScopesProcessor1.Operator = crossoverStochasticBranch; uniformSubScopesProcessor1.Successor = subScopesCounter; crossoverStochasticBranch.FirstBranch = crossover; crossoverStochasticBranch.SecondBranch = randomSelector; randomSelector.Successor = preservingRightReducer; preservingRightReducer.Successor = afterCopyingParents; crossoverStochasticBranch.Successor = mutator; crossover.Successor = afterCrossover; mutator.Successor = doGASubsumptionBranch1; doGASubsumptionBranch1.TrueBranch = checkGASubsumptionOperator; doGASubsumptionBranch1.FalseBranch = new EmptyOperator(); doGASubsumptionBranch1.Successor = subScopesRemover; subScopesRemover.Successor = null; subScopesCounter.Successor = null; mergingReducer.Successor = doGASubsumptionBranch2; doGASubsumptionBranch2.TrueBranch = executeGAsubsumptionOperator; doGASubsumptionBranch2.FalseBranch = new EmptyOperator(); executeGAsubsumptionOperator.Successor = subsumptionSelector; subsumptionSelector.Successor = subsumptionLeftReducer; subsumptionLeftReducer.Successor = null; doGASubsumptionBranch2.Successor = intCounter; intCounter.Successor = comparator; comparator.Successor = conditionalBranch; conditionalBranch.FalseBranch = selector; conditionalBranch.TrueBranch = null; conditionalBranch.Successor = null; #endregion } public override IOperation Apply() { if (CrossoverParameter.ActualValue == null) return null; return base.Apply(); } public void SetChildName(string childName) { checkGASubsumptionOperator.ChildClassifiersParameter.ActualName = childName; checkGASubsumptionOperator.ParentsClassifiersParameter.ActualName = childName; } } }