Changeset 12288 for branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers
- Timestamp:
- 04/06/15 22:41:24 (10 years ago)
- Location:
- branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers/GroupCrossover.cs
r12285 r12288 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 53 54 for (var i = 0; i < length; i++) { 54 55 if (endNodes.Contains(i)) continue; 55 if (endNodes.Contains(p1[i]) && endNodes.Contains(p2[i])) { 56 var p1End = endNodes.Contains(p1[i]); 57 var p2End = endNodes.Contains(p2[i]); 58 if ((p1End && p2End) || (!p1End && !p2End)) { 56 59 child[i] = random.NextDouble() < 0.5 ? p1[i] : p2[i]; 57 } else if ( endNodes.Contains(p1[i])) {60 } else if (p1End) { 58 61 child[i] = p1[i]; 59 } else if (endNodes.Contains(p2[i])){62 } else { 60 63 child[i] = p2[i]; 61 } else {62 var a = p1[i];63 var b = p2[i];64 while (true) {65 if (endNodes.Contains(p2[a])) {66 child[i] = p2[a];67 break;68 }69 if (endNodes.Contains(p1[b])) {70 child[i] = p1[b];71 break;72 }73 var tmp = b;74 b = p2[a];75 a = p1[tmp];76 }77 64 } 78 65 } 66 child.LinearizeTreeStructures(); 79 67 return child; 80 68 } -
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers/LowestIndexFirstCrossover.cs
r12285 r12288 42 42 public static LinearLinkage Apply(IRandom random, ItemArray<LinearLinkage> parents) { 43 43 var len = parents[0].Length; 44 var p = random.Next(parents.Length); 44 45 var child = new LinearLinkage(len); 45 var childGroup = new List<HashSet<int>>(); 46 var currentParent = random.Next(parents.Length); 47 var remaining = new HashSet<int>(Enumerable.Range(0, len)); 46 var remaining = new SortedSet<int>(Enumerable.Range(0, len)); 48 47 do { 49 var i = remaining.Min(); 50 var group = new HashSet<int>(parents[currentParent].GetGroupForward(i)); 51 group.IntersectWith(remaining); 52 childGroup.Add(group); 53 remaining.ExceptWith(group); 54 currentParent = (currentParent + 1) % parents.Length; 48 var i = remaining.Min; 49 foreach (var g in parents[p].GetGroupForward(i)) { 50 if (!remaining.Contains(g)) continue; 51 child[i] = g; 52 i = g; 53 remaining.Remove(g); 54 } 55 child[i] = i; 56 remaining.Remove(i); 57 58 p = (p + 1) % parents.Length; 55 59 } while (remaining.Count > 0); 56 60 57 child.SetGroups(childGroup);58 61 return child; 59 62 } -
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Crossovers/LowestIndexMaxCrossover.cs
r12285 r12288 45 45 var len = parents[0].Length; 46 46 var child = new LinearLinkage(len); 47 var childGroup = new List<HashSet<int>>(); 48 var remaining = new HashSet<int>(Enumerable.Range(0, len)); 47 var remaining = new SortedSet<int>(Enumerable.Range(0, len)); 49 48 do { 50 var i = remaining.Min(); 51 var groups = parents.Select(x => new HashSet<int>(x.GetGroupForward(i))).ToList(); 52 for (var k = 0; k < groups.Count; k++) 53 groups[k].IntersectWith(remaining); 54 55 var maxGroup = groups.Select((v, idx) => Tuple.Create(idx, v)).MaxItems(x => x.Item2.Count).SampleRandom(random).Item1; 56 childGroup.Add(groups[maxGroup]); 57 remaining.ExceptWith(groups[maxGroup]); 49 var groups = parents.Select(x => x.GetGroupForward(remaining.Min).Where(y => remaining.Contains(y)).ToList()).ToList(); 50 var max = groups.Select((v, idx) => Tuple.Create(idx, v.Count)).MaxItems(x => x.Item2).SampleRandom(random).Item1; 51 var i = groups[max][0]; 52 for (var k = 1; k < groups[max].Count; k++) { 53 child[i] = groups[max][k]; 54 remaining.Remove(i); 55 i = child[i]; 56 } 57 child[i] = i; 58 remaining.Remove(i); 58 59 } while (remaining.Count > 0); 59 60 60 child.SetGroups(childGroup);61 61 return child; 62 62 }
Note: See TracChangeset
for help on using the changeset viewer.