- Timestamp:
- 02/16/09 01:21:53 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/DiscreteCrossover.cs
r1184 r1218 27 27 namespace HeuristicLab.RealVector { 28 28 /// <summary> 29 /// Discrete crossover for real vectors: Selects randomly either the value of the first or the30 /// second parent.29 /// Discrete crossover for real vectors: For each position in the new vector an allele 30 /// of one of the parents is randomly selected. 31 31 /// </summary> 32 32 public class DiscreteCrossover : RealVectorCrossoverBase { 33 33 /// <inheritdoc select="summary"/> 34 34 public override string Description { 35 get { return "Discrete crossover for real vectors."; }35 get { return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent."; } 36 36 } 37 37 38 38 /// <summary> 39 /// Performs a discrete crossover operation o f the twogiven parents.39 /// Performs a discrete crossover operation on multiple given parents. 40 40 /// </summary> 41 41 /// <param name="random">A random number generator.</param> 42 /// <param name="parent1">The first parent for the crossover operation.</param> 43 /// <param name="parent2">The second parent for the crossover operation.</param> 42 /// <param name="parents">An array containing the parents that should be crossed.</param> 44 43 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 45 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {46 int length = parent 1.Length;44 public static double[] Apply(IRandom random, double[][] parents) { 45 int length = parents[0].Length; 47 46 double[] result = new double[length]; 48 49 47 for (int i = 0; i < length; i++) { 50 if (random.NextDouble() < 0.5) 51 result[i] = parent1[i]; 52 else 53 result[i] = parent2[i]; 48 result[i] = parents[random.Next(parents.Length)][i]; 54 49 } 55 50 return result; … … 57 52 58 53 /// <summary> 59 /// Performs a discrete crossover operation of the two given parents.54 /// Performs a discrete crossover operation for multiple given parent real vectors. 60 55 /// </summary> 56 /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception> 61 57 /// <param name="scope">The current scope.</param> 62 58 /// <param name="random">A random number generator.</param> 63 /// <param name="parent1">The first parent for the crossover operation.</param> 64 /// <param name="parent2">The second parent for the crossover operation.</param> 59 /// <param name="parents">An array containing the real vectors that should be crossed.</param> 65 60 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 66 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) { 67 return Apply(random, parent1, parent2); 61 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 62 if (parents.Length < 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is less than 2"); 63 return Apply(random, parents); 68 64 } 69 65 }
Note: See TracChangeset
for help on using the changeset viewer.