Changeset 1218 for trunk/sources/HeuristicLab.BitVector
- Timestamp:
- 02/16/09 01:21:53 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.BitVector
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.BitVector/BitVectorCrossoverBase.cs
r1176 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, bool[][]"/> 45 /// and adds the created bit vector to the current scope. 45 46 /// </summary> 46 /// <exception cref="InvalidOperationException">Thrown whenthe parents have different lengths.</exception>47 /// <param name="scope">The current scope .</param>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> 48 49 /// <param name="random">A random number generator.</param> 49 /// <param name="parent1">The first parent for crossover.</param> 50 /// <param name="parent2">The second parent for crossover.</param> 51 /// <param name="child">The resulting child scope.</param> 52 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) { 53 IVariableInfo bitVectorInfo = GetVariableInfo("BitVector"); 54 BoolArrayData vector1 = parent1.GetVariableValue<BoolArrayData>(bitVectorInfo.FormalName, false); 55 BoolArrayData vector2 = parent2.GetVariableValue<BoolArrayData>(bitVectorInfo.FormalName, false); 50 protected sealed override void Cross(IScope scope, IRandom random) { 51 bool[][] parents = new bool[scope.SubScopes.Count][]; 52 int length = -1; 53 for (int i = 0; i < scope.SubScopes.Count; i++) { 54 parents[i] = scope.SubScopes[i].GetVariableValue<BoolArrayData>("BitVector", false).Data; 55 if (i == 0) length = parents[i].Length; 56 else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in BitVectorCrossoverBase: Cannot apply crossover to bit vectors of different length"); 57 } 56 58 57 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to bit vectors of different length."); 58 59 bool[] result = Cross(scope, random, vector1.Data, vector2.Data); 60 child.AddVariable(new Variable(child.TranslateName(bitVectorInfo.FormalName), new BoolArrayData(result))); 59 bool[] result = Cross(scope, random, parents); 60 scope.AddVariable(new Variable(scope.TranslateName("BitVector"), new BoolArrayData(result))); 61 61 } 62 62 63 63 /// <summary> 64 /// Performs a crossover of two given parents.64 /// Performs a crossover of multiple bit vectors. 65 65 /// </summary> 66 66 /// <param name="scope">The current scope.</param> 67 67 /// <param name="random">A random number generator.</param> 68 /// <param name="parent1">The first parent for crossover.</param> 69 /// <param name="parent2">The second parent for crossover.</param> 68 /// <param name="parents">An array containing all parent bit vectors.</param> 70 69 /// <returns>The newly created bit vector, resulting from the crossover operation.</returns> 71 protected abstract bool[] Cross(IScope scope, IRandom random, bool[] parent1, bool[] parent2);70 protected abstract bool[] Cross(IScope scope, IRandom random, bool[][] parents); 72 71 } 73 72 } -
trunk/sources/HeuristicLab.BitVector/SinglePointCrossover.cs
r1176 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 bit 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 bit vectors that should be crossed.</param> 66 66 /// <returns>The newly created bit vector, resulting from the single point crossover.</returns> 67 protected override bool[] Cross(IScope scope, IRandom random, bool[] parent1, bool[] parent2) { 68 return Apply(random, parent1, parent2); 67 protected override bool[] Cross(IScope scope, IRandom random, bool[][] 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.