source: trunk/sources/HeuristicLab.Problems.QuadraticAssignment.Algorithms/3.3/RobustTabooSeachOperator.cs @ 12810

Last change on this file since 12810 was 12810, checked in by abeham, 7 years ago

#2444: Added evaluation output to RTS

File size: 12.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.QuadraticAssignment.Algorithms {
32  [Item("RobustTabooSearchOperator", "Performs an iteration of the robust taboo search algorithm as descrbied in Taillard 1991.")]
33  [StorableClass]
34  public sealed class RobustTabooSeachOperator : SingleSuccessorOperator, IIterationBasedOperator, IStochasticOperator, ISingleObjectiveOperator {
35
36    #region Parameter Properties
37    public ILookupParameter<IntValue> IterationsParameter {
38      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
39    }
40    public ILookupParameter<IRandom> RandomParameter {
41      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
42    }
43    public ILookupParameter<Permutation> PermutationParameter {
44      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
45    }
46    public ILookupParameter<DoubleMatrix> WeightsParameter {
47      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
48    }
49    public ILookupParameter<DoubleMatrix> DistancesParameter {
50      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
51    }
52    public ILookupParameter<IntMatrix> ShortTermMemoryParameter {
53      get { return (ILookupParameter<IntMatrix>)Parameters["ShortTermMemory"]; }
54    }
55    public ILookupParameter<DoubleMatrix> MoveQualityMatrixParameter {
56      get { return (ILookupParameter<DoubleMatrix>)Parameters["MoveQualityMatrix"]; }
57    }
58    public ILookupParameter<DoubleValue> QualityParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
60    }
61    public ILookupParameter<DoubleValue> BestQualityParameter {
62      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
63    }
64    public ILookupParameter<Swap2Move> LastMoveParameter {
65      get { return (ILookupParameter<Swap2Move>)Parameters["LastMove"]; }
66    }
67    public ILookupParameter<BoolValue> UseNewTabuTenureAdaptionSchemeParameter {
68      get { return (ILookupParameter<BoolValue>)Parameters["UseNewTabuTenureAdaptionScheme"]; }
69    }
70    public ILookupParameter<ResultCollection> ResultsParameter {
71      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
72    }
73
74    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
75      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
76    }
77    public IValueLookupParameter<IntValue> MinimumTabuTenureParameter {
78      get { return (IValueLookupParameter<IntValue>)Parameters["MinimumTabuTenure"]; }
79    }
80    public IValueLookupParameter<IntValue> MaximumTabuTenureParameter {
81      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTabuTenure"]; }
82    }
83    public IValueLookupParameter<BoolValue> UseAlternativeAspirationParameter {
84      get { return (IValueLookupParameter<BoolValue>)Parameters["UseAlternativeAspiration"]; }
85    }
86    public IValueLookupParameter<IntValue> AlternativeAspirationTenureParameter {
87      get { return (IValueLookupParameter<IntValue>)Parameters["AlternativeAspirationTenure"]; }
88    }
89
90    private ILookupParameter<BoolValue> AllMovesTabuParameter {
91      get { return (ILookupParameter<BoolValue>)Parameters["AllMovesTabu"]; }
92    }
93
94    public ILookupParameter<IntValue> EvaluatedMovesParameter {
95      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
96    }
97    #endregion
98
99    [StorableConstructor]
100    private RobustTabooSeachOperator(bool deserializing) : base(deserializing) { }
101    private RobustTabooSeachOperator(RobustTabooSeachOperator original, Cloner cloner)
102      : base(original, cloner) {
103    }
104    public RobustTabooSeachOperator() {
105      Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration."));
106      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
107      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation solution."));
108      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
109      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
110      Parameters.Add(new LookupParameter<IntMatrix>("ShortTermMemory", "The table that stores the iteration at which a certain facility has been assigned to a certain location."));
111      Parameters.Add(new LookupParameter<DoubleMatrix>("MoveQualityMatrix", "The quality of all swap moves as evaluated on the current permutation."));
112      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value."));
113      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The best quality value."));
114      Parameters.Add(new LookupParameter<Swap2Move>("LastMove", "The last move that was applied."));
115      Parameters.Add(new LookupParameter<BoolValue>("UseNewTabuTenureAdaptionScheme", "True if the new tabu tenure adaption should be used or false otherwise."));
116      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Collection that houses the results of the algorithm."));
117      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run."));
118      Parameters.Add(new ValueLookupParameter<IntValue>("MinimumTabuTenure", "The minimum tabu tenure."));
119      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTabuTenure", "The maximum tabu tenure."));
120      Parameters.Add(new ValueLookupParameter<BoolValue>("UseAlternativeAspiration", "True if the alternative aspiration condition should be used that takes moves that have not been made for some time above others."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("AlternativeAspirationTenure", "The time t that a move will be remembered for the alternative aspiration condition."));
122      Parameters.Add(new LookupParameter<BoolValue>("AllMovesTabu", "Indicates that all moves are tabu."));
123      Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of move evaluations made."));
124    }
125
126    public override IDeepCloneable Clone(Cloner cloner) {
127      return new RobustTabooSeachOperator(this, cloner);
128    }
129
130    [StorableHook(HookType.AfterDeserialization)]
131    private void AfterDeserialization() {
132      // BackwardsCompatibility3.3
133      #region Backwards compatible code, remove with 3.4
134      if (!Parameters.ContainsKey("AllMovesTabu")) {
135        Parameters.Add(new LookupParameter<BoolValue>("AllMovesTabu", "Indicates that all moves are tabu."));
136      }
137      if (!Parameters.ContainsKey("EvaluatedMoves")) {
138        Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of move evaluations made."));
139      }
140      #endregion
141    }
142
143    public override IOperation Apply() {
144      IRandom random = RandomParameter.ActualValue;
145      int iteration = IterationsParameter.ActualValue.Value;
146      IntMatrix shortTermMemory = ShortTermMemoryParameter.ActualValue;
147      DoubleMatrix weights = WeightsParameter.ActualValue;
148      DoubleMatrix distances = DistancesParameter.ActualValue;
149      DoubleMatrix moveQualityMatrix = MoveQualityMatrixParameter.ActualValue;
150
151      DoubleValue quality = QualityParameter.ActualValue;
152      DoubleValue bestQuality = BestQualityParameter.ActualValue;
153      if (bestQuality == null) {
154        BestQualityParameter.ActualValue = (DoubleValue)quality.Clone();
155        bestQuality = BestQualityParameter.ActualValue;
156      }
157      bool allMovesTabu = false;
158      if (AllMovesTabuParameter.ActualValue == null)
159        AllMovesTabuParameter.ActualValue = new BoolValue(false);
160      else allMovesTabu = AllMovesTabuParameter.ActualValue.Value;
161
162      int minTenure = MinimumTabuTenureParameter.ActualValue.Value;
163      int maxTenure = MaximumTabuTenureParameter.ActualValue.Value;
164      int alternativeAspirationTenure = AlternativeAspirationTenureParameter.ActualValue.Value;
165      bool useAlternativeAspiration = UseAlternativeAspirationParameter.ActualValue.Value;
166      Permutation solution = PermutationParameter.ActualValue;
167      Swap2Move lastMove = LastMoveParameter.ActualValue;
168
169      double bestMoveQuality = double.MaxValue;
170      Swap2Move bestMove = null;
171      bool already_aspired = false;
172
173      var evaluatedMoves = 0;
174      foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(solution)) {
175        double moveQuality;
176        if (lastMove == null) {
177          moveQuality = QAPSwap2MoveEvaluator.Apply(solution, move, weights, distances);
178          evaluatedMoves++;
179        } else if (allMovesTabu) moveQuality = moveQualityMatrix[move.Index1, move.Index2];
180        else {
181          moveQuality = QAPSwap2MoveEvaluator.Apply(solution, move, moveQualityMatrix[move.Index1, move.Index2], weights, distances, lastMove);
182          evaluatedMoves++;
183        }
184
185        moveQualityMatrix[move.Index1, move.Index2] = moveQuality;
186        moveQualityMatrix[move.Index2, move.Index1] = moveQuality;
187
188        bool autorized = shortTermMemory[move.Index1, solution[move.Index2]] < iteration
189                      || shortTermMemory[move.Index2, solution[move.Index1]] < iteration;
190
191        bool aspired = (shortTermMemory[move.Index1, solution[move.Index2]] < iteration - alternativeAspirationTenure
192                     && shortTermMemory[move.Index2, solution[move.Index1]] < iteration - alternativeAspirationTenure)
193                  || quality.Value + moveQuality < bestQuality.Value;
194
195        if ((aspired && !already_aspired) // the first alternative move is aspired
196          || (aspired && already_aspired && moveQuality < bestMoveQuality) // an alternative move was already aspired, but this is better
197          || (autorized && !aspired && !already_aspired && moveQuality < bestMoveQuality)) { // a regular better move is found
198          bestMove = move;
199          bestMoveQuality = moveQuality;
200          if (aspired) already_aspired = true;
201        }
202      }
203
204      ResultCollection results = ResultsParameter.ActualValue;
205      if (results != null) {
206        IntValue aspiredMoves = null;
207        if (!results.ContainsKey("AspiredMoves")) {
208          aspiredMoves = new IntValue(already_aspired ? 1 : 0);
209          results.Add(new Result("AspiredMoves", "Counts the number of moves that were selected because of the aspiration criteria.", aspiredMoves));
210        } else if (already_aspired) {
211          aspiredMoves = (IntValue)results["AspiredMoves"].Value;
212          aspiredMoves.Value++;
213        }
214      }
215
216      EvaluatedMovesParameter.ActualValue.Value += evaluatedMoves;
217
218      allMovesTabu = bestMove == null;
219      if (!allMovesTabu)
220        LastMoveParameter.ActualValue = bestMove;
221      AllMovesTabuParameter.ActualValue.Value = allMovesTabu;
222
223      if (allMovesTabu) return base.Apply();
224
225      bool useNewAdaptionScheme = UseNewTabuTenureAdaptionSchemeParameter.ActualValue.Value;
226      if (useNewAdaptionScheme) {
227        double r = random.NextDouble();
228        if (r == 0) r = 1; // transform to (0;1]
229        shortTermMemory[bestMove.Index1, solution[bestMove.Index1]] = (int)(iteration + r * r * r * maxTenure);
230        r = random.NextDouble();
231        if (r == 0) r = 1; // transform to (0;1]
232        shortTermMemory[bestMove.Index2, solution[bestMove.Index2]] = (int)(iteration + r * r * r * maxTenure);
233      } else {
234        shortTermMemory[bestMove.Index1, solution[bestMove.Index1]] = iteration + random.Next(minTenure, maxTenure);
235        shortTermMemory[bestMove.Index2, solution[bestMove.Index2]] = iteration + random.Next(minTenure, maxTenure);
236      }
237      Swap2Manipulator.Apply(solution, bestMove.Index1, bestMove.Index2);
238      quality.Value += bestMoveQuality;
239
240      if (quality.Value < bestQuality.Value) bestQuality.Value = quality.Value;
241
242      return base.Apply();
243    }
244  }
245}
Note: See TracBrowser for help on using the repository browser.