Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2695_dataset-ids/HeuristicLab.Algorithms.TabuSearch/3.3/TabuSearchMainLoop.cs @ 17842

Last change on this file since 17842 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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