1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Persistence;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
31 | [Item("ExhaustiveSwap2MoveGenerator", "Generates all possible swap-2 moves from a given permutation.")]
|
---|
32 | [StorableType("81d600a0-0564-4925-a326-7d28e0b66e19")]
|
---|
33 | public class ExhaustiveSwap2MoveGenerator : Swap2MoveGenerator, IExhaustiveMoveGenerator {
|
---|
34 | [StorableConstructor]
|
---|
35 | protected ExhaustiveSwap2MoveGenerator(StorableConstructorFlag 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 | }
|
---|