Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Moves/AffinitySwapMove.cs @ 6801

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

#1649

  • Added affinity-based move
File size: 3.1 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.QuadraticAssignment {
29  [Item("AffinitySwapMove", "An affinity swap move may move affine facilities together with another item.")]
30  [StorableClass]
31  public class AffinitySwapMove : Item {
32
33    [Storable]
34    private Dictionary<int, int> reverseSwaps;
35    [Storable]
36    private Dictionary<int, int> swaps;
37    public IEnumerable<KeyValuePair<int, int>> Swaps {
38      get { return swaps.AsEnumerable(); }
39    }
40
41    [StorableConstructor]
42    protected AffinitySwapMove(bool deserializing) : base(deserializing) { }
43    protected AffinitySwapMove(AffinitySwapMove original, Cloner cloner)
44      : base(original, cloner) {
45      swaps = new Dictionary<int, int>(original.swaps);
46      reverseSwaps = new Dictionary<int, int>(original.reverseSwaps);
47    }
48    public AffinitySwapMove()
49      : base() {
50      swaps = new Dictionary<int, int>();
51      reverseSwaps = new Dictionary<int, int>();
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new AffinitySwapMove(this, cloner);
56    }
57
58    public bool AddSwap(int index1, int index2) {
59      if (Contains(index1) || Contains(index2)) return false;
60      swaps.Add(index1, index2);
61      reverseSwaps.Add(index2, index1);
62      return true;
63    }
64
65    public bool Contains(int index) {
66      return swaps.ContainsKey(index) || reverseSwaps.ContainsKey(index);
67    }
68
69    /// <summary>
70    /// Returns the swap partner for the given index.
71    /// </summary>
72    /// <remarks>
73    /// Throws a KeyNotFoundException when the <paramref name="index"/> is not part of the swap.
74    /// Check with <see cref="Contains"/> to see if it is included first.
75    /// </remarks>
76    /// <param name="index">The index for which to find the swap partner.</param>
77    /// <returns>The swap partner of the index.</returns>
78    public int GetSwapLocation(int index) {
79      if (swaps.ContainsKey(index)) return swaps[index];
80      else if (reverseSwaps.ContainsKey(index)) return reverseSwaps[index];
81      else throw new KeyNotFoundException(ItemName + ": The given index is not part of the affinity-based swap move.");
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.