[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
|
---|
| 31 | /// the entries with the same index (starting at position 0) from both parents
|
---|
| 32 | /// (minding already inserted values).
|
---|
| 33 | /// </summary>
|
---|
| 34 | /// <example>First take the value at position 0 from parent1 then take the value at position 0
|
---|
| 35 | /// from parent2 if it has not already been inserted, afterwards take the value at position 1 from
|
---|
| 36 | /// parent1 if it has not already been inserted, then from parent2 and so on.</example>
|
---|
[2] | 37 | public class AbsolutePositionTopologicalCrossover : PermutationCrossoverBase {
|
---|
[850] | 38 | /// <inheritdoc select="summary"/>
|
---|
[2] | 39 | public override string Description {
|
---|
| 40 | get { return @"TODO\r\nOperator description still missing ..."; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[850] | 43 | /// <summary>
|
---|
| 44 | /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
|
---|
| 45 | /// by taking the values from both parents one by one with the same index starting at position 0.
|
---|
| 46 | /// </summary>
|
---|
| 47 | /// <example>First take the value at position 0 from parent1 then take the value at position 0
|
---|
| 48 | /// from parent2 if it has not already been inserted, afterwards take the value at position 1 from
|
---|
| 49 | /// parent1 if it has not already been inserted, then from parent2 and so on.</example>
|
---|
| 50 | /// <param name="random">A random number generator.</param>
|
---|
| 51 | /// <param name="parent1">The parent scope 1 to cross over.</param>
|
---|
| 52 | /// <param name="parent2">The parent scope 2 to cross over.</param>
|
---|
| 53 | /// <returns>The created cross over permutation as int array.</returns>
|
---|
[2] | 54 | public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
|
---|
| 55 | int length = parent1.Length;
|
---|
| 56 | int[] result = new int[length];
|
---|
| 57 | bool[] numberCopied = new bool[length];
|
---|
| 58 | int index;
|
---|
| 59 |
|
---|
| 60 | index = 0;
|
---|
| 61 | for (int i = 0; i < length; i++) { // copy numbers from both parent permutations
|
---|
| 62 | if (!numberCopied[parent1[i]]) {
|
---|
| 63 | result[index] = parent1[i];
|
---|
| 64 | numberCopied[parent1[i]] = true;
|
---|
| 65 | index++;
|
---|
| 66 | }
|
---|
| 67 | if (!numberCopied[parent2[i]]) {
|
---|
| 68 | result[index] = parent2[i];
|
---|
| 69 | numberCopied[parent2[i]] = true;
|
---|
| 70 | index++;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | return result;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[850] | 76 | /// <summary>
|
---|
[1218] | 77 | /// Performs an absolute position topological crossover operation for two given parent permutations.
|
---|
[850] | 78 | /// </summary>
|
---|
[1218] | 79 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
[850] | 80 | /// <param name="scope">The current scope.</param>
|
---|
| 81 | /// <param name="random">A random number generator.</param>
|
---|
[1218] | 82 | /// <param name="parents">An array containing the two permutations that should be crossed.</param>
|
---|
| 83 | /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
|
---|
| 84 | protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
|
---|
| 85 | if (parents.Length != 2) throw new InvalidOperationException("ERROR in AbsolutePositionTopologicalCrossover: The number of parents is not equal to 2");
|
---|
| 86 | return Apply(random, parents[0], parents[1]);
|
---|
[2] | 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|