#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.PackingEncoding.PackingSequence { [Item("Exhaustive Swap2 Move Generator", "Generates all possible Swap Sequence moves from a given PackingSequence.")] [StorableClass] public class ExhaustiveSwap2MoveGenerator : Swap2MoveGenerator, IExhaustiveMoveGenerator { [StorableConstructor] protected ExhaustiveSwap2MoveGenerator(bool deserializing) : base(deserializing) { } protected ExhaustiveSwap2MoveGenerator(ExhaustiveSwap2MoveGenerator original, Cloner cloner) : base(original, cloner) { } public ExhaustiveSwap2MoveGenerator() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new ExhaustiveSwap2MoveGenerator(this, cloner); } public static IEnumerable Generate(Permutation packingSequence, int nrOfBins) { for (int i = 0; i < packingSequence.Length - 1; i++) for (int j = i + 1; j < packingSequence.Length; j++) { if (j != i) yield return new Swap2Move(i, j, packingSequence); } } public static Swap2Move[] Apply(Permutation packingSequence) { int nrOfBins = 0; foreach (int binNr in packingSequence) { if (binNr > nrOfBins) nrOfBins = binNr; } nrOfBins++; int totalMoves = (packingSequence.Length * (packingSequence.Length - 1)) / 2; Swap2Move[] moves = new Swap2Move[totalMoves]; int count = 0; foreach (Swap2Move move in Generate(packingSequence, nrOfBins)) { moves[count++] = move; } return moves; } protected override Swap2Move[] GenerateMoves(Permutation packingSequence) { return Apply(packingSequence); } } }