#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.IntegerVectorEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.BinPacking; namespace HeuristicLab.Encodings.PackingEncoding.GroupingVector { [Item("Swap Grouping Move", "A move on a grouping vector that is specified by two group-assignment-indexes.")] [StorableClass] public class SwapGroupingMove : GroupingMove, IPackingMove { [Storable] public int Index1 { get; protected set; } [Storable] public int Index2 { get; protected set; } [StorableConstructor] protected SwapGroupingMove(bool deserializing) : base(deserializing) { } protected SwapGroupingMove(SwapGroupingMove original, Cloner cloner) : base(original, cloner) { this.Index1 = original.Index1; this.Index2 = original.Index2; } public SwapGroupingMove(int index1, int index2, GroupingVectorEncoding groupingVector) : base(groupingVector) { Index1 = index1; Index2 = index2; } public override IDeepCloneable Clone(Cloner cloner) { return new SwapGroupingMove(this, cloner); } public override IPackingSolutionEncoding GetSolutionAfterMove() { GroupingVectorEncoding newSolution = new GroupingVectorEncoding(); newSolution.GroupingVector = new IntegerVector(GroupingVector.GroupingVector); newSolution.GroupingVector[Index1] = GroupingVector.GroupingVector[Index2]; newSolution.GroupingVector[Index2] = GroupingVector.GroupingVector[Index1]; return newSolution; } public override Type GetMoveAttributeType() { return typeof(SwapGroupingMoveAttribute); } public override GroupingMoveAttribute GetAttribute(double quality) { return new SwapGroupingMoveAttribute(Index1, Index2, GroupingVector.GroupingVector[Index1], GroupingVector.GroupingVector[Index2], quality); } public override bool BelongsToAttribute(GroupingMoveAttribute attribute, bool hardCriterion) { var attr = attribute as SwapGroupingMoveAttribute; if (attr != null) { if (hardCriterion) { if (Index1 == attr.ItemIndex1 || Index2 == attr.ItemIndex2)// || GroupingVector.GroupingVector[Index1] == attr.AssignedBin1 || GroupingVector.GroupingVector[Index2] == attr.AssignedBin2) return true; } else if (Index1 == attr.ItemIndex1 && Index2 == attr.ItemIndex2)// && GroupingVector.GroupingVector[Index1] == attr.AssignedBin1 && GroupingVector.GroupingVector[Index2] == attr.AssignedBin2) return true; } return false; } public override string ToString() { return "SGM(i1="+Index1 + ", i2=" + Index2 +")"; } } }