#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 BestKnownQualityParameter { get { return (IValueLookupParameter)Parameters["BestKnownQuality"]; } } public IValueLookupParameter CrossoverParameter { get { return (IValueLookupParameter)Parameters["Crossover"]; } } public IValueLookupParameter EvaluatorParameter { get { return (IValueLookupParameter)Parameters["Evaluator"]; } } public IValueLookupParameter ExecutePathRelinkingParameter { get { return (IValueLookupParameter)Parameters["ExecutePathRelinking"]; } } public IValueLookupParameter ImproverParameter { get { return (IValueLookupParameter)Parameters["Improver"]; } } 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 NumberOfHighQualitySolutionsParameter { get { return (IValueLookupParameter)Parameters["NumberOfHighQualitySolutions"]; } } public IValueLookupParameter PathRelinkerParameter { get { return (IValueLookupParameter)Parameters["PathRelinker"]; } } public IValueLookupParameter PopulationSizeParameter { get { return (IValueLookupParameter)Parameters["PopulationSize"]; } } public IValueLookupParameter ReferenceSetSizeParameter { get { return (IValueLookupParameter)Parameters["ReferenceSetSize"]; } } 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 SimilarityCalculatorParameter { get { return (IValueLookupParameter)Parameters["SimilarityCalculator"]; } } public IValueLookupParameter SolutionCreatorParameter { get { return (IValueLookupParameter)Parameters["SolutionCreator"]; } } public IValueLookupParameter TargetParameter { get { return (IValueLookupParameter)Parameters["Target"]; } } #endregion #region Properties private IMultiAnalyzer Analyzer { get { return AnalyzerParameter.ActualValue; } set { AnalyzerParameter.ActualValue = value; } } private DoubleValue BestKnownQuality { get { return BestKnownQualityParameter.ActualValue; } set { BestKnownQualityParameter.ActualValue = value; } } private ICrossover Crossover { get { return CrossoverParameter.ActualValue; } set { CrossoverParameter.ActualValue = value; } } private IEvaluator Evaluator { get { return EvaluatorParameter.ActualValue; } set { EvaluatorParameter.ActualValue = value; } } private BoolValue ExecutePathRelinking { get { return ExecutePathRelinkingParameter.ActualValue; } set { ExecutePathRelinkingParameter.ActualValue = value; } } private IImprovementOperator Improver { get { return ImproverParameter.ActualValue; } set { ImproverParameter.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 IntValue NumberOfHighQualitySolutions { get { return NumberOfHighQualitySolutionsParameter.ActualValue; } set { NumberOfHighQualitySolutionsParameter.ActualValue = value; } } private IPathRelinker PathRelinker { get { return PathRelinkerParameter.ActualValue; } set { PathRelinkerParameter.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 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 ISimilarityCalculator SimilarityCalculator { get { return SimilarityCalculatorParameter.ActualValue; } set { SimilarityCalculatorParameter.ActualValue = value; } } private ISolutionCreator SolutionCreator { get { return SolutionCreatorParameter.ActualValue; } set { SolutionCreatorParameter.ActualValue = value; } } private IItem Target { get { return TargetParameter.ActualValue; } set { TargetParameter.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")); Parameters.Add(new ValueLookupParameter("BestKnownQuality")); Parameters.Add(new ValueLookupParameter("Crossover")); Parameters.Add(new ValueLookupParameter("Evaluator")); Parameters.Add(new ValueLookupParameter("ExecutePathRelinking")); Parameters.Add(new ValueLookupParameter("Improver")); Parameters.Add(new ValueLookupParameter("Iterations")); Parameters.Add(new ValueLookupParameter("Maximization")); Parameters.Add(new ValueLookupParameter("MaximumIterations")); Parameters.Add(new ValueLookupParameter("NumberOfHighQualitySolutions")); Parameters.Add(new ValueLookupParameter("PathRelinker")); Parameters.Add(new ValueLookupParameter("PopulationSize")); Parameters.Add(new ValueLookupParameter("ReferenceSetSize")); Parameters.Add(new ValueLookupParameter("Quality")); Parameters.Add(new ValueLookupParameter("Random")); Parameters.Add(new ValueLookupParameter("Results")); Parameters.Add(new ValueLookupParameter("SimilarityCalculator")); Parameters.Add(new ValueLookupParameter("SolutionCreator")); Parameters.Add(new ValueLookupParameter("Target")); #endregion #region Create operators Placeholder analyzer = new Placeholder(); Assigner assigner1 = new Assigner(); Assigner assigner2 = new Assigner(); ChildrenCreator childrenCreator = new ChildrenCreator(); Placeholder crossover = new Placeholder(); Comparator iterationsChecker = new Comparator(); IntCounter iterationsCounter = new IntCounter(); MergingReducer mergingReducer = new MergingReducer(); ConditionalBranch executePathRelinkingBranch = new ConditionalBranch(); ConditionalBranch newSolutionsBranch = new ConditionalBranch(); OffspringProcessor offspringProcessor = new OffspringProcessor(); Placeholder pathRelinker = new Placeholder(); PopulationRebuildMethod populationRebuildMethod = new PopulationRebuildMethod(); ReferenceSetUpdateMethod referenceSetUpdateMethod = new ReferenceSetUpdateMethod(); ResultsCollector resultsCollector = new ResultsCollector(); RightSelector rightSelector = new RightSelector(); Placeholder solutionEvaluator1 = new Placeholder(); Placeholder solutionEvaluator2 = new Placeholder(); Placeholder solutionImprover1 = new Placeholder(); Placeholder solutionImprover2 = new Placeholder(); SolutionPoolUpdateMethod solutionPoolUpdateMethod = new SolutionPoolUpdateMethod(); SolutionsCreator solutionsCreator = new SolutionsCreator(); SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor(); SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor(); SubScopesProcessor subScopesProcessor3 = new SubScopesProcessor(); ConditionalBranch terminateBranch = new ConditionalBranch(); UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor(); UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor(); UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor(); VariableCreator variableCreator = new VariableCreator(); #endregion #region Create operator graph OperatorGraph.InitialOperator = variableCreator; variableCreator.CollectedValues.Add(new ValueParameter("Iterations", new IntValue(0))); variableCreator.CollectedValues.Add(new ValueParameter("NewSolutions", new BoolValue(false))); variableCreator.Successor = resultsCollector; resultsCollector.CopyValue = new BoolValue(false); resultsCollector.CollectedValues.Add(new LookupParameter(IterationsParameter.Name)); resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name; resultsCollector.Successor = iterationsChecker; iterationsChecker.Name = "IterationsChecker"; iterationsChecker.Comparison.Value = ComparisonType.GreaterOrEqual; iterationsChecker.LeftSideParameter.ActualName = "Iterations"; iterationsChecker.RightSideParameter.ActualName = "MaximumIterations"; iterationsChecker.ResultParameter.ActualName = "Terminate"; iterationsChecker.Successor = terminateBranch; terminateBranch.Name = "TerminateChecker"; terminateBranch.ConditionParameter.ActualName = "Terminate"; terminateBranch.FalseBranch = referenceSetUpdateMethod; referenceSetUpdateMethod.TargetParameter.ActualName = TargetParameter.ActualName; referenceSetUpdateMethod.Successor = assigner1; assigner1.Name = "NewSolutions = true"; assigner1.LeftSideParameter.ActualName = "NewSolutions"; assigner1.RightSideParameter.Value = new BoolValue(true); assigner1.Successor = subScopesProcessor1; subScopesProcessor1.DepthParameter.Value = new IntValue(1); subScopesProcessor1.Operators.Add(new EmptyOperator()); subScopesProcessor1.Operators.Add(childrenCreator); subScopesProcessor1.Successor = newSolutionsBranch; childrenCreator.Name = "SubsetGenerator"; childrenCreator.ParentsPerChildParameter.Value = new IntValue(2); childrenCreator.Successor = assigner2; assigner2.Name = "NewSolutions = false"; assigner2.LeftSideParameter.ActualName = "NewSolutions"; assigner2.RightSideParameter.Value = new BoolValue(false); assigner2.Successor = uniformSubScopesProcessor1; uniformSubScopesProcessor1.DepthParameter.Value = new IntValue(1); uniformSubScopesProcessor1.Operator = executePathRelinkingBranch; uniformSubScopesProcessor1.Successor = solutionPoolUpdateMethod; executePathRelinkingBranch.Name = "ExecutePathRelinkingChecker"; executePathRelinkingBranch.ConditionParameter.ActualName = ExecutePathRelinkingParameter.ActualName; executePathRelinkingBranch.TrueBranch = pathRelinker; executePathRelinkingBranch.FalseBranch = crossover; pathRelinker.Name = "PathRelinker"; pathRelinker.OperatorParameter.ActualName = "PathRelinker"; pathRelinker.Successor = offspringProcessor; crossover.Name = "Crossover"; crossover.OperatorParameter.ActualName = "Crossover"; crossover.Successor = offspringProcessor; offspringProcessor.TargetParameter.ActualName = TargetParameter.ActualName; offspringProcessor.Successor = rightSelector; rightSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1); rightSelector.CopySelected = new BoolValue(false); rightSelector.Successor = subScopesProcessor2; subScopesProcessor2.DepthParameter.Value = new IntValue(1); subScopesProcessor2.Operators.Add(new EmptyOperator()); subScopesProcessor2.Operators.Add(uniformSubScopesProcessor2); subScopesProcessor2.Successor = mergingReducer; uniformSubScopesProcessor2.DepthParameter.Value = new IntValue(2); uniformSubScopesProcessor2.Operator = solutionImprover1; solutionImprover1.Name = "SolutionImprover"; solutionImprover1.OperatorParameter.ActualName = "Improver"; solutionImprover1.Successor = solutionEvaluator1; solutionEvaluator1.Name = "SolutionEvaluator"; solutionEvaluator1.OperatorParameter.ActualName = "Evaluator"; solutionPoolUpdateMethod.QualityParameter.ActualName = QualityParameter.ActualName; solutionPoolUpdateMethod.TargetParameter.ActualName = TargetParameter.ActualName; solutionPoolUpdateMethod.Successor = analyzer; analyzer.Name = "Analyzer"; analyzer.OperatorParameter.ActualName = "Analyzer"; newSolutionsBranch.Name = "NewSolutionsChecker"; newSolutionsBranch.ConditionParameter.ActualName = "NewSolutions"; newSolutionsBranch.TrueBranch = subScopesProcessor1; newSolutionsBranch.FalseBranch = populationRebuildMethod; populationRebuildMethod.QualityParameter.ActualName = QualityParameter.ActualName; populationRebuildMethod.Successor = subScopesProcessor3; subScopesProcessor3.DepthParameter.Value = new IntValue(1); subScopesProcessor3.Operators.Add(solutionsCreator); subScopesProcessor3.Operators.Add(new EmptyOperator()); subScopesProcessor3.Successor = iterationsCounter; solutionsCreator.Name = "DiversificationGenerationMethod"; solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize"; solutionsCreator.Successor = uniformSubScopesProcessor3; uniformSubScopesProcessor3.DepthParameter.Value = new IntValue(1); uniformSubScopesProcessor3.Operator = solutionImprover2; solutionImprover2.Name = "SolutionImprover"; solutionImprover2.OperatorParameter.ActualName = "Improver"; solutionImprover2.Successor = solutionEvaluator2; solutionEvaluator2.Name = "SolutionEvaluator"; solutionEvaluator2.OperatorParameter.ActualName = "Evaluator"; iterationsCounter.Name = "IterationCounter"; iterationsCounter.IncrementParameter.Value = new IntValue(1); iterationsCounter.ValueParameter.ActualName = "Iterations"; iterationsCounter.Successor = resultsCollector; #endregion } public override IOperation Apply() { if (ImproverParameter.ActualValue == null) return null; return base.Apply(); } } }