[3078] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11594] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3078] | 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 |
|
---|
[4722] | 22 | using HeuristicLab.Common;
|
---|
[3078] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Selection;
|
---|
| 30 |
|
---|
[3103] | 31 | namespace HeuristicLab.Algorithms.LocalSearch {
|
---|
[3078] | 32 | /// <summary>
|
---|
[3103] | 33 | /// An operator which represents a local search.
|
---|
[3078] | 34 | /// </summary>
|
---|
[3103] | 35 | [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] | 36 | [StorableClass]
|
---|
[3145] | 37 | public sealed class LocalSearchMainLoop : AlgorithmOperator {
|
---|
[3078] | 38 | #region Parameter properties
|
---|
| 39 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 40 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 41 | }
|
---|
| 42 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 43 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 44 | }
|
---|
| 45 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
| 46 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 47 | }
|
---|
[6042] | 48 | public LookupParameter<DoubleValue> BestLocalQualityParameter {
|
---|
[5753] | 49 | get { return (LookupParameter<DoubleValue>)Parameters["BestLocalQuality"]; }
|
---|
| 50 | }
|
---|
[3134] | 51 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 52 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 53 | }
|
---|
[3078] | 54 | public LookupParameter<DoubleValue> MoveQualityParameter {
|
---|
| 55 | get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
| 56 | }
|
---|
[5753] | 57 | public LookupParameter<IntValue> IterationsParameter {
|
---|
[6042] | 58 | get { return (LookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
[5753] | 59 | }
|
---|
[3078] | 60 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 61 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 62 | }
|
---|
| 63 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 64 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 65 | }
|
---|
| 66 | public ValueLookupParameter<IOperator> MoveGeneratorParameter {
|
---|
| 67 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
|
---|
| 68 | }
|
---|
| 69 | public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
|
---|
| 70 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
|
---|
| 71 | }
|
---|
| 72 | public ValueLookupParameter<IOperator> MoveMakerParameter {
|
---|
| 73 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
|
---|
| 74 | }
|
---|
[3621] | 75 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 76 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
[3134] | 77 | }
|
---|
[5356] | 78 | public LookupParameter<IntValue> EvaluatedMovesParameter {
|
---|
| 79 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
|
---|
[3078] | 80 | }
|
---|
| 81 | #endregion
|
---|
| 82 |
|
---|
[3145] | 83 | [StorableConstructor]
|
---|
[4722] | 84 | private LocalSearchMainLoop(bool deserializing) : base(deserializing) { }
|
---|
[3103] | 85 | public LocalSearchMainLoop()
|
---|
[3078] | 86 | : base() {
|
---|
[6042] | 87 | Initialize();
|
---|
[3145] | 88 | }
|
---|
[4722] | 89 | private LocalSearchMainLoop(LocalSearchMainLoop original, Cloner cloner)
|
---|
| 90 | : base(original, cloner) {
|
---|
| 91 | }
|
---|
| 92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 93 | return new LocalSearchMainLoop(this, cloner);
|
---|
| 94 | }
|
---|
[3145] | 95 |
|
---|
| 96 | private void Initialize() {
|
---|
[3078] | 97 | #region Create parameters
|
---|
| 98 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 99 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 100 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
[5753] | 101 | Parameters.Add(new LookupParameter<DoubleValue>("BestLocalQuality", "The value which represents the best quality found so far."));
|
---|
[6042] | 102 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The problem's best known quality value found so far."));
|
---|
[3078] | 103 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
[6042] | 104 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations performed."));
|
---|
[3078] | 105 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
| 106 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 107 |
|
---|
| 108 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
|
---|
| 109 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
|
---|
| 110 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
|
---|
[3134] | 111 |
|
---|
[3809] | 112 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
|
---|
[5356] | 113 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
|
---|
[3078] | 114 | #endregion
|
---|
| 115 |
|
---|
| 116 | #region Create operators
|
---|
[3636] | 117 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
| 118 | Assigner bestQualityInitializer = new Assigner();
|
---|
| 119 | Placeholder analyzer1 = new Placeholder();
|
---|
[3621] | 120 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
[3636] | 121 | SubScopesProcessor mainProcessor = new SubScopesProcessor();
|
---|
[3078] | 122 | Placeholder moveGenerator = new Placeholder();
|
---|
[3193] | 123 | UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
|
---|
[3078] | 124 | Placeholder moveEvaluator = new Placeholder();
|
---|
[5353] | 125 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
[3096] | 126 | BestSelector bestSelector = new BestSelector();
|
---|
[3636] | 127 | SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
|
---|
[3809] | 128 | UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
|
---|
[3078] | 129 | QualityComparator qualityComparator = new QualityComparator();
|
---|
| 130 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
| 131 | Placeholder moveMaker = new Placeholder();
|
---|
[3636] | 132 | Assigner bestQualityUpdater = new Assigner();
|
---|
[5753] | 133 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
[3809] | 134 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 135 | Placeholder analyzer2 = new Placeholder();
|
---|
[3078] | 136 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 137 | IntCounter iterationsCounter = new IntCounter();
|
---|
| 138 | Comparator iterationsComparator = new Comparator();
|
---|
| 139 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
| 140 |
|
---|
[3636] | 141 | bestQualityInitializer.Name = "Initialize BestQuality";
|
---|
[6042] | 142 | bestQualityInitializer.LeftSideParameter.ActualName = BestLocalQualityParameter.Name;
|
---|
[3636] | 143 | bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
[3078] | 144 |
|
---|
[3621] | 145 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 146 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3134] | 147 |
|
---|
[3679] | 148 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
[5753] | 149 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));
|
---|
[6042] | 150 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>(BestLocalQualityParameter.Name, null, BestLocalQualityParameter.Name));
|
---|
[3636] | 151 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3134] | 152 |
|
---|
[3078] | 153 | moveGenerator.Name = "MoveGenerator (placeholder)";
|
---|
[3143] | 154 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
[3078] | 155 |
|
---|
[5353] | 156 | moveEvaluationProcessor.Parallel = new BoolValue(true);
|
---|
| 157 |
|
---|
[3078] | 158 | moveEvaluator.Name = "MoveEvaluator (placeholder)";
|
---|
[3143] | 159 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
[3078] | 160 |
|
---|
[5353] | 161 | subScopesCounter.Name = "Increment EvaluatedMoves";
|
---|
[5356] | 162 | subScopesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name;
|
---|
[3636] | 163 |
|
---|
[3096] | 164 | bestSelector.CopySelected = new BoolValue(false);
|
---|
[3143] | 165 | bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
[3096] | 166 | bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
[3143] | 167 | bestSelector.QualityParameter.ActualName = MoveQualityParameter.Name;
|
---|
[3078] | 168 |
|
---|
[3143] | 169 | qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
|
---|
| 170 | qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
[3078] | 171 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
| 172 |
|
---|
| 173 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
| 174 |
|
---|
| 175 | moveMaker.Name = "MoveMaker (placeholder)";
|
---|
[3143] | 176 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
[3078] | 177 |
|
---|
[3636] | 178 | bestQualityUpdater.Name = "Update BestQuality";
|
---|
[6042] | 179 | bestQualityUpdater.LeftSideParameter.ActualName = BestLocalQualityParameter.Name;
|
---|
[3636] | 180 | bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
| 181 |
|
---|
[5753] | 182 | resultsCollector2.CopyValue = new BoolValue(false);
|
---|
[6042] | 183 | resultsCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>(BestLocalQualityParameter.Name, null, BestLocalQualityParameter.Name));
|
---|
[5753] | 184 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
| 185 |
|
---|
[3809] | 186 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 187 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 188 |
|
---|
[3078] | 189 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 190 |
|
---|
| 191 | iterationsCounter.Name = "Iterations Counter";
|
---|
| 192 | iterationsCounter.Increment = new IntValue(1);
|
---|
[5753] | 193 | iterationsCounter.ValueParameter.ActualName = IterationsParameter.Name;
|
---|
[3078] | 194 |
|
---|
[3143] | 195 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
| 196 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
[5753] | 197 | iterationsComparator.LeftSideParameter.ActualName = IterationsParameter.Name;
|
---|
[3143] | 198 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 199 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
[3078] | 200 |
|
---|
| 201 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
[3143] | 202 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
[3078] | 203 | #endregion
|
---|
| 204 |
|
---|
| 205 | #region Create operator graph
|
---|
[6044] | 206 | OperatorGraph.InitialOperator = subScopesProcessor0; // don't change this without adapting the constructor of LocalSearchImprovementOperator
|
---|
[3636] | 207 | subScopesProcessor0.Operators.Add(bestQualityInitializer);
|
---|
| 208 | subScopesProcessor0.Successor = resultsCollector1;
|
---|
| 209 | bestQualityInitializer.Successor = analyzer1;
|
---|
[3621] | 210 | analyzer1.Successor = null;
|
---|
[5353] | 211 | resultsCollector1.Successor = mainProcessor;
|
---|
[3636] | 212 | mainProcessor.Operators.Add(moveGenerator);
|
---|
[3078] | 213 | mainProcessor.Successor = iterationsCounter;
|
---|
| 214 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
| 215 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
[5353] | 216 | moveEvaluationProcessor.Successor = subScopesCounter;
|
---|
| 217 | moveEvaluator.Successor = null;
|
---|
| 218 | subScopesCounter.Successor = bestSelector;
|
---|
[3809] | 219 | bestSelector.Successor = moveMakingProcessor;
|
---|
| 220 | moveMakingProcessor.Operators.Add(new EmptyOperator());
|
---|
| 221 | moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
|
---|
| 222 | moveMakingProcessor.Successor = mergingReducer;
|
---|
| 223 | selectedMoveMakingProcessor.Operator = qualityComparator;
|
---|
[3078] | 224 | qualityComparator.Successor = improvesQualityBranch;
|
---|
| 225 | improvesQualityBranch.TrueBranch = moveMaker;
|
---|
[3134] | 226 | improvesQualityBranch.FalseBranch = null;
|
---|
| 227 | improvesQualityBranch.Successor = null;
|
---|
[3636] | 228 | moveMaker.Successor = bestQualityUpdater;
|
---|
| 229 | bestQualityUpdater.Successor = null;
|
---|
[3809] | 230 | mergingReducer.Successor = analyzer2;
|
---|
| 231 | analyzer2.Successor = subScopesRemover;
|
---|
| 232 | subScopesRemover.Successor = null;
|
---|
[3078] | 233 | iterationsCounter.Successor = iterationsComparator;
|
---|
[5353] | 234 | iterationsComparator.Successor = iterationsTermination;
|
---|
[3143] | 235 | iterationsTermination.TrueBranch = null;
|
---|
| 236 | iterationsTermination.FalseBranch = mainProcessor;
|
---|
[3078] | 237 | #endregion
|
---|
| 238 | }
|
---|
[3715] | 239 |
|
---|
| 240 | public override IOperation Apply() {
|
---|
| 241 | if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
|
---|
| 242 | return null;
|
---|
| 243 | return base.Apply();
|
---|
| 244 | }
|
---|
[6043] | 245 |
|
---|
| 246 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 247 | private void AfterDeserialization() {
|
---|
| 248 | // BackwardsCompatibility3.3
|
---|
| 249 | #region Backwards compatible code, remove with 3.4
|
---|
| 250 | if (!Parameters.ContainsKey("BestLocalQuality"))
|
---|
| 251 | Parameters.Add(new LookupParameter<DoubleValue>("BestLocalQuality", "The value which represents the best quality found so far."));
|
---|
| 252 | if (!Parameters.ContainsKey("Iterations"))
|
---|
| 253 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations performed."));
|
---|
| 254 | #endregion
|
---|
| 255 | }
|
---|
[3078] | 256 | }
|
---|
| 257 | }
|
---|