Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPSwapMoveEvaluator.cs @ 5838

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

#1330

  • Renamed the DistanceMatrix parameter to Distances
  • Renamed the SwapMove to Swap2Move and renamed operators accordingly
  • Integrated changes in HeuristicLab.Analysis.Views regarding description text box
File size: 3.8 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("QAPSwap2MoveEvaluator", "Evaluated a swap-2 move on a QAP solution.")]
32  [StorableClass]
33  public class QAPSwap2MoveEvaluator : QAPMoveEvaluator, IPermutationSwap2MoveOperator {
34    public ILookupParameter<Swap2Move> Swap2MoveParameter {
35      get { return (ILookupParameter<Swap2Move>)Parameters["Swap2Move"]; }
36    }
37
38    [StorableConstructor]
39    protected QAPSwap2MoveEvaluator(bool deserializing) : base(deserializing) { }
40    protected QAPSwap2MoveEvaluator(QAPSwap2MoveEvaluator original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public QAPSwap2MoveEvaluator() {
44      Parameters.Add(new LookupParameter<Swap2Move>("Swap2Move", "The move to evaluate."));
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new QAPSwap2MoveEvaluator(this, cloner);
49    }
50
51    public static double Apply(Permutation assignment, Swap2Move move, DoubleMatrix weights, DoubleMatrix distances) {
52      if (move.Index1 == move.Index2) return 0;
53      double moveQuality = 0;
54      for (int j = 0; j < assignment.Length; j++) {
55        moveQuality -= weights[move.Index1, j] * distances[assignment[move.Index1], assignment[j]];
56        moveQuality -= weights[j, move.Index1] * distances[assignment[j], assignment[move.Index1]];
57        moveQuality -= weights[move.Index2, j] * distances[assignment[move.Index2], assignment[j]];
58        moveQuality -= weights[j, move.Index2] * distances[assignment[j], assignment[move.Index2]];
59
60        int facility1 = j == move.Index1 ? assignment[move.Index2] : assignment[j];
61        int facility2 = j == move.Index2 ? assignment[move.Index1] : assignment[j];
62
63        moveQuality += weights[move.Index1, j] * distances[assignment[move.Index2], facility2];
64        moveQuality += weights[j, move.Index1] * distances[facility2, assignment[move.Index2]];
65        moveQuality += weights[move.Index2, j] * distances[assignment[move.Index1], facility1];
66        moveQuality += weights[j, move.Index2] * distances[facility1, assignment[move.Index1]];
67      }
68      return moveQuality;
69    }
70
71    public override IOperation Apply() {
72      Swap2Move move = Swap2MoveParameter.ActualValue;
73      if (move == null) throw new InvalidOperationException("Swap-2 move is not found.");
74      Permutation assignment = PermutationParameter.ActualValue;
75      DoubleMatrix distances = DistancesParameter.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.