[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 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 System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Permutation {
|
---|
[850] | 29 | /// <summary>
|
---|
| 30 | /// Performs a cross over permutation between two permutation arrays by taking randomly chosen
|
---|
| 31 | /// reverse and forward intervals from the first permutation array inserting
|
---|
| 32 | /// it in the child on different positions depending on the second permutation array.
|
---|
| 33 | /// </summary>
|
---|
[2] | 34 | public class CosaCrossover : PermutationCrossoverBase {
|
---|
[850] | 35 | /// <inheritdoc select="summary"/>
|
---|
[2] | 36 | public override string Description {
|
---|
| 37 | get { return @"TODO\r\nOperator description still missing ..."; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[850] | 40 | /// <summary>
|
---|
| 41 | /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
|
---|
| 42 | /// by taking first the reverse elements of a randomly chosen interval of parent1
|
---|
| 43 | /// and inserting it in the result at a position specified by the permutation of parent2.
|
---|
| 44 | /// The remaining elements to be inserted are taken again from parent1 in the forward direction.
|
---|
| 45 | /// </summary>
|
---|
| 46 | /// <param name="random">The random number generator.</param>
|
---|
| 47 | /// <param name="parent1">The parent scope 1 to cross over.</param>
|
---|
| 48 | /// <param name="parent2">The parent scope 2 to cross over.</param>
|
---|
| 49 | /// <returns>The created cross over permutation as int array.</returns>
|
---|
[2] | 50 | public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
|
---|
| 51 | int length = parent1.Length;
|
---|
| 52 | int[] result = new int[length];
|
---|
| 53 | int crossPoint, startIndex, endIndex;
|
---|
| 54 |
|
---|
| 55 | crossPoint = random.Next(length);
|
---|
| 56 | startIndex = (crossPoint + 1) % length;
|
---|
| 57 |
|
---|
| 58 | int i = 0;
|
---|
| 59 | while ((i < parent2.Length) && (parent2[i] != parent1[startIndex])) { // find index of start value in second permutation
|
---|
| 60 | i++;
|
---|
| 61 | }
|
---|
| 62 | i = (i + 1) % length;
|
---|
| 63 | int j = 0;
|
---|
| 64 | while ((j < parent1.Length) && (parent1[j] != parent2[i])) { // find index of parent2[i] in first permutation
|
---|
| 65 | j++;
|
---|
| 66 | }
|
---|
| 67 | endIndex = (j - 1 + length) % length;
|
---|
| 68 |
|
---|
| 69 | i = endIndex;
|
---|
| 70 | j = 0;
|
---|
| 71 | do { // permutation from end to crosspoint (backwards)
|
---|
| 72 | result[j] = parent1[i];
|
---|
| 73 | i = (i - 1 + length) % length;
|
---|
| 74 | j++;
|
---|
| 75 | } while (i != crossPoint);
|
---|
| 76 |
|
---|
| 77 | i = (endIndex + 1) % length;
|
---|
| 78 | while (i != crossPoint) { // permutation from end to crosspoint (forwards)
|
---|
| 79 | result[j] = parent1[i];
|
---|
| 80 | i = (i + 1) % length;
|
---|
| 81 | j++;
|
---|
| 82 | }
|
---|
| 83 | result[j] = parent1[crossPoint]; // last station: crosspoint
|
---|
| 84 | return result;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[850] | 87 | /// <summary>
|
---|
| 88 | /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
|
---|
| 89 | /// by taking first the reverse elements of a randomly chosen interval of parent1
|
---|
| 90 | /// and inserting it in the result at a position specified by the permutation of parent2.
|
---|
| 91 | /// The remaining elements to be inserted are taken again from parent1 in the forward direction.
|
---|
| 92 | /// </summary>
|
---|
| 93 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
| 94 | /// <param name="scope">The current scope.</param>
|
---|
| 95 | /// <param name="random">The random number generator.</param>
|
---|
| 96 | /// <param name="parent1">The parent scope 1 to cross over.</param>
|
---|
| 97 | /// <param name="parent2">The parent scope 2 to cross over.</param>
|
---|
| 98 | /// <returns>The created cross over permutation as int array.</returns>
|
---|
[2] | 99 | protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
|
---|
| 100 | return Apply(random, parent1, parent2);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|