Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/PositionBasedCrossover.cs @ 16954

Last change on this file since 16954 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.PermutationEncoding {
28  /// <summary>
29  /// Performs a cross over permutation between two permutation arrays based on randomly chosen positions.
30  /// </summary>
31  /// <remarks>
32  /// 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.
33  /// </remarks>
34  [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.")]
35  [StorableClass]
36  public class PositionBasedCrossover : PermutationCrossover {
37    [StorableConstructor]
38    protected PositionBasedCrossover(bool deserializing) : base(deserializing) { }
39    protected PositionBasedCrossover(PositionBasedCrossover original, Cloner cloner) : base(original, cloner) { }
40    public PositionBasedCrossover() : base() { }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new PositionBasedCrossover(this, cloner);
44    }
45
46    /// <summary>
47    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
48    /// based on randomly chosen positions to define which position to take from where.
49    /// </summary>
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">First parent</param>
53    /// <param name="parent2">Second Parent</param>
54    /// <returns>Child</returns>
55    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
56      if (parent1.Length != parent2.Length) throw new ArgumentException("PositionBasedCrossover: The parent permutations are of unequal length.");
57      int length = parent1.Length;
58      int[] result = new int[length];
59      bool[] randomPosition = new bool[length];
60      bool[] numberCopied = new bool[length];
61      int randomPosNumber = random.Next(length);
62
63      for (int i = 0; i < randomPosNumber; i++) {  // generate random bit mask
64        randomPosition[random.Next(length)] = true;
65      }
66
67      for (int i = 0; i < length; i++) {  // copy numbers masked as true from second permutation
68        if (randomPosition[i]) {
69          result[i] = parent2[i];
70          numberCopied[parent2[i]] = true;
71        }
72      }
73
74      int index = 0;
75      for (int i = 0; i < length; i++) {  // copy numbers masked as false from first permutation
76        if (!numberCopied[parent1[i]]) {
77          if (randomPosition[index]) {
78            while (randomPosition[index]) {
79              index++;
80            }
81          }
82          result[index] = parent1[i];
83          index++;
84        }
85      }
86
87      return new Permutation(parent1.PermutationType, result);
88    }
89
90    /// <summary>
91    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
92    /// </summary>
93    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
94    /// <param name="random">A random number generator.</param>
95    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
96    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
97    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
98      if (parents.Length != 2) throw new InvalidOperationException("ERROR in PositionBasedCrossover: The number of parents is not equal to 2");
99      return Apply(random, parents[0], parents[1]);
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.