Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5160 was 5160, checked in by svonolfe, 14 years ago

Merged changes from branch into trunk (#1343)

File size: 15.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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
32namespace 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      #region Backwards compatible code
103      VariableCreator variableCreator = OperatorGraph.InitialOperator as VariableCreator;
104
105      if (variableCreator != null) {
106        if (!variableCreator.CollectedValues.ContainsKey("Memories")) {
107          variableCreator.CollectedValues.Add(new ValueParameter<VariableCollection>("Memories", new VariableCollection()));
108        }
109      }
110      #endregion
111    }
112
113    private void Initialize() {
114      #region Create parameters
115      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
116      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
117      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
118      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
119      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
120      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The value that indicates if a move is tabu or not."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
122      Parameters.Add(new ValueLookupParameter<IntValue>("TabuTenure", "The length of the tabu list, and also means the number of iterations a move is kept tabu"));
123
124      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
125      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
126      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
127      Parameters.Add(new ValueLookupParameter<IOperator>("TabuChecker", "The operator that checks whether a move is tabu."));
128      Parameters.Add(new ValueLookupParameter<IOperator>("TabuMaker", "The operator that declares a move tabu."));
129
130      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
131      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
132      #endregion
133
134      #region Create operators
135      VariableCreator variableCreator = new VariableCreator();
136      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
137      Assigner bestQualityInitializer = new Assigner();
138      Placeholder analyzer1 = new Placeholder();
139      ResultsCollector resultsCollector1 = new ResultsCollector();
140      ResultsCollector resultsCollector2 = new ResultsCollector();
141      SubScopesProcessor solutionProcessor = new SubScopesProcessor();
142      Placeholder moveGenerator = new Placeholder();
143      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
144      Placeholder moveEvaluator = new Placeholder();
145      IntCounter evaluatedMovesCounter = new IntCounter();
146      Placeholder tabuChecker = new Placeholder();
147      SubScopesSorter moveQualitySorter = new SubScopesSorter();
148      TabuSelector tabuSelector = new TabuSelector();
149      ConditionalBranch emptyNeighborhoodBranch1 = new ConditionalBranch();
150      SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
151      UniformSubScopesProcessor selectedMoveMakingProcesor = new UniformSubScopesProcessor();
152      Placeholder tabuMaker = new Placeholder();
153      Placeholder moveMaker = new Placeholder();
154      MergingReducer mergingReducer = new MergingReducer();
155      Placeholder analyzer2 = new Placeholder();
156      SubScopesRemover subScopesRemover = new SubScopesRemover();
157      ConditionalBranch emptyNeighborhoodBranch2 = new ConditionalBranch();
158      BestQualityMemorizer bestQualityUpdater = new BestQualityMemorizer();
159      IntCounter iterationsCounter = new IntCounter();
160      Comparator iterationsComparator = new Comparator();
161      ResultsCollector resultsCollector3 = new ResultsCollector();
162      ConditionalBranch iterationsTermination = new ConditionalBranch();
163
164      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class TabuSearch expects this to be called Iterations
165      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
166      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("EmptyNeighborhood", new BoolValue(false)));
167      variableCreator.CollectedValues.Add(new ValueParameter<ItemList<IItem>>("TabuList", new ItemList<IItem>()));
168      variableCreator.CollectedValues.Add(new ValueParameter<VariableCollection>("Memories", new VariableCollection()));
169      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
170
171      bestQualityInitializer.Name = "Initialize BestQuality";
172      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
173      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
174
175      analyzer1.Name = "Analyzer (placeholder)";
176      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
177
178      resultsCollector1.CopyValue = new BoolValue(false);
179      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
180      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
181      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
182
183      resultsCollector2.CopyValue = new BoolValue(true);
184      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
185      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
186
187      moveGenerator.Name = "MoveGenerator (placeholder)";
188      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
189
190      moveEvaluator.Name = "MoveEvaluator (placeholder)";
191      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
192
193      evaluatedMovesCounter.Name = "EvaluatedMoves + 1";
194      evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
195      evaluatedMovesCounter.Increment = new IntValue(1);
196
197      tabuChecker.Name = "TabuChecker (placeholder)";
198      tabuChecker.OperatorParameter.ActualName = TabuCheckerParameter.Name;
199
200      moveQualitySorter.DescendingParameter.ActualName = MaximizationParameter.Name;
201      moveQualitySorter.ValueParameter.ActualName = MoveQualityParameter.Name;
202
203      tabuSelector.AspirationParameter.Value = new BoolValue(true);
204      tabuSelector.BestQualityParameter.ActualName = "BestQuality";
205      tabuSelector.CopySelected = new BoolValue(false);
206      tabuSelector.EmptyNeighborhoodParameter.ActualName = "EmptyNeighborhood";
207      tabuSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
208      tabuSelector.MoveQualityParameter.ActualName = MoveQualityParameter.Name;
209      tabuSelector.MoveTabuParameter.ActualName = MoveTabuParameter.Name;
210
211      moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
212
213      emptyNeighborhoodBranch1.Name = "Neighborhood empty?";
214      emptyNeighborhoodBranch1.ConditionParameter.ActualName = "EmptyNeighborhood";
215
216      tabuMaker.Name = "TabuMaker (placeholder)";
217      tabuMaker.OperatorParameter.ActualName = TabuMakerParameter.Name;
218
219      moveMaker.Name = "MoveMaker (placeholder)";
220      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
221
222      analyzer2.Name = "Analyzer (placeholder)";
223      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
224
225      subScopesRemover.RemoveAllSubScopes = true;
226
227      bestQualityUpdater.Name = "Update BestQuality";
228      bestQualityUpdater.MaximizationParameter.ActualName = MaximizationParameter.Name;
229      bestQualityUpdater.QualityParameter.ActualName = QualityParameter.Name;
230      bestQualityUpdater.BestQualityParameter.ActualName = "BestQuality";
231
232      iterationsCounter.Name = "Iterations Counter";
233      iterationsCounter.Increment = new IntValue(1);
234      iterationsCounter.ValueParameter.ActualName = "Iterations";
235
236      iterationsComparator.Name = "Iterations >= MaximumIterations";
237      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
238      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
239      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
240      iterationsComparator.ResultParameter.ActualName = "Terminate";
241
242      resultsCollector3.CopyValue = new BoolValue(true);
243      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
244      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
245
246      emptyNeighborhoodBranch2.Name = "Neighborhood empty?";
247      emptyNeighborhoodBranch2.ConditionParameter.ActualName = "EmptyNeighborhood";
248
249      iterationsTermination.Name = "Iterations Termination Condition";
250      iterationsTermination.ConditionParameter.ActualName = "Terminate";
251      #endregion
252
253      #region Create operator graph
254      OperatorGraph.InitialOperator = variableCreator;
255      variableCreator.Successor = subScopesProcessor0;
256      subScopesProcessor0.Operators.Add(bestQualityInitializer);
257      subScopesProcessor0.Successor = resultsCollector1;
258      bestQualityInitializer.Successor = analyzer1;
259      analyzer1.Successor = null;
260      resultsCollector1.Successor = resultsCollector2;
261      resultsCollector2.Successor = solutionProcessor;
262      solutionProcessor.Operators.Add(moveGenerator);
263      solutionProcessor.Successor = iterationsCounter;
264      moveGenerator.Successor = moveEvaluationProcessor;
265      moveEvaluationProcessor.Operator = moveEvaluator;
266      moveEvaluationProcessor.Successor = moveQualitySorter;
267      moveEvaluator.Successor = evaluatedMovesCounter;
268      evaluatedMovesCounter.Successor = tabuChecker;
269      tabuChecker.Successor = null;
270      moveQualitySorter.Successor = tabuSelector;
271      tabuSelector.Successor = emptyNeighborhoodBranch1;
272      emptyNeighborhoodBranch1.FalseBranch = moveMakingProcessor;
273      emptyNeighborhoodBranch1.TrueBranch = null;
274      emptyNeighborhoodBranch1.Successor = subScopesRemover;
275      moveMakingProcessor.Operators.Add(new EmptyOperator());
276      moveMakingProcessor.Operators.Add(selectedMoveMakingProcesor);
277      moveMakingProcessor.Successor = mergingReducer;
278      selectedMoveMakingProcesor.Operator = tabuMaker;
279      selectedMoveMakingProcesor.Successor = null;
280      tabuMaker.Successor = moveMaker;
281      moveMaker.Successor = null;
282      mergingReducer.Successor = analyzer2;
283      analyzer2.Successor = null;
284      subScopesRemover.Successor = null;
285      iterationsCounter.Successor = iterationsComparator;
286      iterationsComparator.Successor = resultsCollector3;
287      resultsCollector3.Successor = emptyNeighborhoodBranch2;
288      emptyNeighborhoodBranch2.TrueBranch = null;
289      emptyNeighborhoodBranch2.FalseBranch = iterationsTermination;
290      emptyNeighborhoodBranch2.Successor = null;
291      iterationsTermination.TrueBranch = null;
292      iterationsTermination.FalseBranch = solutionProcessor;
293      #endregion
294    }
295
296    public override IOperation Apply() {
297      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null
298        || TabuCheckerParameter.ActualValue == null || TabuMakerParameter.ActualValue == null)
299        return null;
300      return base.Apply();
301    }
302  }
303}
Note: See TracBrowser for help on using the repository browser.