#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.GroupingVector { [Item("Grouping Vector Swap2 Manipulator", "An operator which manipulates a PackingSequence representation.")] [StorableClass] public class GroupingVectorSwap2Manipulator : GroupingVectorManipulator { [StorableConstructor] protected GroupingVectorSwap2Manipulator (bool deserializing) : base(deserializing) { } protected GroupingVectorSwap2Manipulator(GroupingVectorSwap2Manipulator original, Cloner cloner) : base(original, cloner) { } public GroupingVectorSwap2Manipulator() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new GroupingVectorSwap2Manipulator(this, cloner); } protected override void Manipulate(IRandom random, GroupingVectorEncoding individual) { int index1 = 0; int index2 = 1; if (individual.GroupingVector.Length <= 1) return; else if (individual.GroupingVector.Length != 2) { index1 = random.Next(individual.GroupingVector.Length); index2 = random.Next(individual.GroupingVector.Length); while (index2.Equals(index1)) { index2 = random.Next(individual.GroupingVector.Length); } } int temp = individual.GroupingVector [index1]; individual.GroupingVector[index1] = individual.GroupingVector[index2]; individual.GroupingVector[index2] = temp; } } }