Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/02/10 16:48:24 (14 years ago)
Author:
abeham
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SinglePointCrossover.cs

    r2900 r2913  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
     24using HeuristicLab.Data;
     25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2626
    2727namespace HeuristicLab.Encodings.RealVector {
    2828  /// <summary>
    29   /// Single point crossover for real vectors.
     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.
    3032  /// </summary>
    31   public class SinglePointCrossover : RealVectorCrossoverBase {
    32     /// <inheritdoc select="summary"/>
    33     public override string Description {
    34       get { return "Single point crossover for real vectors."; }
    35     }
    36 
     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 {
    3739    /// <summary>
    38     /// Performs a single point crossover at a randomly chosen position of the two
    39     /// given parent real vectors.
     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.
    4042    /// </summary>
     43    /// <exception cref="ArgumentException">Thrown when the length of the vector is not the same in both parents.</exception>
    4144    /// <param name="random">A random number generator.</param>
    4245    /// <param name="parent1">The first parent for crossover.</param>
    4346    /// <param name="parent2">The second parent for crossover.</param>
    4447    /// <returns>The newly created real vector, resulting from the single point crossover.</returns>
    45     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
     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");
    4650      int length = parent1.Length;
    47       double[] result = new double[length];
    48       int breakPoint = random.Next(1, length);
     51      DoubleArrayData result = new DoubleArrayData(length);
     52      int breakPoint = random.Next(1, length - 1);
    4953
    5054      for (int i = 0; i < breakPoint; i++)
     
    5761
    5862    /// <summary>
    59     /// Performs a single point crossover operation for two given parent real vectors.
     63    /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData)"/>.
    6064    /// </summary>
    61     /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    62     /// <param name="scope">The current scope.</param>
    63     /// <param name="random">A random number generator.</param>
    64     /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
    65     /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    66     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    67       if (parents.Length != 2) throw new InvalidOperationException("ERROR in SinglePointCrossover: The number of parents is not equal to 2");
     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");
    6870      return Apply(random, parents[0], parents[1]);
    6971    }
Note: See TracChangeset for help on using the changeset viewer.