[13412] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13412] | 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;
|
---|
[12272] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[13412] | 27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[12272] | 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.PTSP {
|
---|
[13412] | 32 | [Item("Exhaustive 2.5-MoveGenerator", "Generates all possible inversion and shift moves (2.5-opt) from a given permutation.")]
|
---|
[12272] | 33 | [StorableClass]
|
---|
[13412] | 34 | public sealed class ExhaustiveTwoPointFiveMoveGenerator : TwoPointFiveMoveGenerator, IExhaustiveMoveGenerator {
|
---|
[12272] | 35 |
|
---|
| 36 | [StorableConstructor]
|
---|
[13412] | 37 | private ExhaustiveTwoPointFiveMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
| 38 | private ExhaustiveTwoPointFiveMoveGenerator(ExhaustiveTwoPointFiveMoveGenerator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 39 | public ExhaustiveTwoPointFiveMoveGenerator() : base() { }
|
---|
[12272] | 40 |
|
---|
| 41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13412] | 42 | return new ExhaustiveTwoPointFiveMoveGenerator(this, cloner);
|
---|
[12272] | 43 | }
|
---|
[13412] | 44 |
|
---|
[12272] | 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;
|
---|
[13412] | 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");
|
---|
[12272] | 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;
|
---|
[13412] | 60 | yield return new TwoPointFiveMove(i, j, true);
|
---|
[12272] | 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
|
---|
[13412] | 65 | yield return new TwoPointFiveMove(0, 1, true);
|
---|
[12272] | 66 | }
|
---|
| 67 | } else {
|
---|
| 68 | for (int i = 0; i < length - 1; i++)
|
---|
| 69 | for (int j = i + 1; j < length; j++) {
|
---|
[13412] | 70 | yield return new TwoPointFiveMove(i, j, true);
|
---|
[12272] | 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 | }
|
---|