def generate(number=10,r=0.5): """Function to generate and return a random tree with fixed number of internal nodes added. The root has to be a scalarizing node. Biasing of the tree towards (or away from) binary nodes can be controlled with the parameter r.""" root = ScalarNode(CGAFunctions.DataMethodFactory().getScalar()) tree = AlgorithmTree(root) while number > 0: tNode = random.choice(tree.getTermini()) fNode = CGAGenerator._getRandomFunctionalNode(r) CGAGenerator._extend(tNode,fNode) number -= 1 return tree
def expgenerate(p,r=0.5): """Generates and returns a random tree. Current tree is extended at a *single* node with probability p, and process terminates with probability 1-p.""" root = ScalarNode(CGAFunctions.DataMethodFactory().getScalar()) tree = AlgorithmTree(root) extend = True while extend: tNode = random.choice(tree.getTermini()) fNode = CGAGenerator._getRandomFunctionalNode(r) CGAGenerator._extend(tNode,fNode) if uniform() < p: extend = True else: extend = False return tree