#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.Data; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.IntegerVectorEncoding { /// /// The rounded local crossover for integer vectors is similar to the , but where the factor alpha is chosen randomly in the interval [0;1) for each position. /// cribed in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 194. /// [Item("RoundedLocalCrossover", @"The runded local crossover is similar to the arithmetic crossover, but uses a random alpha for each position x = alpha * p1 + (1-alpha) * p2.")] [StorableClass] public class RoundedLocalCrossover : BoundedIntegerVectorCrossover { [StorableConstructor] protected RoundedLocalCrossover(bool deserializing) : base(deserializing) { } protected RoundedLocalCrossover(RoundedLocalCrossover original, Cloner cloner) : base(original, cloner) { } public RoundedLocalCrossover() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new RoundedLocalCrossover(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 integer vector, resulting from the local crossover. public static IntegerVector Apply(IRandom random, IntegerVector parent1, IntegerVector parent2, IntMatrix bounds) { if (parent1.Length != parent2.Length) throw new ArgumentException("RoundedLocalCrossover: the two parents are not of the same length"); double factor; int length = parent1.Length; var result = new IntegerVector(length); int min, max, step = 1; for (int i = 0; i < length; i++) { min = bounds[i % bounds.Rows, 0]; max = bounds[i % bounds.Rows, 1]; if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2]; max = FloorFeasible(min, max, step, max - 1); factor = random.NextDouble(); result[i] = RoundFeasible(min, max, step, (factor * parent1[i]) + ((1 - factor) * parent2[i])); } return result; } /// /// Performs a local crossover operation for two given parent integer 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 bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors). /// The newly created integer vector, resulting from the crossover operation. protected override IntegerVector CrossBounded(IRandom random, ItemArray parents, IntMatrix bounds) { if (parents.Length != 2) throw new ArgumentException("RoundedLocalCrossover: The number of parents is not equal to 2"); return Apply(random, parents[0], parents[1], bounds); } } }