Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/PositionBasedCrossover.cs @ 3053

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

Renamed solution encoding plugins (#909)

File size: 4.4 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.Encodings.PermutationEncoding {
27  /// <summary>
28  /// Performs a cross over permutation between two permutation arrays based on randomly chosen positions.
29  /// </summary>
30  /// <remarks>
31  /// It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.
32  /// </remarks>
33  [Item("PositionBasedCrossover", "An operator which performs the position based crossover on two permutations. It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.")]
34  [StorableClass]
35  [Creatable("Test")]
36  public class PositionBasedCrossover : PermutationCrossover {
37    /// <summary>
38    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
39    /// based on randomly chosen positions to define which position to take from where.
40    /// </summary>
41    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
42    /// <param name="random">The random number generator.</param>
43    /// <param name="parent1">First parent</param>
44    /// <param name="parent2">Second Parent</param>
45    /// <returns>Child</returns>
46    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
47      if (parent1.Length != parent2.Length) throw new ArgumentException("PositionBasedCrossover: The parent permutations are of unequal length.");
48      int length = parent1.Length;
49      int[] result = new int[length];
50      bool[] randomPosition = new bool[length];
51      bool[] numberCopied = new bool[length];
52      int randomPosNumber = random.Next(length);
53
54      for (int i = 0; i < randomPosNumber; i++) {  // generate random bit mask
55        randomPosition[random.Next(length)] = true;
56      }
57
58      for (int i = 0; i < length; i++) {  // copy numbers masked as true from second permutation
59        if (randomPosition[i]) {
60          result[i] = parent2[i];
61          numberCopied[parent2[i]] = true;
62        }
63      }
64
65      int index = 0;
66      for (int i = 0; i < length; i++) {  // copy numbers masked as false from first permutation
67        if (!numberCopied[parent1[i]]) {
68          if (randomPosition[index]) {
69            while (randomPosition[index]) {
70              index++;
71            }
72          }
73          result[index] = parent1[i];
74          index++;
75        }
76      }
77
78      return new Permutation(result);
79    }
80
81    /// <summary>
82    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
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 PositionBasedCrossover: 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.