#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 MoveAnalyzerParameter { get { return (ValueLookupParameter)Parameters["MoveAnalyzer"]; } } public ValueLookupParameter AnalyzerParameter { get { return (ValueLookupParameter)Parameters["Analyzer"]; } } 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("MoveAnalyzer", "The operator used to analyze the moves in each iteration.")); Parameters.Add(new ValueLookupParameter("Analyzer", "The operator used to analyze each iteration.")); 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(); ResultsCollector resultsCollector1 = new ResultsCollector(); UniformSubScopesProcessor uniformSubScopesProcessor0 = new UniformSubScopesProcessor(); Placeholder analyzer1 = new Placeholder(); UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor(); Placeholder moveGenerator = new Placeholder(); UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor(); Placeholder moveEvaluator = new Placeholder(); Placeholder moveAnalyzer = 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(); ResultsCollector resultsCollector2 = new ResultsCollector(); UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor(); Placeholder analyzer2 = new Placeholder(); ConditionalBranch iterationsTermination = new ConditionalBranch(); variableCreator.CollectedValues.Add(new ValueParameter("Iterations", new IntValue(0))); resultsCollector1.CollectedValues.Add(new LookupParameter("Iterations")); resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name; analyzer1.Name = "Analyzer (placeholder)"; analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name; mainProcessor.Name = "Solution processor (UniformSubScopesProcessor)"; moveGenerator.Name = "MoveGenerator (placeholder)"; moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name; moveEvaluator.Name = "MoveEvaluator (placeholder)"; moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name; moveAnalyzer.Name = "MoveAnalyzer (placeholder)"; moveAnalyzer.OperatorParameter.ActualName = MoveAnalyzerParameter.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"; resultsCollector2.CollectedValues.Add(new LookupParameter("Iterations")); resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name; analyzer2.Name = "Analyzer (placeholder)"; analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name; iterationsTermination.Name = "Iterations Termination Condition"; iterationsTermination.ConditionParameter.ActualName = "Terminate"; #endregion #region Create operator graph OperatorGraph.InitialOperator = variableCreator; variableCreator.Successor = resultsCollector1; resultsCollector1.Successor = uniformSubScopesProcessor0; uniformSubScopesProcessor0.Operator = analyzer1; uniformSubScopesProcessor0.Successor = mainProcessor; analyzer1.Successor = null; mainProcessor.Operator = moveGenerator; mainProcessor.Successor = iterationsCounter; moveGenerator.Successor = moveEvaluationProcessor; moveEvaluationProcessor.Operator = moveEvaluator; moveEvaluationProcessor.Successor = moveAnalyzer; moveAnalyzer.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 = resultsCollector2; resultsCollector2.Successor = uniformSubScopesProcessor1; uniformSubScopesProcessor1.Operator = analyzer2; uniformSubScopesProcessor1.Successor = iterationsTermination; analyzer2.Successor = null; iterationsTermination.TrueBranch = null; iterationsTermination.FalseBranch = mainProcessor; #endregion } } }