Changeset 5686 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers
- Timestamp:
- 03/15/11 13:34:38 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r5549 r5686 120 120 // check syntax constraints of direct parent - child relation 121 121 if (!parent.Grammar.ContainsSymbol(branch.Symbol) || 122 !parent.Grammar.IsAllowedChild (parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false;122 !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false; 123 123 124 124 bool result = true; … … 128 128 result && 129 129 parent.Grammar.ContainsSymbol(n.Symbol) && 130 n.Sub Trees.Count() >= parent.Grammar.GetMinSubtreeCount(n.Symbol) &&131 n.Sub Trees.Count() <= parent.Grammar.GetMaxSubtreeCount(n.Symbol);130 n.SubtreesCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) && 131 n.SubtreesCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol); 132 132 }); 133 133 return result; … … 136 136 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 137 137 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 138 List<C rossoverPoint> internalCrossoverPoints = new List<CrossoverPoint>();139 List<C rossoverPoint> leafCrossoverPoints = new List<CrossoverPoint>();138 List<CutPoint> internalCrossoverPoints = new List<CutPoint>(); 139 List<CutPoint> leafCrossoverPoints = new List<CutPoint>(); 140 140 parent0.Root.ForEachNodePostfix((n) => { 141 if (n.SubTrees. Count() > 0&& n != parent0.Root) {141 if (n.SubTrees.Any() && n != parent0.Root) { 142 142 foreach (var child in n.SubTrees) { 143 143 if (child.GetLength() <= maxBranchLength && 144 144 child.GetDepth() <= maxBranchDepth) { 145 if (child.SubTrees. Count() > 0)146 internalCrossoverPoints.Add(new C rossoverPoint(n, child));145 if (child.SubTrees.Any()) 146 internalCrossoverPoints.Add(new CutPoint(n, child)); 147 147 else 148 leafCrossoverPoints.Add(new C rossoverPoint(n, child));148 leafCrossoverPoints.Add(new CutPoint(n, child)); 149 149 } 150 150 } … … 158 158 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 159 159 crossoverPoint = selectedCrossoverPoint.Parent; 160 subtreeIndex = selectedCrossoverPoint. SubtreeIndex;160 subtreeIndex = selectedCrossoverPoint.ChildIndex; 161 161 } else { 162 162 // otherwise select external node 163 163 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 164 164 crossoverPoint = selectedCrossoverPoint.Parent; 165 subtreeIndex = selectedCrossoverPoint. SubtreeIndex;165 subtreeIndex = selectedCrossoverPoint.ChildIndex; 166 166 } 167 167 } else if (leafCrossoverPoints.Count > 0) { … … 169 169 var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)]; 170 170 crossoverPoint = selectedCrossoverPoint.Parent; 171 subtreeIndex = selectedCrossoverPoint. SubtreeIndex;171 subtreeIndex = selectedCrossoverPoint.ChildIndex; 172 172 } else { 173 173 // otherwise select internal crossover point 174 174 var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)]; 175 175 crossoverPoint = selectedCrossoverPoint.Parent; 176 subtreeIndex = selectedCrossoverPoint. SubtreeIndex;176 subtreeIndex = selectedCrossoverPoint.ChildIndex; 177 177 } 178 178 } … … 185 185 // select internal node if possible 186 186 allowedInternalBranches = (from branch in branches 187 where branch.SubTrees. Count() > 0187 where branch.SubTrees.Any() 188 188 select branch).ToList(); 189 189 if (allowedInternalBranches.Count > 0) { … … 192 192 // no internal nodes allowed => select leaf nodes 193 193 allowedLeafBranches = (from branch in branches 194 where branch.SubTrees.Count() == 0194 where !branch.SubTrees.Any() 195 195 select branch).ToList(); 196 196 return allowedLeafBranches.SelectRandom(random); … … 199 199 // select leaf node if possible 200 200 allowedLeafBranches = (from branch in branches 201 where branch.SubTrees.Count() == 0201 where !branch.SubTrees.Any() 202 202 select branch).ToList(); 203 203 if (allowedLeafBranches.Count > 0) { … … 205 205 } else { 206 206 allowedInternalBranches = (from branch in branches 207 where branch.SubTrees. Count() > 0207 where branch.SubTrees.Any() 208 208 select branch).ToList(); 209 209 return allowedInternalBranches.SelectRandom(random);
Note: See TracChangeset
for help on using the changeset viewer.