Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EnhancedProgress/HeuristicLab.Problems.QuadraticAssignment.Algorithms/3.3/RobustTabooSeachOperator.cs @ 15456

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

#2526: Updated year of copyrights in license headers

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