Changeset 5499 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs
- Timestamp:
- 02/16/11 19:01:00 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Property svn:ignore
-
old new 2 2 obj 3 3 HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin.cs 4 *.user
-
- Property svn:ignore
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs
r5445 r5499 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;30 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Parameters; 31 30 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Manipulators{31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 33 32 [StorableClass] 34 33 [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")] 35 public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator { 34 public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator { 35 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 36 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 37 #region Parameter Properties 38 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 39 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 40 } 41 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 42 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 43 } 44 #endregion 45 #region Properties 46 public IntValue MaximumSymbolicExpressionTreeLength { 47 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 48 } 49 public IntValue MaximumSymbolicExpressionTreeDepth { 50 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 51 } 52 #endregion 53 36 54 [StorableConstructor] 37 55 private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { } 38 56 private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { } 39 public ReplaceBranchManipulation() : base() { } 57 public ReplaceBranchManipulation() 58 : base() { 59 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 60 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 61 } 40 62 41 63 public override IDeepCloneable Clone(Cloner cloner) { … … 43 65 } 44 66 45 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {46 ReplaceRandomBranch(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, out success);67 protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { 68 ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 47 69 } 48 70 49 public static void ReplaceRandomBranch(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight, out bool success) { 50 success = false; 71 public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) { 51 72 // select any node as parent (except the root node) 52 73 var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1) 53 74 from subtree in parent.SubTrees 54 select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random); 75 select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) }) 76 .SelectRandom(random); 55 77 56 int max Size = maxTreeSize- symbolicExpressionTree.Size + manipulationPoint.Node.GetSize();57 int max Height = maxTreeHeight- symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight();78 int maxLength = maxTreeLength - symbolicExpressionTree.Size + manipulationPoint.Node.GetSize(); 79 int maxDepth = maxTreeDepth - symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight(); 58 80 // find possible symbols for the node (also considering the existing branches below it) 59 var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)60 where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxHeight61 where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxSize62 select symbol;81 var allowedSymbols = (from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index) 82 where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxDepth 83 where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxLength 84 select symbol).ToList(); 63 85 if (allowedSymbols.Count() == 0) return; 64 65 var seedSymbol = SelectRandomSymbol(random, allowedSymbols); // replace the old node with the new node 86 var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); 87 var seedSymbol = allowedSymbols.SelectRandom(weights, random); 88 // replace the old node with the new node 66 89 var seedNode = seedSymbol.CreateTreeNode(); 67 90 if (seedNode.HasLocalParameters) … … 70 93 manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index); 71 94 manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, seedNode); 72 seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxSize, maxHeight, 0, 0); 73 success = true; 74 } 75 76 private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) { 77 var symbolList = symbols.ToList(); 78 var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum(); 79 if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero."); 80 var r = random.NextDouble() * ticketsSum; 81 double aggregatedTickets = 0; 82 for (int i = 0; i < symbolList.Count; i++) { 83 aggregatedTickets += symbolList[i].InitialFrequency; 84 if (aggregatedTickets > r) { 85 return symbolList[i]; 86 } 87 } 88 // this should never happen 89 throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols."); 95 seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth, 0, 0); 90 96 } 91 97 }
Note: See TracChangeset
for help on using the changeset viewer.