[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2] | 24 | using HeuristicLab.Core;
|
---|
[17097] | 25 | using HEAL.Attic;
|
---|
[2] | 26 |
|
---|
[3053] | 27 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[850] | 28 | /// <summary>
|
---|
[2835] | 29 | /// An operator which performs the order crossover on two permutations.
|
---|
[850] | 30 | /// </summary>
|
---|
[2794] | 31 | /// <remarks>
|
---|
[2835] | 32 | /// 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 />
|
---|
| 33 | /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
|
---|
| 34 | /// the positions. Then, starting from the end of the copied interval, copies the missing values from the second permutation
|
---|
| 35 | /// in the order they occur.
|
---|
[2794] | 36 | /// </remarks>
|
---|
[2871] | 37 | [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.")]
|
---|
[17097] | 38 | [StorableType("DEF33012-C7C5-4C09-8FCD-1145353D06A2")]
|
---|
[2794] | 39 | public class OrderCrossover : PermutationCrossover {
|
---|
[4722] | 40 | [StorableConstructor]
|
---|
[17097] | 41 | protected OrderCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 42 | protected OrderCrossover(OrderCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 43 | public OrderCrossover() : base() { }
|
---|
| 44 |
|
---|
| 45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 46 | return new OrderCrossover(this, cloner);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[850] | 49 | /// <summary>
|
---|
[2871] | 50 | /// Performs the order crossover of two permutations.
|
---|
[850] | 51 | /// </summary>
|
---|
[2835] | 52 | /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
|
---|
| 53 | /// <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>
|
---|
| 54 | /// <remarks>
|
---|
| 55 | /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
|
---|
| 56 | /// the positions. Then, starting from the end of the copied interval, copies the missing values from the second permutation
|
---|
| 57 | /// in the order they occur.
|
---|
| 58 | /// </remarks>
|
---|
[2794] | 59 | /// <param name="random">A random number generator.</param>
|
---|
| 60 | /// <param name="parent1">The first parent permutation to cross.</param>
|
---|
| 61 | /// <param name="parent2">The second parent permutation to cross.</param>
|
---|
| 62 | /// <returns>The new permutation resulting from the crossover.</returns>
|
---|
| 63 | public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
|
---|
[2835] | 64 | if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover: The parent permutations are of unequal length.");
|
---|
| 65 | int length = parent1.Length;
|
---|
| 66 | int[] result = new int[length];
|
---|
| 67 | bool[] copied = new bool[length];
|
---|
[2] | 68 |
|
---|
[2835] | 69 | int breakPoint1 = random.Next(length - 1);
|
---|
| 70 | int breakPoint2 = random.Next(breakPoint1 + 1, length);
|
---|
[2] | 71 |
|
---|
[2835] | 72 | try {
|
---|
| 73 | for (int j = breakPoint1; j <= breakPoint2; j++) { // copy part of first permutation
|
---|
| 74 | result[j] = parent1[j];
|
---|
| 75 | copied[parent1[j]] = true;
|
---|
| 76 | }
|
---|
[2] | 77 |
|
---|
[2835] | 78 | int index = ((breakPoint2 + 1 >= length) ? (0) : (breakPoint2 + 1));
|
---|
| 79 | int i = index; // for moving in parent2
|
---|
| 80 | while (index != breakPoint1) {
|
---|
| 81 | if (!copied[parent2[i]]) {
|
---|
| 82 | result[index] = parent2[i];
|
---|
| 83 | index++;
|
---|
| 84 | if (index >= length) index = 0;
|
---|
| 85 | }
|
---|
| 86 | i++;
|
---|
| 87 | if (i >= length) i = 0;
|
---|
[2] | 88 | }
|
---|
[4068] | 89 | }
|
---|
| 90 | catch (IndexOutOfRangeException) {
|
---|
[2835] | 91 | throw new InvalidOperationException("OrderCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation.");
|
---|
[2] | 92 | }
|
---|
[3231] | 93 | return new Permutation(parent1.PermutationType, result);
|
---|
[2] | 94 | }
|
---|
| 95 |
|
---|
[850] | 96 | /// <summary>
|
---|
[2871] | 97 | /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
|
---|
[850] | 98 | /// </summary>
|
---|
[1218] | 99 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
| 100 | /// <param name="random">A random number generator.</param>
|
---|
| 101 | /// <param name="parents">An array containing the two permutations that should be crossed.</param>
|
---|
[2794] | 102 | /// <returns>The new permutation resulting from the crossover.</returns>
|
---|
| 103 | protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
|
---|
[2835] | 104 | if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover: Number of parents is not equal to 2.");
|
---|
[1218] | 105 | return Apply(random, parents[0], parents[1]);
|
---|
[2] | 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|