Changeset 2835 for trunk/sources/HeuristicLab.Permutation
- Timestamp:
- 02/19/10 09:32:47 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Permutation/3.3
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Permutation/3.3/CyclicCrossover.cs
r2830 r2835 39 39 /// </summary> 40 40 /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception> 41 /// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0 ,N) with N = length of the permutation.</exception>41 /// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0;N) with N = length of the permutation.</exception> 42 42 /// <remarks> 43 43 /// First this method randomly determines from which parent to start with equal probability. … … 52 52 /// <returns>The created cross over permutation as int array.</returns> 53 53 public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) { 54 if (parent1.Length != parent2.Length) throw new ArgumentException("CyclicCrossover: The parent permutations are of unequal length ");54 if (parent1.Length != parent2.Length) throw new ArgumentException("CyclicCrossover: The parent permutations are of unequal length."); 55 55 int length = parent1.Length; 56 56 int[] result = new int[length]; … … 66 66 } 67 67 } catch (IndexOutOfRangeException) { 68 throw new InvalidOperationException("CyclicCrossover: The permutation must consist of consecutive numbers from 0 to N-1 with N = length of the permutation");68 throw new InvalidOperationException("CyclicCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation."); 69 69 } 70 70 … … 101 101 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 102 102 protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) { 103 if (parents.Length != 2) throw new InvalidOperationException("CyclicCrossover: The number of parents is not equal to 2 ");103 if (parents.Length != 2) throw new InvalidOperationException("CyclicCrossover: The number of parents is not equal to 2."); 104 104 return Apply(random, parents[0], parents[1]); 105 105 } -
trunk/sources/HeuristicLab.Permutation/3.3/MaximalPreservativeCrossover.cs
r2830 r2835 29 29 /// Performs a crossover between two permuation arrays by preserving a large number of edges in both parents. 30 30 /// The operator also maintains the position in the arrays to some extent. 31 /// It is implemented as described in Mühlenbein, H. 1991. Evolution in time and space - the parallel genetic algorithm. FOUNDATIONS OF GENETIC ALGORITHMS, pp. 316-337. Morgan Kaufmann. 32 /// 33 /// The length of the segment copied from the first parent to the offspring is uniformly distributed in the interval [3, N/3) with N = length of the permutation. 31 /// It is implemented as described in Mühlenbein, H. 1991. Evolution in time and space - the parallel genetic algorithm. FOUNDATIONS OF GENETIC ALGORITHMS, pp. 316-337. Morgan Kaufmann.<br /><br /> 32 /// The length of the segment copied from the first parent to the offspring is uniformly distributed in the interval [3;N/3) with N = length of the permutation. 34 33 /// This recommendation is mentioned in Pohlheim, H. 1999. Evolutionäre Algorithmen: Verfahren, Operatoren und Hinweise für die Praxis, p. 44, Springer. 35 34 /// If the length of the permutation is smaller than 15, the size of the segment is always equal to 3. … … 44 43 /// </summary> 45 44 /// <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> 46 /// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0 ,N) with N = length of the permutation.</exception>45 /// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0;N) with N = length of the permutation.</exception> 47 46 /// <remarks> 48 47 /// First one segment is copied from the first parent to the offspring in the same position. … … 56 55 /// <returns>The new permutation resulting from the crossover.</returns> 57 56 public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) { 58 if (parent1.Length != parent2.Length) throw new ArgumentException("MaximalPreservativeCrossover: The parent permutations are of unequal length ");59 if (parent1.Length < 4) throw new ArgumentException("MaximalPreservativeCrossover: The parent permutation must be at least of size 4 ");57 if (parent1.Length != parent2.Length) throw new ArgumentException("MaximalPreservativeCrossover: The parent permutations are of unequal length."); 58 if (parent1.Length < 4) throw new ArgumentException("MaximalPreservativeCrossover: The parent permutation must be at least of size 4."); 60 59 int length = parent1.Length; 61 60 int[] result = new int[length]; … … 86 85 } 87 86 } catch (IndexOutOfRangeException) { 88 throw new InvalidOperationException("MaximalPreservativeCrossover: The permutation must consist of consecutive numbers from 0 to N-1 with N = length of the permutation");87 throw new InvalidOperationException("MaximalPreservativeCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation."); 89 88 } 90 89 -
trunk/sources/HeuristicLab.Permutation/3.3/OrderCrossover.cs
r2830 r2835 26 26 namespace HeuristicLab.Permutation { 27 27 /// <summary> 28 /// An operator which performs an order crossover oftwo permutations.28 /// An operator which performs the order crossover on two permutations. 29 29 /// </summary> 30 30 /// <remarks> 31 /// Crosses two permutations by taking a randomly chosen interval from the frist permutation, preserving 32 /// the positions, and then the missing values from the second permutation in the order they occur in the 33 /// second permutation. 31 /// It is implemented as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg.<br /> 32 /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving 33 /// the positions. Then, starting from the end of the copied interval, copies the missing values from the second permutation 34 /// in the order they occur. 34 35 /// </remarks> 35 [Item("OrderCrossover", "An operator which performs an order crossover of two permutations .")]36 [Item("OrderCrossover", "An operator which performs an order crossover of two permutations as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg.")] 36 37 [EmptyStorableClass] 37 38 [Creatable("Test")] … … 40 41 /// Performs an order crossover of two permutations. 41 42 /// </summary> 43 /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception> 44 /// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0,N) with N = length of the permutation.</exception> 45 /// <remarks> 46 /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving 47 /// the positions. Then, starting from the end of the copied interval, copies the missing values from the second permutation 48 /// in the order they occur. 49 /// </remarks> 42 50 /// <param name="random">A random number generator.</param> 43 51 /// <param name="parent1">The first parent permutation to cross.</param> … … 45 53 /// <returns>The new permutation resulting from the crossover.</returns> 46 54 public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) { 47 int[] result = new int[parent1.Length]; 48 bool[] copied = new bool[result.Length]; 55 if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover: The parent permutations are of unequal length."); 56 int length = parent1.Length; 57 int[] result = new int[length]; 58 bool[] copied = new bool[length]; 49 59 50 int breakPoint1 = random.Next( result.Length - 1);51 int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);60 int breakPoint1 = random.Next(length - 1); 61 int breakPoint2 = random.Next(breakPoint1 + 1, length); 52 62 53 for (int i = breakPoint1; i <= breakPoint2; i++) { // copy part of first permutation 54 result[i] = parent1[i]; 55 copied[parent1[i]] = true; 56 } 63 try { 64 for (int j = breakPoint1; j <= breakPoint2; j++) { // copy part of first permutation 65 result[j] = parent1[j]; 66 copied[parent1[j]] = true; 67 } 57 68 58 int index = 0; 59 for (int i = 0; i < parent2.Length; i++) { // copy remaining part of second permutation 60 if (index == breakPoint1) { // skip already copied part 61 index = breakPoint2 + 1; 69 int index = ((breakPoint2 + 1 >= length) ? (0) : (breakPoint2 + 1)); 70 int i = index; // for moving in parent2 71 while (index != breakPoint1) { 72 if (!copied[parent2[i]]) { 73 result[index] = parent2[i]; 74 index++; 75 if (index >= length) index = 0; 76 } 77 i++; 78 if (i >= length) i = 0; 62 79 } 63 if (!copied[parent2[i]]) { 64 result[index] = parent2[i]; 65 index++; 66 } 80 } catch (IndexOutOfRangeException) { 81 throw new InvalidOperationException("OrderCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation."); 67 82 } 68 83 return new Permutation(result); … … 77 92 /// <returns>The new permutation resulting from the crossover.</returns> 78 93 protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) { 79 if (parents.Length != 2) throw new InvalidOperationException(" Number of parents is not equal to 2.");94 if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover: Number of parents is not equal to 2."); 80 95 return Apply(random, parents[0], parents[1]); 81 96 } -
trunk/sources/HeuristicLab.Permutation/3.3/PartiallyMatchedCrossover.cs
r2830 r2835 54 54 /// <returns>The new permutation resulting from the crossover.</returns> 55 55 public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) { 56 if (parent1.Length != parent2.Length) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutations are of unequal length ");57 if (parent1.Length < 4) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutation must be at least of size 4 ");56 if (parent1.Length != parent2.Length) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutations are of unequal length."); 57 if (parent1.Length < 4) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutation must be at least of size 4."); 58 58 int length = parent1.Length; 59 59 int[] result = new int[length]; … … 73 73 } 74 74 } catch (IndexOutOfRangeException) { 75 throw new InvalidOperationException("PartiallyMatchedCrossover: The permutation must consist of consecutive numbers from 0 to N-1 with N = length of the permutation ");75 throw new InvalidOperationException("PartiallyMatchedCrossover: The permutation must consist of consecutive numbers from 0 to N-1 with N = length of the permutation."); 76 76 } 77 77 … … 97 97 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 98 98 protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) { 99 if (parents.Length != 2) throw new InvalidOperationException("PartiallyMatchedCrossover: The number of parents is not equal to 2 ");99 if (parents.Length != 2) throw new InvalidOperationException("PartiallyMatchedCrossover: The number of parents is not equal to 2."); 100 100 return Apply(random, parents[0], parents[1]); 101 101 }
Note: See TracChangeset
for help on using the changeset viewer.