Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Permutation/PositionBasedCrossover.cs @ 1968

Last change on this file since 1968 was 850, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Permutation namespace (#331)

File size: 3.7 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 cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
81    /// based on randomly chosen positions to define which position to take from where.
82    /// </summary>
83    /// <remarks>Calls <see cref="Apply"/>.</remarks>
84    /// <param name="scope">The current scope.</param>
85    /// <param name="random">The random number generator.</param>
86    /// <param name="parent1">The permutation array of parent 1.</param>
87    /// <param name="parent2">The permutation array of parent 2.</param>
88    /// <returns>The created cross over permutation as int array.</returns>
89    protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
90      return Apply(random, parent1, parent2);
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.