source: trunk/sources/HeuristicLab.Algorithms.TabuSearch/3.3/TabuSearchMainLoop.cs @ 3195

Last change on this file since 3195 was 3195, checked in by abeham, 12 years ago

fixed algorithm when neighborhood contains only tabu moves. It will now terminate correctly. #840

File size: 17.8 KB
Line 
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
22using HeuristicLab.Analysis;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Selection;
29
30namespace HeuristicLab.Algorithms.TabuSearch {
31  /// <summary>
32  /// An operator which represents a tabu search.
33  /// </summary>
34  [Item("TabuSearchMainLoop", "An operator which represents the main loop of a tabu search.")]
35  [StorableClass]
36  public sealed class TabuSearchMainLoop : AlgorithmOperator {
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    }
47    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
48      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
49    }
50    public LookupParameter<DoubleValue> MoveQualityParameter {
51      get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
52    }
53    public LookupParameter<BoolValue> MoveTabuParameter {
54      get { return (LookupParameter<BoolValue>)Parameters["MoveTabu"]; }
55    }
56    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
57      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
58    }
59    public ValueLookupParameter<IntValue> TabuTenureParameter {
60      get { return (ValueLookupParameter<IntValue>)Parameters["TabuTenure"]; }
61    }
62    public ValueLookupParameter<IOperator> MoveGeneratorParameter {
63      get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
64    }
65    public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
66      get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
67    }
68    public ValueLookupParameter<IOperator> MoveMakerParameter {
69      get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
70    }
71    public ValueLookupParameter<IOperator> TabuMoveEvaluatorParameter {
72      get { return (ValueLookupParameter<IOperator>)Parameters["TabuMoveEvaluator"]; }
73    }
74    public ValueLookupParameter<IOperator> TabuMoveMakerParameter {
75      get { return (ValueLookupParameter<IOperator>)Parameters["TabuMoveMaker"]; }
76    }
77    public ValueLookupParameter<IOperator> VisualizerParameter {
78      get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
79    }
80    public LookupParameter<IItem> VisualizationParameter {
81      get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
82    }
83    public ValueLookupParameter<VariableCollection> ResultsParameter {
84      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
85    }
86    #endregion
87
88    [StorableConstructor]
89    private TabuSearchMainLoop(bool deserializing) : base() { }
90    public TabuSearchMainLoop()
91      : base() {
92      Initialize();
93    }
94
95    private void Initialize() {
96      #region Create parameters
97      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
98      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
99      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
100      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
101      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
102      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The value that indicates if a move is tabu or not."));
103      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
104      Parameters.Add(new ValueLookupParameter<IntValue>("TabuTenure", "The length of the tabu list, and also means the number of iterations a move is kept tabu"));
105
106      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
107      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
108      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
109      Parameters.Add(new ValueLookupParameter<IOperator>("TabuMoveEvaluator", "The operator that evaluates whether a move is tabu."));
110      Parameters.Add(new ValueLookupParameter<IOperator>("TabuMoveMaker", "The operator that declares a move tabu."));
111
112      Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
113      Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
114      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
115      #endregion
116
117      #region Create operators
118      TabuListCreator tabuListCreator = new TabuListCreator();
119      VariableCreator variableCreator = new VariableCreator();
120      BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
121      BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
122      QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
123      Placeholder visualizer1 = new Placeholder();
124      ResultsCollector resultsCollector = new ResultsCollector();
125      UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
126      Placeholder moveGenerator = new Placeholder();
127      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
128      Placeholder moveEvaluator = new Placeholder();
129      Placeholder tabuMoveEvaluator = new Placeholder();
130      SubScopesSorter moveQualitySorter = new SubScopesSorter();
131      BestAverageWorstQualityCalculator bestAverageWorstMoveQualityCalculator = new BestAverageWorstQualityCalculator();
132      TabuSelector tabuSelector = new TabuSelector();
133      ConditionalBranch emptyNeighborhoodBranch1 = new ConditionalBranch();
134      RightReducer rightReducer = new RightReducer();
135      UniformSubScopesProcessor moveMakingProcessor = new UniformSubScopesProcessor();
136      Placeholder tabuMoveMaker = new Placeholder();
137      Placeholder moveMaker = new Placeholder();
138      DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
139      SubScopesRemover subScopesRemover1 = new SubScopesRemover();
140      ConditionalBranch emptyNeighborhoodBranch2 = new ConditionalBranch();
141      UniformSubScopesProcessor removeMoves = new UniformSubScopesProcessor();
142      SubScopesRemover subScopesRemover2 = new SubScopesRemover();
143      IntCounter iterationsCounter = new IntCounter();
144      Comparator iterationsComparator = new Comparator();
145      BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
146      BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
147      QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
148      Placeholder visualizer2 = new Placeholder();
149      ConditionalBranch iterationsTermination = new ConditionalBranch();
150
151      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
152      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Best Move Quality", new DoubleValue(0)));
153      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Average Move Quality", new DoubleValue(0)));
154      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Worst Move Quality", new DoubleValue(0)));
155      variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("MoveQualities", new DataTable("MoveQualities")));
156      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("EmptyNeighborhood", new BoolValue(false)));
157
158      bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
159      bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
160      bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
161
162      bestQualityMemorizer2.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
163      bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
164      bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
165
166      qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
167      qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
168      qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
169      qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
170
171      visualizer1.Name = "Visualizer (placeholder)";
172      visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
173
174      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
175      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
176      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Move Quality"));
177      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Average Move Quality"));
178      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Worst Move Quality"));
179      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("MoveQualities"));
180      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
181      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
182      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
183      resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
184      resultsCollector.ResultsParameter.ActualName = "Results";
185
186      mainProcessor.Name = "Solution processor (UniformSubScopesProcessor)";
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      tabuMoveEvaluator.Name = "TabuMoveEvaluator (placeholder)";
195      tabuMoveEvaluator.OperatorParameter.ActualName = TabuMoveEvaluatorParameter.Name;
196
197      moveQualitySorter.DescendingParameter.ActualName = MaximizationParameter.Name;
198      moveQualitySorter.ValueParameter.ActualName = MoveQualityParameter.Name;
199
200      bestAverageWorstMoveQualityCalculator.AverageQualityParameter.ActualName = "Average Move Quality";
201      bestAverageWorstMoveQualityCalculator.BestQualityParameter.ActualName = "Best Move Quality";
202      bestAverageWorstMoveQualityCalculator.MaximizationParameter.ActualName = "Maximization";
203      bestAverageWorstMoveQualityCalculator.QualityParameter.ActualName = MoveQualityParameter.Name;
204      bestAverageWorstMoveQualityCalculator.WorstQualityParameter.ActualName = "Worst Move Quality";
205
206      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Move Quality"));
207      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Average Move Quality"));
208      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Worst Move Quality"));
209      valuesCollector.DataTableParameter.ActualName = "MoveQualities";
210
211      moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
212
213      emptyNeighborhoodBranch1.Name = "Neighborhood empty?";
214      emptyNeighborhoodBranch1.ConditionParameter.ActualName = "EmptyNeighborhood";
215
216      tabuMoveMaker.Name = "TabuMoveMaker (placeholder)";
217      tabuMoveMaker.OperatorParameter.ActualName = TabuMoveMakerParameter.Name;
218
219      moveMaker.Name = "MoveMaker (placeholder)";
220      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
221
222      subScopesRemover1.RemoveAllSubScopes = true;
223
224      emptyNeighborhoodBranch2.Name = "Neighborhood empty?";
225      emptyNeighborhoodBranch2.ConditionParameter.ActualName = "EmptyNeighborhood";
226
227      subScopesRemover2.RemoveAllSubScopes = true;
228
229      iterationsCounter.Name = "Iterations Counter";
230      iterationsCounter.Increment = new IntValue(1);
231      iterationsCounter.ValueParameter.ActualName = "Iterations";
232
233      iterationsComparator.Name = "Iterations >= MaximumIterations";
234      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
235      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
236      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
237      iterationsComparator.ResultParameter.ActualName = "Terminate";
238
239      bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
240      bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
241      bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
242
243      bestQualityMemorizer4.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
244      bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
245      bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
246
247      qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
248      qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
249      qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
250      qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
251
252      visualizer2.Name = "Visualizer (placeholder)";
253      visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
254
255      iterationsTermination.Name = "Iterations Termination Condition";
256      iterationsTermination.ConditionParameter.ActualName = "Terminate";
257      #endregion
258
259      #region Create operator graph
260      OperatorGraph.InitialOperator = tabuListCreator;
261      tabuListCreator.Successor = variableCreator;
262      variableCreator.Successor = bestQualityMemorizer1;
263      bestQualityMemorizer1.Successor = bestQualityMemorizer2;
264      bestQualityMemorizer2.Successor = qualityDifferenceCalculator1;
265      qualityDifferenceCalculator1.Successor = visualizer1;
266      visualizer1.Successor = resultsCollector;
267      resultsCollector.Successor = mainProcessor;
268      mainProcessor.Operator = moveGenerator;
269      mainProcessor.Successor = emptyNeighborhoodBranch2;
270      moveGenerator.Successor = moveEvaluationProcessor;
271      moveEvaluationProcessor.Operator = moveEvaluator;
272      moveEvaluationProcessor.Successor = moveQualitySorter;
273      moveEvaluator.Successor = tabuMoveEvaluator;
274      tabuMoveEvaluator.Successor = null;
275      moveQualitySorter.Successor = bestAverageWorstMoveQualityCalculator;
276      bestAverageWorstMoveQualityCalculator.Successor = valuesCollector;
277      valuesCollector.Successor = tabuSelector;
278      tabuSelector.Successor = emptyNeighborhoodBranch1;
279      emptyNeighborhoodBranch1.FalseBranch = rightReducer;
280      emptyNeighborhoodBranch1.TrueBranch = null;
281      emptyNeighborhoodBranch1.Successor = null;
282      rightReducer.Successor = moveMakingProcessor;
283      moveMakingProcessor.Operator = tabuMoveMaker;
284      moveMakingProcessor.Successor = subScopesRemover1;
285      tabuMoveMaker.Successor = moveMaker;
286      moveMaker.Successor = null;
287      subScopesRemover1.Successor = null;
288      emptyNeighborhoodBranch2.FalseBranch = iterationsCounter;
289      emptyNeighborhoodBranch2.TrueBranch = removeMoves;
290      emptyNeighborhoodBranch2.Successor = null;
291      removeMoves.Operator = subScopesRemover2;
292      removeMoves.Successor = null;
293      subScopesRemover2.Successor = null;
294      iterationsCounter.Successor = iterationsComparator;
295      iterationsComparator.Successor = bestQualityMemorizer3;
296      bestQualityMemorizer3.Successor = bestQualityMemorizer4;
297      bestQualityMemorizer4.Successor = qualityDifferenceCalculator2;
298      qualityDifferenceCalculator2.Successor = visualizer2;
299      visualizer2.Successor = iterationsTermination;
300      iterationsTermination.TrueBranch = null;
301      iterationsTermination.FalseBranch = mainProcessor;
302      #endregion
303    }
304  }
305}
Note: See TracBrowser for help on using the repository browser.