#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.PackingEncoding.MultiComponentVector { [Item("ThreeWayMultiComponentVectorManipulator", "An operator which manipulates a PackingSequence representation.")] [StorableClass] public class ThreeWayMultiComponentVectorManipulator : MultiComponentVectorManipulator { [StorableConstructor] protected ThreeWayMultiComponentVectorManipulator(bool deserializing) : base(deserializing) { } protected ThreeWayMultiComponentVectorManipulator(ThreeWayMultiComponentVectorManipulator original, Cloner cloner) : base(original, cloner) { } public ThreeWayMultiComponentVectorManipulator() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new ThreeWayMultiComponentVectorManipulator(this, cloner); } protected override void Manipulate(IRandom random, MultiComponentVectorEncoding individual) { int manipulatorDecision = random.Next(0, 3); int affectedBin = 0; do { affectedBin = random.Next(individual.NrOfBins); } while (individual.PackingInformations[affectedBin].Count <= 0); int affectedIndex = random.Next (individual.PackingInformations[affectedBin].Count); //Randomly rotate one item if (manipulatorDecision == 0) { individual.PackingInformations = SingleItemRotationMove.GetVectorAfterMove(individual, affectedBin, affectedIndex).PackingInformations; } //Randomly change the bin-assignment for one item else if (manipulatorDecision == 1) { int targetBin = 0; if (individual.NrOfBins > 1) do { targetBin = random.Next(individual.NrOfBins); } while (targetBin == affectedBin); individual.PackingInformations = SingleGroupingMove.GetVectorAfterMove(individual, affectedBin, affectedIndex, targetBin).PackingInformations; } //Swap the sequence of two items assigned to the same bin else if (manipulatorDecision == 2) { int targetIndex = 0; int nrOfItems = individual.PackingInformations[affectedBin].Count; if (nrOfItems > 1) do { targetIndex = random.Next(nrOfItems); } while (targetIndex == affectedIndex); individual.PackingInformations = ChangePositionMove.GetVectorAfterMove(individual, affectedBin, affectedIndex, targetIndex).PackingInformations; } } } }