Changeset 7477 for branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers
- Timestamp:
- 02/16/12 13:53:28 (13 years ago)
- Location:
- branches/HeuristicLab.Crossovers
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Crossovers
- Property svn:ignore
-
old new 19 19 protoc.exe 20 20 *.user 21 _ReSharper.HeuristicLab.Crossovers
-
- Property svn:ignore
-
branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Property svn:ignore
-
old new 5 5 *.vs10x 6 6 Plugin.cs 7 *.bak
-
- Property svn:ignore
-
branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers
-
Property
svn:ignore
set to
*.bak
-
Property
svn:ignore
set to
-
branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r7259 r7477 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.PluginInfrastructure; 30 31 31 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 37 38 /// </summary> 38 39 [Item("SubtreeCrossover", "An operator which performs subtree swapping crossover.")] 40 [NonDiscoverableType] 39 41 [StorableClass] 40 public sealedclass SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {42 public class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator { 41 43 private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability"; 42 44 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 43 45 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 46 private const string SymbolicDataAnalysisEvaluationPartitionParameterName = "EvaluationPartition"; 47 44 48 #region Parameter Properties 45 49 public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter { … … 51 55 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 52 56 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 57 } 58 public IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter { 59 get { return (IValueLookupParameter<IntRange>)Parameters[SymbolicDataAnalysisEvaluationPartitionParameterName]; } 53 60 } 54 61 #endregion … … 65 72 #endregion 66 73 [StorableConstructor] 67 pr ivateSubtreeCrossover(bool deserializing) : base(deserializing) { }68 pr ivateSubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }74 protected SubtreeCrossover(bool deserializing) : base(deserializing) { } 75 protected SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { } 69 76 public SubtreeCrossover() 70 77 : base() { … … 72 79 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 73 80 Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9))); 81 Parameters.Add(new ValueLookupParameter<IntRange>(SymbolicDataAnalysisEvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated.")); 74 82 } 75 83 … … 82 90 return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value, 83 91 MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 92 } 93 94 public ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 95 return Cross(random, parent0, parent1); 84 96 } 85 97 … … 94 106 // calculate the max length and depth that the inserted branch can have 95 107 int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength); 96 int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root,crossoverPoint0.Parent);108 int maxInsertedBranchDepth = maxTreeDepth - parent0.Root.GetBranchLevel(crossoverPoint0.Parent); 97 109 98 110 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 99 111 parent1.Root.ForEachNodePostfix((n) => { 100 112 if (n.GetLength() <= maxInsertedBranchLength && 101 n.GetDepth() <= maxInsertedBranchDepth && 102 IsMatchingPointType(crossoverPoint0, n)) 113 n.GetDepth() <= maxInsertedBranchDepth && crossoverPoint0.IsMatchingPointType(n)) 103 114 allowedBranches.Add(n); 104 115 }); 105 116 // empty branch 106 if ( IsMatchingPointType(crossoverPoint0,null)) allowedBranches.Add(null);117 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null); 107 118 108 119 if (allowedBranches.Count == 0) { … … 125 136 } 126 137 return parent0; 127 }128 }129 130 private static bool IsMatchingPointType(CutPoint cutPoint, ISymbolicExpressionTreeNode newChild) {131 var parent = cutPoint.Parent;132 if (newChild == null) {133 // make sure that one subtree can be removed and that only the last subtree is removed134 return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount &&135 cutPoint.ChildIndex == parent.SubtreeCount - 1;136 } else {137 // check syntax constraints of direct parent - child relation138 if (!parent.Grammar.ContainsSymbol(newChild.Symbol) ||139 !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, cutPoint.ChildIndex)) return false;140 141 bool result = true;142 // check point type for the whole branch143 newChild.ForEachNodePostfix((n) => {144 result =145 result &&146 parent.Grammar.ContainsSymbol(n.Symbol) &&147 n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&148 n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);149 });150 return result;151 138 } 152 139 } … … 226 213 } 227 214 } 228 229 private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {230 if (root == point) return 0;231 foreach (var subtree in root.Subtrees) {232 int branchLevel = GetBranchLevel(subtree, point);233 if (branchLevel < int.MaxValue) return 1 + branchLevel;234 }235 return int.MaxValue;236 }237 215 } 238 216 }
Note: See TracChangeset
for help on using the changeset viewer.