#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 System; using System.Linq; 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.Problems.GeneralizedQuadraticAssignment.Common; using HeuristicLab.Selection; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms { [Item("GRASP+PR MainLoop", "The main loop that implements the behavior of the GRASP+PR algorithm.")] [StorableClass] public class GRASPWithPathRelinkingMainLoop : AlgorithmOperator { public IValueLookupParameter SolutionCreatorParameter { get { return (IValueLookupParameter)Parameters["SolutionCreator"]; } } public IValueLookupParameter EvaluatorParameter { get { return (IValueLookupParameter)Parameters["Evaluator"]; } } public IValueLookupParameter EvaluatedSolutionsParameter { get { return (IValueLookupParameter)Parameters["EvaluatedSolutions"]; } } public IValueLookupParameter MaximumIterationsParameter { get { return (IValueLookupParameter)Parameters["MaximumIterations"]; } } public IValueLookupParameter ResultsParameter { get { return (IValueLookupParameter)Parameters["Results"]; } } public IValueLookupParameter EliteSetSizeParameter { get { return (IValueLookupParameter)Parameters["EliteSetSize"]; } } public IValueLookupParameter LocalImprovementParameter { get { return (IValueLookupParameter)Parameters["LocalImprovement"]; } } public IValueLookupParameter PathRelinkingParameter { get { return (IValueLookupParameter)Parameters["PathRelinking"]; } } public IValueLookupParameter EliteSetReplacerParameter { get { return (IValueLookupParameter)Parameters["EliteSetReplacer"]; } } public IValueLookupParameter AnalyzerParameter { get { return (IValueLookupParameter)Parameters["Analyzer"]; } } [StorableConstructor] protected GRASPWithPathRelinkingMainLoop(bool deserializing) : base(deserializing) { } protected GRASPWithPathRelinkingMainLoop(GRASPWithPathRelinkingMainLoop original, Cloner cloner) : base(original, cloner) { } public GRASPWithPathRelinkingMainLoop() : base() { Parameters.Add(new ValueLookupParameter("SolutionCreator", "The solution creation procedure which ideally should be a greedy initialization heuristic.")); Parameters.Add(new ValueLookupParameter("Evaluator", "The evaluator which calculates the fitness of a solutions.")); Parameters.Add(new ValueLookupParameter("EvaluatedSolutions", "The number of evaluated solutions.")); Parameters.Add(new ValueLookupParameter("MaximumIterations", "The number of maximum iterations that should be performed.")); Parameters.Add(new ValueLookupParameter("Results", "Specifies where the results are stored.")); Parameters.Add(new ValueLookupParameter("EliteSetSize", "The size of the elite set.")); Parameters.Add(new ValueLookupParameter("LocalImprovement", "The operator which performs the local improvement.")); Parameters.Add(new ValueLookupParameter("PathRelinking", "The operator which performs the path relinking.")); Parameters.Add(new ValueLookupParameter("EliteSetReplacer", "The operator that replaces elements in the elite set.")); Parameters.Add(new ValueLookupParameter("Analyzer", "The analyzer that is to be applied.")); var variableCreator1 = new VariableCreator(); variableCreator1.CollectedValues.Add(new ValueParameter("Iterations", new IntValue(0))); var variableCreator2 = new VariableCreator(); variableCreator2.CollectedValues.Add(new ValueParameter("ActualEliteSetSize", new IntValue(0))); variableCreator2.Name = "ActualEliteSetSize = 0"; var subScopesCounter1 = new SubScopesCounter(); subScopesCounter1.Name = "ActualEliteSetSize += |SubScopes|"; subScopesCounter1.ValueParameter.ActualName = "ActualEliteSetSize"; var comparator1 = new Comparator(); comparator1.Name = "ActualEliteSetSize >= MinimumEliteSetSize"; comparator1.Comparison.Value = ComparisonType.GreaterOrEqual; comparator1.LeftSideParameter.ActualName = "ActualEliteSetSize"; comparator1.RightSideParameter.ActualName = EliteSetSizeParameter.ActualName; comparator1.ResultParameter.ActualName = "SizeOkay"; var conditionalBranch1 = new ConditionalBranch(); conditionalBranch1.Name = "Elite set has at least ρ elements"; conditionalBranch1.ConditionParameter.ActualName = "SizeOkay"; var selector1 = new RandomSelector(); selector1.NumberOfSelectedSubScopesParameter.Value = new IntValue(1); selector1.CopySelected.Value = true; var selector2 = new RandomSelector(); selector2.NumberOfSelectedSubScopesParameter.Value = new IntValue(0); var ssp1 = new SubScopesProcessor(); var eo1 = new EmptyOperator(); var solutionsCreator = new SolutionsCreator(); solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name; solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name; solutionsCreator.NumberOfSolutions = new IntValue(1); var subScopesCounter2 = new SubScopesCounter(); subScopesCounter2.Name = "Count subscopes"; subScopesCounter2.ValueParameter.ActualName = "SolutionScopes"; var comparator2 = new Comparator(); comparator2.Name = "SolutionScopes == 2"; comparator2.Comparison.Value = ComparisonType.Equal; comparator2.LeftSideParameter.ActualName = "SolutionScopes"; comparator2.RightSideParameter.Value = new IntValue(2); comparator2.ResultParameter.ActualName = "PerformPR"; var conditionalBranch2 = new ConditionalBranch(); conditionalBranch2.Name = "Path relinking?"; conditionalBranch2.ConditionParameter.ActualName = "PerformPR"; var ssp2 = new SubScopesProcessor(); var eo2 = new EmptyOperator(); var placeholder1 = new Placeholder(); placeholder1.Name = "(LocalImprovement)"; placeholder1.OperatorParameter.ActualName = LocalImprovementParameter.Name; var childrenCreator = new ChildrenCreator(); childrenCreator.ParentsPerChild = new IntValue(2); var placeholder2 = new Placeholder(); placeholder2.Name = "(PathRelinking)"; placeholder2.OperatorParameter.ActualName = PathRelinkingParameter.Name; var subScopesRemover = new SubScopesRemover(); subScopesRemover.RemoveAllSubScopes = true; var ssp3 = new SubScopesProcessor(); var placeholder3 = new Placeholder(); placeholder3.Name = "(LocalImprovement)"; placeholder3.OperatorParameter.ActualName = LocalImprovementParameter.Name; var placeholder4 = new Placeholder(); placeholder4.Name = "(Replacer)"; placeholder4.OperatorParameter.ActualName = EliteSetReplacerParameter.Name; var counter = new IntCounter(); counter.Name = "Iterations++"; counter.ValueParameter.ActualName = "Iterations"; counter.Increment = new IntValue(1); var analyzer1 = new Placeholder(); analyzer1.Name = "(Analyzer)"; analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name; var comparator3 = new Comparator(); comparator3.Name = "Iterations >= MaximumIterations"; comparator3.Comparison.Value = ComparisonType.GreaterOrEqual; comparator3.LeftSideParameter.ActualName = "Iterations"; comparator3.RightSideParameter.ActualName = MaximumIterationsParameter.Name; comparator3.ResultParameter.ActualName = "TerminatedByIteration"; var conditionalBranch3 = new ConditionalBranch(); conditionalBranch3.Name = "Terminate by Iterations?"; conditionalBranch3.ConditionParameter.ActualName = "TerminatedByIteration"; OperatorGraph.InitialOperator = variableCreator1; variableCreator1.Successor = variableCreator2; variableCreator2.Successor = subScopesCounter1; subScopesCounter1.Successor = comparator1; comparator1.Successor = conditionalBranch1; conditionalBranch1.TrueBranch = selector1; conditionalBranch1.FalseBranch = selector2; conditionalBranch1.Successor = ssp1; ssp1.Operators.Add(eo1); ssp1.Operators.Add(solutionsCreator); ssp1.Successor = placeholder4; eo1.Successor = null; solutionsCreator.Successor = subScopesCounter2; subScopesCounter2.Successor = comparator2; comparator2.Successor = conditionalBranch2; conditionalBranch2.TrueBranch = ssp2; conditionalBranch2.FalseBranch = null; conditionalBranch2.Successor = ssp3; ssp2.Operators.Add(eo2); ssp2.Operators.Add(placeholder1); ssp2.Successor = null; eo2.Successor = null; placeholder1.Successor = childrenCreator; childrenCreator.Successor = placeholder2; placeholder2.Successor = subScopesRemover; subScopesRemover.Successor = null; ssp3.Operators.Add(placeholder3); ssp3.Successor = null; placeholder4.Successor = counter; counter.Successor = analyzer1; analyzer1.Successor = comparator3; comparator3.Successor = conditionalBranch3; conditionalBranch3.TrueBranch = null; conditionalBranch3.FalseBranch = variableCreator2; conditionalBranch3.Successor = null; } public override IDeepCloneable Clone(Cloner cloner) { return new GRASPWithPathRelinkingMainLoop(this, cloner); } private void Parameterize() { var operators = OperatorGraph.InitialOperator.Walk().ToArray(); foreach (var solutionsCreator in operators.OfType()) { solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name; solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name; } } public event EventHandler ProblemChanged; protected virtual void OnProblemChanged() { Parameterize(); var handler = ProblemChanged; if (handler != null) handler(this, EventArgs.Empty); } } }