using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data.MoveVectorData; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System; namespace HeuristicLab.Encodings.MoveVectorEncoding.Crossovers { [Item("SinglePointCrossover", "Single point crossover for move vectors.")] [StorableClass] public class SinglePointCrossover : MoveVectorCrossover { [StorableConstructor] protected SinglePointCrossover(bool deserializing) : base(deserializing) { } protected SinglePointCrossover(SinglePointCrossover original, Cloner cloner) : base(original, cloner) { } public SinglePointCrossover() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new SinglePointCrossover(this, cloner); } public static MoveVector Apply(IRandom random, MoveVector parent1, MoveVector parent2) { if (parent1.Length != parent2.Length) throw new ArgumentException("SinglePointCrossover: The parents are of different length."); int length = parent1.Length; MoveVector result = new MoveVector(length, parent1.MoveTypes); int breakPoint = random.Next(1, length); for (int i = 0; i < breakPoint; i++) result[i] = parent1[i]; for (int i = breakPoint; i < length; i++) result[i] = parent2[i]; return result; } protected override MoveVector Cross(IRandom random, ItemArray parents) { if (parents.Length != 2) throw new ArgumentException("ERROR in SinglePointCrossover: The number of parents is not equal to 2"); return Apply(random, parents[0], parents[1]); } } }