Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPInversionMoveEvaluator.cs @ 5785

Last change on this file since 5785 was 5785, checked in by abeham, 13 years ago

#1330

  • Added Swap2 move to permutation (+tabu search operators)
  • Added move evaluators for QAP (translocation/insertion missing)
  • Merged trunk-changes from Optimization into QAP branch
File size: 3.6 KB
Line 
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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace 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);
56      for (int i = min; i <= max; i++) {
57        for (int j = 0; j < assignment.Length; j++) {
58          moveQuality -= weights[i, j] * distances[assignment[i], assignment[j]];
59          if (j < min || j > max) {
60            moveQuality -= weights[j, i] * distances[assignment[j], assignment[i]];
61            moveQuality += weights[i, j] * distances[assignment[max - i + min], assignment[j]];
62            moveQuality += weights[j, i] * distances[assignment[j], assignment[max - i + min]];
63          } else {
64            moveQuality += weights[i, j] * distances[assignment[max - i + min], assignment[max - j + min]];
65          }
66        }
67      }
68      return moveQuality;
69    }
70
71    public override IOperation Apply() {
72      InversionMove move = InversionMoveParameter.ActualValue;
73      if (move == null) throw new InvalidOperationException("Inversion move is not found.");
74      Permutation assignment = PermutationParameter.ActualValue;
75      DoubleMatrix distances = DistanceMatrixParameter.ActualValue;
76      DoubleMatrix weights = WeightsParameter.ActualValue;
77
78      double moveQuality = QualityParameter.ActualValue.Value;
79      moveQuality += Apply(assignment, move, weights, distances);
80      MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
81      return base.Apply();
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.