Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.2/MaximalPreservativeCrossover.cs @ 1679

Last change on this file since 1679 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 4.0 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace 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 maximal preservative crossover operation for two given parent permutations.
81    /// </summary>
82    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
83    /// <param name="scope">The current scope.</param>
84    /// <param name="random">A random number generator.</param>
85    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
86    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
87    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
88      if (parents.Length != 2) throw new InvalidOperationException("ERROR in MaximalPreservativeCrossover: The number of parents is not equal to 2");
89      return Apply(random, parents[0], parents[1]);
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.