Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/PartiallyMatchedCrossover.cs @ 898

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

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

File size: 4.6 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 permuation arrays
31  /// by taking a randomly chosen interval from the first, keeping the position,
32  /// then all positions from the second permutation which are still free in the child
33  /// (the position is free and the value is "free")
34  /// and then missing ones from the second array in the order they occur in the second array.
35  /// </summary>
36  public class PartiallyMatchedCrossover : PermutationCrossoverBase {
37    /// <inheritdoc select="summary"/>
38    public override string Description {
39      get { return @"TODO\r\nOperator description still missing ..."; }
40    }
41
42    /// <summary>
43    /// Performs a cross over permuation of <paramref name="parent1"/> and <paramref name="parent2"/>
44    /// by taking a randomly chosen interval from <paramref name="parent1"/>, preserving the position,
45    /// then all positions from <paramref name="parent2"/> which are still free in the child
46    /// (the position is free and the value is "free")
47    /// and then missing ones from <paramref name="parent2"/> in the order they occur
48    /// in <paramref name="parent2"/>.
49    /// </summary>
50    /// <param name="random">The random number generator.</param>
51    /// <param name="parent1">The parent scope 1 to cross over.</param>
52    /// <param name="parent2">The parent scope 2 to cross over.</param>
53    /// <returns>The created cross over permutation as int array.</returns>
54    public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
55      int length = parent1.Length;
56      int[] result = new int[length];
57      bool[] numbersCopied = new bool[length];
58
59      int breakPoint1 = random.Next(length - 1);
60      int breakPoint2 = random.Next(breakPoint1 + 1, length);
61
62      for (int j = breakPoint1; j <= breakPoint2; j++) {  // copy part of first permutation
63        result[j] = parent1[j];
64        numbersCopied[result[j]] = true;
65      }
66
67      int index = breakPoint1;
68      int i = (breakPoint1 == 0 ? (breakPoint2 + 1) : 0);
69      while (i < length) {  // copy rest from second permutation
70        if (!numbersCopied[parent2[i]]) {  // copy directly
71          result[i] = parent2[i];
72        } else {  // copy from area between breakpoints
73          while (numbersCopied[parent2[index]]) {  // find next valid index
74            index++;
75          }
76          result[i] = parent2[index];
77          index++;
78        }
79
80        i++;
81        if (i == breakPoint1) {  // skip area between breakpoints
82          i = breakPoint2 + 1;
83        }
84      }
85      return result;
86    }
87
88    /// <summary>
89    /// Performs a cross over permuation of <paramref name="parent1"/> and <paramref name="parent2"/>
90    /// by taking a randomly chosen interval from <paramref name="parent1"/>, preserving the position,
91    /// then all positions from <paramref name="parent2"/> which are still free in the child
92    /// (the position is free and the value is "free")
93    /// and then missing ones from <paramref name="parent2"/> in the order they occur
94    /// in <paramref name="parent2"/>.
95    /// </summary>
96    /// <remarks>Calls <see cref="Apply"/>.</remarks>
97    /// <param name="scope">The current scope.</param>
98    /// <param name="random">The random number generator.</param>
99    /// <param name="parent1">The parent scope 1 to cross over.</param>
100    /// <param name="parent2">The parent scope 2 to cross over.</param>
101    /// <returns>The created cross over permutation as int array.</returns>
102    protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
103      return Apply(random, parent1, parent2);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.