[5785] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5785] | 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("QAPInversionMoveEvaluator", "Evaluated an inversion move on a QAP solution.")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public class QAPInversionMoveEvaluator : QAPMoveEvaluator, IPermutationInversionMoveOperator {
|
---|
| 34 | public ILookupParameter<InversionMove> InversionMoveParameter {
|
---|
| 35 | get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [StorableConstructor]
|
---|
| 39 | protected QAPInversionMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 40 | protected QAPInversionMoveEvaluator(QAPInversionMoveEvaluator original, Cloner cloner)
|
---|
| 41 | : base(original, cloner) {
|
---|
| 42 | }
|
---|
| 43 | public QAPInversionMoveEvaluator() {
|
---|
| 44 | Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 48 | return new QAPInversionMoveEvaluator(this, cloner);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public static double Apply(Permutation assignment, InversionMove move, DoubleMatrix weights, DoubleMatrix distances) {
|
---|
| 52 | if (move.Index1 == move.Index2) return 0;
|
---|
| 53 | double moveQuality = 0;
|
---|
| 54 | int min = Math.Min(move.Index1, move.Index2);
|
---|
| 55 | int max = Math.Max(move.Index1, move.Index2);
|
---|
[5930] | 56 |
|
---|
[5785] | 57 | for (int i = min; i <= max; i++) {
|
---|
[5930] | 58 | int locI = assignment[i];
|
---|
| 59 | int newlocI = assignment[max - i + min];
|
---|
| 60 |
|
---|
[5785] | 61 | for (int j = 0; j < assignment.Length; j++) {
|
---|
[5930] | 62 | int locJ = assignment[j];
|
---|
| 63 | if (j >= min && j <= max) {
|
---|
| 64 | int newlocJ = assignment[max - j + min];
|
---|
| 65 | moveQuality += weights[i, j] * (distances[newlocI, newlocJ] - distances[locI, locJ]);
|
---|
[5785] | 66 | } else {
|
---|
[5930] | 67 | moveQuality += weights[i, j] * (distances[newlocI, locJ] - distances[locI, locJ]);
|
---|
| 68 | moveQuality += weights[j, i] * (distances[locJ, newlocI] - distances[locJ, locI]);
|
---|
[5785] | 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | return moveQuality;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public override IOperation Apply() {
|
---|
| 76 | InversionMove move = InversionMoveParameter.ActualValue;
|
---|
| 77 | if (move == null) throw new InvalidOperationException("Inversion move is not found.");
|
---|
| 78 | Permutation assignment = PermutationParameter.ActualValue;
|
---|
[5838] | 79 | DoubleMatrix distances = DistancesParameter.ActualValue;
|
---|
[5785] | 80 | DoubleMatrix weights = WeightsParameter.ActualValue;
|
---|
| 81 |
|
---|
| 82 | double moveQuality = QualityParameter.ActualValue.Value;
|
---|
| 83 | moveQuality += Apply(assignment, move, weights, distances);
|
---|
| 84 | MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
|
---|
| 85 | return base.Apply();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|