Changeset 3369 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers
- Timestamp:
- 04/16/10 12:12:29 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs
r3338 r3369 61 61 SymbolicExpressionTreeNode crossoverPoint0; 62 62 int replacedSubtreeIndex; 63 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, out crossoverPoint0, out replacedSubtreeIndex);63 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeSize - 1, maxTreeHeight - 1, out crossoverPoint0, out replacedSubtreeIndex); 64 64 65 65 // calculate the max size and height that the inserted branch can have … … 67 67 int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0); 68 68 69 var allowedBranches = from branch in parent1.Root.IterateNodesPrefix() 70 where branch.GetSize() < maxInsertedBranchSize 71 where branch.GetHeight() < maxInsertedBranchHeight 72 // where crossoverPoint0.GetAllowedSymbols(replacedSubtreeIndex).Contains(branch.Symbol) 73 where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch) 74 select branch; 69 var allowedBranches = (from branch in parent1.Root.IterateNodesPrefix() 70 where branch.GetSize() < maxInsertedBranchSize 71 where branch.GetHeight() < maxInsertedBranchHeight 72 where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch) 73 select branch).ToList(); 75 74 76 75 if (allowedBranches.Count() == 0) { … … 90 89 91 90 private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) { 92 // cannot compare dynamic symbols by reference because new instances of Invoke/Argument are created on demand 93 // instead we must check equality by names and number of arguments 94 var branchSymbols = (from node in branch.IterateNodesPrefix() 95 select new { FunctionName = node.Symbol.Name, SubtreeCount = node.SubTrees.Count }).Distinct(); 91 // check point type for the whole branch 92 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 } 96 97 97 98 // check syntax constraints of direct parent - child relation 98 var matchingSymbolInParentGrammar = (from sym in parent.GetAllowedSymbols(replacedSubtreeIndex) 99 where sym.Name == branch.Symbol.Name 100 select sym).SingleOrDefault(); 101 if (matchingSymbolInParentGrammar == null || 102 branch.SubTrees.Count < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) || 103 branch.SubTrees.Count > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false; 104 105 // check point type for the whole branch 106 foreach (var branchSymbol in branchSymbols) { 107 matchingSymbolInParentGrammar = (from sym in parent.Grammar.Symbols 108 where sym.Name == branchSymbol.FunctionName 109 select sym).SingleOrDefault(); 110 if (matchingSymbolInParentGrammar == null || 111 branchSymbol.SubtreeCount < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) || 112 branchSymbol.SubtreeCount > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false; 113 } 99 if (!parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false; 114 100 115 101 return true; 116 102 } 117 103 118 private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {104 private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 119 105 var crossoverPoints = from branch in parent0.Root.IterateNodesPrefix() 120 106 where branch.SubTrees.Count > 0 121 107 where branch != parent0.Root 108 where branch.GetSize() < maxBranchSize 109 where branch.GetHeight() < maxBranchHeight 122 110 from index in Enumerable.Range(0, branch.SubTrees.Count) 123 111 let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 } … … 151 139 private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<SymbolicExpressionTreeNode> branches, double internalNodeProbability) { 152 140 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 153 var groupedBranches = from branch in branches154 group branch by branch.SubTrees.Count into g155 select g;141 var groupedBranches = (from branch in branches 142 group branch by branch.SubTrees.Count into g 143 select g).ToList(); 156 144 var allowedInternalBranches = (from g in groupedBranches 157 145 where g.Key > 0 … … 163 151 select leaf).ToList(); 164 152 if (allowedInternalBranches.Count == 0) { 165 return allowedLeafBranches [random.Next(allowedLeafBranches.Count)];153 return allowedLeafBranches.SelectRandom(random); 166 154 } else if (allowedLeafBranches.Count == 0) { 167 return allowedInternalBranches [random.Next(allowedInternalBranches.Count)];155 return allowedInternalBranches.SelectRandom(random); 168 156 } else if (random.NextDouble() < internalNodeProbability) { 169 157 // when leaf and internal nodes are possible then choose either a leaf or internal node with internalNodeProbability 170 return allowedInternalBranches [random.Next(allowedInternalBranches.Count)];158 return allowedInternalBranches.SelectRandom(random); 171 159 } else { 172 return allowedLeafBranches [random.Next(allowedLeafBranches.Count)];160 return allowedLeafBranches.SelectRandom(random); 173 161 } 174 162 }
Note: See TracChangeset
for help on using the changeset viewer.