Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/OrderCrossover.cs @ 1218

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

Refactoring of crossover operators (#470)

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 permuation arrays
31  /// by taking a randomly chosen interval from the frist, preserving the positions,
32  /// then the missing values from the second array in the order they occur in the second array.
33  /// </summary>
34  public class OrderCrossover : PermutationCrossoverBase {
35    /// <inheritdoc select="summary"/>
36    public override string Description {
37      get { return @"TODO\r\nOperator description still missing ..."; }
38    }
39
40    /// <summary>
41    /// Performs a cross over permutation of <paramref name="parent1"/> and
42    /// <paramref name="parent2"/> by taking a randomly chosen interval from <paramref name="parent1"/>,
43    /// preserving the positions and then the missing values from <paramref name="parent2"/> in the
44    /// order they occur in <paramref name="parent2"/>.
45    /// </summary>
46    /// <param name="random">The random number generator.</param>
47    /// <param name="parent1">The parent scope 1 to cross over.</param>
48    /// <param name="parent2">The parent scope 2 to cross over.</param>
49    /// <returns>The created cross over permutation as int array.</returns>
50    public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
51      int[] result = new int[parent1.Length];
52      bool[] copied = new bool[result.Length];
53
54      int breakPoint1 = random.Next(result.Length - 1);
55      int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);
56
57      for (int i = breakPoint1; i <= breakPoint2; i++) {  // copy part of first permutation
58        result[i] = parent1[i];
59        copied[parent1[i]] = true;
60      }
61
62      int index = 0;
63      for (int i = 0; i < parent2.Length; i++) {  // copy remaining part of second permutation
64        if (index == breakPoint1) {  // skip already copied part
65          index = breakPoint2 + 1;
66        }
67        if (!copied[parent2[i]]) {
68          result[index] = parent2[i];
69          index++;
70        }
71      }
72      return result;
73    }
74
75    /// <summary>
76    /// Performs an order crossover operation for two given parent permutations.
77    /// </summary>
78    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
79    /// <param name="scope">The current scope.</param>
80    /// <param name="random">A random number generator.</param>
81    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
82    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
83    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
84      if (parents.Length != 2) throw new InvalidOperationException("ERROR in OrderCrossover: The number of parents is not equal to 2");
85      return Apply(random, parents[0], parents[1]);
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.