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> VisualizerParameter {
|
---|
80 | get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
|
---|
81 | }
|
---|
82 | public LookupParameter<IItem> VisualizationParameter {
|
---|
83 | get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
|
---|
84 | }
|
---|
85 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
86 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | [StorableConstructor]
|
---|
91 | private TabuSearchMainLoop(bool deserializing) : base() { }
|
---|
92 | public TabuSearchMainLoop()
|
---|
93 | : base() {
|
---|
94 | Initialize();
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void Initialize() {
|
---|
98 | #region Create parameters
|
---|
99 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
100 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
101 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
102 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
103 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
104 | Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The value that indicates if a move is tabu or not."));
|
---|
105 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
106 | Parameters.Add(new ValueLookupParameter<IntValue>("TabuTenure", "The length of the tabu list, and also means the number of iterations a move is kept tabu"));
|
---|
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."));
|
---|
111 | Parameters.Add(new ValueLookupParameter<IOperator>("TabuChecker", "The operator that checks whether a move is tabu."));
|
---|
112 | Parameters.Add(new ValueLookupParameter<IOperator>("TabuMaker", "The operator that declares a move tabu."));
|
---|
113 |
|
---|
114 | Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
|
---|
115 | Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
|
---|
116 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
117 | #endregion
|
---|
118 |
|
---|
119 | #region Create operators
|
---|
120 | TabuListCreator tabuListCreator = new TabuListCreator();
|
---|
121 | VariableCreator variableCreator = new VariableCreator();
|
---|
122 | BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
|
---|
123 | BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
|
---|
124 | QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
|
---|
125 | Placeholder visualizer1 = new Placeholder();
|
---|
126 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
127 | UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
|
---|
128 | Placeholder moveGenerator = new Placeholder();
|
---|
129 | UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
|
---|
130 | Placeholder moveEvaluator = new Placeholder();
|
---|
131 | Placeholder tabuChecker = new Placeholder();
|
---|
132 | SubScopesSorter moveQualitySorter = new SubScopesSorter();
|
---|
133 | BestAverageWorstQualityCalculator bestAverageWorstMoveQualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
134 | TabuSelector tabuSelector = new TabuSelector();
|
---|
135 | ConditionalBranch emptyNeighborhoodBranch1 = new ConditionalBranch();
|
---|
136 | RightReducer rightReducer = new RightReducer();
|
---|
137 | UniformSubScopesProcessor moveMakingProcessor = new UniformSubScopesProcessor();
|
---|
138 | Placeholder tabuMaker = new Placeholder();
|
---|
139 | Placeholder moveMaker = new Placeholder();
|
---|
140 | DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
|
---|
141 | SubScopesRemover subScopesRemover1 = new SubScopesRemover();
|
---|
142 | ConditionalBranch emptyNeighborhoodBranch2 = new ConditionalBranch();
|
---|
143 | UniformSubScopesProcessor removeMoves = new UniformSubScopesProcessor();
|
---|
144 | SubScopesRemover subScopesRemover2 = new SubScopesRemover();
|
---|
145 | IntCounter iterationsCounter = new IntCounter();
|
---|
146 | Comparator iterationsComparator = new Comparator();
|
---|
147 | BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
|
---|
148 | BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
|
---|
149 | QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
|
---|
150 | Placeholder visualizer2 = new Placeholder();
|
---|
151 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
152 |
|
---|
153 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
154 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Best Move Quality", new DoubleValue(0)));
|
---|
155 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Average Move Quality", new DoubleValue(0)));
|
---|
156 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Worst Move Quality", new DoubleValue(0)));
|
---|
157 | variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("MoveQualities", new DataTable("MoveQualities")));
|
---|
158 | variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("EmptyNeighborhood", new BoolValue(false)));
|
---|
159 |
|
---|
160 | bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
|
---|
161 | bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
162 | bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
|
---|
163 |
|
---|
164 | bestQualityMemorizer2.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
165 | bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
166 | bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
|
---|
167 |
|
---|
168 | qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
|
---|
169 | qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
170 | qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
|
---|
171 | qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
|
---|
172 |
|
---|
173 | visualizer1.Name = "Visualizer (placeholder)";
|
---|
174 | visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
|
---|
175 |
|
---|
176 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
177 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
|
---|
178 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Move Quality"));
|
---|
179 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Average Move Quality"));
|
---|
180 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Worst Move Quality"));
|
---|
181 | resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("MoveQualities"));
|
---|
182 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
|
---|
183 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
|
---|
184 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
|
---|
185 | resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
|
---|
186 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
187 |
|
---|
188 | mainProcessor.Name = "Solution processor (UniformSubScopesProcessor)";
|
---|
189 |
|
---|
190 | moveGenerator.Name = "MoveGenerator (placeholder)";
|
---|
191 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
192 |
|
---|
193 | moveEvaluator.Name = "MoveEvaluator (placeholder)";
|
---|
194 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
195 |
|
---|
196 | tabuChecker.Name = "TabuChecker (placeholder)";
|
---|
197 | tabuChecker.OperatorParameter.ActualName = TabuCheckerParameter.Name;
|
---|
198 |
|
---|
199 | moveQualitySorter.DescendingParameter.ActualName = MaximizationParameter.Name;
|
---|
200 | moveQualitySorter.ValueParameter.ActualName = MoveQualityParameter.Name;
|
---|
201 |
|
---|
202 | bestAverageWorstMoveQualityCalculator.AverageQualityParameter.ActualName = "Average Move Quality";
|
---|
203 | bestAverageWorstMoveQualityCalculator.BestQualityParameter.ActualName = "Best Move Quality";
|
---|
204 | bestAverageWorstMoveQualityCalculator.MaximizationParameter.ActualName = "Maximization";
|
---|
205 | bestAverageWorstMoveQualityCalculator.QualityParameter.ActualName = MoveQualityParameter.Name;
|
---|
206 | bestAverageWorstMoveQualityCalculator.WorstQualityParameter.ActualName = "Worst Move Quality";
|
---|
207 |
|
---|
208 | valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Move Quality"));
|
---|
209 | valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Average Move Quality"));
|
---|
210 | valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Worst Move Quality"));
|
---|
211 | valuesCollector.DataTableParameter.ActualName = "MoveQualities";
|
---|
212 |
|
---|
213 | moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
|
---|
214 |
|
---|
215 | emptyNeighborhoodBranch1.Name = "Neighborhood empty?";
|
---|
216 | emptyNeighborhoodBranch1.ConditionParameter.ActualName = "EmptyNeighborhood";
|
---|
217 |
|
---|
218 | tabuMaker.Name = "TabuMaker (placeholder)";
|
---|
219 | tabuMaker.OperatorParameter.ActualName = TabuMakerParameter.Name;
|
---|
220 |
|
---|
221 | moveMaker.Name = "MoveMaker (placeholder)";
|
---|
222 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
223 |
|
---|
224 | subScopesRemover1.RemoveAllSubScopes = true;
|
---|
225 |
|
---|
226 | emptyNeighborhoodBranch2.Name = "Neighborhood empty?";
|
---|
227 | emptyNeighborhoodBranch2.ConditionParameter.ActualName = "EmptyNeighborhood";
|
---|
228 |
|
---|
229 | subScopesRemover2.RemoveAllSubScopes = true;
|
---|
230 |
|
---|
231 | iterationsCounter.Name = "Iterations Counter";
|
---|
232 | iterationsCounter.Increment = new IntValue(1);
|
---|
233 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
234 |
|
---|
235 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
236 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
237 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
238 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
239 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
240 |
|
---|
241 | bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
|
---|
242 | bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
243 | bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
|
---|
244 |
|
---|
245 | bestQualityMemorizer4.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
246 | bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
247 | bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
|
---|
248 |
|
---|
249 | qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
|
---|
250 | qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
251 | qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
|
---|
252 | qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
|
---|
253 |
|
---|
254 | visualizer2.Name = "Visualizer (placeholder)";
|
---|
255 | visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
|
---|
256 |
|
---|
257 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
258 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
259 | #endregion
|
---|
260 |
|
---|
261 | #region Create operator graph
|
---|
262 | OperatorGraph.InitialOperator = tabuListCreator;
|
---|
263 | tabuListCreator.Successor = variableCreator;
|
---|
264 | variableCreator.Successor = bestQualityMemorizer1;
|
---|
265 | bestQualityMemorizer1.Successor = bestQualityMemorizer2;
|
---|
266 | bestQualityMemorizer2.Successor = qualityDifferenceCalculator1;
|
---|
267 | qualityDifferenceCalculator1.Successor = visualizer1;
|
---|
268 | visualizer1.Successor = resultsCollector;
|
---|
269 | resultsCollector.Successor = mainProcessor;
|
---|
270 | mainProcessor.Operator = moveGenerator;
|
---|
271 | mainProcessor.Successor = emptyNeighborhoodBranch2;
|
---|
272 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
273 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
274 | moveEvaluationProcessor.Successor = moveQualitySorter;
|
---|
275 | moveEvaluator.Successor = tabuChecker;
|
---|
276 | tabuChecker.Successor = null;
|
---|
277 | moveQualitySorter.Successor = bestAverageWorstMoveQualityCalculator;
|
---|
278 | bestAverageWorstMoveQualityCalculator.Successor = valuesCollector;
|
---|
279 | valuesCollector.Successor = tabuSelector;
|
---|
280 | tabuSelector.Successor = emptyNeighborhoodBranch1;
|
---|
281 | emptyNeighborhoodBranch1.FalseBranch = rightReducer;
|
---|
282 | emptyNeighborhoodBranch1.TrueBranch = null;
|
---|
283 | emptyNeighborhoodBranch1.Successor = null;
|
---|
284 | rightReducer.Successor = moveMakingProcessor;
|
---|
285 | moveMakingProcessor.Operator = tabuMaker;
|
---|
286 | moveMakingProcessor.Successor = subScopesRemover1;
|
---|
287 | tabuMaker.Successor = moveMaker;
|
---|
288 | moveMaker.Successor = null;
|
---|
289 | subScopesRemover1.Successor = null;
|
---|
290 | emptyNeighborhoodBranch2.FalseBranch = iterationsCounter;
|
---|
291 | emptyNeighborhoodBranch2.TrueBranch = removeMoves;
|
---|
292 | emptyNeighborhoodBranch2.Successor = null;
|
---|
293 | removeMoves.Operator = subScopesRemover2;
|
---|
294 | removeMoves.Successor = null;
|
---|
295 | subScopesRemover2.Successor = null;
|
---|
296 | iterationsCounter.Successor = iterationsComparator;
|
---|
297 | iterationsComparator.Successor = bestQualityMemorizer3;
|
---|
298 | bestQualityMemorizer3.Successor = bestQualityMemorizer4;
|
---|
299 | bestQualityMemorizer4.Successor = qualityDifferenceCalculator2;
|
---|
300 | qualityDifferenceCalculator2.Successor = visualizer2;
|
---|
301 | visualizer2.Successor = iterationsTermination;
|
---|
302 | iterationsTermination.TrueBranch = null;
|
---|
303 | iterationsTermination.FalseBranch = mainProcessor;
|
---|
304 | #endregion
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|