#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.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.PermutationEncoding {
///
/// Performs a cross over permutation between two permutation arrays based on randomly chosen positions.
///
///
/// It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.
///
[Item("PositionBasedCrossover", "An operator which performs the position based crossover on two permutations. It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.")]
[StorableClass]
public class PositionBasedCrossover : PermutationCrossover {
[StorableConstructor]
protected PositionBasedCrossover(bool deserializing) : base(deserializing) { }
protected PositionBasedCrossover(PositionBasedCrossover original, Cloner cloner) : base(original, cloner) { }
public PositionBasedCrossover() : base() { }
public override IDeepCloneable Clone(Cloner cloner) {
return new PositionBasedCrossover(this, cloner);
}
///
/// Performs a cross over permutation of and
/// based on randomly chosen positions to define which position to take from where.
///
/// Thrown when and are not of equal length.
/// The random number generator.
/// First parent
/// Second Parent
/// Child
public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
if (parent1.Length != parent2.Length) throw new ArgumentException("PositionBasedCrossover: The parent permutations are of unequal length.");
int length = parent1.Length;
int[] result = new int[length];
bool[] randomPosition = new bool[length];
bool[] numberCopied = new bool[length];
int randomPosNumber = random.Next(length);
for (int i = 0; i < randomPosNumber; i++) { // generate random bit mask
randomPosition[random.Next(length)] = true;
}
for (int i = 0; i < length; i++) { // copy numbers masked as true from second permutation
if (randomPosition[i]) {
result[i] = parent2[i];
numberCopied[parent2[i]] = true;
}
}
int index = 0;
for (int i = 0; i < length; i++) { // copy numbers masked as false from first permutation
if (!numberCopied[parent1[i]]) {
if (randomPosition[index]) {
while (randomPosition[index]) {
index++;
}
}
result[index] = parent1[i];
index++;
}
}
return new Permutation(parent1.PermutationType, result);
}
///
/// Checks number of parents and calls .
///
/// Thrown if there are not exactly two parents.
/// A random number generator.
/// An array containing the two permutations that should be crossed.
/// The newly created permutation, resulting from the crossover operation.
protected override Permutation Cross(IRandom random, ItemArray parents) {
if (parents.Length != 2) throw new InvalidOperationException("ERROR in PositionBasedCrossover: The number of parents is not equal to 2");
return Apply(random, parents[0], parents[1]);
}
}
}