Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/CyclicCrossover.cs @ 1243

Last change on this file since 1243 was 1218, checked in by swagner, 16 years ago

Refactoring of crossover operators (#470)

File size: 4.1 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 int arrays by taking first a whole cycle and then the
31  /// missing ones from the second parent.
32  /// </summary>
33  /// <remarks>A whole cycle means: <br/>
34  /// Start at a randomly chosen position x in parent1 and transfer it to the child at the same position.
35  /// Now this position x is no longer available for the node on position x in parent2, so
36  /// the value of the node at position x in parent2 is searched in parent1 and is then transferred
37  /// to the child preserving the position. Now this new position y is no longer available for the node in parent2 ....<br/>
38  /// This procedure is repeated till it is again at position x, then the cycle is over.
39  /// </remarks>
40  public class CyclicCrossover : PermutationCrossoverBase {
41    /// <inheritdoc select="summary"/>
42    public override string Description {
43      get { return @"TODO\r\nOperator description still missing ..."; }
44    }
45
46    /// <summary>
47    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
48    /// by copying a whole cycle starting at a randomly chosen position in parent1 and taking the rest
49    /// from parent2.
50    /// </summary>
51    /// <param name="random">The random number generator.</param>
52    /// <param name="parent1">The parent scope 1 to cross over.</param>
53    /// <param name="parent2">The parent scope 2 to cross over.</param>
54    /// <returns>The created cross over permutation as int array.</returns>
55    public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
56      int length = parent1.Length;
57      int[] result = new int[length];
58      bool[] indexCopied = new bool[length];
59      int j, number;
60
61      j = random.Next(length);  // get number to start cycle
62      while (!indexCopied[j]) {  // copy whole cycle to new permutation
63        result[j] = parent1[j];
64        number = parent2[j];
65        indexCopied[j] = true;
66
67        j = 0;
68        while ((j < length) && (parent1[j] != number)) {  // search copied number in second permutation
69          j++;
70        }
71      }
72
73      for (int i = 0; i < length; i++) {  // copy rest of secound permutation to new permutation
74        if (!indexCopied[i]) {
75          result[i] = parent2[i];
76        }
77      }
78      return result;
79    }
80
81    /// <summary>
82    /// Performs a cyclic crossover operation for two given parent permutations.
83    /// </summary>
84    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
85    /// <param name="scope">The current scope.</param>
86    /// <param name="random">A random number generator.</param>
87    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
88    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
89    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
90      if (parents.Length != 2) throw new InvalidOperationException("ERROR in CyclicCrossover: The number of parents is not equal to 2");
91      return Apply(random, parents[0], parents[1]);
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.