Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3580 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 4.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2854]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[3376]23using HeuristicLab.Common;
[2]24using HeuristicLab.Core;
[2854]25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]26
[3053]27namespace HeuristicLab.Encodings.PermutationEncoding {
[850]28  /// <summary>
29  /// Performs a cross over permutation between two permutation arrays based on randomly chosen positions.
30  /// </summary>
[2854]31  /// <remarks>
[2871]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.
[2854]33  /// </remarks>
[2871]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.")]
[3017]35  [StorableClass]
[2854]36  public class PositionBasedCrossover : PermutationCrossover {
[850]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>
[2854]41    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
[850]42    /// <param name="random">The random number generator.</param>
[2854]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.");
[2]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      }
[2854]77
[3231]78      return new Permutation(parent1.PermutationType, result);
[2]79    }
80
[850]81    /// <summary>
[2871]82    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
[850]83    /// </summary>
[1218]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>
[2854]88    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
[1218]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]);
[2]91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.