[2900] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2900] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2900] | 24 | using HeuristicLab.Core;
|
---|
[16565] | 25 | using HEAL.Attic;
|
---|
[2900] | 26 |
|
---|
[3053] | 27 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[2900] | 28 | /// <summary>
|
---|
[2913] | 29 | /// Single point crossover for real vectors. The implementation is similar to the single point crossover for binary vectors.
|
---|
| 30 | /// After a breakpoint is randomly chosen in the interval [1,N-1) with N = length of the vector, the first part is copied from parent1 the other part copied from parent2.
|
---|
| 31 | /// The interval is chosen such that at least one position is taken from a different parent.
|
---|
[2900] | 32 | /// </summary>
|
---|
[2913] | 33 | /// <remarks>
|
---|
| 34 | /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
|
---|
| 35 | /// </remarks>
|
---|
| 36 | [Item("SinglePointCrossover", "Breaks both parent chromosomes at a randomly chosen point and assembles a child by taking one part of the first parent and the other part of the second pard. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
|
---|
[16565] | 37 | [StorableType("008449CF-6CB0-481D-855A-80DA1F2DFC7E")]
|
---|
[2913] | 38 | public class SinglePointCrossover : RealVectorCrossover {
|
---|
[4722] | 39 | [StorableConstructor]
|
---|
[16565] | 40 | protected SinglePointCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 41 | protected SinglePointCrossover(SinglePointCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 42 | public SinglePointCrossover() : base() { }
|
---|
| 43 |
|
---|
| 44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 45 | return new SinglePointCrossover(this, cloner);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[2900] | 48 | /// <summary>
|
---|
[2913] | 49 | /// Performs the single point crossover for real vectors. The implementation is similar to the single point crossover for binary vectors.
|
---|
| 50 | /// After a breakpoint is randomly chosen in the interval [1,N-1) with N = length of the vector, the first part is copied from parent1 the other part copied from parent2.
|
---|
[2900] | 51 | /// </summary>
|
---|
[2913] | 52 | /// <exception cref="ArgumentException">Thrown when the length of the vector is not the same in both parents.</exception>
|
---|
[2900] | 53 | /// <param name="random">A random number generator.</param>
|
---|
| 54 | /// <param name="parent1">The first parent for crossover.</param>
|
---|
| 55 | /// <param name="parent2">The second parent for crossover.</param>
|
---|
| 56 | /// <returns>The newly created real vector, resulting from the single point crossover.</returns>
|
---|
[3060] | 57 | public static RealVector Apply(IRandom random, RealVector parent1, RealVector parent2) {
|
---|
[2913] | 58 | if (parent1.Length != parent2.Length) throw new ArgumentException("SinglePointCrossover: Parents are of unequal length");
|
---|
[3675] | 59 | if (parent1.Length < 2) throw new ArgumentException("SinglePointCrossover: Cannot be applied to vectors with just one dimension.");
|
---|
[2900] | 60 | int length = parent1.Length;
|
---|
[3060] | 61 | RealVector result = new RealVector(length);
|
---|
[3675] | 62 | int breakPoint;
|
---|
| 63 | if (length == 2) breakPoint = 1;
|
---|
| 64 | else breakPoint = random.Next(1, length - 1);
|
---|
[2900] | 65 |
|
---|
| 66 | for (int i = 0; i < breakPoint; i++)
|
---|
| 67 | result[i] = parent1[i];
|
---|
| 68 | for (int i = breakPoint; i < length; i++)
|
---|
| 69 | result[i] = parent2[i];
|
---|
| 70 |
|
---|
| 71 | return result;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /// <summary>
|
---|
[3060] | 75 | /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, RealVector, RealVector)"/>.
|
---|
[2936] | 76 | /// </summary>
|
---|
| 77 | /// <exception cref="ArgumentException">Thrown when the parents' vectors are of unequal length or when <paramref name="contiguity"/> is smaller than 0.</exception>
|
---|
[2913] | 78 | /// <param name="random">The pseudo random number generator to use.</param>
|
---|
| 79 | /// <param name="parents">The list of parents.</param>
|
---|
| 80 | /// <returns>A new real vector.</returns>
|
---|
[3060] | 81 | protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
|
---|
[2936] | 82 | if (parents.Length != 2) throw new ArgumentException("SinglePointCrossover: The number of parents is not equal to 2");
|
---|
[2900] | 83 | return Apply(random, parents[0], parents[1]);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|