1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HEAL.Fossil;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.PTSP {
|
---|
32 | [Item("Exhaustive 2.5-MoveGenerator", "Generates all possible inversion and shift moves (2.5-opt) from a given permutation.")]
|
---|
33 | [StorableType("DF95E561-1F47-4845-A58D-CEF32B14461B")]
|
---|
34 | public sealed class ExhaustiveTwoPointFiveMoveGenerator : TwoPointFiveMoveGenerator, IExhaustiveMoveGenerator {
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | private ExhaustiveTwoPointFiveMoveGenerator(StorableConstructorFlag _) : base(_) { }
|
---|
38 | private ExhaustiveTwoPointFiveMoveGenerator(ExhaustiveTwoPointFiveMoveGenerator original, Cloner cloner) : base(original, cloner) { }
|
---|
39 | public ExhaustiveTwoPointFiveMoveGenerator() : base() { }
|
---|
40 |
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new ExhaustiveTwoPointFiveMoveGenerator(this, cloner);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static TwoPointFiveMove[] Apply(Permutation permutation) {
|
---|
46 | return Generate(permutation).ToArray();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public static IEnumerable<TwoPointFiveMove> Generate(Permutation permutation) {
|
---|
50 | int length = permutation.Length;
|
---|
51 | if (length == 1) throw new ArgumentException("Exhaustive 2.5-MoveGenerator: There cannot be a 2.5 move given a permutation of length 1.", "permutation");
|
---|
52 | int totalMoves = (length) * (length - 1) / 2;
|
---|
53 |
|
---|
54 | if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
|
---|
55 | if (totalMoves - 3 > 0) {
|
---|
56 | for (int i = 0; i < length - 1; i++) {
|
---|
57 | for (int j = i + 1; j < length; j++) {
|
---|
58 | // doesn't make sense to inverse the whole permutation or the whole but one in case of relative undirected permutations
|
---|
59 | if (j - i >= length - 2) continue;
|
---|
60 | yield return new TwoPointFiveMove(i, j, true);
|
---|
61 | yield return new TwoPointFiveMove(i, j, false);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | } else { // when length is 3 or less, there's actually no difference, but for the sake of not crashing the algorithm create a dummy move
|
---|
65 | yield return new TwoPointFiveMove(0, 1, true);
|
---|
66 | }
|
---|
67 | } else {
|
---|
68 | for (int i = 0; i < length - 1; i++)
|
---|
69 | for (int j = i + 1; j < length; j++) {
|
---|
70 | yield return new TwoPointFiveMove(i, j, true);
|
---|
71 | yield return new TwoPointFiveMove(i, j, false);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected override TwoPointFiveMove[] GenerateMoves(Permutation permutation) {
|
---|
77 | return Apply(permutation);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|