[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2794] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
[2794] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 25 |
|
---|
[3053] | 26 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[850] | 27 | /// <summary>
|
---|
[2835] | 28 | /// An operator which performs the order crossover on two permutations.
|
---|
[850] | 29 | /// </summary>
|
---|
[2794] | 30 | /// <remarks>
|
---|
[2835] | 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.
|
---|
[2794] | 35 | /// </remarks>
|
---|
[2871] | 36 | [Item("OrderCrossover", "An operator which performs an order crossover of two permutations. 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.")]
|
---|
[3017] | 37 | [StorableClass]
|
---|
[2794] | 38 | public class OrderCrossover : PermutationCrossover {
|
---|
[850] | 39 | /// <summary>
|
---|
[2871] | 40 | /// Performs the order crossover of two permutations.
|
---|
[850] | 41 | /// </summary>
|
---|
[2835] | 42 | /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
|
---|
| 43 | /// <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>
|
---|
| 44 | /// <remarks>
|
---|
| 45 | /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
|
---|
| 46 | /// the positions. Then, starting from the end of the copied interval, copies the missing values from the second permutation
|
---|
| 47 | /// in the order they occur.
|
---|
| 48 | /// </remarks>
|
---|
[2794] | 49 | /// <param name="random">A random number generator.</param>
|
---|
| 50 | /// <param name="parent1">The first parent permutation to cross.</param>
|
---|
| 51 | /// <param name="parent2">The second parent permutation to cross.</param>
|
---|
| 52 | /// <returns>The new permutation resulting from the crossover.</returns>
|
---|
| 53 | public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
|
---|
[2835] | 54 | if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover: The parent permutations are of unequal length.");
|
---|
| 55 | int length = parent1.Length;
|
---|
| 56 | int[] result = new int[length];
|
---|
| 57 | bool[] copied = new bool[length];
|
---|
[2] | 58 |
|
---|
[2835] | 59 | int breakPoint1 = random.Next(length - 1);
|
---|
| 60 | int breakPoint2 = random.Next(breakPoint1 + 1, length);
|
---|
[2] | 61 |
|
---|
[2835] | 62 | try {
|
---|
| 63 | for (int j = breakPoint1; j <= breakPoint2; j++) { // copy part of first permutation
|
---|
| 64 | result[j] = parent1[j];
|
---|
| 65 | copied[parent1[j]] = true;
|
---|
| 66 | }
|
---|
[2] | 67 |
|
---|
[2835] | 68 | int index = ((breakPoint2 + 1 >= length) ? (0) : (breakPoint2 + 1));
|
---|
| 69 | int i = index; // for moving in parent2
|
---|
| 70 | while (index != breakPoint1) {
|
---|
| 71 | if (!copied[parent2[i]]) {
|
---|
| 72 | result[index] = parent2[i];
|
---|
| 73 | index++;
|
---|
| 74 | if (index >= length) index = 0;
|
---|
| 75 | }
|
---|
| 76 | i++;
|
---|
| 77 | if (i >= length) i = 0;
|
---|
[2] | 78 | }
|
---|
[4068] | 79 | }
|
---|
| 80 | catch (IndexOutOfRangeException) {
|
---|
[2835] | 81 | throw new InvalidOperationException("OrderCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation.");
|
---|
[2] | 82 | }
|
---|
[3231] | 83 | return new Permutation(parent1.PermutationType, result);
|
---|
[2] | 84 | }
|
---|
| 85 |
|
---|
[850] | 86 | /// <summary>
|
---|
[2871] | 87 | /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
|
---|
[850] | 88 | /// </summary>
|
---|
[1218] | 89 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
| 90 | /// <param name="random">A random number generator.</param>
|
---|
| 91 | /// <param name="parents">An array containing the two permutations that should be crossed.</param>
|
---|
[2794] | 92 | /// <returns>The new permutation resulting from the crossover.</returns>
|
---|
| 93 | protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
|
---|
[2835] | 94 | if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover: Number of parents is not equal to 2.");
|
---|
[1218] | 95 | return Apply(random, parents[0], parents[1]);
|
---|
[2] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|