Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPScrambleMoveEvaluator.cs @ 17180

Last change on this file since 17180 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.4 KB
Line 
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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
28using HEAL.Attic;
29
30namespace HeuristicLab.Problems.QuadraticAssignment {
31  [Item("QAPScrambleMoveEvaluator", "Evaluated a scramble move on a QAP solution.")]
32  [StorableType("E5D1B682-6ADA-4DEE-8546-6C5BCE1C91D0")]
33  public class QAPScrambleMoveEvaluator : QAPMoveEvaluator, IPermutationScrambleMoveOperator {
34    public ILookupParameter<ScrambleMove> ScrambleMoveParameter {
35      get { return (ILookupParameter<ScrambleMove>)Parameters["ScrambleMove"]; }
36    }
37
38    [StorableConstructor]
39    protected QAPScrambleMoveEvaluator(StorableConstructorFlag _) : base(_) { }
40    protected QAPScrambleMoveEvaluator(QAPScrambleMoveEvaluator original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public QAPScrambleMoveEvaluator() {
44      Parameters.Add(new LookupParameter<ScrambleMove>("ScrambleMove", "The move to evaluate."));
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new QAPScrambleMoveEvaluator(this, cloner);
49    }
50
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>
60    public static double Apply(Permutation assignment, ScrambleMove move, DoubleMatrix weights, DoubleMatrix distances) {
61      double moveQuality = 0;
62      int min = move.StartIndex;
63      int max = min + move.ScrambledIndices.Length - 1;
64
65      for (int i = min; i <= max; i++) {
66        int locI = assignment[i];
67        int newlocI = assignment[min + move.ScrambledIndices[i - min]];
68        if (locI == newlocI) continue;
69
70        for (int j = 0; j < assignment.Length; j++) {
71          int locJ = assignment[j];
72          if (j >= min && j <= max) {
73            int newlocJ = assignment[min + move.ScrambledIndices[j - min]];
74            moveQuality += weights[i, j] * (distances[newlocI, newlocJ] - distances[locI, locJ]);
75            if (locJ == newlocJ)
76              moveQuality += weights[j, i] * (distances[newlocJ, newlocI] - distances[locJ, locI]);
77          } else {
78            moveQuality += weights[i, j] * (distances[newlocI, locJ] - distances[locI, locJ]);
79            moveQuality += weights[j, i] * (distances[locJ, newlocI] - distances[locJ, locI]);
80          }
81        }
82      }
83      return moveQuality;
84    }
85
86    public override IOperation Apply() {
87      ScrambleMove move = ScrambleMoveParameter.ActualValue;
88      if (move == null) throw new InvalidOperationException("Scramble move is not found.");
89      Permutation assignment = PermutationParameter.ActualValue;
90      DoubleMatrix distances = DistancesParameter.ActualValue;
91      DoubleMatrix weights = WeightsParameter.ActualValue;
92
93      double moveQuality = QualityParameter.ActualValue.Value;
94      moveQuality += Apply(assignment, move, weights, distances);
95      MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
96      return base.Apply();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.