Free cookie consent management tool by TermsFeed Policy Generator

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, 9 years ago

#2444: Added evaluation output to RTS

File size: 12.8 KB
RevLine 
[6586]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6586]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.")]
[7040]33  [StorableClass]
[11970]34  public sealed class RobustTabooSeachOperator : SingleSuccessorOperator, IIterationBasedOperator, IStochasticOperator, ISingleObjectiveOperator {
[6586]35
36    #region Parameter Properties
37    public ILookupParameter<IntValue> IterationsParameter {
38      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
39    }
[6593]40    public ILookupParameter<IRandom> RandomParameter {
41      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
42    }
[6586]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    }
[6593]67    public ILookupParameter<BoolValue> UseNewTabuTenureAdaptionSchemeParameter {
68      get { return (ILookupParameter<BoolValue>)Parameters["UseNewTabuTenureAdaptionScheme"]; }
69    }
[6586]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    }
[7040]89
90    private ILookupParameter<BoolValue> AllMovesTabuParameter {
91      get { return (ILookupParameter<BoolValue>)Parameters["AllMovesTabu"]; }
92    }
[12810]93
94    public ILookupParameter<IntValue> EvaluatedMovesParameter {
95      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
96    }
[6586]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."));
[6593]115      Parameters.Add(new LookupParameter<BoolValue>("UseNewTabuTenureAdaptionScheme", "True if the new tabu tenure adaption should be used or false otherwise."));
[6586]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."));
[7040]122      Parameters.Add(new LookupParameter<BoolValue>("AllMovesTabu", "Indicates that all moves are tabu."));
[12810]123      Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of move evaluations made."));
[6586]124    }
125
126    public override IDeepCloneable Clone(Cloner cloner) {
127      return new RobustTabooSeachOperator(this, cloner);
128    }
129
[7040]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      }
[12810]137      if (!Parameters.ContainsKey("EvaluatedMoves")) {
138        Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of move evaluations made."));
139      }
[7040]140      #endregion
141    }
142
[6586]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      }
[7040]157      bool allMovesTabu = false;
158      if (AllMovesTabuParameter.ActualValue == null)
159        AllMovesTabuParameter.ActualValue = new BoolValue(false);
160      else allMovesTabu = AllMovesTabuParameter.ActualValue.Value;
[6586]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
[12810]173      var evaluatedMoves = 0;
[6586]174      foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(solution)) {
175        double moveQuality;
[12810]176        if (lastMove == null) {
[6586]177          moveQuality = QAPSwap2MoveEvaluator.Apply(solution, move, weights, distances);
[12810]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        }
[6586]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
[6593]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;
[6586]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);
[6593]209          results.Add(new Result("AspiredMoves", "Counts the number of moves that were selected because of the aspiration criteria.", aspiredMoves));
[6586]210        } else if (already_aspired) {
211          aspiredMoves = (IntValue)results["AspiredMoves"].Value;
212          aspiredMoves.Value++;
213        }
214      }
215
[12810]216      EvaluatedMovesParameter.ActualValue.Value += evaluatedMoves;
217
[7040]218      allMovesTabu = bestMove == null;
219      if (!allMovesTabu)
220        LastMoveParameter.ActualValue = bestMove;
221      AllMovesTabuParameter.ActualValue.Value = allMovesTabu;
[6586]222
[7040]223      if (allMovesTabu) return base.Apply();
[6586]224
[6593]225      bool useNewAdaptionScheme = UseNewTabuTenureAdaptionSchemeParameter.ActualValue.Value;
226      if (useNewAdaptionScheme) {
227        double r = random.NextDouble();
[6594]228        if (r == 0) r = 1; // transform to (0;1]
[6593]229        shortTermMemory[bestMove.Index1, solution[bestMove.Index1]] = (int)(iteration + r * r * r * maxTenure);
230        r = random.NextDouble();
[6594]231        if (r == 0) r = 1; // transform to (0;1]
[6593]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      }
[6586]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.