[5930] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5930] | 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 System;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
| 31 | [Item("QAPSwap2MoveEvaluator", "Evaluated a swap-2 move on a QAP solution.")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public class QAPSwap2MoveEvaluator : QAPMoveEvaluator, IPermutationSwap2MoveOperator {
|
---|
| 34 | public ILookupParameter<Swap2Move> Swap2MoveParameter {
|
---|
| 35 | get { return (ILookupParameter<Swap2Move>)Parameters["Swap2Move"]; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [StorableConstructor]
|
---|
| 39 | protected QAPSwap2MoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 40 | protected QAPSwap2MoveEvaluator(QAPSwap2MoveEvaluator original, Cloner cloner)
|
---|
| 41 | : base(original, cloner) {
|
---|
| 42 | }
|
---|
| 43 | public QAPSwap2MoveEvaluator() {
|
---|
| 44 | Parameters.Add(new LookupParameter<Swap2Move>("Swap2Move", "The move to evaluate."));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 48 | return new QAPSwap2MoveEvaluator(this, cloner);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[6628] | 51 | /// <summary>
|
---|
| 52 | /// Calculates the quality of the move <paramref name="move"/> by evaluating the changes.
|
---|
| 53 | /// The runtime complexity of this method is O(N) with N being the size of the permutation.
|
---|
| 54 | /// </summary>
|
---|
| 55 | /// <param name="assignment">The current permutation.</param>
|
---|
| 56 | /// <param name="move">The move that is to be evaluated if it was applied to the current permutation.</param>
|
---|
| 57 | /// <param name="weights">The weights matrix.</param>
|
---|
| 58 | /// <param name="distances">The distances matrix.</param>
|
---|
| 59 | /// <returns>The relative change in quality if <paramref name="move"/> was applied to <paramref name="assignment"/>.</returns>
|
---|
[5930] | 60 | public static double Apply(Permutation assignment, Swap2Move move, DoubleMatrix weights, DoubleMatrix distances) {
|
---|
| 61 | if (move.Index1 == move.Index2) return 0;
|
---|
| 62 | double moveQuality = 0;
|
---|
| 63 | int fac1 = move.Index1, fac2 = move.Index2;
|
---|
| 64 | int loc1 = assignment[fac1], loc2 = assignment[fac2];
|
---|
| 65 |
|
---|
| 66 | for (int j = 0; j < assignment.Length; j++) {
|
---|
| 67 | if (j == fac1) {
|
---|
| 68 | moveQuality += weights[fac1, fac1] * (distances[loc2, loc2] - distances[loc1, loc1]);
|
---|
| 69 | moveQuality += weights[fac1, fac2] * (distances[loc2, loc1] - distances[loc1, loc2]);
|
---|
| 70 | } else if (j == fac2) {
|
---|
| 71 | moveQuality += weights[fac2, fac2] * (distances[loc1, loc1] - distances[loc2, loc2]);
|
---|
| 72 | moveQuality += weights[fac2, fac1] * (distances[loc1, loc2] - distances[loc2, loc1]);
|
---|
| 73 | } else {
|
---|
| 74 | int locJ = assignment[j];
|
---|
| 75 | moveQuality += weights[fac1, j] * (distances[loc2, locJ] - distances[loc1, locJ]);
|
---|
| 76 | moveQuality += weights[j, fac1] * (distances[locJ, loc2] - distances[locJ, loc1]);
|
---|
| 77 | moveQuality += weights[fac2, j] * (distances[loc1, locJ] - distances[loc2, locJ]);
|
---|
| 78 | moveQuality += weights[j, fac2] * (distances[locJ, loc1] - distances[locJ, loc2]);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | return moveQuality;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[6628] | 84 | /// <summary>
|
---|
| 85 | /// Is able to compute the move qualities faster O(1) in some cases if it knows the quality of
|
---|
| 86 | /// performing the move <paramref name="move"/> previously. In other cases it performs a
|
---|
| 87 | /// standard move quality calculation with runtime complexity O(N).
|
---|
| 88 | /// </summary>
|
---|
| 89 | /// <remarks>
|
---|
| 90 | /// The number of cases that the calculation can be performed faster grows with N^2
|
---|
| 91 | /// while the number of cases which require a larger recalculation grows linearly with N.
|
---|
| 92 | /// Larger problem instances thus benefit from this faster method to a larger degree.
|
---|
| 93 | /// </remarks>
|
---|
| 94 | /// <param name="assignment">The current permutation.</param>
|
---|
| 95 | /// <param name="move">The current move that is to be evaluated.</param>
|
---|
| 96 | /// <param name="previousQuality">The quality of that move as evaluated for the previous permutation.</param>
|
---|
| 97 | /// <param name="weights">The weigths matrix.</param>
|
---|
| 98 | /// <param name="distances">The distances matrix.</param>
|
---|
| 99 | /// <param name="lastMove">The move that was applied to transform the permutation from the previous to the current one.</param>
|
---|
| 100 | /// <returns>The relative change in quality if <paramref name="move"/> was applied to <paramref name="assignment"/>.</returns>
|
---|
| 101 | public static double Apply(Permutation assignment, Swap2Move move, double previousQuality,
|
---|
| 102 | DoubleMatrix weights, DoubleMatrix distances, Swap2Move lastMove) {
|
---|
| 103 | bool overlapsLastMove = move.Index1 == lastMove.Index1
|
---|
| 104 | || move.Index2 == lastMove.Index1
|
---|
| 105 | || move.Index1 == lastMove.Index2
|
---|
| 106 | || move.Index2 == lastMove.Index2;
|
---|
| 107 |
|
---|
| 108 | if (!overlapsLastMove) {
|
---|
| 109 | int r = lastMove.Index1, u = move.Index1, s = lastMove.Index2, v = move.Index2;
|
---|
| 110 | int pR = assignment[lastMove.Index1], pU = assignment[move.Index1], pS = assignment[lastMove.Index2], pV = assignment[move.Index2];
|
---|
| 111 |
|
---|
| 112 | return previousQuality
|
---|
| 113 | + (weights[r, u] - weights[r, v] + weights[s, v] - weights[s, u])
|
---|
| 114 | * (distances[pS, pU] - distances[pS, pV] + distances[pR, pV] - distances[pR, pU])
|
---|
| 115 | + (weights[u, r] - weights[v, r] + weights[v, s] - weights[u, s])
|
---|
| 116 | * (distances[pU, pS] - distances[pV, pS] + distances[pV, pR] - distances[pU, pR]);
|
---|
| 117 | } else {
|
---|
| 118 | return Apply(assignment, move, weights, distances);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[5930] | 122 | public override IOperation Apply() {
|
---|
| 123 | Swap2Move move = Swap2MoveParameter.ActualValue;
|
---|
| 124 | if (move == null) throw new InvalidOperationException("Swap-2 move is not found.");
|
---|
| 125 | Permutation assignment = PermutationParameter.ActualValue;
|
---|
| 126 | DoubleMatrix distances = DistancesParameter.ActualValue;
|
---|
| 127 | DoubleMatrix weights = WeightsParameter.ActualValue;
|
---|
| 128 |
|
---|
| 129 | double moveQuality = QualityParameter.ActualValue.Value;
|
---|
| 130 | moveQuality += Apply(assignment, move, weights, distances);
|
---|
| 131 | MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
|
---|
| 132 | return base.Apply();
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|