#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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.Data;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.BinaryVectorEncoding {
///
/// Single point crossover for binary vectors.
///
///
/// It is implemented based on the NPointCrossover
///
[Item("SinglePointCrossover", "Single point crossover for binary vectors. It is implemented based on the NPointCrossover.")]
[StorableClass]
public sealed class SinglePointCrossover : BinaryVectorCrossover {
[StorableConstructor]
private SinglePointCrossover(bool deserializing) : base(deserializing) { }
private SinglePointCrossover(SinglePointCrossover original, Cloner cloner) : base(original, cloner) { }
public SinglePointCrossover() : base() { }
public override IDeepCloneable Clone(Cloner cloner) {
return new SinglePointCrossover(this, cloner);
}
///
/// Performs a single point crossover at a randomly chosen position of two
/// given parent binary vectors.
///
/// Thrown if there are not exactly two parents.
/// A random number generator.
/// An array containing the two binary vectors that should be crossed.
/// The newly created binary vector, resulting from the single point crossover.
protected override BinaryVector 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 NPointCrossover.Apply(random, parents[0], parents[1], 1);
}
}
}