Changeset 3997 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers
- Timestamp:
- 07/06/10 11:59:50 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs
r3989 r3997 67 67 int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0); 68 68 69 var allowedBranches = (from branch in parent1.Root.IterateNodesPostfix() 70 where branch.GetSize() < maxInsertedBranchSize 71 where branch.GetHeight() < maxInsertedBranchHeight 72 where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch) 73 select branch).ToList(); 69 List<SymbolicExpressionTreeNode> allowedBranches = new List<SymbolicExpressionTreeNode>(); 70 parent1.Root.ForEachNodePostfix((n) => { 71 if (n.GetSize() < maxInsertedBranchSize && 72 n.GetHeight() < maxInsertedBranchHeight && 73 IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, n)) 74 allowedBranches.Add(n); 75 }); 74 76 75 if (allowedBranches.Count ()== 0) {77 if (allowedBranches.Count == 0) { 76 78 success = false; 77 79 return parent0; … … 89 91 90 92 private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) { 91 // check point type for the whole branch92 foreach (var node in branch.IterateNodesPostfix()) {93 if (!parent.Grammar.ContainsSymbol(node.Symbol)) return false;94 else if (node.SubTrees.Count < parent.Grammar.GetMinSubtreeCount(node.Symbol)) return false;95 else if (node.SubTrees.Count > parent.Grammar.GetMaxSubtreeCount(node.Symbol)) return false;96 }97 98 93 // check syntax constraints of direct parent - child relation 99 94 if (!parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false; 100 95 101 return true; 96 bool result = true; 97 // check point type for the whole branch 98 branch.ForEachNodePostfix((n) => { 99 result &= n.SubTrees.Count >= parent.Grammar.GetMinSubtreeCount(n.Symbol); 100 result &= n.SubTrees.Count <= parent.Grammar.GetMaxSubtreeCount(n.Symbol); 101 result &= parent.Grammar.ContainsSymbol(n.Symbol); 102 }); 103 return result; 102 104 } 103 105 104 106 private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 105 var crossoverPoints = (from branch in parent0.Root.IterateNodesPostfix() 106 where branch.SubTrees.Count > 0 107 where branch != parent0.Root 108 where branch.GetSize() < maxBranchSize 109 where branch.GetHeight() < maxBranchHeight 110 from index in Enumerable.Range(0, branch.SubTrees.Count) 111 let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 } 112 select p).ToList(); 113 var internalCrossoverPoints = (from p in crossoverPoints 114 where !p.IsLeaf 115 select p).ToList(); 116 var leafCrossoverPoints = (from p in crossoverPoints 117 where p.IsLeaf 118 select p).ToList(); 119 if (internalCrossoverPoints.Count == 0) { 107 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 108 List<CrossoverPoint> internalCrossoverPoints = new List<CrossoverPoint>(); 109 List<CrossoverPoint> leafCrossoverPoints = new List<CrossoverPoint>(); 110 parent0.Root.ForEachNodePostfix((n) => { 111 if (n.SubTrees.Count > 0 && 112 n.GetSize() < maxBranchSize && 113 n.GetHeight() < maxBranchHeight && 114 n != parent0.Root 115 ) { 116 foreach (var child in n.SubTrees) { 117 if (child.SubTrees.Count > 0) 118 internalCrossoverPoints.Add(new CrossoverPoint(n, child)); 119 else 120 leafCrossoverPoints.Add(new CrossoverPoint(n, child)); 121 } 122 } 123 }); 124 if (random.NextDouble() < internalNodeProbability) { 125 // select from internal node if possible 126 if (internalCrossoverPoints.Count > 0) { 127 // select internal crossover point or leaf 128 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 129 crossoverPoint = selectedCrossoverPoint.Parent; 130 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 131 } else { 132 // otherwise select external node 133 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 134 crossoverPoint = selectedCrossoverPoint.Parent; 135 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 136 } 137 } else if (leafCrossoverPoints.Count > 0) { 138 // select from leaf crossover point if possible 120 139 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 121 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; 122 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 123 } else if (leafCrossoverPoints.Count == 0) { 124 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 125 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; 126 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 127 } else if (random.NextDouble() < internalNodeProbability && internalCrossoverPoints.Count > 0) { 128 // select internal crossover point or leaf 129 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 130 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; 140 crossoverPoint = selectedCrossoverPoint.Parent; 131 141 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 132 142 } else { 133 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 134 crossoverPoint = selectedCrossoverPoint.CrossoverPoint; 143 // otherwise select internal crossover point 144 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 145 crossoverPoint = selectedCrossoverPoint.Parent; 135 146 subtreeIndex = selectedCrossoverPoint.SubtreeIndex; 136 147 } … … 139 150 private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<SymbolicExpressionTreeNode> branches, double internalNodeProbability) { 140 151 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 141 var allowedInternalBranches = (from branch in branches 152 List<SymbolicExpressionTreeNode> allowedInternalBranches; 153 List<SymbolicExpressionTreeNode> allowedLeafBranches; 154 if (random.NextDouble() < internalNodeProbability) { 155 // select internal node if possible 156 allowedInternalBranches = (from branch in branches 157 where branch.SubTrees.Count > 0 158 select branch).ToList(); 159 if (allowedInternalBranches.Count > 0) { 160 return allowedInternalBranches.SelectRandom(random); 161 } else { 162 // no internal nodes allowed => select leaf nodes 163 allowedLeafBranches = (from branch in branches 164 where branch.SubTrees.Count == 0 165 select branch).ToList(); 166 return allowedLeafBranches.SelectRandom(random); 167 } 168 } else { 169 // select leaf node if possible 170 allowedLeafBranches = (from branch in branches 171 where branch.SubTrees.Count == 0 172 select branch).ToList(); 173 if (allowedLeafBranches.Count > 0) { 174 return allowedLeafBranches.SelectRandom(random); 175 } else { 176 allowedInternalBranches = (from branch in branches 142 177 where branch.SubTrees.Count > 0 143 178 select branch).ToList(); 144 var allowedLeafBranches = (from branch in branches 145 where branch.SubTrees.Count == 0 146 select branch).ToList(); 147 if (allowedInternalBranches.Count == 0) { 148 return allowedLeafBranches.SelectRandom(random); 149 } else if (allowedLeafBranches.Count == 0) { 150 return allowedInternalBranches.SelectRandom(random); 151 } else if (random.NextDouble() < internalNodeProbability) { 152 // when leaf and internal nodes are possible then choose either a leaf or internal node with internalNodeProbability 153 return allowedInternalBranches.SelectRandom(random); 154 } else { 155 return allowedLeafBranches.SelectRandom(random); 179 return allowedInternalBranches.SelectRandom(random); 180 } 156 181 } 157 182 }
Note: See TracChangeset
for help on using the changeset viewer.