#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.Common; 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.LocalSearch { /// /// An operator which represents a local search. /// [Item("LocalSearchMainLoop", "An operator which represents the main loop of a best improvement local search (if only a single move is generated in each iteration it is a first improvement local search).")] [StorableClass] public sealed class LocalSearchMainLoop : AlgorithmOperator { #region Parameter properties public ValueLookupParameter RandomParameter { get { return (ValueLookupParameter)Parameters["Random"]; } } public ValueLookupParameter MaximizationParameter { get { return (ValueLookupParameter)Parameters["Maximization"]; } } public LookupParameter QualityParameter { get { return (LookupParameter)Parameters["Quality"]; } } public ValueLookupParameter BestKnownQualityParameter { get { return (ValueLookupParameter)Parameters["BestKnownQuality"]; } } public LookupParameter MoveQualityParameter { get { return (LookupParameter)Parameters["MoveQuality"]; } } public ValueLookupParameter MaximumIterationsParameter { get { return (ValueLookupParameter)Parameters["MaximumIterations"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public ValueLookupParameter MoveGeneratorParameter { get { return (ValueLookupParameter)Parameters["MoveGenerator"]; } } public ValueLookupParameter MoveEvaluatorParameter { get { return (ValueLookupParameter)Parameters["MoveEvaluator"]; } } public ValueLookupParameter MoveMakerParameter { get { return (ValueLookupParameter)Parameters["MoveMaker"]; } } public ValueLookupParameter VisualizerParameter { get { return (ValueLookupParameter)Parameters["Visualizer"]; } } public LookupParameter VisualizationParameter { get { return (LookupParameter)Parameters["Visualization"]; } } private ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } #endregion [StorableConstructor] private LocalSearchMainLoop(bool deserializing) : base() { } public LocalSearchMainLoop() : 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 LookupParameter("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 LookupParameter("MoveQuality", "The value which represents the quality of a move.")); Parameters.Add(new ValueLookupParameter("MaximumIterations", "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("MoveGenerator", "The operator that generates the moves.")); Parameters.Add(new ValueLookupParameter("MoveMaker", "The operator that performs a move and updates the quality.")); Parameters.Add(new ValueLookupParameter("MoveEvaluator", "The operator that evaluates a move.")); Parameters.Add(new ValueLookupParameter("Visualizer", "The operator used to visualize solutions.")); Parameters.Add(new LookupParameter("Visualization", "The item which represents the visualization of solutions.")); Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied.")); #endregion #region Create operators VariableCreator variableCreator = new VariableCreator(); BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer(); BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer(); DataTableValuesCollector dataTableValuesCollector1 = new DataTableValuesCollector(); QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator(); Placeholder visualizer1 = new Placeholder(); ResultsCollector resultsCollector = new ResultsCollector(); UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor(); Placeholder moveGenerator = new Placeholder(); UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor(); Placeholder moveEvaluator = new Placeholder(); BestSelector bestSelector = new BestSelector(); RightReducer rightReducer = new RightReducer(); UniformSubScopesProcessor moveMakingProcessor = new UniformSubScopesProcessor(); QualityComparator qualityComparator = new QualityComparator(); ConditionalBranch improvesQualityBranch = new ConditionalBranch(); Placeholder moveMaker = new Placeholder(); SubScopesRemover subScopesRemover = new SubScopesRemover(); IntCounter iterationsCounter = new IntCounter(); Comparator iterationsComparator = new Comparator(); BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer(); BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer(); DataTableValuesCollector dataTableValuesCollector2 = new DataTableValuesCollector(); QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator(); Placeholder visualizer2 = new Placeholder(); ConditionalBranch iterationsTermination = new ConditionalBranch(); variableCreator.CollectedValues.Add(new ValueParameter("Iterations", new IntValue(0))); bestQualityMemorizer1.BestQualityParameter.ActualName = BestKnownQualityParameter.Name; bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name; bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name; bestQualityMemorizer2.BestQualityParameter.ActualName = "BestQuality"; bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name; bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name; dataTableValuesCollector1.CollectedValues.Add(new LookupParameter("BestQuality")); dataTableValuesCollector1.CollectedValues.Add(new LookupParameter("Best Known Quality", null, BestKnownQualityParameter.Name)); dataTableValuesCollector1.DataTableParameter.ActualName = "Qualities"; qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest"; qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name; qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest"; qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality"; visualizer1.Name = "Visualizer (placeholder)"; visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name; resultsCollector.CollectedValues.Add(new LookupParameter("Iterations")); resultsCollector.CollectedValues.Add(new LookupParameter("Best Quality", null, "BestQuality")); resultsCollector.CollectedValues.Add(new LookupParameter("Best Known Quality", null, BestKnownQualityParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest")); resultsCollector.CollectedValues.Add(new LookupParameter("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest")); resultsCollector.CollectedValues.Add(new LookupParameter("Solution Visualization", null, VisualizationParameter.Name)); resultsCollector.CollectedValues.Add(new LookupParameter("Qualities")); resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name; mainProcessor.Name = "Solution processor (UniformSubScopesProcessor)"; moveGenerator.Name = "MoveGenerator (placeholder)"; moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name; moveEvaluator.Name = "MoveEvaluator (placeholder)"; moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name; bestSelector.CopySelected = new BoolValue(false); bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name; bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1); bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name; moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)"; qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name; qualityComparator.RightSideParameter.ActualName = QualityParameter.Name; qualityComparator.ResultParameter.ActualName = "IsBetter"; improvesQualityBranch.ConditionParameter.ActualName = "IsBetter"; moveMaker.Name = "MoveMaker (placeholder)"; moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name; subScopesRemover.RemoveAllSubScopes = true; iterationsCounter.Name = "Iterations Counter"; iterationsCounter.Increment = new IntValue(1); iterationsCounter.ValueParameter.ActualName = "Iterations"; iterationsComparator.Name = "Iterations >= MaximumIterations"; iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual); iterationsComparator.LeftSideParameter.ActualName = "Iterations"; iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name; iterationsComparator.ResultParameter.ActualName = "Terminate"; bestQualityMemorizer3.BestQualityParameter.ActualName = BestKnownQualityParameter.Name; bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name; bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name; bestQualityMemorizer4.BestQualityParameter.ActualName = "BestQuality"; bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name; bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name; dataTableValuesCollector2.CollectedValues.Add(new LookupParameter("BestQuality")); dataTableValuesCollector2.CollectedValues.Add(new LookupParameter("Best Known Quality", null, BestKnownQualityParameter.Name)); dataTableValuesCollector2.DataTableParameter.ActualName = "Qualities"; qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest"; qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name; qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest"; qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality"; visualizer2.Name = "Visualizer (placeholder)"; visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name; iterationsTermination.Name = "Iterations Termination Condition"; iterationsTermination.ConditionParameter.ActualName = "Terminate"; #endregion #region Create operator graph OperatorGraph.InitialOperator = variableCreator; variableCreator.Successor = bestQualityMemorizer1; bestQualityMemorizer1.Successor = bestQualityMemorizer2; bestQualityMemorizer2.Successor = dataTableValuesCollector1; dataTableValuesCollector1.Successor = qualityDifferenceCalculator1; qualityDifferenceCalculator1.Successor = visualizer1; visualizer1.Successor = resultsCollector; resultsCollector.Successor = mainProcessor; mainProcessor.Operator = moveGenerator; mainProcessor.Successor = iterationsCounter; moveGenerator.Successor = moveEvaluationProcessor; moveEvaluationProcessor.Operator = moveEvaluator; moveEvaluationProcessor.Successor = bestSelector; bestSelector.Successor = rightReducer; rightReducer.Successor = moveMakingProcessor; moveMakingProcessor.Operator = qualityComparator; moveMakingProcessor.Successor = subScopesRemover; subScopesRemover.Successor = null; qualityComparator.Successor = improvesQualityBranch; improvesQualityBranch.TrueBranch = moveMaker; improvesQualityBranch.FalseBranch = null; improvesQualityBranch.Successor = null; moveMaker.Successor = null; iterationsCounter.Successor = iterationsComparator; iterationsComparator.Successor = bestQualityMemorizer3; bestQualityMemorizer3.Successor = bestQualityMemorizer4; bestQualityMemorizer4.Successor = dataTableValuesCollector2; dataTableValuesCollector2.Successor = qualityDifferenceCalculator2; qualityDifferenceCalculator2.Successor = visualizer2; visualizer2.Successor = iterationsTermination; iterationsTermination.TrueBranch = null; iterationsTermination.FalseBranch = mainProcessor; #endregion } } }