Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Permutation/EdgeRecombinationCrossover.cs @ 10145

Last change on this file since 10145 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.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  public class EdgeRecombinationCrossover : PermutationCrossoverBase {
30    public override string Description {
31      get { return @"TODO\r\nOperator description still missing ..."; }
32    }
33
34    public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
35      int length = parent1.Length;
36      int[] result = new int[length];
37      int[,] edgeList = new int[length, 4];
38      bool[] remainingNumbers = new bool[length];
39      int index, currentEdge, currentNumber, nextNumber, currentEdgeCount, minEdgeCount;
40
41      for (int i = 0; i < length; i++) {  // generate edge list for every number
42        remainingNumbers[i] = true;
43
44        index = 0;
45        while ((index < length) && (parent1[index] != i)) {  // search edges in parent1
46          index++;
47        }
48        if (index == length) {
49          throw (new InvalidOperationException("Permutation doesn't contain number " + i + "."));
50        } else {
51          edgeList[i, 0] = parent1[(index - 1 + length) % length];
52          edgeList[i, 1] = parent1[(index + 1) % length];
53        }
54        index = 0;
55        while ((index < length) && (parent2[index] != i)) {  // search edges in parent2
56          index++;
57        }
58        if (index == length) {
59          throw (new InvalidOperationException("Permutation doesn't contain number " + i + "."));
60        } else {
61          currentEdge = parent2[(index - 1 + length) % length];
62          if ((edgeList[i, 0] != currentEdge) && (edgeList[i, 1] != currentEdge)) {  // new edge found ?
63            edgeList[i, 2] = currentEdge;
64          } else {
65            edgeList[i, 2] = -1;
66          }
67          currentEdge = parent2[(index + 1) % length];
68          if ((edgeList[i, 0] != currentEdge) && (edgeList[i, 1] != currentEdge)) {  // new edge found ?
69            edgeList[i, 3] = currentEdge;
70          } else {
71            edgeList[i, 3] = -1;
72          }
73        }
74      }
75
76      currentNumber = random.Next(length);  // get number to start
77      for (int i = 0; i < length; i++) {
78        result[i] = currentNumber;
79        remainingNumbers[currentNumber] = false;
80
81        for (int j = 0; j < 4; j++) {  // remove all edges to / from currentNumber
82          if (edgeList[currentNumber, j] != -1) {
83            for (int k = 0; k < 4; k++) {
84              if (edgeList[edgeList[currentNumber, j], k] == currentNumber) {
85                edgeList[edgeList[currentNumber, j], k] = -1;
86              }
87            }
88          }
89        }
90
91        minEdgeCount = 5;  // every number hasn't more than 4 edges
92        nextNumber = -1;
93        for (int j = 0; j < 4; j++) {  // find next number with least edges
94          if (edgeList[currentNumber, j] != -1) {  // next number found
95            currentEdgeCount = 0;
96            for (int k = 0; k < 4; k++) {  // count edges of next number
97              if (edgeList[edgeList[currentNumber, j], k] != -1) {
98                currentEdgeCount++;
99              }
100            }
101            if ((currentEdgeCount < minEdgeCount) ||
102              ((currentEdgeCount == minEdgeCount) && (random.NextDouble() < 0.5))) {
103              nextNumber = edgeList[currentNumber, j];
104              minEdgeCount = currentEdgeCount;
105            }
106          }
107        }
108        currentNumber = nextNumber;
109        if (currentNumber == -1) {  // current number has no more edge
110          index = 0;
111          while ((index < length) && (!remainingNumbers[index])) {  // choose next remaining number
112            index++;
113          }
114          if (index < length) {
115            currentNumber = index;
116          }
117        }
118      }
119      return result;
120    }
121
122    protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
123      return Apply(random, parent1, parent2);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.