#region License Information /* HeuristicLab * Copyright (C) 2002-2019 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 { /// /// The local crossover for real vectors is similar to the , but where the factor alpha is chosen randomly in the interval [0;1) for each position. /// /// /// It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 194. /// [Item("LocalCrossover", @"The local crossover is similar to the arithmetic all positions crossover, but uses a random alpha for each position x = alpha * p1 + (1-alpha) * p2. It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL., p. 194.")] [StorableType("FB601681-7738-4380-889F-44A7CFA1D38F")] public class LocalCrossover : RealVectorCrossover { [StorableConstructor] protected LocalCrossover(StorableConstructorFlag _) : base(_) { } protected LocalCrossover(LocalCrossover original, Cloner cloner) : base(original, cloner) { } public LocalCrossover() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new LocalCrossover(this, cloner); } /// /// Performs a local crossover on the two given parent vectors. /// /// Thrown when two parents are not of the same length. /// The random number generator. /// The first parent for the crossover operation. /// The second parent for the crossover operation. /// The newly created real vector, resulting from the local crossover. public static RealVector Apply(IRandom random, RealVector parent1, RealVector parent2) { if (parent1.Length != parent2.Length) throw new ArgumentException("LocalCrossover: the two parents are not of the same length"); double factor; int length = parent1.Length; double[] result = new double[length]; for (int i = 0; i < length; i++) { factor = random.NextDouble(); result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]); } return new RealVector(result); } /// /// Performs a local crossover operation for two given parent real vectors. /// /// Thrown if there are not exactly two parents. /// 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 RealVector Cross(IRandom random, ItemArray parents) { if (parents.Length != 2) throw new ArgumentException("LocalCrossover: The number of parents is not equal to 2"); return Apply(random, parents[0], parents[1]); } } }