Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPSwap2MoveEvaluator.cs @ 6087

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

#1330

  • Merged QAP from branch into trunk
  • Merged MDS from branch into trunk
  • Merged Swap2 moves from branch into trunk
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      int fac1 = move.Index1, fac2 = move.Index2;
55      int loc1 = assignment[fac1], loc2 = assignment[fac2];
56
57      for (int j = 0; j < assignment.Length; j++) {
58        if (j == fac1) {
59          moveQuality += weights[fac1, fac1] * (distances[loc2, loc2] - distances[loc1, loc1]);
60          moveQuality += weights[fac1, fac2] * (distances[loc2, loc1] - distances[loc1, loc2]);
61        } else if (j == fac2) {
62          moveQuality += weights[fac2, fac2] * (distances[loc1, loc1] - distances[loc2, loc2]);
63          moveQuality += weights[fac2, fac1] * (distances[loc1, loc2] - distances[loc2, loc1]);
64        } else {
65          int locJ = assignment[j];
66          moveQuality += weights[fac1, j] * (distances[loc2, locJ] - distances[loc1, locJ]);
67          moveQuality += weights[j, fac1] * (distances[locJ, loc2] - distances[locJ, loc1]);
68          moveQuality += weights[fac2, j] * (distances[loc1, locJ] - distances[loc2, locJ]);
69          moveQuality += weights[j, fac2] * (distances[locJ, loc1] - distances[locJ, loc2]);
70        }
71      }
72      return moveQuality;
73    }
74
75    public override IOperation Apply() {
76      Swap2Move move = Swap2MoveParameter.ActualValue;
77      if (move == null) throw new InvalidOperationException("Swap-2 move is not found.");
78      Permutation assignment = PermutationParameter.ActualValue;
79      DoubleMatrix distances = DistancesParameter.ActualValue;
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}
Note: See TracBrowser for help on using the repository browser.