- Timestamp:
- 02/16/09 01:21:53 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/ContinuousCrossover.cs
r1184 r1218 28 28 namespace HeuristicLab.RealVector { 29 29 /// <summary> 30 /// Continuous crossover for real vectors: for each element randomlyeither the element of the first31 /// parent or the average of both parents is selected.30 /// Continuous crossover for real vectors: for each element either the element of the first 31 /// parent or the average of all parents is selected randomly. 32 32 /// </summary> 33 33 public class ContinuousCrossover : RealVectorCrossoverBase { … … 38 38 39 39 /// <summary> 40 /// Performs a continuous crossover of the twogiven real vectors.40 /// Performs a continuous crossover of multiple given real vectors. 41 41 /// </summary> 42 42 /// <param name="random">The random number generator.</param> 43 /// <param name="parent1">The first parent for the crossover.</param> 44 /// <param name="parent2">The second parent for the crossover.</param> 43 /// <param name="parents">An array containing the parents that should be crossed.</param> 45 44 /// <returns>The newly created real vector, resulting from the continuous crossover.</returns> 46 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {47 int length = parent 1.Length;45 public static double[] Apply(IRandom random, double[][] parents) { 46 int length = parents[0].Length; 48 47 double[] result = new double[length]; 49 48 50 49 for (int i = 0; i < length; i++) { 51 50 if (random.NextDouble() < 0.5) { 52 result[i] = (parent1[i] + parent2[i]) / 2; 51 double sum = 0.0; 52 for (int j = 0; j < parents.Length; j++) 53 sum += parents[j][i]; 54 result[i] = sum / parents.Length; 53 55 } else { 54 result[i] = parent 1[i];56 result[i] = parents[0][i]; 55 57 } 56 58 } … … 59 61 60 62 /// <summary> 61 /// Performs a continuous crossover o f the two givenreal vectors.63 /// Performs a continuous crossover operation for multiple given parent real vectors. 62 64 /// </summary> 63 /// < remarks>Calls <see cref="Apply"/>.</remarks>65 /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception> 64 66 /// <param name="scope">The current scope.</param> 65 /// <param name="random"> Therandom number generator.</param>66 /// <param name="parent 1">The first parent for the crossover.</param>67 /// < param name="parent2">The second parent for the crossover.</param>68 /// <returns>The newly created real vector, resulting from the continuous crossover.</returns>69 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {70 return Apply(random, parent 1, parent2);67 /// <param name="random">A random number generator.</param> 68 /// <param name="parents">An array containing the real vectors that should be crossed.</param> 69 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 70 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 71 if (parents.Length < 2) throw new InvalidOperationException("ERROR in ContinuousCrossover: The number of parents is less than 2"); 72 return Apply(random, parents); 71 73 } 72 74 }
Note: See TracChangeset
for help on using the changeset viewer.