[2871] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2871] | 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;
|
---|
[2871] | 24 | using HeuristicLab.Core;
|
---|
[16565] | 25 | using HEAL.Attic;
|
---|
[2871] | 26 |
|
---|
[3053] | 27 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[2871] | 28 | /// <summary>
|
---|
| 29 | /// An operator which performs a slight variant of the order crossover on two permutations.
|
---|
| 30 | /// </summary>
|
---|
| 31 | /// <remarks>
|
---|
| 32 | /// It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. p. 135.<br />
|
---|
| 33 | /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
|
---|
| 34 | /// the positions. But then, instead of starting from the end of the copied interval, the missing values are copied from the start of the permutation in the order they occur.
|
---|
| 35 | /// </remarks>
|
---|
| 36 | [Item("OrderCrossover2", "An operator which performs an order crossover of two permutations. It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. p. 135.")]
|
---|
[16565] | 37 | [StorableType("9E9A828E-E853-43FD-A39B-F47D17BCE539")]
|
---|
[2871] | 38 | public class OrderCrossover2 : PermutationCrossover {
|
---|
[4722] | 39 | [StorableConstructor]
|
---|
[16565] | 40 | protected OrderCrossover2(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 41 | protected OrderCrossover2(OrderCrossover2 original, Cloner cloner) : base(original, cloner) { }
|
---|
| 42 | public OrderCrossover2() : base() { }
|
---|
| 43 |
|
---|
| 44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 45 | return new OrderCrossover2(this, cloner);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[2871] | 48 | /// <summary>
|
---|
| 49 | /// Performs a slight variation of the order crossover of two permutations.
|
---|
| 50 | /// </summary>
|
---|
| 51 | /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
|
---|
| 52 | /// <remarks>
|
---|
| 53 | /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
|
---|
| 54 | /// the positions. Then, from the beginning of the permutation, copies the missing values from the second permutation
|
---|
| 55 | /// in the order they occur.
|
---|
| 56 | /// </remarks>
|
---|
| 57 | /// <param name="random">A random number generator.</param>
|
---|
| 58 | /// <param name="parent1">The first parent permutation to cross.</param>
|
---|
| 59 | /// <param name="parent2">The second parent permutation to cross.</param>
|
---|
| 60 | /// <returns>The new permutation resulting from the crossover.</returns>
|
---|
| 61 | public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
|
---|
| 62 | if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover2: The parent permutations are of unequal length.");
|
---|
| 63 | int[] result = new int[parent1.Length];
|
---|
| 64 | bool[] copied = new bool[result.Length];
|
---|
| 65 |
|
---|
| 66 | int breakPoint1 = random.Next(result.Length - 1);
|
---|
| 67 | int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);
|
---|
| 68 |
|
---|
| 69 | for (int i = breakPoint1; i <= breakPoint2; i++) { // copy part of first permutation
|
---|
| 70 | result[i] = parent1[i];
|
---|
| 71 | copied[parent1[i]] = true;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | int index = 0;
|
---|
| 75 | for (int i = 0; i < parent2.Length; i++) { // copy remaining part of second permutation
|
---|
| 76 | if (index == breakPoint1) { // skip already copied part
|
---|
| 77 | index = breakPoint2 + 1;
|
---|
| 78 | }
|
---|
| 79 | if (!copied[parent2[i]]) {
|
---|
| 80 | result[index] = parent2[i];
|
---|
| 81 | index++;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[3231] | 84 | return new Permutation(parent1.PermutationType, result);
|
---|
[2871] | 85 | }
|
---|
| 86 |
|
---|
| 87 | /// <summary>
|
---|
| 88 | /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
|
---|
| 89 | /// </summary>
|
---|
| 90 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
| 91 | /// <param name="random">A random number generator.</param>
|
---|
| 92 | /// <param name="parents">An array containing the two permutations that should be crossed.</param>
|
---|
| 93 | /// <returns>The new permutation resulting from the crossover.</returns>
|
---|
| 94 | protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
|
---|
| 95 | if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover2: Number of parents is not equal to 2.");
|
---|
| 96 | return Apply(random, parents[0], parents[1]);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|