/// <summary> /// Randomly returns a terminal node for the given <paramref name="parentNode"/>. /// (A terminal has got a minimum and maximum arity of 0.) /// </summary> /// <param name="parentNode">parent node for which a child node is returned randomly</param> /// <param name="grammar">grammar to determine the allowed child symbols for parentNode</param> /// <param name="random">random number generator</param> /// <returns>randomly chosen terminal node with arity 0 or null, if no terminal node exists</returns> protected ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode, ISymbolicExpressionGrammar grammar, IRandom random) { // only select specific symbols, which can be interpreted ... var possibleSymbolsList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol) where s.InitialFrequency > 0.0 where s.MaximumArity == 0 where s.MinimumArity == 0 select s).ToList(); // no terminal node exists for the given parent node if (!possibleSymbolsList.Any()) { return(null); } var newNode = possibleSymbolsList.SampleRandom(random).CreateTreeNode(); if (newNode.HasLocalParameters) { newNode.ResetLocalParameters(random); } return(newNode); }
/// <summary> /// Returns a randomly chosen child node for the given <paramref name="parentNode"/>. /// </summary> /// <param name="parentNode">parent node to find a child node randomly for</param> /// <param name="genotype">integer vector, which should be mapped to a tree</param> /// <param name="grammar">grammar used to define the allowed child symbols</param> /// <param name="genotypeIndex">index in the integer vector; can be greater than vector length</param> /// <param name="random">random number generator</param> /// <returns>randomly chosen child node or null, if no child node exits</returns> protected ISymbolicExpressionTreeNode GetNewChildNode(ISymbolicExpressionTreeNode parentNode, IntegerVector genotype, ISymbolicExpressionGrammar grammar, int genotypeIndex, IRandom random) { // only select specific symbols, which can be interpreted ... IEnumerable <ISymbol> symbolList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol) where s.InitialFrequency > 0.0 select s).ToList(); int prodRuleCount = symbolList.Count(); // no child node exists for the given parent node if (prodRuleCount < 1) { return(null); } // genotypeIndex % genotype.Length, if wrapping is allowed int prodRuleIndex = genotype[genotypeIndex] % prodRuleCount; var newNode = symbolList.ElementAt(prodRuleIndex).CreateTreeNode(); if (newNode.HasLocalParameters) { newNode.ResetLocalParameters(random); } return(newNode); }
private void BuildAllowedChildSymbolsTree() { if (Symbol == null) { symbolicExpressionTreeChart.Tree = null; return; } var tree = new SymbolicExpressionTree(new SymbolicExpressionTreeNode(Symbol)); if (Grammar.GetMaximumSubtreeCount(Symbol) > 0) { for (int i = 0; i < Grammar.GetMaximumSubtreeCount(Symbol); i++) { var node = new DummySymbol("Subtree " + i).CreateTreeNode(); var groupSymbols = grammar.GetAllowedChildSymbols(Symbol, i).OfType <GroupSymbol>().ToList(); foreach (var childSymbol in Grammar.GetAllowedChildSymbols(Symbol, i)) { if (!groupSymbols.Any(g => g != childSymbol && g.Flatten().Contains(childSymbol))) { node.AddSubtree(new SymbolicExpressionTreeNode(childSymbol)); } } tree.Root.AddSubtree(node); } } symbolicExpressionTreeChart.Tree = tree; symbolicExpressionTreeChart.SuspendRepaint = true; foreach (var subtreeNode in tree.Root.Subtrees) { foreach (var allowedChildNode in subtreeNode.Subtrees) { var visualLine = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(subtreeNode, allowedChildNode); visualLine.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; } } for (int i = Grammar.GetMinimumSubtreeCount(symbol); i < Grammar.GetMaximumSubtreeCount(symbol); i++) { var subtreeNode = tree.Root.GetSubtree(i); var visualTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(subtreeNode); visualTreeNode.TextColor = Color.Gray; visualTreeNode.LineColor = Color.LightGray; var visualLine = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(tree.Root, subtreeNode); visualLine.LineColor = Color.LightGray; foreach (var allowedChildNode in subtreeNode.Subtrees) { visualTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(allowedChildNode); visualTreeNode.TextColor = Color.Gray; visualTreeNode.LineColor = Color.LightGray; visualLine = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(subtreeNode, allowedChildNode); visualLine.LineColor = Color.LightGray; } } symbolicExpressionTreeChart.SuspendRepaint = false; UpdateSelectedSymbolicExpressionTreeNodes(); }
/// <summary> /// Randomly returns a terminal node for the given <paramref name="parentNode"/>. /// (A terminal has got a minimum and maximum arity of 0.) /// </summary> /// <param name="parentNode">parent node for which a child node is returned randomly</param> /// <param name="grammar">grammar to determine the allowed child symbols for parentNode</param> /// <param name="random">random number generator</param> /// <returns>randomly chosen terminal node with arity 0 or null, if no terminal node exists</returns> protected ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode, ISymbolicExpressionGrammar grammar, IRandom random) { // only select specific symbols, which can be interpreted ... var possibleSymbolsList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol) where s.InitialFrequency > 0.0 where s.MaximumArity == 0 where s.MinimumArity == 0 select s).ToList(); // no terminal node exists for the given parent node if (!possibleSymbolsList.Any()) return null; var newNode = possibleSymbolsList.SampleRandom(random).CreateTreeNode(); if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random); return newNode; }
public IEnumerable <ISymbol> GetAllowedChildSymbols(ISymbol parent) { return(grammar.GetAllowedChildSymbols(parent)); }
/// <summary> /// Returns a randomly chosen child node for the given <paramref name="parentNode"/>. /// </summary> /// <param name="parentNode">parent node to find a child node randomly for</param> /// <param name="genotype">integer vector, which should be mapped to a tree</param> /// <param name="grammar">grammar used to define the allowed child symbols</param> /// <param name="genotypeIndex">index in the integer vector; can be greater than vector length</param> /// <param name="random">random number generator</param> /// <returns>randomly chosen child node or null, if no child node exits</returns> protected ISymbolicExpressionTreeNode GetNewChildNode(ISymbolicExpressionTreeNode parentNode, IntegerVector genotype, ISymbolicExpressionGrammar grammar, int genotypeIndex, IRandom random) { // only select specific symbols, which can be interpreted ... IEnumerable<ISymbol> symbolList = (from s in grammar.GetAllowedChildSymbols(parentNode.Symbol) where s.InitialFrequency > 0.0 select s).ToList(); int prodRuleCount = symbolList.Count(); // no child node exists for the given parent node if (prodRuleCount < 1) return null; // genotypeIndex % genotype.Length, if wrapping is allowed int prodRuleIndex = genotype[genotypeIndex] % prodRuleCount; var newNode = symbolList.ElementAt(prodRuleIndex).CreateTreeNode(); if (newNode.HasLocalParameters) newNode.ResetLocalParameters(random); return newNode; }