Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.3/OrderCrossover.cs @ 2830

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

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 3.5 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  /// An operator which performs an order crossover of two permutations.
29  /// </summary>
30  /// <remarks>
31  /// Crosses two permutations by taking a randomly chosen interval from the frist permutation, preserving
32  /// the positions, and then the missing values from the second permutation in the order they occur in the
33  /// second permutation.
34  /// </remarks>
35  [Item("OrderCrossover", "An operator which performs an order crossover of two permutations.")]
36  [EmptyStorableClass]
37  [Creatable("Test")]
38  public class OrderCrossover : PermutationCrossover {
39    /// <summary>
40    /// Performs an order crossover of two permutations.
41    /// </summary>
42    /// <param name="random">A random number generator.</param>
43    /// <param name="parent1">The first parent permutation to cross.</param>
44    /// <param name="parent2">The second parent permutation to cross.</param>
45    /// <returns>The new permutation resulting from the crossover.</returns>
46    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
47      int[] result = new int[parent1.Length];
48      bool[] copied = new bool[result.Length];
49
50      int breakPoint1 = random.Next(result.Length - 1);
51      int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);
52
53      for (int i = breakPoint1; i <= breakPoint2; i++) {  // copy part of first permutation
54        result[i] = parent1[i];
55        copied[parent1[i]] = true;
56      }
57
58      int index = 0;
59      for (int i = 0; i < parent2.Length; i++) {  // copy remaining part of second permutation
60        if (index == breakPoint1) {  // skip already copied part
61          index = breakPoint2 + 1;
62        }
63        if (!copied[parent2[i]]) {
64          result[index] = parent2[i];
65          index++;
66        }
67      }
68      return new Permutation(result);
69    }
70
71    /// <summary>
72    /// Performs an order crossover of two permutations.
73    /// </summary>
74    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
75    /// <param name="random">A random number generator.</param>
76    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
77    /// <returns>The new permutation resulting from the crossover.</returns>
78    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
79      if (parents.Length != 2) throw new InvalidOperationException("Number of parents is not equal to 2.");
80      return Apply(random, parents[0], parents[1]);
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.