Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SinglePointCrossover.cs @ 2928

Last change on this file since 2928 was 2913, checked in by abeham, 14 years ago

checked and corrected (where necessary) BLX-a, BLX-a-b, as well the SinglePointCrossover
checked the UniformOnePositionManipulator
fixed a copy-paste oversight in RealVectorManipulator
#890

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.RealVector {
28  /// <summary>
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.
32  /// </summary>
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.")]
37  [EmptyStorableClass]
38  public class SinglePointCrossover : RealVectorCrossover {
39    /// <summary>
40    /// Performs the single point crossover for real vectors. The implementation is similar to the single point crossover for binary vectors.
41    /// 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.
42    /// </summary>
43    /// <exception cref="ArgumentException">Thrown when the length of the vector is not the same in both parents.</exception>
44    /// <param name="random">A random number generator.</param>
45    /// <param name="parent1">The first parent for crossover.</param>
46    /// <param name="parent2">The second parent for crossover.</param>
47    /// <returns>The newly created real vector, resulting from the single point crossover.</returns>
48    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2) {
49      if (parent1.Length != parent2.Length) throw new ArgumentException("SinglePointCrossover: Parents are of unequal length");
50      int length = parent1.Length;
51      DoubleArrayData result = new DoubleArrayData(length);
52      int breakPoint = random.Next(1, length - 1);
53
54      for (int i = 0; i < breakPoint; i++)
55        result[i] = parent1[i];
56      for (int i = breakPoint; i < length; i++)
57        result[i] = parent2[i];
58
59      return result;
60    }
61
62    /// <summary>
63    /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData)"/>.
64    /// </summary>
65    /// <param name="random">The pseudo random number generator to use.</param>
66    /// <param name="parents">The list of parents.</param>
67    /// <returns>A new real vector.</returns>
68    protected override HeuristicLab.Data.DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
69      if (parents.Length != 2) throw new InvalidOperationException("SinglePointCrossover: The number of parents is not equal to 2");
70      return Apply(random, parents[0], parents[1]);
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.