Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.3/AbsolutePositionTopologicalCrossover.cs @ 2865

Last change on this file since 2865 was 2865, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • worked on algorithms
File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Permutation {
27  /// <summary>
28  /// Performs a cross over permutation between two permutation arrays by taking
29  /// the entries with the same index (starting at position 0) from both parents
30  /// (minding already inserted values).
31  /// </summary>
32  /// <remarks>It is implemented as described in Moraglio, A. and Poli, R. 2005. Topological crossover for the permutation representation. In Proceedings of the 2005 Workshops on Genetic and Evolutionary Computation.<br />
33  /// </remarks>
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>
37  [Item("AbsolutePositionTopologicalCrossover", @"An operator which performs a cross over permutation between two permutation arrays by taking the
38entries with the same index (starting at position 0) from both parents
39(minding already inserted values).")]
40  [EmptyStorableClass]
41  [Creatable("Test")]
42  public class AbsolutePositionTopologicalCrossover : PermutationCrossover {
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    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
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 Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
56      if (parent1.Length != parent2.Length) throw new ArgumentException("AbsolutePositionTopologicalCrossover: The parent permutations are of unequal length.");
57      int length = parent1.Length;
58      int[] result = new int[length];
59      bool[] numberCopied = new bool[length];
60      int index;
61
62      index = 0;
63      for (int i = 0; i < length; i++) {  // copy numbers from both parent permutations
64        if (!numberCopied[parent1[i]]) {
65          result[index] = parent1[i];
66          numberCopied[parent1[i]] = true;
67          index++;
68        }
69        if (!numberCopied[parent2[i]]) {
70          result[index] = parent2[i];
71          numberCopied[parent2[i]] = true;
72          index++;
73        }
74      }
75      return new Permutation(result);
76    }
77
78    /// <summary>
79    /// Performs an absolute position topological crossover operation for two given parent permutations.
80    /// </summary>
81    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
82    /// <param name="random">A random number generator.</param>
83    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
84    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
85    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
86      if (parents.Length != 2) throw new InvalidOperationException("ERROR in AbsolutePositionTopologicalCrossover: The number of parents is not equal to 2");
87      return Apply(random, parents[0], parents[1]);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.