1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HEAL.Attic;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
31 | [Item("QAPTranslocationMoveEvaluator", "Evaluates translocation moves on a QAP solution.")]
|
---|
32 | [StorableType("0B1B33E0-7F88-485C-8E00-79FBB266CE4E")]
|
---|
33 | public class QAPTranslocationMoveEvaluator : QAPMoveEvaluator, IPermutationTranslocationMoveOperator {
|
---|
34 |
|
---|
35 | public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
|
---|
36 | get { return (ILookupParameter<TranslocationMove>)Parameters["TranslocationMove"]; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | protected QAPTranslocationMoveEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
41 | protected QAPTranslocationMoveEvaluator(QAPTranslocationMoveEvaluator original, Cloner cloner)
|
---|
42 | : base(original, cloner) {
|
---|
43 | }
|
---|
44 | public QAPTranslocationMoveEvaluator()
|
---|
45 | : base() {
|
---|
46 | Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "A move which takes part of a permutation and inserts it in a different part."));
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new QAPTranslocationMoveEvaluator(this, cloner);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static double Apply(Permutation assignment, TranslocationMove move, DoubleMatrix weights, DoubleMatrix distances) {
|
---|
54 | double moveQuality = 0;
|
---|
55 | int min = Math.Min(move.Index1, move.Index3);
|
---|
56 | int max = Math.Max(move.Index2, move.Index3 + (move.Index2 - move.Index1));
|
---|
57 | int iOffset, changeOffset;
|
---|
58 | if (move.Index1 < move.Index3) {
|
---|
59 | iOffset = move.Index2 - move.Index1 + 1;
|
---|
60 | changeOffset = min + max - move.Index2;
|
---|
61 | } else {
|
---|
62 | iOffset = move.Index1 - move.Index3;
|
---|
63 | changeOffset = min + move.Index2 - move.Index1 + 1;
|
---|
64 | }
|
---|
65 | for (int i = min; i <= max; i++) {
|
---|
66 | if (i == changeOffset) iOffset -= (max - min + 1);
|
---|
67 | int jOffset = ((move.Index1 < move.Index3) ? (move.Index2 - move.Index1 + 1) : (move.Index1 - move.Index3));
|
---|
68 | for (int j = 0; j < assignment.Length; j++) {
|
---|
69 | moveQuality -= weights[i, j] * distances[assignment[i], assignment[j]];
|
---|
70 | if (j < min || j > max) {
|
---|
71 | moveQuality -= weights[j, i] * distances[assignment[j], assignment[i]];
|
---|
72 | moveQuality += weights[i, j] * distances[assignment[i + iOffset], assignment[j]];
|
---|
73 | moveQuality += weights[j, i] * distances[assignment[j], assignment[i + iOffset]];
|
---|
74 | } else {
|
---|
75 | if (j == changeOffset) jOffset -= (max - min + 1);
|
---|
76 | moveQuality += weights[i, j] * distances[assignment[i + iOffset], assignment[j + jOffset]];
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | return moveQuality;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override IOperation Apply() {
|
---|
84 | TranslocationMove move = TranslocationMoveParameter.ActualValue;
|
---|
85 | if (move == null) throw new InvalidOperationException("Translocation move is not found.");
|
---|
86 | Permutation assignment = PermutationParameter.ActualValue;
|
---|
87 | DoubleMatrix distances = DistancesParameter.ActualValue;
|
---|
88 | DoubleMatrix weights = WeightsParameter.ActualValue;
|
---|
89 |
|
---|
90 | double moveQuality = QualityParameter.ActualValue.Value;
|
---|
91 | moveQuality += Apply(assignment, move, weights, distances);
|
---|
92 | MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
|
---|
93 | return base.Apply();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|