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 {
|
---|
29 | /// <summary>
|
---|
30 | /// Performs a cross over permutation between two permuation arrays by preserving a preferably big
|
---|
31 | /// region from one permutation array.
|
---|
32 | /// </summary>
|
---|
33 | public class MaximalPreservativeCrossover : PermutationCrossoverBase {
|
---|
34 | /// <inheritdoc select="summary"/>
|
---|
35 | public override string Description {
|
---|
36 | get { return @"TODO\r\nOperator description still missing ..."; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
|
---|
41 | /// by preserving a preferably big randomly chosen region of one permutation and taking
|
---|
42 | /// the missing ones from the other permuation array.
|
---|
43 | /// </summary>
|
---|
44 | /// <param name="random">The random number generator.</param>
|
---|
45 | /// <param name="parent1">The permutation array of parent 1.</param>
|
---|
46 | /// <param name="parent2">The permutation array of parent 2.</param>
|
---|
47 | /// <returns>The created cross over permutation as int array.</returns>
|
---|
48 | public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
|
---|
49 | int length = parent1.Length;
|
---|
50 | int[] result = new int[length];
|
---|
51 | bool[] numberCopied = new bool[length];
|
---|
52 | int breakPoint1, breakPoint2, subsegmentLength, index;
|
---|
53 |
|
---|
54 | if (length >= 20) { // length of subsegment must be >= 10 and <= length / 2
|
---|
55 | breakPoint1 = random.Next(length - 9);
|
---|
56 | subsegmentLength = random.Next(10, Math.Min((int)(length / 2), length - breakPoint1) + 1);
|
---|
57 | breakPoint2 = breakPoint1 + subsegmentLength - 1;
|
---|
58 | } else {
|
---|
59 | breakPoint1 = random.Next(length - 1);
|
---|
60 | breakPoint2 = random.Next(breakPoint1 + 1, length);
|
---|
61 | }
|
---|
62 |
|
---|
63 | index = 0;
|
---|
64 | for (int i = breakPoint1; i <= breakPoint2; i++) { // copy part of first permutation
|
---|
65 | result[index] = parent1[i];
|
---|
66 | numberCopied[result[index]] = true;
|
---|
67 | index++;
|
---|
68 | }
|
---|
69 |
|
---|
70 | for (int i = 0; i < parent2.Length; i++) { // copy remaining part of second permutation
|
---|
71 | if (!numberCopied[parent2[i]]) {
|
---|
72 | result[index] = parent2[i];
|
---|
73 | index++;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return result;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
|
---|
81 | /// by preserving a big randomly chosen region of one permutation and taking the missing ones from the other
|
---|
82 | /// permuation array.
|
---|
83 | /// </summary>
|
---|
84 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
85 | /// <param name="scope">The current scope.</param>
|
---|
86 | /// <param name="random">The random number generator.</param>
|
---|
87 | /// <param name="parent1">The permutation array of parent 1.</param>
|
---|
88 | /// <param name="parent2">The permutation array of parent 2.</param>
|
---|
89 | /// <returns>The created cross over permutation as int array.</returns>
|
---|
90 | protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
|
---|
91 | return Apply(random, parent1, parent2);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|