Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPInversionMoveEvaluator.cs @ 18242

Last change on this file since 18242 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
57      for (int i = min; i <= max; i++) {
58        int locI = assignment[i];
59        int newlocI = assignment[max - i + min];
60
61        for (int j = 0; j < assignment.Length; j++) {
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]);
66          } else {
67            moveQuality += weights[i, j] * (distances[newlocI, locJ] - distances[locI, locJ]);
68            moveQuality += weights[j, i] * (distances[locJ, newlocI] - distances[locJ, locI]);
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;
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.