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.Analysis;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Selection;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.TabuSearch {
|
---|
33 | /// <summary>
|
---|
34 | /// An operator which represents a tabu search.
|
---|
35 | /// </summary>
|
---|
36 | [Item("TabuSearchMainLoop", "An operator which represents the main loop of a tabu search.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class TabuSearchMainLoop : AlgorithmOperator {
|
---|
39 | #region Parameter properties
|
---|
40 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
41 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
42 | }
|
---|
43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
45 | }
|
---|
46 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
47 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
48 | }
|
---|
49 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
50 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
51 | }
|
---|
52 | public LookupParameter<DoubleValue> MoveQualityParameter {
|
---|
53 | get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
54 | }
|
---|
55 | public LookupParameter<BoolValue> MoveTabuParameter {
|
---|
56 | get { return (LookupParameter<BoolValue>)Parameters["MoveTabu"]; }
|
---|
57 | }
|
---|
58 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
59 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
60 | }
|
---|
61 | public ValueLookupParameter<IntValue> TabuTenureParameter {
|
---|
62 | get { return (ValueLookupParameter<IntValue>)Parameters["TabuTenure"]; }
|
---|
63 | }
|
---|
64 | public ValueLookupParameter<IOperator> MoveGeneratorParameter {
|
---|
65 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
|
---|
66 | }
|
---|
67 | public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
|
---|
68 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
|
---|
69 | }
|
---|
70 | public ValueLookupParameter<IOperator> MoveMakerParameter {
|
---|
71 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
|
---|
72 | }
|
---|
73 | public ValueLookupParameter<IOperator> TabuCheckerParameter {
|
---|
74 | get { return (ValueLookupParameter<IOperator>)Parameters["TabuChecker"]; }
|
---|
75 | }
|
---|
76 | public ValueLookupParameter<IOperator> TabuMakerParameter {
|
---|
77 | get { return (ValueLookupParameter<IOperator>)Parameters["TabuMaker"]; }
|
---|
78 | }
|
---|
79 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
80 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
81 | }
|
---|
82 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
83 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
84 | }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | [StorableConstructor]
|
---|
88 | private TabuSearchMainLoop(bool deserializing) : base(deserializing) { }
|
---|
89 | public TabuSearchMainLoop()
|
---|
90 | : base() {
|
---|
91 | Initialize();
|
---|
92 | }
|
---|
93 | private TabuSearchMainLoop(TabuSearchMainLoop original, Cloner cloner)
|
---|
94 | : base(original, cloner) {
|
---|
95 | }
|
---|
96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
97 | return new TabuSearchMainLoop(this, cloner);
|
---|
98 | }
|
---|
99 |
|
---|
100 | [StorableHook(HookType.AfterDeserialization)]
|
---|
101 | private void AfterDeserialization() {
|
---|
102 | // BackwardsCompatibility3.3
|
---|
103 | #region Backwards compatible code, remove with 3.4
|
---|
104 | VariableCreator variableCreator = OperatorGraph.InitialOperator as VariableCreator;
|
---|
105 |
|
---|
106 | if (variableCreator != null) {
|
---|
107 | if (!variableCreator.CollectedValues.ContainsKey("Memories")) {
|
---|
108 | variableCreator.CollectedValues.Add(new ValueParameter<VariableCollection>("Memories", new VariableCollection()));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void Initialize() {
|
---|
115 | #region Create parameters
|
---|
116 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
117 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
118 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
119 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
120 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
121 | Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The value that indicates if a move is tabu or not."));
|
---|
122 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
123 | Parameters.Add(new ValueLookupParameter<IntValue>("TabuTenure", "The length of the tabu list, and also means the number of iterations a move is kept tabu"));
|
---|
124 |
|
---|
125 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
|
---|
126 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
|
---|
127 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
|
---|
128 | Parameters.Add(new ValueLookupParameter<IOperator>("TabuChecker", "The operator that checks whether a move is tabu."));
|
---|
129 | Parameters.Add(new ValueLookupParameter<IOperator>("TabuMaker", "The operator that declares a move tabu."));
|
---|
130 |
|
---|
131 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
|
---|
132 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | #region Create operators
|
---|
136 | VariableCreator variableCreator = new VariableCreator();
|
---|
137 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
138 | Assigner bestQualityInitializer = new Assigner();
|
---|
139 | Placeholder analyzer1 = new Placeholder();
|
---|
140 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
141 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
142 | SubScopesProcessor solutionProcessor = new SubScopesProcessor();
|
---|
143 | Placeholder moveGenerator = new Placeholder();
|
---|
144 | UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
|
---|
145 | Placeholder moveEvaluator = new Placeholder();
|
---|
146 | IntCounter evaluatedMovesCounter = new IntCounter();
|
---|
147 | Placeholder tabuChecker = new Placeholder();
|
---|
148 | SubScopesSorter moveQualitySorter = new SubScopesSorter();
|
---|
149 | TabuSelector tabuSelector = new TabuSelector();
|
---|
150 | ConditionalBranch emptyNeighborhoodBranch1 = new ConditionalBranch();
|
---|
151 | SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
|
---|
152 | UniformSubScopesProcessor selectedMoveMakingProcesor = new UniformSubScopesProcessor();
|
---|
153 | Placeholder tabuMaker = new Placeholder();
|
---|
154 | Placeholder moveMaker = new Placeholder();
|
---|
155 | MergingReducer mergingReducer = new MergingReducer();
|
---|
156 | Placeholder analyzer2 = new Placeholder();
|
---|
157 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
158 | ConditionalBranch emptyNeighborhoodBranch2 = new ConditionalBranch();
|
---|
159 | BestQualityMemorizer bestQualityUpdater = new BestQualityMemorizer();
|
---|
160 | IntCounter iterationsCounter = new IntCounter();
|
---|
161 | Comparator iterationsComparator = new Comparator();
|
---|
162 | ResultsCollector resultsCollector3 = new ResultsCollector();
|
---|
163 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
164 |
|
---|
165 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class TabuSearch expects this to be called Iterations
|
---|
166 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
|
---|
167 | variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("EmptyNeighborhood", new BoolValue(false)));
|
---|
168 | variableCreator.CollectedValues.Add(new ValueParameter<ItemList<IItem>>("TabuList", new ItemList<IItem>()));
|
---|
169 | variableCreator.CollectedValues.Add(new ValueParameter<VariableCollection>("Memories", new VariableCollection()));
|
---|
170 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
|
---|
171 |
|
---|
172 | bestQualityInitializer.Name = "Initialize BestQuality";
|
---|
173 | bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
|
---|
174 | bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
175 |
|
---|
176 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
177 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
178 |
|
---|
179 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
180 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
181 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
182 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
183 |
|
---|
184 | resultsCollector2.CopyValue = new BoolValue(true);
|
---|
185 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
186 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
187 |
|
---|
188 | moveGenerator.Name = "MoveGenerator (placeholder)";
|
---|
189 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
190 |
|
---|
191 | moveEvaluator.Name = "MoveEvaluator (placeholder)";
|
---|
192 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
193 |
|
---|
194 | evaluatedMovesCounter.Name = "EvaluatedMoves + 1";
|
---|
195 | evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
|
---|
196 | evaluatedMovesCounter.Increment = new IntValue(1);
|
---|
197 |
|
---|
198 | tabuChecker.Name = "TabuChecker (placeholder)";
|
---|
199 | tabuChecker.OperatorParameter.ActualName = TabuCheckerParameter.Name;
|
---|
200 |
|
---|
201 | moveQualitySorter.DescendingParameter.ActualName = MaximizationParameter.Name;
|
---|
202 | moveQualitySorter.ValueParameter.ActualName = MoveQualityParameter.Name;
|
---|
203 |
|
---|
204 | tabuSelector.AspirationParameter.Value = new BoolValue(true);
|
---|
205 | tabuSelector.BestQualityParameter.ActualName = "BestQuality";
|
---|
206 | tabuSelector.CopySelected = new BoolValue(false);
|
---|
207 | tabuSelector.EmptyNeighborhoodParameter.ActualName = "EmptyNeighborhood";
|
---|
208 | tabuSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
209 | tabuSelector.MoveQualityParameter.ActualName = MoveQualityParameter.Name;
|
---|
210 | tabuSelector.MoveTabuParameter.ActualName = MoveTabuParameter.Name;
|
---|
211 |
|
---|
212 | moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
|
---|
213 |
|
---|
214 | emptyNeighborhoodBranch1.Name = "Neighborhood empty?";
|
---|
215 | emptyNeighborhoodBranch1.ConditionParameter.ActualName = "EmptyNeighborhood";
|
---|
216 |
|
---|
217 | tabuMaker.Name = "TabuMaker (placeholder)";
|
---|
218 | tabuMaker.OperatorParameter.ActualName = TabuMakerParameter.Name;
|
---|
219 |
|
---|
220 | moveMaker.Name = "MoveMaker (placeholder)";
|
---|
221 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
222 |
|
---|
223 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
224 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
225 |
|
---|
226 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
227 |
|
---|
228 | bestQualityUpdater.Name = "Update BestQuality";
|
---|
229 | bestQualityUpdater.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
230 | bestQualityUpdater.QualityParameter.ActualName = QualityParameter.Name;
|
---|
231 | bestQualityUpdater.BestQualityParameter.ActualName = "BestQuality";
|
---|
232 |
|
---|
233 | iterationsCounter.Name = "Iterations Counter";
|
---|
234 | iterationsCounter.Increment = new IntValue(1);
|
---|
235 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
236 |
|
---|
237 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
238 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
239 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
240 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
241 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
242 |
|
---|
243 | resultsCollector3.CopyValue = new BoolValue(true);
|
---|
244 | resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
|
---|
245 | resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
246 |
|
---|
247 | emptyNeighborhoodBranch2.Name = "Neighborhood empty?";
|
---|
248 | emptyNeighborhoodBranch2.ConditionParameter.ActualName = "EmptyNeighborhood";
|
---|
249 |
|
---|
250 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
251 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
252 | #endregion
|
---|
253 |
|
---|
254 | #region Create operator graph
|
---|
255 | OperatorGraph.InitialOperator = variableCreator;
|
---|
256 | variableCreator.Successor = subScopesProcessor0;
|
---|
257 | subScopesProcessor0.Operators.Add(bestQualityInitializer);
|
---|
258 | subScopesProcessor0.Successor = resultsCollector1;
|
---|
259 | bestQualityInitializer.Successor = analyzer1;
|
---|
260 | analyzer1.Successor = null;
|
---|
261 | resultsCollector1.Successor = resultsCollector2;
|
---|
262 | resultsCollector2.Successor = solutionProcessor;
|
---|
263 | solutionProcessor.Operators.Add(moveGenerator);
|
---|
264 | solutionProcessor.Successor = iterationsCounter;
|
---|
265 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
266 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
267 | moveEvaluationProcessor.Successor = moveQualitySorter;
|
---|
268 | moveEvaluator.Successor = evaluatedMovesCounter;
|
---|
269 | evaluatedMovesCounter.Successor = tabuChecker;
|
---|
270 | tabuChecker.Successor = null;
|
---|
271 | moveQualitySorter.Successor = tabuSelector;
|
---|
272 | tabuSelector.Successor = emptyNeighborhoodBranch1;
|
---|
273 | emptyNeighborhoodBranch1.FalseBranch = moveMakingProcessor;
|
---|
274 | emptyNeighborhoodBranch1.TrueBranch = null;
|
---|
275 | emptyNeighborhoodBranch1.Successor = subScopesRemover;
|
---|
276 | moveMakingProcessor.Operators.Add(new EmptyOperator());
|
---|
277 | moveMakingProcessor.Operators.Add(selectedMoveMakingProcesor);
|
---|
278 | moveMakingProcessor.Successor = mergingReducer;
|
---|
279 | selectedMoveMakingProcesor.Operator = tabuMaker;
|
---|
280 | selectedMoveMakingProcesor.Successor = null;
|
---|
281 | tabuMaker.Successor = moveMaker;
|
---|
282 | moveMaker.Successor = null;
|
---|
283 | mergingReducer.Successor = analyzer2;
|
---|
284 | analyzer2.Successor = null;
|
---|
285 | subScopesRemover.Successor = null;
|
---|
286 | iterationsCounter.Successor = iterationsComparator;
|
---|
287 | iterationsComparator.Successor = resultsCollector3;
|
---|
288 | resultsCollector3.Successor = emptyNeighborhoodBranch2;
|
---|
289 | emptyNeighborhoodBranch2.TrueBranch = null;
|
---|
290 | emptyNeighborhoodBranch2.FalseBranch = iterationsTermination;
|
---|
291 | emptyNeighborhoodBranch2.Successor = null;
|
---|
292 | iterationsTermination.TrueBranch = null;
|
---|
293 | iterationsTermination.FalseBranch = solutionProcessor;
|
---|
294 | #endregion
|
---|
295 | }
|
---|
296 |
|
---|
297 | public override IOperation Apply(IExecutionContext context) {
|
---|
298 | if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null
|
---|
299 | || TabuCheckerParameter.ActualValue == null || TabuMakerParameter.ActualValue == null)
|
---|
300 | return null;
|
---|
301 | return base.Apply(context);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|