Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3715 was 3715, checked in by abeham, 14 years ago

#893

  • prevented exception in the algorithms when an operator parameter from the problem is missing
File size: 15.3 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> MoveAnalyzerParameter {
80      get { return (ValueLookupParameter<IOperator>)Parameters["MoveAnalyzer"]; }
81    }
82    public ValueLookupParameter<IOperator> AnalyzerParameter {
83      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
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>("MoveAnalyzer", "The operator used to analyze the moves."));
115      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution."));
116      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
117      #endregion
118
119      #region Create operators
120      VariableCreator variableCreator = new VariableCreator();
121      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
122      Assigner bestQualityInitializer = new Assigner();
123      Placeholder analyzer1 = new Placeholder();
124      ResultsCollector resultsCollector1 = new ResultsCollector();
125      ResultsCollector resultsCollector2 = new ResultsCollector();
126      SubScopesProcessor solutionProcessor = new SubScopesProcessor();
127      Placeholder moveGenerator = new Placeholder();
128      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
129      Placeholder moveEvaluator = new Placeholder();
130      IntCounter evaluatedMovesCounter = new IntCounter();
131      Placeholder tabuChecker = new Placeholder();
132      Placeholder moveAnalyzer = new Placeholder();
133      SubScopesSorter moveQualitySorter = new SubScopesSorter();
134      TabuSelector tabuSelector = new TabuSelector();
135      ConditionalBranch emptyNeighborhoodBranch1 = new ConditionalBranch();
136      RightReducer rightReducer = new RightReducer();
137      SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
138      Placeholder tabuMaker = new Placeholder();
139      Placeholder moveMaker = new Placeholder();
140      SubScopesRemover subScopesRemover = new SubScopesRemover();
141      ConditionalBranch emptyNeighborhoodBranch2 = new ConditionalBranch();
142      BestQualityMemorizer bestQualityUpdater = new BestQualityMemorizer();
143      IntCounter iterationsCounter = new IntCounter();
144      Comparator iterationsComparator = new Comparator();
145      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
146      Placeholder analyzer2 = new Placeholder();
147      ResultsCollector resultsCollector3 = new ResultsCollector();
148      ConditionalBranch iterationsTermination = new ConditionalBranch();
149
150      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
151      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
152      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("EmptyNeighborhood", new BoolValue(false)));
153      variableCreator.CollectedValues.Add(new ValueParameter<ItemList<IItem>>("TabuList", new ItemList<IItem>()));
154      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
155
156      bestQualityInitializer.Name = "Initialize BestQuality";
157      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
158      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.Name;
159
160      analyzer1.Name = "Analyzer (placeholder)";
161      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
162
163      resultsCollector1.CopyValue = new BoolValue(false);
164      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
165      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
166      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
167
168      resultsCollector2.CopyValue = new BoolValue(true);
169      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
170      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
171
172      moveGenerator.Name = "MoveGenerator (placeholder)";
173      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
174
175      moveEvaluator.Name = "MoveEvaluator (placeholder)";
176      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
177
178      evaluatedMovesCounter.Name = "EvaluatedMoves + 1";
179      evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
180      evaluatedMovesCounter.Increment = new IntValue(1);
181
182      tabuChecker.Name = "TabuChecker (placeholder)";
183      tabuChecker.OperatorParameter.ActualName = TabuCheckerParameter.Name;
184
185      moveAnalyzer.Name = "MoveAnalyzer (placeholder)";
186      moveAnalyzer.OperatorParameter.ActualName = MoveAnalyzerParameter.Name;
187
188      moveQualitySorter.DescendingParameter.ActualName = MaximizationParameter.Name;
189      moveQualitySorter.ValueParameter.ActualName = MoveQualityParameter.Name;
190
191      tabuSelector.AspirationParameter.Value = new BoolValue(true);
192      tabuSelector.BestQualityParameter.ActualName = "BestQuality";
193      tabuSelector.CopySelected = new BoolValue(false);
194      tabuSelector.EmptyNeighborhoodParameter.ActualName = "EmptyNeighborhood";
195      tabuSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
196      tabuSelector.MoveQualityParameter.ActualName = MoveQualityParameter.Name;
197      tabuSelector.MoveTabuParameter.ActualName = MoveTabuParameter.Name;
198     
199      moveMakingProcessor.Name = "MoveMaking processor (UniformSubScopesProcessor)";
200
201      emptyNeighborhoodBranch1.Name = "Neighborhood empty?";
202      emptyNeighborhoodBranch1.ConditionParameter.ActualName = "EmptyNeighborhood";
203
204      tabuMaker.Name = "TabuMaker (placeholder)";
205      tabuMaker.OperatorParameter.ActualName = TabuMakerParameter.Name;
206
207      moveMaker.Name = "MoveMaker (placeholder)";
208      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
209
210      subScopesRemover.RemoveAllSubScopes = true;
211
212      bestQualityUpdater.Name = "Update BestQuality";
213      bestQualityUpdater.MaximizationParameter.ActualName = MaximizationParameter.Name;
214      bestQualityUpdater.QualityParameter.ActualName = QualityParameter.Name;
215      bestQualityUpdater.BestQualityParameter.ActualName = "BestQuality";
216
217      iterationsCounter.Name = "Iterations Counter";
218      iterationsCounter.Increment = new IntValue(1);
219      iterationsCounter.ValueParameter.ActualName = "Iterations";
220
221      iterationsComparator.Name = "Iterations >= MaximumIterations";
222      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
223      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
224      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
225      iterationsComparator.ResultParameter.ActualName = "Terminate";
226
227      analyzer2.Name = "Analyzer (placeholder)";
228      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
229
230      resultsCollector3.CopyValue = new BoolValue(true);
231      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
232      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
233
234      emptyNeighborhoodBranch2.Name = "Neighborhood empty?";
235      emptyNeighborhoodBranch2.ConditionParameter.ActualName = "EmptyNeighborhood";
236
237      iterationsTermination.Name = "Iterations Termination Condition";
238      iterationsTermination.ConditionParameter.ActualName = "Terminate";
239      #endregion
240
241      #region Create operator graph
242      OperatorGraph.InitialOperator = variableCreator;
243      variableCreator.Successor = subScopesProcessor0;
244      subScopesProcessor0.Operators.Add(bestQualityInitializer);
245      subScopesProcessor0.Successor = resultsCollector1;
246      resultsCollector1.Successor = resultsCollector2;
247      resultsCollector2.Successor = solutionProcessor;
248      solutionProcessor.Operators.Add(moveGenerator);
249      solutionProcessor.Successor = iterationsCounter;
250      moveGenerator.Successor = moveEvaluationProcessor;
251      moveEvaluationProcessor.Operator = moveEvaluator;
252      moveEvaluationProcessor.Successor = moveAnalyzer;
253      moveEvaluator.Successor = evaluatedMovesCounter;
254      evaluatedMovesCounter.Successor = tabuChecker;
255      tabuChecker.Successor = null;
256      moveAnalyzer.Successor = moveQualitySorter;
257      moveQualitySorter.Successor = tabuSelector;
258      tabuSelector.Successor = emptyNeighborhoodBranch1;
259      emptyNeighborhoodBranch1.FalseBranch = rightReducer;
260      emptyNeighborhoodBranch1.TrueBranch = null;
261      emptyNeighborhoodBranch1.Successor = subScopesRemover;
262      rightReducer.Successor = moveMakingProcessor;
263      moveMakingProcessor.Operators.Add(tabuMaker);
264      moveMakingProcessor.Successor = null;
265      tabuMaker.Successor = moveMaker;
266      moveMaker.Successor = null;
267      subScopesRemover.Successor = null;
268      iterationsCounter.Successor = iterationsComparator;
269      iterationsComparator.Successor = subScopesProcessor1;
270      subScopesProcessor1.Operators.Add(analyzer2);
271      subScopesProcessor1.Successor = resultsCollector3;
272      resultsCollector3.Successor = emptyNeighborhoodBranch2;
273      emptyNeighborhoodBranch2.TrueBranch = null;
274      emptyNeighborhoodBranch2.FalseBranch = iterationsTermination;
275      emptyNeighborhoodBranch2.Successor = null;
276      iterationsTermination.TrueBranch = null;
277      iterationsTermination.FalseBranch = solutionProcessor;
278      #endregion
279    }
280
281    public override IOperation Apply() {
282      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null
283        || TabuCheckerParameter.ActualValue == null || TabuMakerParameter.ActualValue == null)
284        return null;
285      return base.Apply();
286    }
287  }
288}
Note: See TracBrowser for help on using the repository browser.