#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.RealVectorEncoding {
///
/// The average crossover (intermediate recombination) calculates the average or centroid of a number of parent vectors.
///
///
/// It is implemented as described by Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.
///
[Item("AverageCrossover", "The average crossover (intermediate recombination) produces a new offspring by calculating in each position the average of a number of parents. It is implemented as described by Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.")]
[StorableClass]
public class AverageCrossover : RealVectorCrossover {
[StorableConstructor]
protected AverageCrossover(bool deserializing) : base(deserializing) { }
protected AverageCrossover(AverageCrossover original, Cloner cloner) : base(original, cloner) { }
public AverageCrossover() : base() { }
public override IDeepCloneable Clone(Cloner cloner) {
return new AverageCrossover(this, cloner);
}
///
/// Performs the average crossover (intermediate recombination) on a list of parents.
///
/// Thrown when there is just one parent or when the parent vectors are of different length.
///
/// There can be more than two parents.
///
/// The random number generator.
/// The list of parents.
/// The child vector (average) of the parents.
public static RealVector Apply(IRandom random, ItemArray parents) {
int length = parents[0].Length, parentsCount = parents.Length;
if (parents.Length < 2) throw new ArgumentException("AverageCrossover: The number of parents is less than 2.", "parents");
RealVector result = new RealVector(length);
try {
double avg;
for (int i = 0; i < length; i++) {
avg = 0;
for (int j = 0; j < parentsCount; j++)
avg += parents[j][i];
result[i] = avg / (double)parentsCount;
}
}
catch (IndexOutOfRangeException) {
throw new ArgumentException("AverageCrossover: The parents' vectors are of different length.", "parents");
}
return result;
}
///
/// Forwards the call to .
///
/// The random number generator.
/// The list of parents.
/// The child vector (average) of the parents.
protected override RealVector Cross(IRandom random, ItemArray parents) {
return Apply(random, parents);
}
}
}