#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.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.PackingEncoding.PackingSequence { [Item("Exhaustive Insertion Move Generator", "Generates all possible Swap Sequence moves from a given PackingSequence.")] [StorableClass] public class ExhaustiveInsertionMoveGenerator : InsertionMoveGenerator, IExhaustiveMoveGenerator { [StorableConstructor] protected ExhaustiveInsertionMoveGenerator(bool deserializing) : base(deserializing) { } protected ExhaustiveInsertionMoveGenerator(ExhaustiveInsertionMoveGenerator original, Cloner cloner) : base(original, cloner) { } public ExhaustiveInsertionMoveGenerator() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new ExhaustiveInsertionMoveGenerator(this, cloner); } public static IEnumerable Generate(PackingSequenceEncoding PackingSequence, int nrOfBins) { for (int i = 0; i < PackingSequence.PackingSequence.Length - 1; i++) for (int j = i + 1; j < PackingSequence.PackingSequence.Length; j++) { if (j != i) yield return new InsertionMove(i, j, PackingSequence); } } public static InsertionMove[] Apply(PackingSequenceEncoding PackingSequence) { int nrOfBins = 0; foreach (int binNr in PackingSequence.PackingSequence) { if (binNr > nrOfBins) nrOfBins = binNr; } nrOfBins++; int totalMoves = (PackingSequence.PackingSequence.Length * (PackingSequence.PackingSequence.Length - 1)) / 2; InsertionMove[] moves = new InsertionMove[totalMoves]; int count = 0; foreach (InsertionMove move in Generate(PackingSequence, nrOfBins)) { moves[count++] = move; } return moves; } protected override InsertionMove[] GenerateMoves(PackingSequenceEncoding PackingSequence) { return Apply(PackingSequence); } } }