#region License Information
/* HeuristicLab
* Copyright (C) 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 HEAL.Attic;
namespace HeuristicLab.Encodings.RealVectorEncoding {
///
/// Single point crossover for real vectors. The implementation is similar to the single point crossover for binary vectors.
/// After a breakpoint is randomly chosen in the interval [1,N-1) with N = length of the vector, the first part is copied from parent1 the other part copied from parent2.
/// The interval is chosen such that at least one position is taken from a different parent.
///
///
/// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
///
[Item("SinglePointCrossover", "Breaks both parent chromosomes at a randomly chosen point and assembles a child by taking one part of the first parent and the other part of the second pard. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
[StorableType("008449CF-6CB0-481D-855A-80DA1F2DFC7E")]
public class SinglePointCrossover : RealVectorCrossover {
[StorableConstructor]
protected SinglePointCrossover(StorableConstructorFlag _) : base(_) { }
protected SinglePointCrossover(SinglePointCrossover original, Cloner cloner) : base(original, cloner) { }
public SinglePointCrossover() : base() { }
public override IDeepCloneable Clone(Cloner cloner) {
return new SinglePointCrossover(this, cloner);
}
///
/// Performs the single point crossover for real vectors. The implementation is similar to the single point crossover for binary vectors.
/// After a breakpoint is randomly chosen in the interval [1,N-1) with N = length of the vector, the first part is copied from parent1 the other part copied from parent2.
///
/// Thrown when the length of the vector is not the same in both parents.
/// A random number generator.
/// The first parent for crossover.
/// The second parent for crossover.
/// The newly created real vector, resulting from the single point crossover.
public static RealVector Apply(IRandom random, RealVector parent1, RealVector parent2) {
if (parent1.Length != parent2.Length) throw new ArgumentException("SinglePointCrossover: Parents are of unequal length");
if (parent1.Length < 2) throw new ArgumentException("SinglePointCrossover: Cannot be applied to vectors with just one dimension.");
int length = parent1.Length;
RealVector result = new RealVector(length);
int breakPoint;
if (length == 2) breakPoint = 1;
else breakPoint = random.Next(1, length - 1);
for (int i = 0; i < breakPoint; i++)
result[i] = parent1[i];
for (int i = breakPoint; i < length; i++)
result[i] = parent2[i];
return result;
}
///
/// Checks number of parents and forwards the call to .
///
/// Thrown when the parents' vectors are of unequal length or when is smaller than 0.
/// The pseudo random number generator to use.
/// The list of parents.
/// A new real vector.
protected override RealVector Cross(IRandom random, ItemArray parents) {
if (parents.Length != 2) throw new ArgumentException("SinglePointCrossover: The number of parents is not equal to 2");
return Apply(random, parents[0], parents[1]);
}
}
}