Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Permutation/3.2/PartiallyMatchedCrossover.cs @ 5740

Last change on this file since 5740 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: 4.4 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 partially matched crossover operation for two given parent permutations.
90    /// </summary>
91    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
92    /// <param name="scope">The current scope.</param>
93    /// <param name="random">A random number generator.</param>
94    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
95    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
96    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
97      if (parents.Length != 2) throw new InvalidOperationException("ERROR in PartiallyMatchedCrossover: The number of parents is not equal to 2");
98      return Apply(random, parents[0], parents[1]);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.