Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2865 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.3 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 based on randomly chosen positions.
32  /// </summary>
33  /// <remarks>
34  /// Implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms,
35  /// in Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.
36  /// </remarks>
37  [Item("PositionBasedCrossover", "An operator which performs the position based crossover on two permutations.")]
38  [EmptyStorableClass]
39  [Creatable("Test")]
40  public class PositionBasedCrossover : PermutationCrossover {
41    /// <summary>
42    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
43    /// based on randomly chosen positions to define which position to take from where.
44    /// </summary>
45    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
46    /// <param name="random">The random number generator.</param>
47    /// <param name="parent1">First parent</param>
48    /// <param name="parent2">Second Parent</param>
49    /// <returns>Child</returns>
50    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
51      if (parent1.Length != parent2.Length) throw new ArgumentException("PositionBasedCrossover: The parent permutations are of unequal length.");
52      int length = parent1.Length;
53      int[] result = new int[length];
54      bool[] randomPosition = new bool[length];
55      bool[] numberCopied = new bool[length];
56      int randomPosNumber = random.Next(length);
57
58      for (int i = 0; i < randomPosNumber; i++) {  // generate random bit mask
59        randomPosition[random.Next(length)] = true;
60      }
61
62      for (int i = 0; i < length; i++) {  // copy numbers masked as true from second permutation
63        if (randomPosition[i]) {
64          result[i] = parent2[i];
65          numberCopied[parent2[i]] = true;
66        }
67      }
68
69      int index = 0;
70      for (int i = 0; i < length; i++) {  // copy numbers masked as false from first permutation
71        if (!numberCopied[parent1[i]]) {
72          if (randomPosition[index]) {
73            while (randomPosition[index]) {
74              index++;
75            }
76          }
77          result[index] = parent1[i];
78          index++;
79        }
80      }
81
82      return new Permutation(result);
83    }
84
85    /// <summary>
86    /// Performs a position-based crossover operation for two given parent permutations.
87    /// </summary>
88    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
89    /// <param name="random">A random number generator.</param>
90    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
91    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
92    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
93      if (parents.Length != 2) throw new InvalidOperationException("ERROR in PositionBasedCrossover: The number of parents is not equal to 2");
94      return Apply(random, parents[0], parents[1]);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.