Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2080 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
27
28namespace HeuristicLab.Permutation {
29  /// <summary>
30  /// Performs a cross over permutation between two permutation arrays based on randomly chosen positions.
31  /// </summary>
32  public class PositionBasedCrossover : PermutationCrossoverBase {
33    /// <inheritdoc select="summary"/>
34    public override string Description {
35      get { return @"TODO\r\nOperator description still missing ..."; }
36    }
37
38    /// <summary>
39    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
40    /// based on randomly chosen positions to define which position to take from where.
41    /// </summary>
42    /// <param name="random">The random number generator.</param>
43    /// <param name="parent1">The permutation array of parent 1.</param>
44    /// <param name="parent2">The permutation array of parent 2.</param>
45    /// <returns>The created cross over permutation as int array.</returns>
46    public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
47      int length = parent1.Length;
48      int[] result = new int[length];
49      bool[] randomPosition = new bool[length];
50      bool[] numberCopied = new bool[length];
51      int randomPosNumber = random.Next(length);
52
53      for (int i = 0; i < randomPosNumber; i++) {  // generate random bit mask
54        randomPosition[random.Next(length)] = true;
55      }
56
57      for (int i = 0; i < length; i++) {  // copy numbers masked as true from second permutation
58        if (randomPosition[i]) {
59          result[i] = parent2[i];
60          numberCopied[parent2[i]] = true;
61        }
62      }
63
64      int index = 0;
65      for (int i = 0; i < length; i++) {  // copy numbers masked as false from first permutation
66        if (!numberCopied[parent1[i]]) {
67          if (randomPosition[index]) {
68            while (randomPosition[index]) {
69              index++;
70            }
71          }
72          result[index] = parent1[i];
73          index++;
74        }
75      }
76      return result;
77    }
78
79    /// <summary>
80    /// Performs a position-based crossover operation for two given parent permutations.
81    /// </summary>
82    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
83    /// <param name="scope">The current scope.</param>
84    /// <param name="random">A random number generator.</param>
85    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
86    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
87    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
88      if (parents.Length != 2) throw new InvalidOperationException("ERROR in PositionBasedCrossover: The number of parents is not equal to 2");
89      return Apply(random, parents[0], parents[1]);
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.