[3078] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
| 24 | using HeuristicLab.Operators;
|
---|
| 25 | using HeuristicLab.Optimization.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Selection;
|
---|
| 29 |
|
---|
[3103] | 30 | namespace HeuristicLab.Algorithms.LocalSearch {
|
---|
[3078] | 31 | /// <summary>
|
---|
[3103] | 32 | /// An operator which represents a local search.
|
---|
[3078] | 33 | /// </summary>
|
---|
[3103] | 34 | [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).")]
|
---|
[3078] | 35 | [StorableClass]
|
---|
[3145] | 36 | public sealed class LocalSearchMainLoop : AlgorithmOperator {
|
---|
[3078] | 37 | #region Parameter properties
|
---|
| 38 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 39 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 40 | }
|
---|
| 41 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 42 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 43 | }
|
---|
| 44 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
| 45 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 46 | }
|
---|
[3134] | 47 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 48 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 49 | }
|
---|
[3078] | 50 | public LookupParameter<DoubleValue> MoveQualityParameter {
|
---|
| 51 | get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
| 52 | }
|
---|
| 53 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 54 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 55 | }
|
---|
| 56 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 57 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 58 | }
|
---|
| 59 | public ValueLookupParameter<IOperator> MoveGeneratorParameter {
|
---|
| 60 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
|
---|
| 61 | }
|
---|
| 62 | public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
|
---|
| 63 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
|
---|
| 64 | }
|
---|
| 65 | public ValueLookupParameter<IOperator> MoveMakerParameter {
|
---|
| 66 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
|
---|
| 67 | }
|
---|
[3621] | 68 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 69 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
[3134] | 70 | }
|
---|
[3078] | 71 |
|
---|
| 72 | private ScopeParameter CurrentScopeParameter {
|
---|
| 73 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 74 | }
|
---|
| 75 | public IScope CurrentScope {
|
---|
| 76 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
[3145] | 80 | [StorableConstructor]
|
---|
| 81 | private LocalSearchMainLoop(bool deserializing) : base() { }
|
---|
[3103] | 82 | public LocalSearchMainLoop()
|
---|
[3078] | 83 | : base() {
|
---|
[3145] | 84 | Initialize();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | private void Initialize() {
|
---|
[3078] | 88 | #region Create parameters
|
---|
| 89 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 90 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 91 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
[3134] | 92 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
[3078] | 93 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
| 94 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
| 95 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 96 |
|
---|
| 97 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
|
---|
| 98 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
|
---|
| 99 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
|
---|
[3134] | 100 |
|
---|
[3809] | 101 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
|
---|
[3078] | 102 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied."));
|
---|
| 103 | #endregion
|
---|
| 104 |
|
---|
| 105 | #region Create operators
|
---|
| 106 | VariableCreator variableCreator = new VariableCreator();
|
---|
[3636] | 107 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
| 108 | Assigner bestQualityInitializer = new Assigner();
|
---|
| 109 | Placeholder analyzer1 = new Placeholder();
|
---|
[3621] | 110 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
[3679] | 111 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
[3636] | 112 | SubScopesProcessor mainProcessor = new SubScopesProcessor();
|
---|
[3078] | 113 | Placeholder moveGenerator = new Placeholder();
|
---|
[3193] | 114 | UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
|
---|
[3078] | 115 | Placeholder moveEvaluator = new Placeholder();
|
---|
[3636] | 116 | IntCounter evaluatedMovesCounter = new IntCounter();
|
---|
[3621] | 117 | Placeholder moveAnalyzer = new Placeholder();
|
---|
[3096] | 118 | BestSelector bestSelector = new BestSelector();
|
---|
[3636] | 119 | SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
|
---|
[3809] | 120 | UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
|
---|
[3078] | 121 | QualityComparator qualityComparator = new QualityComparator();
|
---|
| 122 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
| 123 | Placeholder moveMaker = new Placeholder();
|
---|
[3636] | 124 | Assigner bestQualityUpdater = new Assigner();
|
---|
[3809] | 125 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 126 | Placeholder analyzer2 = new Placeholder();
|
---|
[3078] | 127 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 128 | IntCounter iterationsCounter = new IntCounter();
|
---|
| 129 | Comparator iterationsComparator = new Comparator();
|
---|
[3679] | 130 | ResultsCollector resultsCollector3 = new ResultsCollector();
|
---|
[3078] | 131 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
| 132 |
|
---|
[3750] | 133 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class LocalSearch expects this to be called Iterations
|
---|
[3636] | 134 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
|
---|
| 135 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
|
---|
[3078] | 136 |
|
---|
[3636] | 137 | bestQualityInitializer.Name = "Initialize BestQuality";
|
---|
| 138 | bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
|
---|
| 139 | bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
[3078] | 140 |
|
---|
[3621] | 141 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 142 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3134] | 143 |
|
---|
[3679] | 144 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
[3636] | 145 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
| 146 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
| 147 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3134] | 148 |
|
---|
[3679] | 149 | resultsCollector2.CopyValue = new BoolValue(true);
|
---|
| 150 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
| 151 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
| 152 |
|
---|
[3078] | 153 | moveGenerator.Name = "MoveGenerator (placeholder)";
|
---|
[3143] | 154 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
[3078] | 155 |
|
---|
| 156 | moveEvaluator.Name = "MoveEvaluator (placeholder)";
|
---|
[3143] | 157 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
[3078] | 158 |
|
---|
[3636] | 159 | evaluatedMovesCounter.Name = "EvaluatedMoves + 1";
|
---|
| 160 | evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
|
---|
| 161 | evaluatedMovesCounter.Increment = new IntValue(1);
|
---|
| 162 |
|
---|
[3096] | 163 | bestSelector.CopySelected = new BoolValue(false);
|
---|
[3143] | 164 | bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
[3096] | 165 | bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
[3143] | 166 | bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
|
---|
[3078] | 167 |
|
---|
[3143] | 168 | qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
|
---|
| 169 | qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
[3078] | 170 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
| 171 |
|
---|
| 172 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
| 173 |
|
---|
| 174 | moveMaker.Name = "MoveMaker (placeholder)";
|
---|
[3143] | 175 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
[3078] | 176 |
|
---|
[3636] | 177 | bestQualityUpdater.Name = "Update BestQuality";
|
---|
| 178 | bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
|
---|
| 179 | bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
| 180 |
|
---|
[3809] | 181 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 182 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 183 |
|
---|
[3078] | 184 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 185 |
|
---|
| 186 | iterationsCounter.Name = "Iterations Counter";
|
---|
| 187 | iterationsCounter.Increment = new IntValue(1);
|
---|
| 188 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
| 189 |
|
---|
[3143] | 190 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
| 191 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
[3078] | 192 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
[3143] | 193 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 194 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
[3078] | 195 |
|
---|
[3679] | 196 | resultsCollector3.CopyValue = new BoolValue(true);
|
---|
| 197 | resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
| 198 | resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3134] | 199 |
|
---|
[3078] | 200 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
[3143] | 201 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
[3078] | 202 | #endregion
|
---|
| 203 |
|
---|
| 204 | #region Create operator graph
|
---|
| 205 | OperatorGraph.InitialOperator = variableCreator;
|
---|
[3636] | 206 | variableCreator.Successor = subScopesProcessor0;
|
---|
| 207 | subScopesProcessor0.Operators.Add(bestQualityInitializer);
|
---|
| 208 | subScopesProcessor0.Successor = resultsCollector1;
|
---|
| 209 | bestQualityInitializer.Successor = analyzer1;
|
---|
[3621] | 210 | analyzer1.Successor = null;
|
---|
[3679] | 211 | resultsCollector1.Successor = resultsCollector2;
|
---|
| 212 | resultsCollector2.Successor = mainProcessor;
|
---|
[3636] | 213 | mainProcessor.Operators.Add(moveGenerator);
|
---|
[3078] | 214 | mainProcessor.Successor = iterationsCounter;
|
---|
| 215 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
| 216 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
[3621] | 217 | moveEvaluationProcessor.Successor = moveAnalyzer;
|
---|
[3636] | 218 | moveEvaluator.Successor = evaluatedMovesCounter;
|
---|
| 219 | evaluatedMovesCounter.Successor = null;
|
---|
[3621] | 220 | moveAnalyzer.Successor = bestSelector;
|
---|
[3809] | 221 | bestSelector.Successor = moveMakingProcessor;
|
---|
| 222 | moveMakingProcessor.Operators.Add(new EmptyOperator());
|
---|
| 223 | moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
|
---|
| 224 | moveMakingProcessor.Successor = mergingReducer;
|
---|
| 225 | selectedMoveMakingProcessor.Operator = qualityComparator;
|
---|
[3078] | 226 | qualityComparator.Successor = improvesQualityBranch;
|
---|
| 227 | improvesQualityBranch.TrueBranch = moveMaker;
|
---|
[3134] | 228 | improvesQualityBranch.FalseBranch = null;
|
---|
| 229 | improvesQualityBranch.Successor = null;
|
---|
[3636] | 230 | moveMaker.Successor = bestQualityUpdater;
|
---|
| 231 | bestQualityUpdater.Successor = null;
|
---|
[3809] | 232 | mergingReducer.Successor = analyzer2;
|
---|
| 233 | analyzer2.Successor = subScopesRemover;
|
---|
| 234 | subScopesRemover.Successor = null;
|
---|
[3078] | 235 | iterationsCounter.Successor = iterationsComparator;
|
---|
[3809] | 236 | iterationsComparator.Successor = resultsCollector3;
|
---|
[3679] | 237 | resultsCollector3.Successor = iterationsTermination;
|
---|
[3143] | 238 | iterationsTermination.TrueBranch = null;
|
---|
| 239 | iterationsTermination.FalseBranch = mainProcessor;
|
---|
[3078] | 240 | #endregion
|
---|
| 241 | }
|
---|
[3715] | 242 |
|
---|
| 243 | public override IOperation Apply() {
|
---|
| 244 | if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
|
---|
| 245 | return null;
|
---|
| 246 | return base.Apply();
|
---|
| 247 | }
|
---|
[3078] | 248 | }
|
---|
| 249 | }
|
---|