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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
33 | [Item("AffinitySwapMoveEvaluator", "Evaluated an affinity-based swap move on a QAP solution.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class AffinitySwapMoveEvaluator : QAPMoveEvaluator, IAffinitySwapMoveOperator {
|
---|
36 | public ILookupParameter<AffinitySwapMove> AffinitySwapMoveParameter {
|
---|
37 | get { return (ILookupParameter<AffinitySwapMove>)Parameters["AffinitySwapMove"]; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [StorableConstructor]
|
---|
41 | protected AffinitySwapMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
42 | protected AffinitySwapMoveEvaluator(AffinitySwapMoveEvaluator original, Cloner cloner)
|
---|
43 | : base(original, cloner) {
|
---|
44 | }
|
---|
45 | public AffinitySwapMoveEvaluator() {
|
---|
46 | Parameters.Add(new LookupParameter<AffinitySwapMove>("AffinitySwapMove", "The move to evaluate."));
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new AffinitySwapMoveEvaluator(this, cloner);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Calculates the quality of the move <paramref name="move"/> by evaluating the changes.
|
---|
55 | /// The runtime complexity of this method is O(M*N) with N being the size of the permutation and M the number of swaps
|
---|
56 | /// </summary>
|
---|
57 | /// <param name="assignment">The current permutation.</param>
|
---|
58 | /// <param name="move">The move that is to be evaluated if it was applied to the current permutation.</param>
|
---|
59 | /// <param name="weights">The weights matrix.</param>
|
---|
60 | /// <param name="distances">The distances matrix.</param>
|
---|
61 | /// <returns>The relative change in quality if <paramref name="move"/> was applied to <paramref name="assignment"/>.</returns>
|
---|
62 | public static double Apply(Permutation assignment, AffinitySwapMove move, DoubleMatrix weights, DoubleMatrix distances) {
|
---|
63 | if (!move.Swaps.Any()) return 0;
|
---|
64 | double moveQuality = 0;
|
---|
65 |
|
---|
66 | foreach (KeyValuePair<int, int> swap in move.Swaps) {
|
---|
67 | int fac1 = swap.Key, fac2 = swap.Value;
|
---|
68 | int loc1 = assignment[fac1], loc2 = assignment[fac2];
|
---|
69 | for (int j = 0; j < assignment.Length; j++) {
|
---|
70 | if (j == fac1) {
|
---|
71 | moveQuality += weights[fac1, fac1] * (distances[loc2, loc2] - distances[loc1, loc1]);
|
---|
72 | moveQuality += weights[fac1, fac2] * (distances[loc2, loc1] - distances[loc1, loc2]);
|
---|
73 | } else if (j == fac2) {
|
---|
74 | moveQuality += weights[fac2, fac2] * (distances[loc1, loc1] - distances[loc2, loc2]);
|
---|
75 | moveQuality += weights[fac2, fac1] * (distances[loc1, loc2] - distances[loc2, loc1]);
|
---|
76 | } else {
|
---|
77 | int locJ = assignment[j];
|
---|
78 | bool isMoved = move.Contains(j);
|
---|
79 | if (isMoved) {
|
---|
80 | moveQuality -= weights[fac1, j] * distances[loc1, locJ];
|
---|
81 | moveQuality -= weights[fac2, j] * distances[loc2, locJ];
|
---|
82 | locJ = assignment[move.GetSwapLocation(j)];
|
---|
83 | moveQuality += weights[fac1, j] * distances[loc2, locJ];
|
---|
84 | moveQuality += weights[fac2, j] * distances[loc1, locJ];
|
---|
85 | } else {
|
---|
86 | moveQuality += weights[fac1, j] * (distances[loc2, locJ] - distances[loc1, locJ]);
|
---|
87 | moveQuality += weights[fac2, j] * (distances[loc1, locJ] - distances[loc2, locJ]);
|
---|
88 | moveQuality += weights[j, fac1] * (distances[locJ, loc2] - distances[locJ, loc1]);
|
---|
89 | moveQuality += weights[j, fac2] * (distances[locJ, loc1] - distances[locJ, loc2]);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return moveQuality;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public override IOperation Apply() {
|
---|
98 | AffinitySwapMove move = AffinitySwapMoveParameter.ActualValue;
|
---|
99 | if (move == null) throw new InvalidOperationException("Swap move is not found.");
|
---|
100 | Permutation assignment = PermutationParameter.ActualValue;
|
---|
101 | DoubleMatrix distances = DistancesParameter.ActualValue;
|
---|
102 | DoubleMatrix weights = WeightsParameter.ActualValue;
|
---|
103 |
|
---|
104 | double moveQuality = QualityParameter.ActualValue.Value;
|
---|
105 | moveQuality += Apply(assignment, move, weights, distances);
|
---|
106 | MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
|
---|
107 | return base.Apply();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|