Changeset 7686 for branches/IntegerVectorEncoding/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/DiscreteCrossover.cs
- Timestamp:
- 04/02/12 16:45:39 (12 years ago)
- Location:
- branches/IntegerVectorEncoding
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/IntegerVectorEncoding
-
Property
svn:ignore
set to
*.suo
-
Property
svn:ignore
set to
-
branches/IntegerVectorEncoding/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/DiscreteCrossover.cs
r7259 r7686 45 45 46 46 /// <summary> 47 /// Performs a discrete crossover operation of the twogiven parents.47 /// Performs a discrete crossover operation of any number of given parents. 48 48 /// </summary> 49 49 /// <exception cref="ArgumentException">Thrown when the vectors of the parents are of different length.</exception> 50 50 /// <param name="random">A random number generator.</param> 51 /// <param name="parent1">The first parent for the crossover operation.</param> 52 /// <param name="parent2">The second parent for the crossover operation.</param> 51 /// <param name="parents">The list of parents for the crossover operation.</param> 53 52 /// <returns>The newly created integer vector, resulting from the crossover operation.</returns> 54 public static IntegerVector Apply(IRandom random, IntegerVector parent1, IntegerVector parent2) { 55 if (parent1.Length != parent2.Length) 56 throw new ArgumentException("DiscreteCrossover: The parents are of different length."); 53 public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents) { 54 int length = parents[0].Length; 57 55 58 int length = parent1.Length; 59 int[] result = new int[length]; 56 for (int i = 0; i < parents.Length; i++) { 57 if (parents[i].Length != length) 58 throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents"); 59 } 60 60 61 var result = new IntegerVector(length); 61 62 for (int i = 0; i < length; i++) { 62 if (random.NextDouble() < 0.5) 63 result[i] = parent1[i]; 64 else 65 result[i] = parent2[i]; 63 result[i] = parents[random.Next(parents.Length)][i]; 66 64 } 67 return new IntegerVector(result); 65 66 return result; 68 67 } 69 68 70 69 /// <summary> 71 /// Performs a discrete crossover operation for twogiven parent integer vectors.70 /// Performs a discrete crossover operation for any number of given parent integer vectors. 72 71 /// </summary> 73 /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception>74 72 /// <param name="random">A random number generator.</param> 75 /// <param name="parents">An array containing the twointeger vectors that should be crossed.</param>73 /// <param name="parents">An array containing integer vectors that should be crossed.</param> 76 74 /// <returns>The newly created integer vector, resulting from the crossover operation.</returns> 77 75 protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents) { 78 if (parents.Length != 2) throw new ArgumentException("ERROR in DiscreteCrossover: The number of parents is not equal to 2"); 79 return Apply(random, parents[0], parents[1]); 76 return Apply(random, parents); 80 77 } 81 78 }
Note: See TracChangeset
for help on using the changeset viewer.