public GraphNode(ISymbolicExpressionTreeNode node, string label) { SymbolicExpressionTreeNode = node; Label = label; Hash = GetHashCode(); Depth = node.GetDepth(); Length = node.GetLength(); }
public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxLength, int maxDepth) { // make sure it is possible to create a trees smaller than maxLength and maxDepth if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength) { throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength"); } if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth) { throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth"); } // tree length is limited by the grammar and by the explicit size constraints int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol); int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol, maxDepth)); int tries = 0; while (tries++ < MAX_TRIES) { // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar) int targetTreeLength; targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1); if (targetTreeLength <= 1 || maxDepth <= 1) { return; } bool success = TryCreateFullTreeFromSeed(random, seedNode, targetTreeLength - 1, maxDepth - 1); // if successful => check constraints and return the tree if everything looks ok if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) { return; } else { // clean seedNode while (seedNode.Subtrees.Any()) { seedNode.RemoveSubtree(0); } } // try a different length MAX_TRIES times } throw new ArgumentException("Couldn't create a random valid tree."); }
public static Tuple <string, int, int, int> CalculateGeneticMaterialChange(ISymbolicExpressionTree child, ItemArray <ISymbolicExpressionTree> parents) { Tuple <ISymbolicExpressionTreeNode, int> crossoverPoint = FindCrossoverPoint(child, parents[0]); if (crossoverPoint == null) { return(new Tuple <string, int, int, int>(NOCHANGE, 0, 0, 0)); } ISymbolicExpressionTreeNode crossoverPointNode = crossoverPoint.Item1; //may be needed to check difference to parent subtree, otherwise delete //int indexOfCrossoverPoint = child.Root.IterateNodesBreadth().ToList().IndexOf(crossoverPointNode); //var parentSubtree = parents[0].Root.IterateNodesBreadth().ToList()[indexOfCrossoverPoint]; return(new Tuple <string, int, int, int>(crossoverPointNode.Symbol.Name, crossoverPointNode.GetDepth(), crossoverPointNode.GetLength(), crossoverPoint.Item2)); }
public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode, int maxLength, int maxDepth) { // make sure it is possible to create a trees smaller than maxLength and maxDepth if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength) throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength"); if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth) throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth"); // tree length is limited by the grammar and by the explicit size constraints int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol); int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol, maxDepth)); int tries = 0; while (tries++ < MAX_TRIES) { // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar) int targetTreeLength; targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1); if (targetTreeLength <= 1 || maxDepth <= 1) return; bool success = TryCreateFullTreeFromSeed(random, seedNode, targetTreeLength - 1, maxDepth - 1); // if successful => check constraints and return the tree if everything looks ok if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) { return; } else { // clean seedNode while (seedNode.Subtrees.Any()) seedNode.RemoveSubtree(0); } // try a different length MAX_TRIES times } throw new ArgumentException("Couldn't create a random valid tree."); }