#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 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 System.Collections.Generic;
using System.Text;
using HeuristicLab.Core;
using HeuristicLab.Data;
namespace HeuristicLab.RealVector {
///
/// Blend alpha crossover for real vectors. Creates a new offspring by selecting a random value
/// from the interval between the two alleles of the parent solutions. The interval is increased
/// in both directions by the factor alpha.
///
public class BlendAlphaCrossover : RealVectorCrossoverBase {
///
public override string Description {
get { return
@"Blend alpha crossover for real vectors. Creates a new offspring by selecting a random value from the interval between the two alleles of the parent solutions. The interval is increased in both directions by the factor alpha.
Please use the operator BoundsChecker if necessary.";
}
}
///
/// Initializes a new instance of with one variable info (Alpha).
///
public BlendAlphaCrossover()
: base() {
VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
alphaVarInfo.Local = true;
AddVariableInfo(alphaVarInfo);
AddVariable(new Variable("Alpha", new DoubleData(0.5)));
}
///
/// Performs a blend alpha crossover of two real vectors.
///
/// The random number generator.
/// The first parent for the crossover operation.
/// The second parent for the crossover operation.
/// The alpha value for the crossover.
/// The newly created real vector resulting from the crossover operation.
public static double[] Apply(IRandom random, double[] parent1, double[] parent2, double alpha) {
int length = parent1.Length;
double[] result = new double[length];
double cMax = 0, cMin = 0, interval = 0, resMin = 0, resMax = 0;
for (int i = 0; i < length; i++) {
cMax = Math.Max(parent1[i], parent2[i]);
cMin = Math.Min(parent1[i], parent2[i]);
interval = Math.Abs(cMax - cMin);
resMin = cMin - interval * alpha;
resMax = cMax + interval * alpha;
result[i] = resMin + random.NextDouble() * Math.Abs(resMax - resMin);
}
return result;
}
///
/// Performs a blend alpha crossover operation for two given parent real vectors.
///
/// Thrown if there are not exactly two parents.
/// The current scope.
/// A random number generator.
/// An array containing the two real vectors that should be crossed.
/// The newly created real vector, resulting from the crossover operation.
protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaCrossover: The number of parents is not equal to 2");
double alpha = GetVariableValue("Alpha", scope, true).Data;
return Apply(random, parents[0], parents[1], alpha);
}
}
}