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

Last change on this file since 3226 was 3226, checked in by swagner, 12 years ago

Implemented first version of algorithm batch processing (#947).

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