#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.Operators; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Selection; namespace HeuristicLab.Algorithms.ScatterSearch { /// /// An operator which represents a scatter search. /// [Item("ScatterSearchMainLoop", "An operator which represents a scatter search.")] [StorableClass] public sealed class ScatterSearchMainLoop : AlgorithmOperator { #region Parameter properties public IValueLookupParameter AnalyzerParameter { get { return (IValueLookupParameter)Parameters["Analyzer"]; } } public IValueLookupParameter CrossoverParameter { get { return (IValueLookupParameter)Parameters["Crossover"]; } } public IValueLookupParameter ImproverParameter { get { return (IValueLookupParameter)Parameters["Improver"]; } } public IValueLookupParameter DiversityCalculatorParameter { get { return (IValueLookupParameter)Parameters["DiversityCalculator"]; } } public IValueLookupParameter NumberOfHighQualitySolutionsParameter { get { return (IValueLookupParameter)Parameters["NumberOfHighQualitySolutions"]; } } public IValueLookupParameter PopulationSizeParameter { get { return (IValueLookupParameter)Parameters["PopulationSize"]; } } public IValueLookupParameter ReferenceSetSizeParameter { get { return (IValueLookupParameter)Parameters["ReferenceSetSize"]; } } public IValueLookupParameter BestKnownQualityParameter { get { return (IValueLookupParameter)Parameters["BestKnownQuality"]; } } public IValueLookupParameter EvaluatorParameter { get { return (IValueLookupParameter)Parameters["Evaluator"]; } } public IValueLookupParameter IterationsParameter { get { return (IValueLookupParameter)Parameters["Iterations"]; } } public IValueLookupParameter MaximizationParameter { get { return (IValueLookupParameter)Parameters["Maximization"]; } } public IValueLookupParameter MaximumIterationsParameter { get { return (IValueLookupParameter)Parameters["MaximumIterations"]; } } public IValueLookupParameter QualityParameter { get { return (IValueLookupParameter)Parameters["Quality"]; } } public IValueLookupParameter RandomParameter { get { return (IValueLookupParameter)Parameters["Random"]; } } public IValueLookupParameter ResultsParameter { get { return (IValueLookupParameter)Parameters["Results"]; } } public IValueLookupParameter SampleSizeParameter { get { return (IValueLookupParameter)Parameters["SampleSize"]; } } public IValueLookupParameter SolutionCreatorParameter { get { return (IValueLookupParameter)Parameters["SolutionCreator"]; } } #endregion #region Properties private IMultiAnalyzer Analyzer { get { return AnalyzerParameter.ActualValue; } set { AnalyzerParameter.ActualValue = value; } } private ICrossover Crossover { get { return CrossoverParameter.ActualValue; } set { CrossoverParameter.ActualValue = value; } } private IOperator Improver { get { return ImproverParameter.ActualValue; } set { ImproverParameter.ActualValue = value; } } private DiversityCalculator DiversityCalculator { get { return DiversityCalculatorParameter.ActualValue; } set { DiversityCalculatorParameter.ActualValue = value; } } private IntValue NumberOfHighQualitySolutions { get { return NumberOfHighQualitySolutionsParameter.ActualValue; } set { NumberOfHighQualitySolutionsParameter.ActualValue = value; } } private IntValue PopulationSize { get { return PopulationSizeParameter.ActualValue; } set { PopulationSizeParameter.ActualValue = value; } } private IntValue ReferenceSetSize { get { return ReferenceSetSizeParameter.ActualValue; } set { ReferenceSetSizeParameter.ActualValue = value; } } private DoubleValue BestKnownQuality { get { return BestKnownQualityParameter.ActualValue; } set { BestKnownQualityParameter.ActualValue = value; } } private IEvaluator Evaluator { get { return EvaluatorParameter.ActualValue; } set { EvaluatorParameter.ActualValue = value; } } private IntValue Iterations { get { return IterationsParameter.ActualValue; } set { IterationsParameter.ActualValue = value; } } private BoolValue Maximization { get { return MaximizationParameter.ActualValue; } set { MaximizationParameter.ActualValue = value; } } private IntValue MaximumIterations { get { return MaximumIterationsParameter.ActualValue; } set { MaximumIterationsParameter.ActualValue = value; } } private DoubleValue Quality { get { return QualityParameter.ActualValue; } set { QualityParameter.ActualValue = value; } } private IRandom Random { get { return RandomParameter.ActualValue; } set { RandomParameter.ActualValue = value; } } private VariableCollection Results { get { return ResultsParameter.ActualValue; } set { ResultsParameter.ActualValue = value; } } private IntValue SampleSize { get { return SampleSizeParameter.ActualValue; } set { SampleSizeParameter.ActualValue = value; } } private ISolutionCreator SolutionCreator { get { return SolutionCreatorParameter.ActualValue; } set { SolutionCreatorParameter.ActualValue = value; } } #endregion [StorableConstructor] private ScatterSearchMainLoop(bool deserializing) : base(deserializing) { } private ScatterSearchMainLoop(ScatterSearchMainLoop original, Cloner cloner) : base(original, cloner) { } public ScatterSearchMainLoop() : base() { Initialize(); } public override IDeepCloneable Clone(Cloner cloner) { return new ScatterSearchMainLoop(this, cloner); } private void Initialize() { #region Create parameters Parameters.Add(new ValueLookupParameter("Analyzer", "The operator used to analyze the solution and moves.")); Parameters.Add(new ValueLookupParameter("Crossover", "The operator used to combine solutions.")); Parameters.Add(new ValueLookupParameter("Improver", "The operator used to improve solutions.")); Parameters.Add(new ValueLookupParameter("DiversityCalculator", "The operator used to calculate the diversity of two solutions.")); Parameters.Add(new ValueLookupParameter("NumberOfHighQualitySolutions", "The number of high quality solutions that should be added to the reference set.")); Parameters.Add(new ValueLookupParameter("PopulationSize", "The size of the population.")); Parameters.Add(new ValueLookupParameter("ReferenceSetSize", "The size of the reference set.")); Parameters.Add(new ValueLookupParameter("BestKnownQuality", "The problem's best known quality value found so far.")); Parameters.Add(new ValueLookupParameter("Evaluator", "The operator which is used to evaluate new solutions.")); Parameters.Add(new ValueLookupParameter("Iterations", "The number of iterations performed.")); Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false.")); Parameters.Add(new ValueLookupParameter("MaximumIterations", "The maximum number of generations which should be processed.")); Parameters.Add(new ValueLookupParameter("Quality", "The value which represents the quality of a solution.")); Parameters.Add(new ValueLookupParameter("Random", "A pseudo random number generator.")); Parameters.Add(new ValueLookupParameter("Results", "The variable collection where results should be stored.")); Parameters.Add(new ValueLookupParameter("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.")); Parameters.Add(new ValueLookupParameter("SolutionCreator", "The operator which is used to create new solutions.")); #endregion #region Create operators Assigner assigner1 = new Assigner(); Assigner assigner2 = new Assigner(); ChildrenCreator childrenCreator = new ChildrenCreator(); Comparator iterationsChecker = new Comparator(); ConditionalBranch newSolutionsBranch = new ConditionalBranch(); ConditionalBranch terminateBranch = new ConditionalBranch(); IntCounter interationsCounter = new IntCounter(); MergingReducer mergingReducer = new MergingReducer(); OffspringProcessor offspringProcessor = new OffspringProcessor(); Placeholder analyzer = new Placeholder(); Placeholder crossover = new Placeholder(); Placeholder solutionEvaluator1 = new Placeholder(); Placeholder solutionEvaluator2 = new Placeholder(); Placeholder solutionImprover1 = new Placeholder(); Placeholder solutionImprover2 = new Placeholder(); PopulationRebuildMethod populationRebuildMethod = new PopulationRebuildMethod(); ResultsCollector resultsCollector = new ResultsCollector(); ReferenceSetUpdateMethod referenceSetUpdateMethod = new ReferenceSetUpdateMethod(); RightSelector rightSelector = new RightSelector(); SolutionPoolUpdateMethod solutionPoolUpdateMethod = new SolutionPoolUpdateMethod(); SolutionsCreator solutionsCreator = new SolutionsCreator(); SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor(); SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor(); SubScopesProcessor subScopesProcessor3 = new SubScopesProcessor(); UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor(); UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor(); UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor(); #endregion #region Create operator graph OperatorGraph.InitialOperator = interationsCounter; assigner1.Name = "NewSolutions = true"; assigner1.LeftSideParameter.ActualName = "NewSolutions"; assigner1.RightSideParameter.Value = new BoolValue(true); assigner1.Successor = newSolutionsBranch; assigner2.Name = "NewSolutions = false"; assigner2.LeftSideParameter.ActualName = "NewSolutions"; assigner2.RightSideParameter.Value = new BoolValue(false); assigner2.Successor = uniformSubScopesProcessor1; childrenCreator.Name = "SubsetGenerator"; childrenCreator.ParentsPerChildParameter.Value = new IntValue(2); childrenCreator.Successor = assigner2; iterationsChecker.Name = "IterationsChecker"; iterationsChecker.Comparison.Value = ComparisonType.GreaterOrEqual; iterationsChecker.LeftSideParameter.ActualName = "Iterations"; iterationsChecker.RightSideParameter.ActualName = "MaximumIterations"; iterationsChecker.ResultParameter.ActualName = "Terminate"; iterationsChecker.Successor = terminateBranch; solutionImprover2.Name = "SolutionImprover"; solutionImprover2.OperatorParameter.ActualName = "Improver"; solutionImprover2.Successor = solutionEvaluator2; solutionEvaluator2.Name = "SolutionEvaluator"; solutionEvaluator2.OperatorParameter.ActualName = "Evaluator"; solutionEvaluator2.Successor = null; newSolutionsBranch.Name = "NewSolutionChecker"; newSolutionsBranch.ConditionParameter.ActualName = "NewSolutions"; newSolutionsBranch.TrueBranch = subScopesProcessor1; newSolutionsBranch.FalseBranch = populationRebuildMethod; newSolutionsBranch.Successor = null; terminateBranch.Name = "TerminateChecker"; terminateBranch.ConditionParameter.ActualName = "Terminate"; terminateBranch.TrueBranch = new EmptyOperator(); terminateBranch.FalseBranch = referenceSetUpdateMethod; terminateBranch.Successor = null; interationsCounter.Name = "IterationCounter"; interationsCounter.IncrementParameter.Value = new IntValue(1); interationsCounter.ValueParameter.ActualName = "Iterations"; interationsCounter.Successor = resultsCollector; offspringProcessor.Successor = rightSelector; rightSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1); rightSelector.CopySelected = new BoolValue(false); rightSelector.Successor = subScopesProcessor2; analyzer.Name = "Analyzer"; analyzer.OperatorParameter.ActualName = "Analyzer"; crossover.Name = "Crossover"; crossover.OperatorParameter.ActualName = "Crossover"; crossover.Successor = offspringProcessor; solutionImprover1.Name = "SolutionImprover"; solutionImprover1.OperatorParameter.ActualName = "Improver"; solutionImprover1.Successor = solutionEvaluator1; solutionEvaluator1.Name = "SolutionEvaluator"; solutionEvaluator1.OperatorParameter.ActualName = "Evaluator"; solutionEvaluator1.Successor = null; populationRebuildMethod.Successor = subScopesProcessor3; resultsCollector.CopyValue = new BoolValue(false); resultsCollector.CollectedValues.Add(new LookupParameter(IterationsParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter(BestKnownQualityParameter.Name, null, BestKnownQualityParameter.Name)); resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name; resultsCollector.Successor = iterationsChecker; referenceSetUpdateMethod.Successor = assigner1; solutionPoolUpdateMethod.Successor = analyzer; solutionsCreator.Name = "DiversificationGenerationMethod"; solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize"; solutionsCreator.Successor = uniformSubScopesProcessor3; subScopesProcessor1.DepthParameter.Value = new IntValue(1); subScopesProcessor1.Operators.Add(new EmptyOperator()); subScopesProcessor1.Operators.Add(childrenCreator); subScopesProcessor1.Successor = newSolutionsBranch; subScopesProcessor2.DepthParameter.Value = new IntValue(1); subScopesProcessor2.Operators.Add(new EmptyOperator()); subScopesProcessor2.Operators.Add(uniformSubScopesProcessor2); subScopesProcessor2.Successor = mergingReducer; subScopesProcessor3.DepthParameter.Value = new IntValue(1); subScopesProcessor3.Operators.Add(solutionsCreator); subScopesProcessor3.Operators.Add(new EmptyOperator()); subScopesProcessor3.Successor = interationsCounter; uniformSubScopesProcessor1.DepthParameter.Value = new IntValue(1); uniformSubScopesProcessor1.Operator = crossover; uniformSubScopesProcessor1.Successor = solutionPoolUpdateMethod; uniformSubScopesProcessor2.DepthParameter.Value = new IntValue(2); uniformSubScopesProcessor2.Operator = solutionImprover1; uniformSubScopesProcessor2.Successor = null; uniformSubScopesProcessor3.DepthParameter.Value = new IntValue(1); uniformSubScopesProcessor3.Operator = solutionImprover2; uniformSubScopesProcessor3.Successor = null; #endregion } public override IOperation Apply() { return base.Apply(); } } }