Changeset 5499 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs
- Timestamp:
- 02/16/11 19:01:00 (13 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/ChangeNodeTypeManipulation.cs
r5445 r5499 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 29 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding .Manipulators{30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 32 31 [StorableClass] 33 32 [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")] 34 33 public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator { 35 36 34 [StorableConstructor] 37 35 private ChangeNodeTypeManipulation(bool deserializing) : base(deserializing) { } … … 43 41 } 44 42 45 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree , ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {46 ChangeNodeType(random, symbolicExpressionTree , grammar, maxTreeSize.Value, maxTreeHeight.Value, out success);43 protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree) { 44 ChangeNodeType(random, symbolicExpressionTree); 47 45 } 48 46 49 public static void ChangeNodeType(IRandom random, SymbolicExpressionTree symbolicExpressionTree , ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight, out bool success) {47 public static void ChangeNodeType(IRandom random, SymbolicExpressionTree symbolicExpressionTree) { 50 48 51 49 // select any node as parent (except the root node) 52 50 var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1) 53 51 from subtree in parent.SubTrees 54 select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random); 52 select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) }) 53 .SelectRandom(random); 54 int subTreeCount = manipulationPoint.Node.SubTrees.Count(); 55 55 // find possible symbols for the node (also considering the existing branches below it) 56 56 var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index) 57 where manipulationPoint.Node.SubTrees.Count <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol)58 where manipulationPoint.Node.SubTrees.Count >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol)57 where subTreeCount <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol) 58 where subTreeCount >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol) 59 59 select symbol; 60 60 61 61 if (allowedSymbols.Count() == 0) { 62 success = false;63 62 return; 64 63 } 65 64 var node = manipulationPoint.Node; 66 65 // keep only symbols that are still possible considering the existing sub-trees 67 var constrainedSymbols = from symbol in allowedSymbols 68 let disallowedSubtrees = 69 from subtree in node.SubTrees 70 where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.SubTrees.IndexOf(subtree)) 71 select subtree 72 where disallowedSubtrees.Count() == 0 73 select symbol; 66 var constrainedSymbols = (from symbol in allowedSymbols 67 let disallowedSubtrees = 68 from subtree in node.SubTrees 69 where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.IndexOfSubTree(subtree)) 70 select subtree 71 where disallowedSubtrees.Count() == 0 72 select symbol) 73 .ToList(); 74 74 if (constrainedSymbols.Count() == 0) { 75 success = false;76 75 return; 77 76 } 78 var newSymbol = SelectRandomSymbol(random, constrainedSymbols); 77 var weights = constrainedSymbols.Select(s => s.InitialFrequency).ToList(); 78 var newSymbol = constrainedSymbols.SelectRandom(weights, random); 79 79 80 80 // replace the old node with the new node … … 86 86 manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index); 87 87 manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, newNode); 88 success = true;89 }90 91 private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {92 var symbolList = symbols.ToList();93 var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();94 if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero.");95 var r = random.NextDouble() * ticketsSum;96 double aggregatedTickets = 0;97 for (int i = 0; i < symbolList.Count; i++) {98 aggregatedTickets += symbolList[i].InitialFrequency;99 if (aggregatedTickets > r) {100 return symbolList[i];101 }102 }103 // this should never happen104 throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");105 88 } 106 89 }
Note: See TracChangeset
for help on using the changeset viewer.