Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/17/16 15:42:19 (8 years ago)
Author:
gkronber
Message:

#2650: merged r14457:14494 from trunk to branch (resolving conflicts)

Location:
branches/symbreg-factors-2650
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650

  • branches/symbreg-factors-2650/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers/GroupCrossover.cs

    r14185 r14498  
    3939      return new GroupCrossover(this, cloner);
    4040    }
    41 
     41   
    4242    public static LinearLinkage Apply(IRandom random, LinearLinkage p1, LinearLinkage p2) {
    4343      var length = p1.Length;
    44       var child = new LinearLinkage(length);
    45       var endNodes = new HashSet<int>();
    46       for (var i = 0; i < length; i++) {
    47         if ((p1[i] == i && p2[i] == i)
    48           || ((p1[i] == i || p2[i] == i) && random.NextDouble() < 0.5)) {
    49           child[i] = i;
    50           endNodes.Add(i);
     44      var lleeP1 = p1.ToEndLinks();
     45      var lleeP2 = p2.ToEndLinks();
     46      var lleeChild = new int[length];
     47      var isTransfered = new bool[length];
     48
     49      for (var i = p1.Length - 1; i >= 0; i--) {
     50        lleeChild[i] = i;
     51
     52        // Step 1
     53        var isEndP1 = p1[i] == i;
     54        var isEndP2 = p2[i] == i;
     55        if (isEndP1 & isEndP2 || (isEndP1 | isEndP2 && random.NextDouble() < 0.5)) {
     56          isTransfered[i] = true;
     57          continue;
     58        }
     59
     60        // Step 2
     61        var end1 = lleeP1[i];
     62        var end2 = lleeP2[i];
     63
     64        if (isTransfered[end1] & isTransfered[end2]) {
     65          var end = random.NextDouble() < 0.5 ? end1 : end2;
     66          lleeChild[i] = end;
     67        } else if (isTransfered[end1]) {
     68          lleeChild[i] = end1;
     69        } else if (isTransfered[end2]) {
     70          lleeChild[i] = end2;
     71        } else {
     72          var next = random.NextDouble() < 0.5 ? p1[i] : p2[i];
     73          var end = lleeChild[next];
     74          lleeChild[i] = end;
    5175        }
    5276      }
    53       for (var i = 0; i < length; i++) {
    54         if (endNodes.Contains(i)) continue;
    55         var p1End = endNodes.Contains(p1[i]);
    56         var p2End = endNodes.Contains(p2[i]);
    57         if ((p1End && p2End) || (!p1End && !p2End)) {
    58           child[i] = random.NextDouble() < 0.5 ? p1[i] : p2[i];
    59         } else if (p1End) {
    60           child[i] = p1[i];
    61         } else {
    62           child[i] = p2[i];
    63         }
    64       }
    65       child.LinearizeTreeStructures();
    66       return child;
     77      return LinearLinkage.FromEndLinks(lleeChild);
    6778    }
    6879
Note: See TracChangeset for help on using the changeset viewer.