#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("Swap2 Move", "A move on a Sequence vector that is specified by two group-assignment-indexes.")] [StorableClass] public class Swap2Move : SequenceMove, IPackingMove { [Storable] public int Index1 { get; protected set; } [Storable] public int Index2 { get; protected set; } [StorableConstructor] protected Swap2Move(bool deserializing) : base(deserializing) { } protected Swap2Move(Swap2Move original, Cloner cloner) : base(original, cloner) { this.Index1 = original.Index1; this.Index2 = original.Index2; } public Swap2Move(int index1, int index2, PackingSequenceEncoding PackingSequence) : base(PackingSequence) { Index1 = index1; Index2 = index2; } public override IDeepCloneable Clone(Cloner cloner) { return new Swap2Move(this, cloner); } public override IPackingSolutionEncoding GetSolutionAfterMove() { PackingSequenceEncoding newSolution = new PackingSequenceEncoding(); newSolution.PackingSequence = (Permutation)PackingSequence.PackingSequence.Clone(new Cloner()); newSolution.PackingSequence[Index1] = PackingSequence.PackingSequence[Index2]; newSolution.PackingSequence[Index2] = PackingSequence.PackingSequence[Index1]; return newSolution; } public override Type GetMoveAttributeType() { return typeof(Swap2MoveAttribute); } public override SequenceMoveAttribute GetAttribute(double quality) { return new Swap2MoveAttribute(Index1, Index2, PackingSequence.PackingSequence[Index1], PackingSequence.PackingSequence[Index2], quality); } public override bool BelongsToAttribute(SequenceMoveAttribute attribute, bool hardCriterion) { var attr = attribute as Swap2MoveAttribute; if (attr != null) { if (hardCriterion) { if (Index1 == attr.Index1 || Index2 == attr.Index2)// || PackingSequence.PackingSequence[Index1] == attr.AssignedBin1 || PackingSequence.PackingSequence[Index2] == attr.AssignedBin2) return true; } else if (Index1 == attr.Index1 && Index2 == attr.Index2)// && PackingSequence.PackingSequence[Index1] == attr.AssignedBin1 && PackingSequence.PackingSequence[Index2] == attr.AssignedBin2) return true; } return false; } public override string ToString() { return "SGM(i1="+Index1 + ", i2=" + Index2 +")"; } } }