Changeset 8333 for trunk/sources
- Timestamp:
- 07/26/12 14:12:26 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj
r6978 r8333 152 152 <Compile Include="Manipulators\ChangeNodeTypeManipulation.cs" /> 153 153 <Compile Include="Interfaces\Operators\ISymbolicExpressionTreeManipulator.cs" /> 154 <Compile Include="Manipulators\RemoveBranchManipulation.cs" /> 154 155 <Compile Include="Manipulators\ReplaceBranchManipulation.cs" /> 155 156 <Compile Include="Manipulators\FullTreeShaker.cs" /> -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/RemoveBranchManipulation.cs
r8330 r8333 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 26 27 using HeuristicLab.Parameters; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using System.Collections.Generic;29 29 30 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 31 31 [StorableClass] 32 [Item("Re placeBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")]33 public sealed class Re placeBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator {32 [Item("RemoveBranchManipulation", "Removes a random sub-tree of the input tree and fixes the tree by generating random subtrees if necessary..")] 33 public sealed class RemoveBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator { 34 34 private const int MAX_TRIES = 100; 35 35 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; … … 53 53 54 54 [StorableConstructor] 55 private Re placeBranchManipulation(bool deserializing) : base(deserializing) { }56 private Re placeBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { }57 public Re placeBranchManipulation()55 private RemoveBranchManipulation(bool deserializing) : base(deserializing) { } 56 private RemoveBranchManipulation(RemoveBranchManipulation original, Cloner cloner) : base(original, cloner) { } 57 public RemoveBranchManipulation() 58 58 : base() { 59 59 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); … … 62 62 63 63 public override IDeepCloneable Clone(Cloner cloner) { 64 return new Re placeBranchManipulation(this, cloner);64 return new RemoveBranchManipulation(this, cloner); 65 65 } 66 66 67 67 protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { 68 Re placeRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);68 RemoveRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 69 69 } 70 70 71 public static void Re placeRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {71 public static void RemoveRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) { 72 72 var allowedSymbols = new List<ISymbol>(); 73 73 ISymbolicExpressionTreeNode parent; … … 77 77 // repeat until a fitting parent and child are found (MAX_TRIES times) 78 78 int tries = 0; 79 80 var nodes = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).ToList(); 79 81 do { 80 parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);82 parent = nodes.SelectRandom(random); 81 83 childIndex = random.Next(parent.SubtreeCount); 82 84 var child = parent.GetSubtree(childIndex); … … 97 99 } while (tries < MAX_TRIES && allowedSymbols.Count == 0); 98 100 99 if (tries < MAX_TRIES) { 100 var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); 101 var seedSymbol = allowedSymbols.SelectRandom(weights, random); 102 // replace the old node with the new node 103 var seedNode = seedSymbol.CreateTreeNode(); 104 if (seedNode.HasLocalParameters) 105 seedNode.ResetLocalParameters(random); 101 if (tries >= MAX_TRIES) return; 102 ReplaceWithMinimalTree(random, symbolicExpressionTree.Root, parent, childIndex); 103 } 106 104 107 parent.RemoveSubtree(childIndex); 108 parent.InsertSubtree(childIndex, seedNode); 109 ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth); 105 private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent, int childIndex) { 106 // determine possible symbols that will lead to the smallest possible tree 107 var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex) 108 where s.InitialFrequency > 0.0 109 group s by parent.Grammar.GetMinimumExpressionLength(s) into g 110 orderby g.Key 111 select g).First().ToList(); 112 var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList(); 113 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 114 var newTreeNode = selectedSymbol.CreateTreeNode(); 115 if (newTreeNode.HasLocalParameters) newTreeNode.ResetLocalParameters(random); 116 parent.RemoveSubtree(childIndex); 117 parent.InsertSubtree(childIndex, newTreeNode); 118 119 var topLevelNode = newTreeNode as SymbolicExpressionTreeTopLevelNode; 120 if (topLevelNode != null) 121 topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone()); 122 123 for (int i = 0; i < newTreeNode.Grammar.GetMinimumSubtreeCount(newTreeNode.Symbol); i++) { 124 // insert a dummy sub-tree and add the pending extension to the list 125 var dummy = new SymbolicExpressionTreeNode(); 126 newTreeNode.AddSubtree(dummy); 127 // replace the just inserted dummy by recursive application 128 ReplaceWithMinimalTree(random, root, newTreeNode, i); 110 129 } 111 130 }
Note: See TracChangeset
for help on using the changeset viewer.