Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment.Algorithms/3.3/RobustTabooSeachOperator.cs @ 6703

Last change on this file since 6703 was 6647, checked in by mkommend, 13 years ago

#1479: Updated grammar editor branch.

File size: 11.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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  public sealed class RobustTabooSeachOperator : SingleSuccessorOperator, IIterationBasedOperator, IStochasticOperator {
34
35    #region Parameter Properties
36    public ILookupParameter<IntValue> IterationsParameter {
37      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
38    }
39    public ILookupParameter<IRandom> RandomParameter {
40      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    public ILookupParameter<Permutation> PermutationParameter {
43      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
44    }
45    public ILookupParameter<DoubleMatrix> WeightsParameter {
46      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
47    }
48    public ILookupParameter<DoubleMatrix> DistancesParameter {
49      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
50    }
51    public ILookupParameter<IntMatrix> ShortTermMemoryParameter {
52      get { return (ILookupParameter<IntMatrix>)Parameters["ShortTermMemory"]; }
53    }
54    public ILookupParameter<DoubleMatrix> MoveQualityMatrixParameter {
55      get { return (ILookupParameter<DoubleMatrix>)Parameters["MoveQualityMatrix"]; }
56    }
57    public ILookupParameter<DoubleValue> QualityParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
59    }
60    public ILookupParameter<DoubleValue> BestQualityParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
62    }
63    public ILookupParameter<Swap2Move> LastMoveParameter {
64      get { return (ILookupParameter<Swap2Move>)Parameters["LastMove"]; }
65    }
66    public ILookupParameter<BoolValue> UseNewTabuTenureAdaptionSchemeParameter {
67      get { return (ILookupParameter<BoolValue>)Parameters["UseNewTabuTenureAdaptionScheme"]; }
68    }
69    public ILookupParameter<ResultCollection> ResultsParameter {
70      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
71    }
72
73    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
74      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
75    }
76    public IValueLookupParameter<IntValue> MinimumTabuTenureParameter {
77      get { return (IValueLookupParameter<IntValue>)Parameters["MinimumTabuTenure"]; }
78    }
79    public IValueLookupParameter<IntValue> MaximumTabuTenureParameter {
80      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTabuTenure"]; }
81    }
82    public IValueLookupParameter<BoolValue> UseAlternativeAspirationParameter {
83      get { return (IValueLookupParameter<BoolValue>)Parameters["UseAlternativeAspiration"]; }
84    }
85    public IValueLookupParameter<IntValue> AlternativeAspirationTenureParameter {
86      get { return (IValueLookupParameter<IntValue>)Parameters["AlternativeAspirationTenure"]; }
87    }
88    #endregion
89
90    [StorableConstructor]
91    private RobustTabooSeachOperator(bool deserializing) : base(deserializing) { }
92    private RobustTabooSeachOperator(RobustTabooSeachOperator original, Cloner cloner)
93      : base(original, cloner) {
94    }
95    public RobustTabooSeachOperator() {
96      Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration."));
97      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
98      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation solution."));
99      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
100      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
101      Parameters.Add(new LookupParameter<IntMatrix>("ShortTermMemory", "The table that stores the iteration at which a certain facility has been assigned to a certain location."));
102      Parameters.Add(new LookupParameter<DoubleMatrix>("MoveQualityMatrix", "The quality of all swap moves as evaluated on the current permutation."));
103      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value."));
104      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The best quality value."));
105      Parameters.Add(new LookupParameter<Swap2Move>("LastMove", "The last move that was applied."));
106      Parameters.Add(new LookupParameter<BoolValue>("UseNewTabuTenureAdaptionScheme", "True if the new tabu tenure adaption should be used or false otherwise."));
107      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Collection that houses the results of the algorithm."));
108      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run."));
109      Parameters.Add(new ValueLookupParameter<IntValue>("MinimumTabuTenure", "The minimum tabu tenure."));
110      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTabuTenure", "The maximum tabu tenure."));
111      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."));
112      Parameters.Add(new ValueLookupParameter<IntValue>("AlternativeAspirationTenure", "The time t that a move will be remembered for the alternative aspiration condition."));
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new RobustTabooSeachOperator(this, cloner);
117    }
118
119    public override IOperation Apply() {
120      IRandom random = RandomParameter.ActualValue;
121      int iteration = IterationsParameter.ActualValue.Value;
122      IntMatrix shortTermMemory = ShortTermMemoryParameter.ActualValue;
123      DoubleMatrix weights = WeightsParameter.ActualValue;
124      DoubleMatrix distances = DistancesParameter.ActualValue;
125      DoubleMatrix moveQualityMatrix = MoveQualityMatrixParameter.ActualValue;
126
127      DoubleValue quality = QualityParameter.ActualValue;
128      DoubleValue bestQuality = BestQualityParameter.ActualValue;
129      if (bestQuality == null) {
130        BestQualityParameter.ActualValue = (DoubleValue)quality.Clone();
131        bestQuality = BestQualityParameter.ActualValue;
132      }
133
134      int minTenure = MinimumTabuTenureParameter.ActualValue.Value;
135      int maxTenure = MaximumTabuTenureParameter.ActualValue.Value;
136      int alternativeAspirationTenure = AlternativeAspirationTenureParameter.ActualValue.Value;
137      bool useAlternativeAspiration = UseAlternativeAspirationParameter.ActualValue.Value;
138      Permutation solution = PermutationParameter.ActualValue;
139      Swap2Move lastMove = LastMoveParameter.ActualValue;
140
141      double bestMoveQuality = double.MaxValue;
142      Swap2Move bestMove = null;
143      bool already_aspired = false;
144
145      foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(solution)) {
146        double moveQuality;
147        if (lastMove == null)
148          moveQuality = QAPSwap2MoveEvaluator.Apply(solution, move, weights, distances);
149        else moveQuality = QAPSwap2MoveEvaluator.Apply(solution, move, moveQualityMatrix[move.Index1, move.Index2], weights, distances, lastMove);
150
151        moveQualityMatrix[move.Index1, move.Index2] = moveQuality;
152        moveQualityMatrix[move.Index2, move.Index1] = moveQuality;
153
154        bool autorized = shortTermMemory[move.Index1, solution[move.Index2]] < iteration
155                      || shortTermMemory[move.Index2, solution[move.Index1]] < iteration;
156
157        bool aspired = (shortTermMemory[move.Index1, solution[move.Index2]] < iteration - alternativeAspirationTenure
158                     && shortTermMemory[move.Index2, solution[move.Index1]] < iteration - alternativeAspirationTenure)
159                  || quality.Value + moveQuality < bestQuality.Value;
160
161        if ((aspired && !already_aspired) // the first alternative move is aspired
162          || (aspired && already_aspired && moveQuality < bestMoveQuality) // an alternative move was already aspired, but this is better
163          || (autorized && !aspired && !already_aspired && moveQuality < bestMoveQuality)) { // a regular better move is found
164          bestMove = move;
165          bestMoveQuality = moveQuality;
166          if (aspired) already_aspired = true;
167        }
168      }
169
170      ResultCollection results = ResultsParameter.ActualValue;
171      if (results != null) {
172        IntValue aspiredMoves = null;
173        if (!results.ContainsKey("AspiredMoves")) {
174          aspiredMoves = new IntValue(already_aspired ? 1 : 0);
175          results.Add(new Result("AspiredMoves", "Counts the number of moves that were selected because of the aspiration criteria.", aspiredMoves));
176        } else if (already_aspired) {
177          aspiredMoves = (IntValue)results["AspiredMoves"].Value;
178          aspiredMoves.Value++;
179        }
180      }
181
182      LastMoveParameter.ActualValue = bestMove;
183
184      if (bestMove == null) return base.Apply();
185
186      bool useNewAdaptionScheme = UseNewTabuTenureAdaptionSchemeParameter.ActualValue.Value;
187      if (useNewAdaptionScheme) {
188        double r = random.NextDouble();
189        if (r == 0) r = 1; // transform to (0;1]
190        shortTermMemory[bestMove.Index1, solution[bestMove.Index1]] = (int)(iteration + r * r * r * maxTenure);
191        r = random.NextDouble();
192        if (r == 0) r = 1; // transform to (0;1]
193        shortTermMemory[bestMove.Index2, solution[bestMove.Index2]] = (int)(iteration + r * r * r * maxTenure);
194      } else {
195        shortTermMemory[bestMove.Index1, solution[bestMove.Index1]] = iteration + random.Next(minTenure, maxTenure);
196        shortTermMemory[bestMove.Index2, solution[bestMove.Index2]] = iteration + random.Next(minTenure, maxTenure);
197      }
198      Swap2Manipulator.Apply(solution, bestMove.Index1, bestMove.Index2);
199      quality.Value += bestMoveQuality;
200
201      if (quality.Value < bestQuality.Value) bestQuality.Value = quality.Value;
202
203      return base.Apply();
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.