source: branches/2839_HiveProjectManagement/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Moves/Swap/ExhaustiveSwap2MoveGenerator.cs @ 16057

Last change on this file since 16057 was 16057, checked in by jkarder, 17 months ago

#2839:

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.LinearLinkageEncoding {
31  [Item("ExhaustiveSwap2MoveGenerator", "Generates all possible swap-2 moves from a given lle grouping.")]
32  [StorableClass]
33  public class ExhaustiveSwap2MoveGenerator : Swap2MoveGenerator, IExhaustiveMoveGenerator {
34    [StorableConstructor]
35    protected ExhaustiveSwap2MoveGenerator(bool deserializing) : base(deserializing) { }
36    protected ExhaustiveSwap2MoveGenerator(ExhaustiveSwap2MoveGenerator original, Cloner cloner) : base(original, cloner) { }
37    public ExhaustiveSwap2MoveGenerator() : base() { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new ExhaustiveSwap2MoveGenerator(this, cloner);
41    }
42
43    public static IEnumerable<Swap2Move> Generate(LinearLinkage lle) {
44      int length = lle.Length;
45      if (length == 1) throw new ArgumentException("ExhaustiveSwap2MoveGenerator: There cannot be an Swap move given only one item.", "lle");
46
47      var groups = lle.GetGroups().ToList();
48      if (groups.Count == 1) throw new InvalidOperationException("ExhaustiveSwap2MoveGenerator: Swap moves cannot be applied when there is only one group.");
49
50      for (int i = 0; i < groups.Count; i++)
51        for (int j = i + 1; j < groups.Count; j++)
52          for (var k = 0; k < groups[i].Count; k++)
53            for (var m = 0; m < groups[j].Count; m++) {
54              yield return new Swap2Move(groups[i][k], groups[j][m]);
55            }
56    }
57
58    protected override Swap2Move[] GenerateMoves(LinearLinkage lle) {
59      return Generate(lle).ToArray();
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.