Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2861 was 2854, checked in by svonolfe, 15 years ago

Ported ported permutation operators to HL 3.3 and added test cases (#889)

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 System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Permutation {
30  /// <summary>
31  /// Performs a cross over permutation between two permutation arrays by taking
32  /// the entries with the same index (starting at position 0) from both parents
33  /// (minding already inserted values).
34  /// </summary>
35  /// <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 />
36  /// </remarks>
37  /// <example>First take the value at position 0 from parent1 then take the value at position 0
38  /// from parent2 if it has not already been inserted, afterwards take the value at position 1 from
39  /// parent1 if it has not already been inserted, then from parent2 and so on.</example>
40  [Item("AbsolutePositionTopologicalCrossover", @"An operator which performs a cross over permutation between two permutation arrays by taking the
41entries with the same index (starting at position 0) from both parents
42(minding already inserted values).")]
43  [EmptyStorableClass]
44  [Creatable("Test")]
45  public class AbsolutePositionTopologicalCrossover : PermutationCrossover {
46    /// <summary>
47    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
48    /// by taking the values from both parents one by one with the same index starting at position 0.
49    /// </summary>
50    /// <example>First take the value at position 0 from parent1 then take the value at position 0
51    /// from parent2 if it has not already been inserted, afterwards take the value at position 1 from
52    /// parent1 if it has not already been inserted, then from parent2 and so on.</example>
53    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
54    /// <param name="random">The random number generator.</param>
55    /// <param name="parent1">The parent scope 1 to cross over.</param>
56    /// <param name="parent2">The parent scope 2 to cross over.</param>
57    /// <returns>The created cross over permutation as int array.</returns>
58    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
59      if (parent1.Length != parent2.Length) throw new ArgumentException("AbsolutePositionTopologicalCrossover: The parent permutations are of unequal length.");
60      int length = parent1.Length;
61      int[] result = new int[length];
62      bool[] numberCopied = new bool[length];
63      int index;
64
65      index = 0;
66      for (int i = 0; i < length; i++) {  // copy numbers from both parent permutations
67        if (!numberCopied[parent1[i]]) {
68          result[index] = parent1[i];
69          numberCopied[parent1[i]] = true;
70          index++;
71        }
72        if (!numberCopied[parent2[i]]) {
73          result[index] = parent2[i];
74          numberCopied[parent2[i]] = true;
75          index++;
76        }
77      }
78      return new Permutation(result);
79    }
80
81    /// <summary>
82    /// Performs an absolute position topological 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="random">A random number generator.</param>
86    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
87    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
88    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
89      if (parents.Length != 2) throw new InvalidOperationException("ERROR in AbsolutePositionTopologicalCrossover: The number of parents is not equal to 2");
90      return Apply(random, parents[0], parents[1]);
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.