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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.QuadraticAssignment.Algorithms {
|
---|
32 | [Item("QAPRobustTabooSearchOperator", "Performs an iteration of the robust taboo search algorithm as descrbied in Taillard 1991.")]
|
---|
33 | public sealed class QAPRobustTabooSeachOperator : SingleSuccessorOperator, IIterationBasedOperator, IStochasticOperator {
|
---|
34 |
|
---|
35 | #region Parameter Properties
|
---|
36 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
37 | get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
38 | }
|
---|
39 | public ILookupParameter<IntValue> TabuTenureParameter {
|
---|
40 | get { return (ILookupParameter<IntValue>)Parameters["TabuTenure"]; }
|
---|
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<DoubleMatrix> ShortTermMemoryParameter {
|
---|
52 | get { return (ILookupParameter<DoubleMatrix>)Parameters["ShortTermMemory"]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<DoubleMatrix> LongTermMemoryParameter {
|
---|
55 | get { return (ILookupParameter<DoubleMatrix>)Parameters["LongTermMemory"]; }
|
---|
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 |
|
---|
64 | public ILookupParameter<IRandom> RandomParameter {
|
---|
65 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
66 | }
|
---|
67 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
68 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
69 | }
|
---|
70 | public IValueLookupParameter<IntValue> MinimumTabuTenureParameter {
|
---|
71 | get { return (IValueLookupParameter<IntValue>)Parameters["MinimumTabuTenure"]; }
|
---|
72 | }
|
---|
73 | public IValueLookupParameter<IntValue> MaximumTabuTenureParameter {
|
---|
74 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTabuTenure"]; }
|
---|
75 | }
|
---|
76 | public IValueLookupParameter<IntValue> TabuTenureAdaptionIntervalParameter {
|
---|
77 | get { return (IValueLookupParameter<IntValue>)Parameters["TabuTenureAdaptionInterval"]; }
|
---|
78 | }
|
---|
79 | public IValueLookupParameter<BoolValue> UseAlternativeAspirationParameter {
|
---|
80 | get { return (IValueLookupParameter<BoolValue>)Parameters["UseAlternativeAspiration"]; }
|
---|
81 | }
|
---|
82 | public IValueLookupParameter<IntValue> AlternativeAspirationTenureParameter {
|
---|
83 | get { return (IValueLookupParameter<IntValue>)Parameters["AlternativeAspirationTenure"]; }
|
---|
84 | }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | [StorableConstructor]
|
---|
88 | private QAPRobustTabooSeachOperator(bool deserializing) : base(deserializing) { }
|
---|
89 | private QAPRobustTabooSeachOperator(QAPRobustTabooSeachOperator original, Cloner cloner)
|
---|
90 | : base(original, cloner) {
|
---|
91 | }
|
---|
92 | public QAPRobustTabooSeachOperator() {
|
---|
93 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration."));
|
---|
94 | Parameters.Add(new LookupParameter<IntValue>("TabuTenure", "The current tabu tenure."));
|
---|
95 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
96 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation solution."));
|
---|
97 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
|
---|
98 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
|
---|
99 | Parameters.Add(new LookupParameter<DoubleMatrix>("ShortTermMemory", "The table that stores the iteration at which a certain facility has been assigned to a certain location."));
|
---|
100 | Parameters.Add(new LookupParameter<DoubleMatrix>("LongTermMemory", "Same as the tabu table, but constantly updates the information given the current solution."));
|
---|
101 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value."));
|
---|
102 | Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The best quality value."));
|
---|
103 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run."));
|
---|
104 | Parameters.Add(new ValueLookupParameter<IntValue>("MinimumTabuTenure", "The minimum tabu tenure."));
|
---|
105 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTabuTenure", "The maximum tabu tenure."));
|
---|
106 | Parameters.Add(new ValueLookupParameter<IntValue>("TabuTenureAdaptionInterval", "The amount of iterations that have to pass before the tabu tenure is adapted."));
|
---|
107 | 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."));
|
---|
108 | Parameters.Add(new ValueLookupParameter<IntValue>("AlternativeAspirationTenure", "The time t that a move will be remembered for the alternative aspiration condition."));
|
---|
109 | }
|
---|
110 |
|
---|
111 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
112 | return new QAPRobustTabooSeachOperator(this, cloner);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public override IOperation Apply() {
|
---|
116 | IRandom random = RandomParameter.ActualValue;
|
---|
117 | int iteration = IterationsParameter.ActualValue.Value;
|
---|
118 | if (iteration % TabuTenureAdaptionIntervalParameter.ActualValue.Value == 0)
|
---|
119 | TabuTenureParameter.ActualValue.Value = MinimumTabuTenureParameter.ActualValue.Value
|
---|
120 | + random.Next(MaximumTabuTenureParameter.ActualValue.Value - MinimumTabuTenureParameter.ActualValue.Value + 1);
|
---|
121 |
|
---|
122 | DoubleMatrix longTermMemory = LongTermMemoryParameter.ActualValue;
|
---|
123 | DoubleMatrix shortTermMemory = ShortTermMemoryParameter.ActualValue;
|
---|
124 | DoubleMatrix weights = WeightsParameter.ActualValue;
|
---|
125 | DoubleMatrix distances = DistancesParameter.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 tabuTenure = TabuTenureParameter.ActualValue.Value;
|
---|
135 | int alternativeAspirationTenure = AlternativeAspirationTenureParameter.ActualValue.Value;
|
---|
136 | Permutation solution = PermutationParameter.ActualValue;
|
---|
137 |
|
---|
138 | for (int i = 0; i < solution.Length; i++)
|
---|
139 | longTermMemory[i, solution[i]] = iteration;
|
---|
140 |
|
---|
141 | double bestMoveQuality = double.MaxValue;
|
---|
142 | Swap2Move bestMove = null;
|
---|
143 |
|
---|
144 | foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(solution)) {
|
---|
145 | double moveQuality = QAPSwap2MoveEvaluator.Apply(solution, move, weights, distances);
|
---|
146 | if (UseAlternativeAspirationParameter.ActualValue.Value
|
---|
147 | && longTermMemory[move.Index1, solution[move.Index2]] < iteration - alternativeAspirationTenure
|
---|
148 | && longTermMemory[move.Index2, solution[move.Index1]] < iteration - alternativeAspirationTenure) {
|
---|
149 | bestMove = move;
|
---|
150 | bestMoveQuality = moveQuality;
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | bool isTabu = shortTermMemory[move.Index1, solution[move.Index2]] > 0
|
---|
154 | && shortTermMemory[move.Index2, solution[move.Index1]] > 0
|
---|
155 | && ((iteration - shortTermMemory[move.Index1, solution[move.Index2]]) <= tabuTenure)
|
---|
156 | && ((iteration - shortTermMemory[move.Index2, solution[move.Index1]]) <= tabuTenure);
|
---|
157 | if (!isTabu || quality.Value + moveQuality < bestQuality.Value) {
|
---|
158 | if (moveQuality < bestMoveQuality) {
|
---|
159 | bestMoveQuality = moveQuality;
|
---|
160 | bestMove = move;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (bestMove == null) return base.Apply();
|
---|
166 |
|
---|
167 | shortTermMemory[bestMove.Index1, solution[bestMove.Index1]] = iteration;
|
---|
168 | shortTermMemory[bestMove.Index2, solution[bestMove.Index2]] = iteration;
|
---|
169 | Swap2Manipulator.Apply(solution, bestMove.Index1, bestMove.Index2);
|
---|
170 | quality.Value += bestMoveQuality;
|
---|
171 |
|
---|
172 | if (quality.Value < bestQuality.Value) bestQuality.Value = quality.Value;
|
---|
173 |
|
---|
174 | return base.Apply();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|