Changeset 1218 for trunk/sources/HeuristicLab.IntVector
- Timestamp:
- 02/16/09 01:21:53 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.IntVector
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.IntVector/DiscreteCrossover.cs
r1157 r1218 56 56 57 57 /// <summary> 58 /// Performs a discrete crossover operation of the two given parents.58 /// Performs a discrete crossover operation for two given parent integer vectors. 59 59 /// </summary> 60 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 60 61 /// <param name="scope">The current scope.</param> 61 62 /// <param name="random">A random number generator.</param> 62 /// <param name="parent1">The first parent for the crossover operation.</param> 63 /// <param name="parent2">The second parent for the crossover operation.</param> 63 /// <param name="parents">An array containing the two integer vectors that should be crossed.</param> 64 64 /// <returns>The newly created integer vector, resulting from the crossover operation.</returns> 65 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) { 66 return Apply(random, parent1, parent2); 65 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 66 if (parents.Length != 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is not equal to 2"); 67 return Apply(random, parents[0], parents[1]); 67 68 } 68 69 } -
trunk/sources/HeuristicLab.IntVector/IntVectorCrossoverBase.cs
r1157 r1218 42 42 43 43 /// <summary> 44 /// Performs a crossover of two given parents. 44 /// Performs a crossover by calling <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom, int[][]"/> 45 /// and adds the created integer vector to the current scope. 46 /// </summary> 47 /// <exception cref="InvalidOperationException">Thrown if the parents have different lengths.</exception> 48 /// <param name="scope">The current scope which represents a new child.</param> 49 /// <param name="random">A random number generator.</param> 50 protected sealed override void Cross(IScope scope, IRandom random) { 51 int[][] parents = new int[scope.SubScopes.Count][]; 52 int length = -1; 53 for (int i = 0; i < scope.SubScopes.Count; i++) { 54 parents[i] = scope.SubScopes[i].GetVariableValue<IntArrayData>("IntVector", false).Data; 55 if (i == 0) length = parents[i].Length; 56 else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in IntVectorCrossoverBase: Cannot apply crossover to integer vectors of different length"); 57 } 58 59 int[] result = Cross(scope, random, parents); 60 scope.AddVariable(new Variable(scope.TranslateName("IntVector"), new IntArrayData(result))); 61 } 62 63 /// <summary> 64 /// Performs a crossover of multiple integer vectors. 45 65 /// </summary> 46 66 /// <param name="scope">The current scope.</param> 47 67 /// <param name="random">A random number generator.</param> 48 /// <param name="parent1">The first parent for crossover.</param> 49 /// <param name="parent2">The second parent for crossover.</param> 50 /// <param name="child">The resulting child scope.</param> 51 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) { 52 IVariableInfo intVectorInfo = GetVariableInfo("IntVector"); 53 IntArrayData vector1 = parent1.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false); 54 IntArrayData vector2 = parent2.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false); 55 56 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to integer vectors of different length."); 57 58 int[] result = Cross(scope, random, vector1.Data, vector2.Data); 59 child.AddVariable(new Variable(child.TranslateName(intVectorInfo.FormalName), new IntArrayData(result))); 60 } 61 62 /// <summary> 63 /// Performs a crossover of two given parents. 64 /// </summary> 65 /// <param name="scope">The current scope.</param> 66 /// <param name="random">A random number generator.</param> 67 /// <param name="parent1">The first parent for crossover.</param> 68 /// <param name="parent2">The second parent for crossover.</param> 68 /// <param name="parents">An array containing all parent integer vectors.</param> 69 69 /// <returns>The newly created integer vector, resulting from the crossover operation.</returns> 70 protected abstract int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2);70 protected abstract int[] Cross(IScope scope, IRandom random, int[][] parents); 71 71 } 72 72 } -
trunk/sources/HeuristicLab.IntVector/SinglePointCrossover.cs
r1157 r1218 57 57 58 58 /// <summary> 59 /// Performs a single point crossover at a randomly chosen position of t he two59 /// Performs a single point crossover at a randomly chosen position of two 60 60 /// given parent integer vectors. 61 61 /// </summary> 62 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 62 63 /// <param name="scope">The current scope.</param> 63 64 /// <param name="random">A random number generator.</param> 64 /// <param name="parent1">The first parent for crossover.</param> 65 /// <param name="parent2">The second parent for crossover.</param> 65 /// <param name="parents">An array containing the two integer vectors that should be crossed.</param> 66 66 /// <returns>The newly created integer vector, resulting from the single point crossover.</returns> 67 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) { 68 return Apply(random, parent1, parent2); 67 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 68 if (parents.Length != 2) throw new InvalidOperationException("ERROR in SinglePointCrossover: The number of parents is not equal to 2"); 69 return Apply(random, parents[0], parents[1]); 69 70 } 70 71 }
Note: See TracChangeset
for help on using the changeset viewer.