Changeset 5510 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers
- Timestamp:
- 02/17/11 13:51:04 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/CrossoverPoint.cs
r5499 r5510 23 23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 24 24 internal class CrossoverPoint { 25 public SymbolicExpressionTreeNode Parent { get; set; }26 public SymbolicExpressionTreeNode Child { get; set; }25 public ISymbolicExpressionTreeNode Parent { get; set; } 26 public ISymbolicExpressionTreeNode Child { get; set; } 27 27 public int SubtreeIndex { 28 get { return Parent. SubTrees.IndexOf(Child); }28 get { return Parent.IndexOfSubTree(Child); } 29 29 } 30 public CrossoverPoint( SymbolicExpressionTreeNode parent,SymbolicExpressionTreeNode child) {30 public CrossoverPoint(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) { 31 31 this.Parent = parent; 32 32 this.Child = child; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r5499 r5510 78 78 } 79 79 80 protected override SymbolicExpressionTree Cross(IRandom random,81 SymbolicExpressionTree parent0,SymbolicExpressionTree parent1) {80 protected override ISymbolicExpressionTree Cross(IRandom random, 81 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 82 82 return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value, 83 83 MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 84 84 } 85 85 86 public static SymbolicExpressionTree Cross(IRandom random,87 SymbolicExpressionTree parent0,SymbolicExpressionTree parent1,86 public static ISymbolicExpressionTree Cross(IRandom random, 87 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 88 88 double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight) { 89 89 // select a random crossover point in the first parent 90 SymbolicExpressionTreeNode crossoverPoint0;90 ISymbolicExpressionTreeNode crossoverPoint0; 91 91 int replacedSubtreeIndex; 92 92 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeSize, maxTreeHeight, out crossoverPoint0, out replacedSubtreeIndex); 93 93 94 94 // calculate the max size and height that the inserted branch can have 95 int maxInsertedBranchSize = maxTreeSize - (parent0.Size - crossoverPoint0. SubTrees[replacedSubtreeIndex].GetSize());95 int maxInsertedBranchSize = maxTreeSize - (parent0.Size - crossoverPoint0.GetSubTree(replacedSubtreeIndex).GetSize()); 96 96 int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0); 97 97 98 List< SymbolicExpressionTreeNode> allowedBranches = new List<SymbolicExpressionTreeNode>();98 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 99 99 parent1.Root.ForEachNodePostfix((n) => { 100 100 if (n.GetSize() <= maxInsertedBranchSize && … … 117 117 } 118 118 119 private static bool IsMatchingPointType( SymbolicExpressionTreeNode parent, int replacedSubtreeIndex,SymbolicExpressionTreeNode branch) {119 private static bool IsMatchingPointType(ISymbolicExpressionTreeNode parent, int replacedSubtreeIndex, ISymbolicExpressionTreeNode branch) { 120 120 // check syntax constraints of direct parent - child relation 121 121 if (!parent.Grammar.ContainsSymbol(branch.Symbol) || … … 128 128 result && 129 129 parent.Grammar.ContainsSymbol(n.Symbol) && 130 n.SubTrees.Count >= parent.Grammar.GetMinSubtreeCount(n.Symbol) &&131 n.SubTrees.Count <= parent.Grammar.GetMaxSubtreeCount(n.Symbol);130 n.SubTrees.Count() >= parent.Grammar.GetMinSubtreeCount(n.Symbol) && 131 n.SubTrees.Count() <= parent.Grammar.GetMaxSubtreeCount(n.Symbol); 132 132 }); 133 133 return result; 134 134 } 135 135 136 private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, outSymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {136 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 137 137 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 138 138 List<CrossoverPoint> internalCrossoverPoints = new List<CrossoverPoint>(); 139 139 List<CrossoverPoint> leafCrossoverPoints = new List<CrossoverPoint>(); 140 140 parent0.Root.ForEachNodePostfix((n) => { 141 if (n.SubTrees.Count > 0 && n != parent0.Root) {141 if (n.SubTrees.Count() > 0 && n != parent0.Root) { 142 142 foreach (var child in n.SubTrees) { 143 143 if (child.GetSize() <= maxBranchSize && 144 144 child.GetHeight() <= maxBranchHeight) { 145 if (child.SubTrees.Count > 0)145 if (child.SubTrees.Count() > 0) 146 146 internalCrossoverPoints.Add(new CrossoverPoint(n, child)); 147 147 else … … 178 178 } 179 179 180 private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<SymbolicExpressionTreeNode> branches, double internalNodeProbability) {180 private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability) { 181 181 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 182 List< SymbolicExpressionTreeNode> allowedInternalBranches;183 List< SymbolicExpressionTreeNode> allowedLeafBranches;182 List<ISymbolicExpressionTreeNode> allowedInternalBranches; 183 List<ISymbolicExpressionTreeNode> allowedLeafBranches; 184 184 if (random.NextDouble() < internalNodeProbability) { 185 185 // select internal node if possible 186 186 allowedInternalBranches = (from branch in branches 187 where branch.SubTrees.Count > 0187 where branch.SubTrees.Count() > 0 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.Count() == 0 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.Count() == 0 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.Count() > 0 208 208 select branch).ToList(); 209 209 return allowedInternalBranches.SelectRandom(random); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs
r5499 r5510 37 37 private const string ChildParameterName = "Child"; 38 38 #region Parameter Properties 39 public ILookupParameter<ItemArray< SymbolicExpressionTree>> ParentsParameter {40 get { return (ScopeTreeLookupParameter< SymbolicExpressionTree>)Parameters[ParentsParameterName]; }39 public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter { 40 get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; } 41 41 } 42 public ILookupParameter< SymbolicExpressionTree> ChildParameter {43 get { return (ILookupParameter< SymbolicExpressionTree>)Parameters[ChildParameterName]; }42 public ILookupParameter<ISymbolicExpressionTree> ChildParameter { 43 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; } 44 44 } 45 45 #endregion 46 46 #region Properties 47 public ItemArray< SymbolicExpressionTree> Parents {47 public ItemArray<ISymbolicExpressionTree> Parents { 48 48 get { return ParentsParameter.ActualValue; } 49 49 } 50 public SymbolicExpressionTree Child {50 public ISymbolicExpressionTree Child { 51 51 get { return ChildParameter.ActualValue; } 52 52 set { ChildParameter.ActualValue = value; } … … 58 58 protected SymbolicExpressionTreeCrossover() 59 59 : base() { 60 Parameters.Add(new ScopeTreeLookupParameter< SymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed."));61 Parameters.Add(new LookupParameter< SymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));60 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed.")); 61 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover.")); 62 62 } 63 63 … … 66 66 throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators."); 67 67 68 SymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]);68 ISymbolicExpressionTree result = Cross(Random, Parents[0], Parents[1]); 69 69 70 70 Child = result; … … 72 72 } 73 73 74 protected abstract SymbolicExpressionTree Cross(IRandom random,75 SymbolicExpressionTree parent0,SymbolicExpressionTree parent1);74 protected abstract ISymbolicExpressionTree Cross(IRandom random, 75 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 76 76 } 77 77 }
Note: See TracChangeset
for help on using the changeset viewer.