Changeset 2913 for trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SinglePointCrossover.cs
- Timestamp:
- 03/02/10 16:48:24 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SinglePointCrossover.cs
r2900 r2913 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Text;25 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 26 27 27 namespace HeuristicLab.Encodings.RealVector { 28 28 /// <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. 30 32 /// </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 { 37 39 /// <summary> 38 /// Performs a single point crossover at a randomly chosen position of the two39 /// 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. 40 42 /// </summary> 43 /// <exception cref="ArgumentException">Thrown when the length of the vector is not the same in both parents.</exception> 41 44 /// <param name="random">A random number generator.</param> 42 45 /// <param name="parent1">The first parent for crossover.</param> 43 46 /// <param name="parent2">The second parent for crossover.</param> 44 47 /// <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"); 46 50 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); 49 53 50 54 for (int i = 0; i < breakPoint; i++) … … 57 61 58 62 /// <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)"/>. 60 64 /// </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"); 68 70 return Apply(random, parents[0], parents[1]); 69 71 }
Note: See TracChangeset
for help on using the changeset viewer.