Changeset 7522 for branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Timestamp:
- 02/24/12 17:30:16 (13 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed (with no actual effect on merging)
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/MinAverageMaxSymbolicExpressionTreeLengthAnalyzer.cs
r7439 r7522 73 73 private MinAverageMaxSymbolicExpressionTreeLengthAnalyzer(MinAverageMaxSymbolicExpressionTreeLengthAnalyzer original, Cloner cloner) 74 74 : base(original, cloner) { 75 valueAnalyzer = cloner.Clone(original.valueAnalyzer); 76 subScopesProcessor = cloner.Clone(original.subScopesProcessor); 75 77 AfterDeserialization(); 76 78 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r7479 r7522 36 36 /// until a valid configuration is found. 37 37 /// </summary> 38 [Item("Subtree Crossover", "An operator which performs subtree swapping crossover.")]38 [Item("SubtreeSwappingCrossover", "An operator which performs subtree swapping crossover.")] 39 39 [StorableClass] 40 public sealedclass SubtreeCrossover : TracingSymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {40 public class SubtreeCrossover : TracingSymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator { 41 41 private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability"; 42 42 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 43 43 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 44 44 45 #region Parameter Properties 45 46 public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter { … … 65 66 #endregion 66 67 [StorableConstructor] 67 pr ivateSubtreeCrossover(bool deserializing) : base(deserializing) { }68 pr ivateSubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }68 protected SubtreeCrossover(bool deserializing) : base(deserializing) { } 69 protected SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { } 69 70 public SubtreeCrossover() 70 71 : base() { … … 78 79 } 79 80 80 p rotected override ISymbolicExpressionTree Cross(IRandom random,81 public override ISymbolicExpressionTree Crossover(IRandom random, 81 82 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 82 83 return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value, … … 94 95 // calculate the max length and depth that the inserted branch can have 95 96 int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength); 96 int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root,crossoverPoint0.Parent);97 int maxInsertedBranchDepth = maxTreeDepth - parent0.Root.GetBranchLevel(crossoverPoint0.Parent); 97 98 98 99 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 99 100 parent1.Root.ForEachNodePostfix((n) => { 100 101 if (n.GetLength() <= maxInsertedBranchLength && 101 n.GetDepth() <= maxInsertedBranchDepth && 102 IsMatchingPointType(crossoverPoint0, n)) 102 n.GetDepth() <= maxInsertedBranchDepth && crossoverPoint0.IsMatchingPointType(n)) 103 103 allowedBranches.Add(n); 104 104 }); 105 105 // empty branch 106 if ( IsMatchingPointType(crossoverPoint0,null)) allowedBranches.Add(null);106 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null); 107 107 108 108 if (allowedBranches.Count == 0) { … … 128 128 } 129 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 }152 }153 154 130 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out CutPoint crossoverPoint) { 155 131 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); … … 157 133 List<CutPoint> leafCrossoverPoints = new List<CutPoint>(); 158 134 parent0.Root.ForEachNodePostfix((n) => { 159 if (n.Subtree s.Any()&& n != parent0.Root) {135 if (n.SubtreeCount > 0 && n != parent0.Root) { 160 136 foreach (var child in n.Subtrees) { 161 137 if (child.GetLength() <= maxBranchLength && 162 138 child.GetDepth() <= maxBranchDepth) { 163 if (child.Subtree s.Any())139 if (child.SubtreeCount > 0) 164 140 internalCrossoverPoints.Add(new CutPoint(n, child)); 165 141 else … … 167 143 } 168 144 } 145 169 146 // add one additional extension point if the number of sub trees for the symbol is not full 170 147 if (n.SubtreeCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) { … … 173 150 } 174 151 } 175 }); 152 } 153 ); 176 154 177 155 if (random.NextDouble() < internalNodeProbability) { … … 200 178 // select internal node if possible 201 179 allowedInternalBranches = (from branch in branches 202 where branch != null && branch.Subtree s.Any()180 where branch != null && branch.SubtreeCount > 0 203 181 select branch).ToList(); 204 182 if (allowedInternalBranches.Count > 0) { … … 207 185 // no internal nodes allowed => select leaf nodes 208 186 allowedLeafBranches = (from branch in branches 209 where branch == null || !branch.Subtrees.Any()187 where branch == null || branch.SubtreeCount == 0 210 188 select branch).ToList(); 211 189 return allowedLeafBranches.SelectRandom(random); … … 214 192 // select leaf node if possible 215 193 allowedLeafBranches = (from branch in branches 216 where branch == null || !branch.Subtrees.Any()194 where branch == null || branch.SubtreeCount == 0 217 195 select branch).ToList(); 218 196 if (allowedLeafBranches.Count > 0) { … … 220 198 } else { 221 199 allowedInternalBranches = (from branch in branches 222 where branch != null && branch.Subtree s.Any()200 where branch != null && branch.SubtreeCount > 0 223 201 select branch).ToList(); 224 202 return allowedInternalBranches.SelectRandom(random); … … 226 204 } 227 205 } 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 206 } 238 207 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs
r7439 r7522 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 25 using HeuristicLab.Parameters; 27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 66 65 throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators."); 67 66 68 ISymbolicExpressionTree result = Cross (Random, Parents[0], Parents[1]);67 ISymbolicExpressionTree result = Crossover(Random, Parents[0], Parents[1]); 69 68 70 69 Child = result; … … 72 71 } 73 72 74 protected abstract ISymbolicExpressionTree Cross(IRandom random, 75 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 73 public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 76 74 } 77 75 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/TracingSymbolicExpressionTreeCrossover.cs
r7514 r7522 91 91 } 92 92 var originalParents = new ItemList<IItem>(Parents.Select(x => GlobalCloneMap[x])); 93 ISymbolicExpressionTree result = Cross (Random, Parents[0], Parents[1]);93 ISymbolicExpressionTree result = Crossover(Random, Parents[0], Parents[1]); 94 94 Child = result; 95 95 GlobalTraceMap.Add(Child, originalParents); … … 98 98 } 99 99 100 protected abstract ISymbolicExpressionTree Cross(IRandom random, 101 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 100 public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 102 101 } 103 102 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/CutPoint.cs
r7439 r7522 22 22 23 23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 24 internalclass CutPoint {24 public class CutPoint { 25 25 public ISymbolicExpressionTreeNode Parent { get; set; } 26 26 public ISymbolicExpressionTreeNode Child { get; set; } … … 39 39 this.Child = null; 40 40 } 41 42 public bool IsMatchingPointType(ISymbolicExpressionTreeNode newChild) { 43 var parent = this.Parent; 44 if (newChild == null) { 45 // make sure that one subtree can be removed and that only the last subtree is removed 46 return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount && 47 this.ChildIndex == parent.SubtreeCount - 1; 48 } else { 49 // check syntax constraints of direct parent - child relation 50 if (!parent.Grammar.ContainsSymbol(newChild.Symbol) || 51 !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, this.ChildIndex)) 52 return false; 53 54 bool result = true; 55 // check point type for the whole branch 56 newChild.ForEachNodePostfix((n) => { 57 result = 58 result && 59 parent.Grammar.ContainsSymbol(n.Symbol) && 60 n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) && 61 n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol); 62 }); 63 return result; 64 } 65 } 41 66 } 42 67 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs
r7439 r7522 32 32 int GetDepth(); 33 33 int GetLength(); 34 int GetBranchLevel(ISymbolicExpressionTreeNode child); 34 35 35 36 IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix(); -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeCrossover.cs
r7439 r7522 30 30 ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter { get; } 31 31 ILookupParameter<ISymbolicExpressionTree> ChildParameter { get; } 32 ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 32 33 } 33 34 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Plugin.cs
r7514 r7522 26 26 27 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 28 [Plugin("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding","Provides operators and related classes for the symbolic expression tree encoding.", "3.4.2.7 479")]28 [Plugin("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding","Provides operators and related classes for the symbolic expression tree encoding.", "3.4.2.7514")] 29 29 [PluginFile("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll", PluginFileType.Assembly)] 30 30 [PluginDependency("HeuristicLab.Analysis", "3.3")] -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Properties
-
Property
svn:ignore
set to
*.bak
-
Property
svn:ignore
set to
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Properties/AssemblyInfo.cs
r7439 r7522 45 45 46 46 [assembly: AssemblyVersion("3.4.0.0")] 47 [assembly: AssemblyFileVersion("3.4.2.7 280")]47 [assembly: AssemblyFileVersion("3.4.2.7507")] -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs
r7439 r7522 125 125 } 126 126 127 public int GetBranchLevel(ISymbolicExpressionTreeNode child) { 128 return GetBranchLevel(this, child); 129 } 130 131 private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) { 132 if (root == point) 133 return 0; 134 foreach (var subtree in root.Subtrees) { 135 int branchLevel = GetBranchLevel(subtree, point); 136 if (branchLevel < int.MaxValue) 137 return 1 + branchLevel; 138 } 139 return int.MaxValue; 140 } 141 127 142 public virtual void ResetLocalParameters(IRandom random) { } 128 143 public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
Note: See TracChangeset
for help on using the changeset viewer.