Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.2/EdgeRecombinationCrossover.cs @ 2173

Last change on this file since 2173 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: 6.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 permutation arrays by calculating the edges (neighbours)
31  /// of each element. Starts at a randomly chosen position, the next element is a neighbour with the least
32  /// number of neighbours, the next again a neighbour and so on.
33  /// </summary>
34  public class EdgeRecombinationCrossover : 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 <paramref name="2"/>
42    /// by calculating the edges of each element. Starts at a randomly chosen position,
43    /// the next element is a neighbour with the least
44    /// number of neighbours, the next again a neighbour and so on.
45    /// </summary>
46    /// <exception cref="InvalidOperationException">Thrown when the permutation lacks a number.
47    /// </exception>
48    /// <param name="random">The random number generator.</param>
49    /// <param name="parent1">The parent scope 1 to cross over.</param>
50    /// <param name="parent2">The parent scope 2 to cross over.</param>
51    /// <returns>The created cross over permutation as int array.</returns>
52    public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
53      int length = parent1.Length;
54      int[] result = new int[length];
55      int[,] edgeList = new int[length, 4];
56      bool[] remainingNumbers = new bool[length];
57      int index, currentEdge, currentNumber, nextNumber, currentEdgeCount, minEdgeCount;
58
59      for (int i = 0; i < length; i++) {  // generate edge list for every number
60        remainingNumbers[i] = true;
61
62        index = 0;
63        while ((index < length) && (parent1[index] != i)) {  // search edges in parent1
64          index++;
65        }
66        if (index == length) {
67          throw (new InvalidOperationException("Permutation doesn't contain number " + i + "."));
68        } else {
69          edgeList[i, 0] = parent1[(index - 1 + length) % length];
70          edgeList[i, 1] = parent1[(index + 1) % length];
71        }
72        index = 0;
73        while ((index < length) && (parent2[index] != i)) {  // search edges in parent2
74          index++;
75        }
76        if (index == length) {
77          throw (new InvalidOperationException("Permutation doesn't contain number " + i + "."));
78        } else {
79          currentEdge = parent2[(index - 1 + length) % length];
80          if ((edgeList[i, 0] != currentEdge) && (edgeList[i, 1] != currentEdge)) {  // new edge found ?
81            edgeList[i, 2] = currentEdge;
82          } else {
83            edgeList[i, 2] = -1;
84          }
85          currentEdge = parent2[(index + 1) % length];
86          if ((edgeList[i, 0] != currentEdge) && (edgeList[i, 1] != currentEdge)) {  // new edge found ?
87            edgeList[i, 3] = currentEdge;
88          } else {
89            edgeList[i, 3] = -1;
90          }
91        }
92      }
93
94      currentNumber = random.Next(length);  // get number to start
95      for (int i = 0; i < length; i++) {
96        result[i] = currentNumber;
97        remainingNumbers[currentNumber] = false;
98
99        for (int j = 0; j < 4; j++) {  // remove all edges to / from currentNumber
100          if (edgeList[currentNumber, j] != -1) {
101            for (int k = 0; k < 4; k++) {
102              if (edgeList[edgeList[currentNumber, j], k] == currentNumber) {
103                edgeList[edgeList[currentNumber, j], k] = -1;
104              }
105            }
106          }
107        }
108
109        minEdgeCount = 5;  // every number hasn't more than 4 edges
110        nextNumber = -1;
111        for (int j = 0; j < 4; j++) {  // find next number with least edges
112          if (edgeList[currentNumber, j] != -1) {  // next number found
113            currentEdgeCount = 0;
114            for (int k = 0; k < 4; k++) {  // count edges of next number
115              if (edgeList[edgeList[currentNumber, j], k] != -1) {
116                currentEdgeCount++;
117              }
118            }
119            if ((currentEdgeCount < minEdgeCount) ||
120              ((currentEdgeCount == minEdgeCount) && (random.NextDouble() < 0.5))) {
121              nextNumber = edgeList[currentNumber, j];
122              minEdgeCount = currentEdgeCount;
123            }
124          }
125        }
126        currentNumber = nextNumber;
127        if (currentNumber == -1) {  // current number has no more edge
128          index = 0;
129          while ((index < length) && (!remainingNumbers[index])) {  // choose next remaining number
130            index++;
131          }
132          if (index < length) {
133            currentNumber = index;
134          }
135        }
136      }
137      return result;
138    }
139
140    /// <summary>
141    /// Performs an edge recombination crossover operation for two given parent permutations.
142    /// </summary>
143    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
144    /// <param name="scope">The current scope.</param>
145    /// <param name="random">A random number generator.</param>
146    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
147    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
148    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
149      if (parents.Length != 2) throw new InvalidOperationException("ERROR in EdgeRecombinationCrossover: The number of parents is not equal to 2");
150      return Apply(random, parents[0], parents[1]);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.