Changeset 2823
- Timestamp:
- 02/17/10 20:24:23 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Permutation/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Permutation/3.3/MaximalPreservativeCrossover.cs
r2820 r2823 41 41 /// </summary> 42 42 /// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation array are not in the range [0,N) with N = length of the permutation.</exception> 43 /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length or when the permutations are shorter than 4 elements.</exception> 43 44 /// <remarks> 44 45 /// First one segment is copied from the first parent to the offspring in the same position. -
trunk/sources/HeuristicLab.Permutation/3.3/PartiallyMatchedCrossover.cs
r2820 r2823 40 40 /// Performs the partially matched crossover on <paramref name="parent1"/> and <paramref name="parent2"/>. 41 41 /// </summary> 42 /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length or when the permutations are shorter than 4 elements.</exception> 42 43 /// <remarks> 43 44 /// First a segment from the first parent is copied to the offspring. … … 51 52 /// <returns>The new permutation resulting from the crossover.</returns> 52 53 public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) { 54 if (parent1.Length != parent2.Length) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutations are of unequal length"); 55 if (parent1.Length < 4) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutation must be at least of size 4"); 53 56 int length = parent1.Length; 54 Permutation result = new Permutation(length); 55 bool[] numbersCopied = new bool[length];57 Permutation result = new Permutation(length); ; 58 int[] invResult = new int[length]; 56 59 57 int breakPoint1 = random.Next(length - 1); 58 int breakPoint2 = random.Next(breakPoint1 + 1, length); 60 int breakPoint1, breakPoint2; 61 do { 62 breakPoint1 = random.Next(length - 1); 63 breakPoint2 = random.Next(breakPoint1 + 1, length); 64 } while (breakPoint2 - breakPoint1 >= length - 2); // prevent the case [0,length-1) -> clone of parent1 59 65 60 for (int j = breakPoint1; j <= breakPoint2; j++) { // copy part of first permutation 61 result[j] = parent1[j]; 62 numbersCopied[result[j]] = true; 66 // clone parent2 and calculate inverse permutation (number -> index) 67 for (int j = 0; j < length; j++) { 68 result[j] = parent2[j]; 69 invResult[result[j]] = j; 63 70 } 64 71 65 // calculate the inverse permutation (number -> index) of parent1 66 int[] invParent1 = new int[length]; 67 for (int j = 0; j < length; j++) { 68 invParent1[parent1[j]] = j; 72 for (int j = breakPoint1; j <= breakPoint2; j++) { 73 int orig = result[j]; // save the former value 74 result[j] = parent1[j]; // overwrite the former value with the new value 75 int index = invResult[result[j]]; // look where the new value is in the child 76 result[index] = orig; // write the former value to this position 77 invResult[orig] = index; // update the inverse mapping 78 // it's not necessary to do 'invResult[result[j]] = j' as this will not be needed again 69 79 } 70 80 71 int i = ((breakPoint1 == 0) ? (breakPoint2 + 1) : (0));72 while (i < length) { // copy rest from second permutation73 if (!numbersCopied[parent2[i]]) { // copy directly74 result[i] = parent2[i];75 } else { // copy from area between breakpoints76 int index = invParent1[parent2[i]]; // find the index of the corresponding occupied number in parent177 result[i] = parent2[index]; // use that index to take the number from parent278 }79 80 i++;81 if (i == breakPoint1) { // skip area between breakpoints82 i = breakPoint2 + 1;83 }84 }85 81 return result; 86 82 }
Note: See TracChangeset
for help on using the changeset viewer.