[5603] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Data;
|
---|
| 32 | using HeuristicLab.Optimization.Operators;
|
---|
| 33 | using HeuristicLab.Selection;
|
---|
[5622] | 34 | using HeuristicLab.Optimization;
|
---|
[5603] | 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.VariableNeighborhoodSearch {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// An operator which represents a variable neighborhood search.
|
---|
| 39 | /// </summary>
|
---|
| 40 | [Item("VariableNeighborhoodSearchMainLoop", "An operator which represents the main loop of a variable neighborhood search.")]
|
---|
| 41 | [StorableClass]
|
---|
| 42 | public sealed class VariableNeighborhoodSearchMainLoop : AlgorithmOperator {
|
---|
| 43 | #region Parameter properties
|
---|
| 44 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 45 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 46 | }
|
---|
| 47 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 48 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 49 | }
|
---|
| 50 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
| 51 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 52 | }
|
---|
| 53 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 54 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 55 | }
|
---|
| 56 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 57 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 58 | }
|
---|
| 59 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 60 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 61 | }
|
---|
| 62 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 63 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 64 | }
|
---|
| 65 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 66 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 67 | }
|
---|
| 68 | public LookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 69 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 70 | }
|
---|
| 71 | public ValueLookupParameter<ILocalImprovement> LocalImprovementParameter {
|
---|
| 72 | get { return (ValueLookupParameter<ILocalImprovement>)Parameters["LocalImprovement"]; }
|
---|
| 73 | }
|
---|
| 74 | public ValueLookupParameter<ShakingOperator> ShakingParameter {
|
---|
| 75 | get { return (ValueLookupParameter<ShakingOperator>)Parameters["Shaking"]; }
|
---|
| 76 | }
|
---|
| 77 | #endregion
|
---|
| 78 |
|
---|
| 79 | [StorableConstructor]
|
---|
| 80 | private VariableNeighborhoodSearchMainLoop(bool deserializing) : base(deserializing) { }
|
---|
| 81 | public VariableNeighborhoodSearchMainLoop()
|
---|
| 82 | : base() {
|
---|
| 83 | Initialize();
|
---|
| 84 | }
|
---|
| 85 | private VariableNeighborhoodSearchMainLoop(VariableNeighborhoodSearchMainLoop original, Cloner cloner)
|
---|
| 86 | : base(original, cloner) {
|
---|
| 87 | }
|
---|
| 88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 89 | return new VariableNeighborhoodSearchMainLoop(this, cloner);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private void Initialize() {
|
---|
| 93 | #region Create parameters
|
---|
| 94 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 95 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 96 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 97 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
| 98 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
|
---|
| 99 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
| 100 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 101 |
|
---|
| 102 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution."));
|
---|
| 103 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
|
---|
| 104 | Parameters.Add(new ValueLookupParameter<ILocalImprovement>("LocalImprovement", "The local improvement operation."));
|
---|
| 105 | Parameters.Add(new ValueLookupParameter<ShakingOperator>("Shaking", "The shaking operation."));
|
---|
| 106 | #endregion
|
---|
| 107 |
|
---|
| 108 | #region Create operators
|
---|
| 109 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 110 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
| 111 | Assigner bestQualityInitializer = new Assigner();
|
---|
| 112 | Placeholder analyzer1 = new Placeholder();
|
---|
| 113 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 114 |
|
---|
| 115 | CombinedOperator iteration = new CombinedOperator();
|
---|
| 116 | Assigner iterationInit = new Assigner();
|
---|
| 117 |
|
---|
| 118 | ChildrenCreator createChild = new ChildrenCreator();
|
---|
| 119 | SubScopesProcessor childProcessor = new SubScopesProcessor();
|
---|
| 120 | ParentCopyCrossover parentCloner = new ParentCopyCrossover();
|
---|
| 121 |
|
---|
| 122 | Assigner qualityAssigner = new Assigner();
|
---|
| 123 | Placeholder shaking = new Placeholder();
|
---|
| 124 | Placeholder localImprovement = new Placeholder();
|
---|
| 125 | Placeholder evaluator = new Placeholder();
|
---|
[5622] | 126 | IntCounter evalCounter = new IntCounter();
|
---|
[5603] | 127 |
|
---|
| 128 | QualityComparator qualityComparator = new QualityComparator();
|
---|
| 129 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
[5609] | 130 | ConditionalBranch improvesQualityBranch2 = new ConditionalBranch();
|
---|
[5603] | 131 |
|
---|
| 132 | Assigner bestQualityUpdater = new Assigner();
|
---|
| 133 | ScopeCleaner cleaner = new ScopeCleaner();
|
---|
| 134 | ParentCopyCrossover originalRestorer = new ParentCopyCrossover();
|
---|
| 135 |
|
---|
| 136 | IntCounter indexCounter = new IntCounter();
|
---|
| 137 | Assigner indexResetter = new Assigner();
|
---|
| 138 |
|
---|
[5609] | 139 | SubScopesRemover remover1 = new SubScopesRemover();
|
---|
| 140 | SubScopesRemover remover2 = new SubScopesRemover();
|
---|
[5603] | 141 | Placeholder analyzer2 = new Placeholder();
|
---|
| 142 |
|
---|
| 143 | ConditionalBranch indexTermination = new ConditionalBranch();
|
---|
| 144 |
|
---|
| 145 | IntCounter iterationsCounter = new IntCounter();
|
---|
| 146 | Comparator iterationsComparator = new Comparator();
|
---|
| 147 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
| 148 |
|
---|
[5610] | 149 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OuterIterations", new IntValue(0)));
|
---|
[5603] | 150 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Index", new DoubleValue(0)));
|
---|
[5609] | 151 | variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("Continue", new BoolValue(false)));
|
---|
[5610] | 152 | variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("IsBetter", new BoolValue(false)));
|
---|
[5603] | 153 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
|
---|
| 154 |
|
---|
| 155 | bestQualityInitializer.Name = "Initialize BestQuality";
|
---|
| 156 | bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
|
---|
| 157 | bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
| 158 |
|
---|
| 159 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 160 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 161 |
|
---|
| 162 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
[5610] | 163 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("OuterIterations"));
|
---|
[5603] | 164 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
| 165 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
| 166 |
|
---|
| 167 | iteration.Name = "Iteration";
|
---|
| 168 |
|
---|
| 169 | iterationInit.Name = "Init iteration";
|
---|
| 170 | iterationInit.LeftSideParameter.ActualName = "Index";
|
---|
| 171 | iterationInit.RightSideParameter.Value = new IntValue(0);
|
---|
| 172 |
|
---|
| 173 | createChild.Name = "Create child";
|
---|
| 174 | createChild.ParentsPerChild.Value = 1;
|
---|
| 175 |
|
---|
| 176 | parentCloner.Name = "Copy parent";
|
---|
| 177 |
|
---|
| 178 | qualityAssigner.Name = "Assign quality";
|
---|
| 179 | qualityAssigner.LeftSideParameter.ActualName = "OriginalQuality";
|
---|
| 180 | qualityAssigner.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
| 181 |
|
---|
| 182 | shaking.Name = "Shaking operator (placeholder)";
|
---|
| 183 | shaking.OperatorParameter.ActualName = ShakingParameter.Name;
|
---|
| 184 |
|
---|
| 185 | localImprovement.Name = "Local improvement operator (placeholder)";
|
---|
| 186 | localImprovement.OperatorParameter.ActualName = LocalImprovementParameter.Name;
|
---|
| 187 |
|
---|
| 188 | evaluator.Name = "Evaluation operator (placeholder)";
|
---|
| 189 | evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 190 |
|
---|
[5622] | 191 | evalCounter.Name = "Count evaluateions";
|
---|
| 192 | evalCounter.Increment.Value = 1;
|
---|
| 193 | evalCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.ActualName;
|
---|
| 194 |
|
---|
[5609] | 195 | qualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
|
---|
| 196 | qualityComparator.RightSideParameter.ActualName = "OriginalQuality";
|
---|
[5603] | 197 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
| 198 |
|
---|
| 199 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
[5609] | 200 | improvesQualityBranch2.ConditionParameter.ActualName = "IsBetter";
|
---|
[5603] | 201 |
|
---|
| 202 | bestQualityUpdater.Name = "Update BestQuality";
|
---|
| 203 | bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
|
---|
| 204 | bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
| 205 |
|
---|
| 206 | cleaner.Name = "Clean scope";
|
---|
[5609] | 207 | cleaner.ClearSubScopesParameter.Value = new BoolValue(false);
|
---|
[5603] | 208 | originalRestorer.Name = "Restore original solution";
|
---|
| 209 |
|
---|
| 210 | indexCounter.Name = "Count index";
|
---|
| 211 | indexCounter.Increment.Value = 1;
|
---|
| 212 | indexCounter.ValueParameter.ActualName = "Index";
|
---|
| 213 |
|
---|
| 214 | indexResetter.Name = "Reset index";
|
---|
| 215 | indexResetter.LeftSideParameter.ActualName = "Index";
|
---|
| 216 | indexResetter.RightSideParameter.Value = new IntValue(0);
|
---|
| 217 |
|
---|
[5609] | 218 | remover1.Name = remover2.Name = "Remove subscope";
|
---|
[5603] | 219 |
|
---|
| 220 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 221 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 222 |
|
---|
| 223 | originalRestorer.Name = "Restore parent";
|
---|
| 224 |
|
---|
| 225 | iterationsCounter.Name = "Iterations Counter";
|
---|
| 226 | iterationsCounter.Increment = new IntValue(1);
|
---|
[5610] | 227 | iterationsCounter.ValueParameter.ActualName = "OuterIterations";
|
---|
[5603] | 228 |
|
---|
| 229 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
| 230 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
[5610] | 231 | iterationsComparator.LeftSideParameter.ActualName = "OuterIterations";
|
---|
[5603] | 232 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 233 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
| 234 |
|
---|
| 235 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
| 236 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
| 237 |
|
---|
| 238 | indexTermination.Name = "Index Termination Condition";
|
---|
| 239 | indexTermination.ConditionParameter.ActualName = "Continue";
|
---|
| 240 | #endregion
|
---|
| 241 |
|
---|
| 242 | #region Create operator graph
|
---|
| 243 | OperatorGraph.InitialOperator = variableCreator;
|
---|
[5622] | 244 | variableCreator.Successor = analyzer1;
|
---|
| 245 | analyzer1.Successor = subScopesProcessor0;
|
---|
[5603] | 246 | subScopesProcessor0.Operators.Add(bestQualityInitializer);
|
---|
| 247 | subScopesProcessor0.Successor = resultsCollector1;
|
---|
| 248 | /////////
|
---|
| 249 | resultsCollector1.Successor = iteration;
|
---|
[5610] | 250 |
|
---|
[5603] | 251 | iteration.OperatorGraph.InitialOperator = iterationInit;
|
---|
| 252 | iteration.Successor = iterationsCounter;
|
---|
| 253 | iterationInit.Successor = createChild;
|
---|
| 254 |
|
---|
| 255 | createChild.Successor = childProcessor;
|
---|
| 256 | childProcessor.Operators.Add(parentCloner);
|
---|
[5622] | 257 | childProcessor.Successor = analyzer2;
|
---|
[5603] | 258 | /////////
|
---|
| 259 | parentCloner.Successor = qualityAssigner;
|
---|
| 260 | qualityAssigner.Successor = shaking;
|
---|
[5622] | 261 | shaking.Successor = evaluator;
|
---|
| 262 | evaluator.Successor = evalCounter;
|
---|
| 263 | evalCounter.Successor = localImprovement;
|
---|
| 264 | localImprovement.Successor = qualityComparator;
|
---|
[5603] | 265 | qualityComparator.Successor = improvesQualityBranch;
|
---|
| 266 | improvesQualityBranch.TrueBranch = bestQualityUpdater;
|
---|
| 267 | improvesQualityBranch.FalseBranch = cleaner;
|
---|
| 268 |
|
---|
| 269 | bestQualityUpdater.Successor = indexResetter;
|
---|
[5609] | 270 | indexResetter.Successor = remover1;
|
---|
[5603] | 271 |
|
---|
| 272 | cleaner.Successor = originalRestorer;
|
---|
[5609] | 273 | originalRestorer.Successor = remover2;
|
---|
| 274 | remover2.Successor = indexCounter;
|
---|
[5603] | 275 | /////////
|
---|
[5622] | 276 | analyzer2.Successor = indexTermination;
|
---|
[5609] | 277 | indexTermination.TrueBranch = improvesQualityBranch2;
|
---|
| 278 | indexTermination.FalseBranch = null;
|
---|
[5603] | 279 |
|
---|
[5609] | 280 | improvesQualityBranch2.TrueBranch = null;
|
---|
| 281 | improvesQualityBranch2.FalseBranch = createChild;
|
---|
| 282 |
|
---|
[5603] | 283 | iterationsCounter.Successor = iterationsComparator;
|
---|
| 284 | iterationsComparator.Successor = iterationsTermination;
|
---|
| 285 | iterationsTermination.TrueBranch = null;
|
---|
| 286 | iterationsTermination.FalseBranch = iteration;
|
---|
| 287 | #endregion
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | public override IOperation Apply() {
|
---|
| 291 | if (LocalImprovementParameter.ActualValue == null || EvaluatorParameter.ActualValue == null)
|
---|
| 292 | return null;
|
---|
| 293 | return base.Apply();
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 | }
|
---|