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

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

Unified *SequentialSubScopesProcessor and *ParallelSubScopesProcessor (#943).

File size: 16.6 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      RightReducer rightReducer = new RightReducer();
134      UniformSubScopesProcessor moveMakingProcessor = new UniformSubScopesProcessor();
135      Placeholder tabuMoveMaker = new Placeholder();
136      Placeholder moveMaker = new Placeholder();
137      DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
138      SubScopesRemover subScopesRemover = new SubScopesRemover();
139      IntCounter iterationsCounter = new IntCounter();
140      Comparator iterationsComparator = new Comparator();
141      BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
142      BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
143      QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
144      Placeholder visualizer2 = new Placeholder();
145      ConditionalBranch iterationsTermination = new ConditionalBranch();
146
147      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
148      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Best Move Quality", new DoubleValue(0)));
149      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Average Move Quality", new DoubleValue(0)));
150      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Worst Move Quality", new DoubleValue(0)));
151      variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("MoveQualities", new DataTable("MoveQualities")));
152
153      bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
154      bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
155      bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
156
157      bestQualityMemorizer2.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
158      bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
159      bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
160
161      qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
162      qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
163      qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
164      qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
165
166      visualizer1.Name = "Visualizer (placeholder)";
167      visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
168
169      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
170      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
171      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Move Quality"));
172      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Average Move Quality"));
173      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Worst Move Quality"));
174      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("MoveQualities"));
175      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
176      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
177      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
178      resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
179      resultsCollector.ResultsParameter.ActualName = "Results";
180
181      mainProcessor.Name = "Solution processor (UniformSubScopesProcessor)";
182
183      moveGenerator.Name = "MoveGenerator (placeholder)";
184      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
185
186      moveEvaluator.Name = "MoveEvaluator (placeholder)";
187      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
188
189      tabuMoveEvaluator.Name = "TabuMoveEvaluator (placeholder)";
190      tabuMoveEvaluator.OperatorParameter.ActualName = TabuMoveEvaluatorParameter.Name;
191
192      moveQualitySorter.DescendingParameter.ActualName = MaximizationParameter.Name;
193      moveQualitySorter.ValueParameter.ActualName = MoveQualityParameter.Name;
194
195      bestAverageWorstMoveQualityCalculator.AverageQualityParameter.ActualName = "Average Move Quality";
196      bestAverageWorstMoveQualityCalculator.BestQualityParameter.ActualName = "Best Move Quality";
197      bestAverageWorstMoveQualityCalculator.MaximizationParameter.ActualName = "Maximization";
198      bestAverageWorstMoveQualityCalculator.QualityParameter.ActualName = MoveQualityParameter.Name;
199      bestAverageWorstMoveQualityCalculator.WorstQualityParameter.ActualName = "Worst Move Quality";
200
201      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Move Quality"));
202      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Average Move Quality"));
203      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Worst Move Quality"));
204      valuesCollector.DataTableParameter.ActualName = "MoveQualities";
205
206      moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
207
208      tabuMoveMaker.Name = "TabuMoveMaker (placeholder)";
209      tabuMoveMaker.OperatorParameter.ActualName = TabuMoveMakerParameter.Name;
210
211      moveMaker.Name = "MoveMaker (placeholder)";
212      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
213
214      subScopesRemover.RemoveAllSubScopes = true;
215
216      iterationsCounter.Name = "Iterations Counter";
217      iterationsCounter.Increment = new IntValue(1);
218      iterationsCounter.ValueParameter.ActualName = "Iterations";
219
220      iterationsComparator.Name = "Iterations >= MaximumIterations";
221      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
222      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
223      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
224      iterationsComparator.ResultParameter.ActualName = "Terminate";
225
226      bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
227      bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
228      bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
229
230      bestQualityMemorizer4.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
231      bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
232      bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
233
234      qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
235      qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
236      qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
237      qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
238
239      visualizer2.Name = "Visualizer (placeholder)";
240      visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
241
242      iterationsTermination.Name = "Iterations Termination Condition";
243      iterationsTermination.ConditionParameter.ActualName = "Terminate";
244      #endregion
245
246      #region Create operator graph
247      OperatorGraph.InitialOperator = tabuListCreator;
248      tabuListCreator.Successor = variableCreator;
249      variableCreator.Successor = bestQualityMemorizer1;
250      bestQualityMemorizer1.Successor = bestQualityMemorizer2;
251      bestQualityMemorizer2.Successor = qualityDifferenceCalculator1;
252      qualityDifferenceCalculator1.Successor = visualizer1;
253      visualizer1.Successor = resultsCollector;
254      resultsCollector.Successor = mainProcessor;
255      mainProcessor.Operator = moveGenerator;
256      mainProcessor.Successor = iterationsCounter;
257      moveGenerator.Successor = moveEvaluationProcessor;
258      moveEvaluationProcessor.Operator = moveEvaluator;
259      moveEvaluationProcessor.Successor = moveQualitySorter;
260      moveEvaluator.Successor = tabuMoveEvaluator;
261      tabuMoveEvaluator.Successor = null;
262      moveQualitySorter.Successor = bestAverageWorstMoveQualityCalculator;
263      bestAverageWorstMoveQualityCalculator.Successor = valuesCollector;
264      valuesCollector.Successor = tabuSelector;
265      tabuSelector.Successor = rightReducer;
266      rightReducer.Successor = moveMakingProcessor;
267      moveMakingProcessor.Operator = tabuMoveMaker;
268      moveMakingProcessor.Successor = subScopesRemover;
269      tabuMoveMaker.Successor = moveMaker;
270      moveMaker.Successor = null;
271      subScopesRemover.Successor = null;
272      iterationsCounter.Successor = iterationsComparator;
273      iterationsComparator.Successor = bestQualityMemorizer3;
274      bestQualityMemorizer3.Successor = bestQualityMemorizer4;
275      bestQualityMemorizer4.Successor = qualityDifferenceCalculator2;
276      qualityDifferenceCalculator2.Successor = visualizer2;
277      visualizer2.Successor = iterationsTermination;
278      iterationsTermination.TrueBranch = null;
279      iterationsTermination.FalseBranch = mainProcessor;
280      #endregion
281    }
282  }
283}
Note: See TracBrowser for help on using the repository browser.