#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; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.BinPacking; namespace HeuristicLab.Encodings.PackingEncoding.PackingSequence { [Item("Insertion Move", "A move on a Sequence vector that is specified by two group-assignment-indexes.")] [StorableClass] public class InsertionMove : SequenceMove { [Storable] public int Index1 { get; protected set; } [Storable] public int Index2 { get; protected set; } [StorableConstructor] protected InsertionMove(bool deserializing) : base(deserializing) { } protected InsertionMove(InsertionMove original, Cloner cloner) : base(original, cloner) { this.Index1 = original.Index1; this.Index2 = original.Index2; } public InsertionMove(int index1, int index2, Permutation packingSequence) : base(packingSequence) { Index1 = index1; Index2 = index2; } public override IDeepCloneable Clone(Cloner cloner) { return new InsertionMove(this, cloner); } public override Permutation GetSolutionAfterMove() { Permutation original = PackingSequence; Permutation resultingPermutation = (Permutation)PackingSequence.Clone(); int cutIndex, insertIndex, number; cutIndex = Index1; insertIndex = Index2; number = original[cutIndex]; int i = 0; // index in new permutation int j = 0; // index in old permutation while (i < PackingSequence.Length) { if (j == cutIndex) { j++; } if (i == insertIndex) { resultingPermutation[i] = number; i++; } if ((i < original.Length) && (j < original.Length)) { resultingPermutation[i] = original[j]; i++; j++; } } return resultingPermutation; } public override Type GetMoveAttributeType() { return typeof(InsertionMoveAttribute); } public override SequenceMoveAttribute GetAttribute(double quality) { return new InsertionMoveAttribute(Index1, Index2, PackingSequence[Index1], PackingSequence[Index2], quality); } public override bool BelongsToAttribute(SequenceMoveAttribute attribute, bool hardCriterion) { var attr = attribute as InsertionMoveAttribute; if (attr != null) { if (hardCriterion) { if (Index1 == attr.Index1 || Index2 == attr.Index2) return true; } else if (Index1 == attr.Index1 && Index2 == attr.Index2) return true; } return false; } public override string ToString() { return "IM(i1=" + Index1 + ", i2=" + Index2 + ")"; } } }