Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4614 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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